-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalts.py
390 lines (287 loc) · 11 KB
/
alts.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from dotenv import load_dotenv
import keyboard
from litellm import completion
from notifypy import Notify
import os
from PIL import Image, ImageDraw
from pystray import Icon, Menu, MenuItem
import queue
from simpleaudio import WaveObject
from sounddevice import InputStream, default, query_devices
from soundfile import SoundFile
import sys
import tempfile
import threading
from TTS.api import TTS
import whisper
import yaml
load_dotenv()
icon=Image.open("icon_v2.png")
notification = Notify(
default_notification_application_name="alts ",
default_notification_title="",
default_notification_icon="icon_v2.png",
# custom_mac_notificator="ALTS.app"
)
SENTENCE_DELIMITERS = (".", "?", "!", ";", ":", ": ", " (", ")", "\n-", "\n- ", " -", "- ", "\n–", "\n– ", " –", "– ")
# TODO: improve logging (use a proper logger), remove hardcoded stdout prints.
# TODO: implement cancelling of assistant jobs (automatically when querying something new? by keyboard?)
class ALTS:
def __init__(self, auto_start=True):
self._config()
if auto_start:
self.start()
def _config(self):
self.current_lang = None
self.speech_q = queue.Queue()
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
self.hotkey = config["hotkey"]
self.messages = config["messages"]
self.show_notifications = config["showNotifications"]
self._notify(message=self.messages["starting"])
# Load STT model
self.stt_config = config["whisper"]
self.stt = whisper.load_model(self.stt_config["model"])
# Load TTS model
self.tts_config = config["tts"]
self.tts = TTS(model_name=config["tts"]["model"], progress_bar=False)
# Load LLM config
self.llm = config["llm"]
self.tray_icon = Icon("alts", icon, "alts")
def _quit(self):
os._exit(0)
def _notify(self, message=""):
if(self.show_notifications):
notification.message = message
notification.send(block=False)
def _toggle_notifications(self):
self.show_notifications = not self.show_notifications
def _initialize_chat(self, chat=None):
self.current_chat = chat["title"] if chat else ""
self.llm["messages"] = chat["messages"] if chat else []
if not self.llm["messages"] and self.llm["system"]:
self.llm["messages"] = [{ "role": "system", "content": self.llm["system"] }]
os.system('cls||clear')
self.tray_icon.menu = self._tray_menu()
self._notify(message=self.messages["ready"])
print(self.messages["ready"])
def _tray_menu(self):
return Menu(
MenuItem(
text="Show Notifications",
action=self._toggle_notifications,
checked=lambda _: self.show_notifications
),
Menu.SEPARATOR,
MenuItem(
text="Quit",
action=self._quit,
),
)
def _user_audio_input_worker(self):
"""Process user audio input"""
print("\n🎙️ LISTENING...")
self._notify(message=self.messages["listening"])
audio = self.listen()
print("\n💬 TRANSCRIBING...")
transcription_data = self.transcribe(audio=audio)
transcription = transcription_data["text"]
self.current_lang = transcription_data["language"]
print(f">>>{transcription}")
self._llm_worker(transcription)
def _user_text_input_worker(self):
"""Process user text input"""
while True:
self._llm_worker(input(self.messages["textInput"]))
def _llm_worker(self, query):
"""Process llm response"""
print("\n💭 THINKING...\n")
self._notify(message=self.messages["thinking"])
speech_thread = threading.Thread(target=self._speech_worker, daemon=True)
speech_thread.start()
for sentence in self.think(query=query):
synth_data = self.synthesize(text=sentence)
self.speech_q.put(synth_data)
self.speech_q.put(None)
speech_thread.join()
def _speech_worker(self):
"""Process speech audio files"""
while True:
synth_data = self.speech_q.get()
if synth_data is None:
print(self.messages["ready"])
self._notify(message=self.messages["ready"])
break
self._notify(message=f'"{synth_data["text"].strip()}"')
print("\n🔊 SPEAKING...\n")
self.speak(synth_data["audio"])
def start(self):
"""Start assistant with default behavior"""
self._initialize_chat()
keyboard.add_hotkey(self.hotkey, lambda: self._user_audio_input_worker())
user_input_thread = threading.Thread(target = self._user_text_input_worker, daemon=True)
user_input_thread.start()
self.tray_icon.run()
keyboard.wait()
user_input_thread.join()
def listen(self):
"""Record microphone audio to a .wav file"""
try:
device_info = query_devices(default.device, 'input')
samplerate = int(device_info['default_samplerate'])
channels = device_info['max_input_channels']
device = device_info['index']
mic_record_q = queue.Queue()
filename = tempfile.mktemp(suffix='.wav', dir='')
file = SoundFile(
filename,
mode='x',
samplerate=samplerate,
channels=channels
)
def _input_stream_callback(indata, frames, time, status):
if status:
print(status, file=sys.stderr)
mic_record_q.put(indata.copy())
stream = InputStream(
samplerate=samplerate,
device=device,
channels=channels,
callback=_input_stream_callback
)
stream.start()
while keyboard.is_pressed(self.hotkey):
file.write(mic_record_q.get())
stream.close()
file.close()
return file.name
except Exception as e:
raise type(e)(str(e))
def transcribe(self, audio, remove_audio=True):
"""
Transcribe an audio file using Whisper
Parameters
----------
`audio`: str
Path to the audio file to transcribe
`remove_audio`: bool
Whether to delete the audio file after transcribing it
Returns
-------
A dictionary containing the resulting text ("text") and segment-level details ("segments"), and
the spoken language ("language") detected.
"""
try:
result = self.stt.transcribe(audio, fp16=False)
if remove_audio:
os.remove(audio)
return result
except Exception as e:
raise type(e)(str(e))
def think(self, query, buffer_sentences=True):
"""
Send a query to an LLM and stream the response
Parameters
----------
`query`: str
Query to prompt the LLM with
`buffer_sentences`: bool
Whether to buffer and return the result sentence by sentence
Returns
-------
A generator object containing strings.
"""
try:
self.llm["messages"].append({ "role": "user", "content": query })
full_response = ""
if not self.llm["custom_provider"] or "":
response = completion(
model=self.llm["model"],
messages=self.llm["messages"],
api_base=self.llm["url"],
stream=True
)
else:
response = completion(
model=self.llm["model"],
messages=self.llm["messages"],
api_base=self.llm["url"],
custom_llm_provider=self.llm["custom_provider"],
stream=True
)
for sentence in self._parse_response(response, buffer_sentences):
full_response += sentence
yield sentence
self.llm["messages"].append({"role": "assistant", "content": full_response})
except Exception as e:
raise type(e)(str(e))
# TODO: move to helpers
# TODO: improve parsing abbreviations, markdown?... for proper talk-back edge-cases
def _parse_response(self, chunks, buffer_sentences):
"""Split/compile LLM response chunks into sentences"""
buffer = ""
for chunk in chunks:
token = chunk['choices'][0]['delta']['content'] or ""
if not buffer_sentences:
yield token
if token.startswith(SENTENCE_DELIMITERS):
yield buffer + token[0]
buffer = token[1:]
else:
buffer += token
if buffer != "":
yield buffer
def synthesize(self, text, split_sentences=True):
"""
Synthesize text into an audio file
Parameters
----------
`sentence`: str
Text to be synthesized into audio
`lang`: str
Language code of the text to synthesize (only applies to multilingual models)
`split_sentences`: bool
Whether the text should be splitted into sentences
Returns
-------
A dictionary containing the path to the audio file of the synthesized text ("audio") and the text itself ("text").
"""
try:
speaker = self.tts_config["speakerId"] if self.tts.is_multi_speaker else None
language = self.current_lang if self.tts.is_multi_lingual and self.current_lang in self.tts.languages else None
audio = self.tts.tts_to_file(
text=text,
speaker=speaker,
language=language,
split_sentences=split_sentences,
file_path=tempfile.mktemp(suffix='.wav', dir='')
)
return dict(audio=audio, text=text)
except Exception as e:
raise type(e)(str(e))
def speak(self, audio, remove_audio=True):
"""
Play an audio file
Parameters
----------
`audio`: str
Path to the audio file to play
`remove_audio`: bool
Whether to delete the audio file after transcribing it
"""
try:
wave_obj = WaveObject.from_wave_file(audio)
play_obj = wave_obj.play()
play_obj.wait_done()
if remove_audio:
os.remove(audio)
except Exception as e:
raise type(e)(str(e))
def main():
try:
ALTS()
except Exception as e:
print(e)
if __name__ == "__main__":
main()