import speech_recognition as sr
import requests
from pydub import AudioSegment
from pydub.playback import play
import io

r = sr.Recognizer()

while True:
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print(">说点什么:")
        audio_in = r.listen(source)
        print("Processing...")
    try: 
        text_input = r.recognize_whisper(audio_in, model="small", language="chinese") 
        print("You said: " + text_input) 
    except sr.UnknownValueError: 
        print("Whisper could not understand audio") 
        text_input = None
    except sr.RequestError as _error: 
        print("Could not request results from Whisper") 
        print(_error)
        text_input = None
    if text_input:
        reply = requests.get('http://192.168.199.124:8088?question=' + text_input).content
        audio_out = AudioSegment.from_file(io.BytesIO(reply), format="mp3")
        play(audio_out)