File size: 1,247 Bytes
45c30cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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