Spaces:
Sleeping
Sleeping
| import vosk | |
| import pyaudio | |
| import json | |
| import requests | |
| model_path = "model/vosk-model-en-us-0.42-gigaspeech/vosk-model-en-us-0.42-gigaspeech" | |
| # Initialize the model with model-path | |
| model = vosk.Model(model_path) | |
| rec = vosk.KaldiRecognizer(model, 16000) | |
| print("Ready to record") | |
| while True: | |
| print("Ready") | |
| p = pyaudio.PyAudio() | |
| stream = p.open(format=pyaudio.paInt16, | |
| channels=1, | |
| rate=16000, | |
| input=True, | |
| frames_per_buffer=8192) | |
| data = stream.read(8192) | |
| if rec.AcceptWaveform(data):#accept waveform of input voice | |
| # Parse the JSON result and get the recognized text | |
| res = rec.Result() | |
| try: | |
| res = json.loads(res)#Turn to json object | |
| except: | |
| continue | |
| #Turn to json object | |
| recognized_text = res['text'] | |
| print(f"User: {recognized_text}") | |
| url = "http://127.0.0.1:5007/query" | |
| json = { | |
| "input_text": recognized_text, | |
| } | |
| req = requests.post(url, json=json) | |
| print(f"Bot: {req.json()['output']}") | |
| if "terminate" in recognized_text.lower(): | |
| print("Termination keyword detected. Stopping...") | |
| break |