-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_assistant.py
123 lines (86 loc) · 3.38 KB
/
run_assistant.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import json
import numpy as np
import os
import pyaudio
import wave
from aiy.board import Board, Led
from datetime import datetime
from deepspeech import Model, version
from timeit import default_timer as timer
import audio_response
import rasa_api_call
import log_input
MODEL_NAME = "deepspeech-0.8.1-models.tflite"
MODEL_SCORER = "deepspeech-0.8.1-models.scorer"
CHUNK = 2048 # Buffer size
FORMAT = pyaudio.paInt16 # Sample Size
CHANNELS = 1 # Sample Depth
record_bool = True
def end_record():
"""
Callback function to start and stop the audio recordings
"""
global record_bool
print("released...")
record_bool = False
def load_data(response_path:str) -> tuple:
"""
Loads the response audio mapper and Deepspeech model data
:param response_path: path to the response dir with speech response data
:return file_map: dict with intent str as key and file name str as value
:return ds: Deepspeech object with loaded model
"""
response_json = os.path.join(response_path, "answer_to_file.json")
with open(response_json, "r") as in_json:
file_map = json.load(in_json)
ds = Model(MODEL_NAME)
ds.enableExternalScorer(MODEL_SCORER)
return file_map, ds
def main():
"""
Main function that waits for the button press and predicts on audio stream
data until the button is released. The question is sent to the local Rasa
server and the response intent is mapped to the appropriate audio file
which is output.
"""
global record_bool
response_path = os.path.join(os.getcwd(), "response_data")
file_map, ds = load_data(response_path)
file_map["<<GENERICS>>"] = set(file_map["<<GENERICS>>"])
desired_sample_rate = ds.sampleRate()
p = pyaudio.PyAudio()
with Board() as board:
board.button.when_released = end_record
while(True):
print("Waiting...")
board.button.wait_for_press()
record_bool = True
frames = []
stream = ds.createStream()
i_stream = p.open(format=FORMAT, channels=CHANNELS, rate=desired_sample_rate,
input=True, output=True, frames_per_buffer=CHUNK)
board.led.state = Led.ON
while(record_bool):
print("Recording...")
buff = np.frombuffer(i_stream.read(CHUNK, exception_on_overflow=False), dtype=np.int16)
stream.feedAudioContent(buff)
board.led.state = Led.OFF
buff = np.frombuffer(i_stream.read(2 * CHUNK), dtype=np.int16)
stream.feedAudioContent(buff)
i_stream.stop_stream()
i_stream.close()
result = stream.finishStream()
print("result: ", result)
intents = rasa_api_call.get_intent(result)
now = datetime.now()
log_input.log_to_csv({
"question" : result,
"answer" : ' '.join(str(resp) for resp in intents),
"time" : now.strftime("%m/%d/%Y %H:%M:%S"),
"confidence" : "N/A"
})
for intent in intents:
audio_response.audio_response(file_map, intent, response_path)
p.terminate()
if __name__ == "__main__":
main()