-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient.py
236 lines (216 loc) · 9.88 KB
/
client.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import argparse
import json
import time
import threading
from copy import deepcopy
from os.path import join
import numpy as np
from components.nw import Nw
from components.vad import Vad
from components.ap import Ap
from components.mic import Mic
from components.login_ui import LoginUi
from components.ui import Ui
from components.utils import find_code_blocks
# import scipy.io.wavfile as wf
def load_config(config_file):
with open(config_file, "r") as file:
json_data = json.load(file)
return json_data
def main(nw, ui, mic, vad, ap, llm_params):
ap.play_sound(ap.listening_sound)
ui.load_visual("You")
mic_muted = False
mic_last_chunk = None
mic.start_mic()
while True:
skip_sleep = False
if ui.kill:
print("Shutting down...")
nw.close_connection()
break
mic_chunk = mic.get_chunk()
if len(mic_chunk) == mic.buffer_size * 2: # 2 bytes per sample when pcm16
if max(mic_chunk) == 0 and not mic_muted:
mic_muted = True
mic.update_ui = False
ui.load_visual("system_muted_mic")
vad.reset_vad()
mic.reset_recording()
skip_sleep = True
elif not (
mic_chunk == mic_last_chunk
): # .all() and max(mic_chunk) != 0: TODO
if mic_muted:
ui.load_visual("You")
mic.update_ui = True
mic_muted = False
mic_last_chunk = deepcopy(mic_chunk)
mic.vad_time = vad.no_voice_wait_sec - vad.no_voice_sec
chunk_time = mic.buffer_size / mic.samplerate
vad_status = vad.check(
np.frombuffer(mic_chunk, np.int16)
.flatten()
.astype(np.float32, order="C")
/ 32768.0,
chunk_time,
)
# print(vad_status, vad.no_voice_sec)
if vad_status == "None":
mic.reset_recording()
skip_sleep = True
elif vad_status == "vad_end":
mic.stop_mic()
ui.load_visual("system_transition")
ap.play_sound(ap.transition_sound)
mic_recording = mic.get_recording()
vad_no_voice_wait_sec = vad_params.get("no_voice_wait_sec", None)
mic_recording = mic_recording[
: -vad_no_voice_wait_sec
* (mic.samplerate * 2) # 2 bytes per sample when pcm16
]
# wf.write('test.wav', mic.samplerate, np.frombuffer(mic_recording, np.int16).flatten())
nw.send_msg("stt_transcribe")
nw.receive_ack()
nw.send_audio_recording(mic_recording)
stt_data = nw.receive_msg()
if len(stt_data) != 1:
ui.add_message("You", stt_data, new_entry=True)
nw.send_msg("llm_get_answer")
if not llm_params.get("streaming_output", None):
llm_data = nw.receive_msg()
code_blocks = find_code_blocks(llm_data)
if len(code_blocks) > 0:
color_code_block = True
else:
color_code_block = False
ui.add_message(
"Aria",
llm_data,
new_entry=True,
color_code_block=color_code_block,
code_blocks=code_blocks,
)
while True:
check_tts = nw.receive_msg()
if check_tts == "tts_end":
break
else:
tts_chunk = nw.receive_audio_recording()
ap.stream_sound(
np.frombuffer(tts_chunk, np.int16).flatten(),
update_ui=True,
)
ap.check_audio_finished()
else:
nw.receive_ack()
ui.add_message("Aria", "", new_entry=True)
while True:
llm_or_tts = nw.receive_msg()
if llm_or_tts == "llm":
llm_chunk = nw.receive_msg()
color_code_block = nw.receive_msg()
ui.add_message(
"Aria",
llm_chunk,
new_entry=False,
color_code_block=color_code_block.lower()
== "true",
)
elif llm_or_tts == "tts":
while True:
check_tts = nw.receive_msg()
if check_tts == "tts_end":
break
else:
tts_chunk = nw.receive_audio_recording()
ap.stream_sound(
np.frombuffer(
tts_chunk, np.int16
).flatten(),
update_ui=True,
)
elif llm_or_tts == "streaming_end":
break
ap.check_audio_finished()
else:
# TODO add to llm context
ui.add_message("You", "...", new_entry=True)
# nw.send_msg("fixed_answer")
# ui.add_message("Aria", "Did you say something?", new_entry=True)
# while True:
# check_tts = nw.receive_msg()
# if check_tts == "tts_end":
# break
# else:
# tts_chunk = nw.receive_audio_recording()
# ap.stream_sound(
# np.frombuffer(tts_chunk, np.int16).flatten(),
# update_ui=True,
# )
# ap.check_audio_finished()
time.sleep(1)
ap.play_sound(ap.listening_sound)
ui.load_visual("You")
mic.start_mic()
skip_sleep = True
else:
pass
else:
pass
if not skip_sleep:
time.sleep(mic.buffer_size / mic.samplerate)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Aria.")
parser.add_argument(
"--config",
default="default.json",
help="Path to JSON config file in the configs folder",
)
args = parser.parse_args()
config_path = join("configs", args.config)
config = load_config(config_path)
nw_params = config.get("Nw", {}).get("params", {})
vad_params = config.get("Vad", {}).get("params", {})
ap_params = config.get("Ap", {}).get("params", {})
mic_params = config.get("Mic", {}).get("params", {})
loginui_params = config.get("LoginUi", {}).get("params", {})
ui_params = config.get("Ui", {}).get("params", {})
llm_params = config.get("Llm", {}).get("params", {})
nw = Nw(params=nw_params)
def connect_callback(connection_data):
print("Connecting...")
def connect():
con_result = nw.client_connect(
connection_data["ip_address"],
connection_data["port"],
connection_data["username"],
connection_data["password"]
)
if con_result == "success":
print("Connected!")
loginui.set_status_message("Connected!", is_error=False)
loginui.connection_status = True
loginui.close()
elif con_result == "timed_out":
loginui.set_status_message("Connection failed!\nCheck IP and port!", is_error=True)
elif con_result == "socket_error":
loginui.set_status_message("Connection failed!\nCheck IP and port!", is_error=True)
elif con_result == "authentication_failed":
loginui.set_status_message("Authentication failed!\nCheck username and password!", is_error=True)
threading.Thread(target=connect).start()
loginui = LoginUi(params=loginui_params, config_file=config_path, callback=connect_callback)
connection_status = loginui.start()
if connection_status:
vad = Vad(params=vad_params)
ui = Ui(params=ui_params)
ap = Ap(params=ap_params, ui=ui)
mic = Mic(params=mic_params, ui=ui, vad=vad)
if nw.audio_compression:
nw.init_audio_encoder(mic.samplerate, mic.channels, mic.buffer_size)
nw.init_audio_decoder(ap.samplerate, ap.channels, ap.buffer_size)
com_thread = threading.Thread(
target=main, args=(nw, ui, mic, vad, ap, llm_params)
)
com_thread.start()
ui.start()