forked from masapasa/VoiceBotChatGPT-RaspberryPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1045 lines (960 loc) · 42.5 KB
/
main.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import collections
from datetime import date, datetime
import gc
import json
import os
import platform
import queue
import re
import signal
import sys
import time
from typing import Iterable
import numpy as np
from chat_gpt_service import ChatGPTService
from input_listener import InputListener
import openwakeword
from openwakeword.model import Model
import pyaudio
from sound_effect_service import SoundEffectService
from tts_service import TextToSpeechService
from alarm_timer_service import AlarmTimerService
import threading
from flask import Flask, jsonify, render_template, send_from_directory, request
from flask_socketio import SocketIO
from werkzeug.utils import secure_filename
import requests
from radio_player import RadioPlayer
transcript_seperator = f"_"*40
script_dir = os.path.dirname(os.path.abspath(__file__))
shairport_handler = None
radio_player = None
detector = None
alarm_timer_service = None
app = None
socketio = None
config = None
config_file = None
assistants = None
assistant_name = None
assistant_acronym = None
led_service = None
is_rpi = False
loading_sound = None
file_chunks = {}
is_exiting = False
# One-time download of all pre-trained models (or only select models)
openwakeword.utils.download_models()
def is_running_on_raspberry_pi():
try:
with open('/proc/cpuinfo', 'r') as cpuinfo:
for line in cpuinfo:
if "Raspberry Pi" in line:
return True
except IOError:
# /proc/cpuinfo is not accessible, not running on Raspberry Pi
pass
return False
config_file = os.path.join(script_dir, "config.json")
assistants_file = os.path.join(script_dir, "assistants.json")
print(f"Loading config from {config_file}...")
config = json.load(open(config_file))
print(f"Loading assistants from {assistants_file}...")
assistants = json.load(open(assistants_file))
assistant = assistants[config["assistant"]]
config["old_assistant"] = config["assistant"]
config["assistant_dict"] = assistant
assistant_name = assistant["name"]
assistant_acronym = assistant["acronym"]
vad_threshold = config["vad_threshold"]
print_audio_level = config["print_audio_level"]
max_threshold = config["max_threshold"]
if not os.path.exists("chatlogs"):
os.makedirs("chatlogs")
led_service = None
led_brightness = min(config["led_brightness"], 31)
# Check if the script is running on rpi
is_rpi= platform.system() == 'Linux' and is_running_on_raspberry_pi()
if is_rpi:
try:
import dbus
from led_service import LEDService
led_service = LEDService(led_brightness=led_brightness)
led_service.handle_event("Starting")
except ImportError:
print("Make sure you're running this on a Raspberry Pi.")
else:
print("LED event: Starting")
if config["use_frontend"]:
app = Flask(__name__)
socketio = SocketIO(app, async_mode='threading')
def getChatFilename(dateStr):
chatlog_filename = os.path.join(script_dir, "chatlogs", f"{config['assistant']}_chatlog-{dateStr}.txt")
return chatlog_filename
# save conversation to a log file
def append2log(text, noNewLine=False):
chatlog_filename = getChatFilename(str(date.today()))
with open(chatlog_filename, "a", encoding='utf-8') as f:
f.write(text + ("\n" if not noNewLine else ""))
f.close
if text and text != transcript_seperator:
socketio.emit('update_chat', {'message': text.strip()})
class ShairportSyncHandler:
def __init__(self, wakeword_detector, radio_player):
self.wakeword_detector = wakeword_detector
self.radio_player = radio_player
self.is_running = True
self.shairport_active = False
self.blink_led_thread = None
self.bus = dbus.SystemBus()
self.shairport_proxy = self.bus.get_object('org.gnome.ShairportSync', '/org/gnome/ShairportSync')
self.shairport_interface = dbus.Interface(self.shairport_proxy, 'org.freedesktop.DBus.Properties')
self.thread = threading.Thread(target=self.check_if_active)
self.thread.start()
def check_if_active(self):
while self.wakeword_detector.is_running and self.is_running:
try:
if self.shairport_interface.Get('org.gnome.ShairportSync', 'Active'):
if not self.shairport_active:
if self.radio_player is not None and self.radio_player.running:
self.radio_player.stop()
self.shairport_active = True
self.wakeword_detector.is_awoken = self.shairport_active
self.blink_led_thread = threading.Thread(target=self.blink_led)
self.blink_led_thread.start()
print("Pausing chatbot vad...")
socketio.emit('music_active', {'status': 'ready'})
else:
if self.shairport_active:
self.shairport_active = False
self.wakeword_detector.is_awoken = self.shairport_active
if self.blink_led_thread is not None and self.blink_led_thread.is_alive():
self.blink_led_thread.join()
self.wakeword_detector.handle_led_event("Running")
print("Resuming chatbot vad...")
socketio.emit('music_active', {'status': 'done'})
except dbus.DBusException as e:
print(f"Error communicating with Shairport Sync: {e}")
time.sleep(1)
def blink_led(self):
while self.is_running and self.shairport_active:
self.wakeword_detector.handle_led_event("Paused")
time.sleep(0.5)
self.wakeword_detector.handle_led_event("Off")
time.sleep(0.5)
def cleanup(self):
print("Cleaning up Shairport Sync handler...")
self.shairport_active = False
if self.blink_led_thread is not None and self.blink_led_thread.is_alive():
self.blink_led_thread.join()
self.is_running = False
if self.thread.is_alive():
self.thread.join()
self.bus = None
self.shairport_interface = None
self.shairport_proxy = None
self.blink_led_thread = None
self.thread = None
self.wakeword_detector = None
self.radio_player = None
class WakeWordDetector:
def __init__(self):
self.chat_gpt_service = ChatGPTService(config)
self.chat_gpt_service.append2log = append2log
oww_model_path = os.path.join(script_dir, "oww_models", config["oww_model"].replace("{assistant_name}", assistant_name))
oww_additional = config["oww_model"].replace("{assistant_name}", f"{assistant_name}1")
oww_model_path1 = os.path.join(script_dir, "oww_models", oww_additional) if os.path.exists(os.path.join(script_dir, "oww_models", oww_additional)) else None
oww_models = [oww_model_path]
if oww_model_path1:
oww_models.append(oww_model_path1)
if assistant_name.lower() == "jarvis":
oww_models.append("hey jarvis")
oww_inference_framework = config["oww_model"].split(".")[-1]
self.language = config["language"]
self.oww_chunk_size = config["oww_chunk_size"]
self.oww_sample_rate = config["oww_sample_rate"]
self.oww_channels = config["oww_channels"]
self.is_request_processing = False
self.is_awoken = False
self.use_elevenlabs = config["use_elevenlabs"]
self.audio_queue = queue.Queue()
self.is_running = True
self.consumer_thread = None
self.restart_app = False
self.mic_stream = None
self.handle = Model(
wakeword_models=oww_models,
inference_framework=oww_inference_framework,
vad_threshold=vad_threshold/max_threshold,
)
self.pa = pyaudio.PyAudio()
#stop loading sound so we can test ambient noise properly
loading_sound.stop_sound()
self.listener = InputListener(config)
self.speech = TextToSpeechService(config)
self.sound_effect = SoundEffectService(config)
def audio_consumer(self):
current_time = time.time()
last_audio_level_emit_time = current_time
last_audio_level_over_threshold = current_time
self._init_mic_stream()
while self.is_running:
try:
if self.is_awoken:
#print("Audio consumer paused")
if self.mic_stream is not None:
self.mic_stream.stop_stream()
while self.is_running and self.is_awoken:
time.sleep(1)
if self.mic_stream is not None:
self.mic_stream.start_stream()
#print("Audio consumer resumed")
self.handle_led_event("Running")
oww_audio = self.audio_queue.get()
audio_level = np.abs(oww_audio).mean()
if current_time - last_audio_level_emit_time >= 0.1:
socketio.emit('processing_audio', {'status': 'ready'})
current_time = time.time()
# if audio level is below the threshold, skip processing,
# but if the audio level was just above the threshold in the last 0.5 seconds,
# process the audio as its the tail end of the audio
if audio_level < vad_threshold and current_time - last_audio_level_over_threshold > 0.75:
continue
if audio_level > vad_threshold:
last_audio_level_over_threshold = current_time
if print_audio_level:
print(f"Audio level threshold ({audio_level}) triggered. Processing audio...")
# we don't want to send too many messages to the frontend. only send every audio level if its been 0.1 seconds
if current_time - last_audio_level_emit_time >= 0.1:
socketio.emit('processing_audio', {'status': 'done', 'audio_level': audio_level})
last_audio_level_emit_time = time.time()
# Make the prediction
prediction = self.handle.predict(oww_audio)
prediction_models = list(prediction.keys())
mdl = prediction_models[0]
score = float(prediction[mdl])
if score >= 0.5 and not self.is_request_processing:
socketio.emit('awake', {'status': 'ready'})
self.is_awoken = True
print(f"Awoken with score {round(score, 3)}!")
self.handle_led_event("Transcript")
self.sound_effect.play(self.sound_effect.get_random_wake_sound())
socketio.emit('listening_for_prompt', {'status': 'ready'})
self.listener.listen()
self.handle_led_event("StreamingStarted")
socketio.emit('prompt_received', {'status': 'ready'})
self.listener.sound_effect = self.sound_effect.play_loop("loading")
self.listener.transcribe()
prediction = self.predictSilence()
if self.listener.transcript is None:
self.sound_effect.play("error")
self._init_mic_stream()
continue
self.process_transcript(self.listener.transcript)
except Exception as e:
print("Error processing audio in audio_consumer...")
self.something_went_wrong()
print(f"Error: {e}")
continue
def process_audio(self):
self.consumer_thread = threading.Thread(target=self.audio_consumer)
self.is_awoken = True
self.consumer_thread.start()
self.handle_led_event("VoiceStarted")
if self.use_elevenlabs:
self.sound_effect.play("ready")
else:
self.speech.speak(f"{assistant_name} ready!")
self.is_awoken = False
while self.is_running:
sys.stdout.flush()
time.sleep(1)
def handle_led_event(self, event):
if led_service is not None:
led_service.handle_event(event)
else:
if event == "Running":
return
print(f"LED event: {event}")
def predictSilence(self):
# Calculate the number of samples for the given duration of silence
duration_seconds=2
num_samples = int(self.oww_sample_rate * duration_seconds)
silence_data = np.zeros(num_samples, dtype=np.int16)
# Predict the silence data to initialize the model
try:
prediction = self.handle.predict(silence_data)
except Exception as e:
print(f"Error: {e}")
pass
return prediction
def _init_mic_stream(self):
self.handle_led_event("Connected")
def audio_callback(in_data, frame_count, time_info, status):
audio = np.frombuffer(in_data, dtype=np.int16)
self.audio_queue.put(audio)
return (in_data, pyaudio.paContinue)
if self.pa is not None:
if self.mic_stream is not None:
self.mic_stream.stop_stream()
self.mic_stream.close()
self.mic_stream = self.pa.open(
rate=self.oww_sample_rate,
channels=self.oww_channels,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=self.oww_chunk_size,
stream_callback=audio_callback
)
self.is_request_processing = False
if (shairport_handler is not None and shairport_handler.shairport_active) \
or (radio_player is not None and radio_player.running) and not self.is_awoken:
self.is_awoken = True
socketio.emit('music_active', {'status': 'ready'})
print("Music active. Pausing chatbot vad...")
else:
self.is_awoken = False
socketio.emit('chatbot_ready', {'status': 'ready'})
print("Listening for '" + assistant["wake_word"] + "'...")
def something_went_wrong(self):
if self.listener.sound_effect is not None:
self.listener.sound_effect.stop_sound()
if self.chat_gpt_service.sound_effect is not None:
self.chat_gpt_service.sound_effect.stop_sound()
self.sound_effect.play("error")
if self.use_elevenlabs:
self.sound_effect.play("something_went_wrong")
else:
self.speech.speak(f"Something went wrong!")
self._init_mic_stream()
def extract_time_from_transcript(self, transcript):
# Regular expression to match time in HH:MM AM/PM or HH:MM a.m./p.m. format
time_pattern = re.compile(r'(\d{1,2}:\d{2}\s?(?:AM|PM|am|pm|a\.m\.|p\.m\.)?)')
match = time_pattern.search(transcript)
if match:
time_str = match.group(1)
# Normalize the time string to a standard format
time_str = time_str.replace('.', '').upper()
# Convert the matched time string to a datetime object
alarm_time = datetime.strptime(time_str, '%I:%M %p')
return alarm_time
else:
raise ValueError("No valid time found in transcript")
def extract_duration_from_transcript(self, transcript):
# Regular expression to match duration in format: 1 second, 2 minutes, 3 hours.
# or 1 minute 30 seconds.
# or 2 hours and 30 minutes. etc.
duration_pattern = re.compile(r'(\d+)\s?(second|minute|hour)s?(?:\s+and\s+(\d+)\s?(second|minute|hour)s?)?')
matches = duration_pattern.findall(transcript)
total_duration_seconds = 0
for match in matches:
for i in range(0, len(match), 2):
if match[i]:
duration_value = int(match[i])
duration_unit = match[i + 1].lower()
if 'day' in duration_unit:
total_duration_seconds += duration_value * 24 * 3600
elif 'hour' in duration_unit:
total_duration_seconds += duration_value * 3600
elif 'minute' in duration_unit:
total_duration_seconds += duration_value * 60
else:
total_duration_seconds += duration_value
if total_duration_seconds > 0:
return total_duration_seconds
else:
raise ValueError("No valid duration found in transcript")
def durationSecondsToMaxUnits(self, seconds):
days = seconds // (24 * 3600)
seconds = seconds % (24 * 3600)
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return days, hours, minutes, seconds
def process_transcript(self, transcript, image=None, image_name=''):
if self.is_request_processing:
print("A request is already being processed. Please wait.")
return
self.handle_led_event("Processing")
self.is_request_processing = True
try:
start_time = time.time()
print(f"You: {transcript}")
append2log("")
# if the user's question is none or too short, skip
if len(transcript) < 2 and not image:
self.handle_led_event("VoiceStarted")
short_response = "Hi, there, how can I help?"
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
if self.use_elevenlabs:
self.sound_effect.play("hi_how_can_i_help")
else:
self.speech.speak(f"Hi, how can I help?")
append2log(f"You: {transcript} \n")
append2log(f"{assistant_name}: {short_response} \n")
return
time_phrases = [
"what time is it",
"what is the time",
"what is the current time",
"what's the time",
"what's the current time",
"do you have the time",
"do you have the current time",
"do you know the time",
"do you know the current time",
"tell me the time",
"tell me the current time",
"tell me what time it is",
]
if any(phrase in transcript for phrase in time_phrases) and "in" not in transcript and not image:
append2log(f"You: {transcript} \n")
self.handle_led_event("VoiceStarted")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
# get the current time in am/pm format without leading zeros
current_time = time.strftime('%I:%M %p').lstrip("0").replace("AM", "a.m.").replace("PM", "p.m.")
response = f"{current_time}"
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
radio_phrases = [
"play radio",
"play music",
"play some music",
"play some radio",
"play some tunes",
"play some songs",
"play the radio",
"play the music",
"play the tunes",
"play the songs",
]
if any(phrase in transcript.lower() for phrase in radio_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Starting radio...")
append2log(f"You: {transcript} \n")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
radio_player.start(config["radio_stream_url"])
response = "Radio started."
append2log(f"{assistant_name}: {response} \n")
return
radio_phrases = [
"play kids radio",
"play kids music",
"play kids songs",
"play kids tunes",
"play kid radio",
"play kid music",
"play kid songs",
"play kid tunes",
"play the kids radio",
"play the kids music",
"play the kids songs",
"play the kids tunes",
"play the kid radio",
"play the kid music",
"play the kid songs",
"play the kid tunes",
"play children's radio",
"play children's music",
"play children's songs",
"play children's tunes",
"play the children's radio",
"play the children's music",
"play the children's songs",
"play the children's tunes",
]
if any(phrase in transcript.lower() for phrase in radio_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Starting kids radio...")
append2log(f"You: {transcript} \n")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
radio_player.start(config["kids_radio_stream_url"])
response = "Kids radio started."
append2log(f"{assistant_name}: {response} \n")
return
radio_phrases = [
"stop playing",
"stop the radio",
"stop the music",
"stop the tunes",
"stop the songs",
"stop music",
"stop radio",
"stop tunes",
"stop songs",
"stop playing music",
"stop playing radio",
"stop playing tunes",
"stop playing songs",
]
if any(phrase in transcript.lower() for phrase in radio_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Stopping radio...")
append2log(f"You: {transcript} \n")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
radio_player.stop()
response = "Radio stopped."
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
alarm_phrases = [
"set an alarm",
"set a alarm",
"set alarm",
"set the alarm",
"wake me up",
]
if any(phrase in transcript.lower() for phrase in alarm_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Setting an alarm...")
append2log(f"You: {transcript} \n")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
# Extract time from transcript and set alarm
alarm_time = self.extract_time_from_transcript(transcript)
alarm_timer_service.add_alarm(alarm_time)
response = "Alarm set for " + alarm_time.strftime('%I:%M %p')
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
timer_phrases = [
"set a timer",
"set timer",
"set the timer",
]
if any(phrase in transcript.lower() for phrase in timer_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Setting a timer...")
append2log(f"You: {transcript} \n")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
# Extract duration from transcript and set timer
duration = self.extract_duration_from_transcript(transcript)
alarm_timer_service.add_timer(duration)
days, hours, minutes, seconds = self.durationSecondsToMaxUnits(duration)
day = f"{days} day" + ("s" if days > 1 else "") + ", " if days else ""
hour = f"{hours} hour" + ("s" if hours > 1 else "") + ", " if hours else ""
minute = f"{minutes} minute" + ("s" if minutes > 1 else "") + ", " if minutes else ""
second = f"{seconds} second" + ("s" if seconds > 1 else "") + ", " if seconds else ""
response = "Timer set for " + f"{day}{hour}{minute}{second}"
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
delete_phrases = [
"delete all alarm",
"reset all alarm",
"turn off all alarm",
"cancel all alarm",
"clear all alarm",
"forget all alarm",
"delete alarm",
"reset alarm",
"turn off alarm",
"cancel alarm",
"clear alarm",
"forget alarm",
]
if any(phrase in transcript.lower() for phrase in delete_phrases) and not image:
self.handle_led_event("VoiceStarted")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
alarm_timer_service.delete_all_jobs("alarm")
response = "All alarms and timers deleted"
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
delete_phrases = [
"delete all timer",
"reset all timer",
"turn off all timer",
"cancel all timer",
"clear all timer",
"forget all timer",
"delete timer",
"reset timer",
"turn off timer",
"cancel timer",
"clear timer",
"forget timer",
]
if any(phrase in transcript.lower() for phrase in delete_phrases) and not image:
self.handle_led_event("VoiceStarted")
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
alarm_timer_service.delete_all_jobs("timer")
response = "All alarms and timers deleted"
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
change_assistant_phrases = [
"change assistant",
"switch assistant",
"change the assistant",
"switch the assistant",
"change voice assistant",
"switch voice assistant",
"change the voice assistant",
"switch the voice assistant",
"change the voice",
"switch the voice",
"change voice",
"switch voice",
"change your voice",
"switch your voice",
"change your name",
"switch your name",
]
if any(phrase in transcript.lower() for phrase in change_assistant_phrases) and not image:
self.handle_led_event("VoiceStarted")
print("Changing assistant...")
if "taurus" in transcript.lower():
print("Handling mispronunciation of Taurus for TARS...")
transcript = transcript.replace("Taurus", "taurus").replace("taurus", "Taurus (TARS)")
append2log(f"You: {transcript} \n")
# grab the assisant name from the transcript
new_assistant = next((assistant for assistant in assistants if assistants.get(assistant, {}).get('name', '').lower() in transcript.lower()), None)
new_assistant_name = assistants.get(new_assistant, {}).get('name', '')
if new_assistant and new_assistant_name != assistant_name:
print(f"Switching to {new_assistant_name}...")
change_assistant({'assistant': new_assistant_name.lower()})
elif new_assistant_name == assistant_name:
response = f"I'm already {assistant_name}."
print(response)
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
else:
response = "Assistant not found."
print(response)
append2log(f"{assistant_name}: {response} \n")
self.speech.speak(response)
return
self.sound_effect.play(self.sound_effect.get_random_filler_sound())
print("Sending to chat GPT...")
append2log(f"You: {transcript}", noNewLine=True)
self.chat_gpt_service.sound_effect = self.sound_effect.play_loop("loading")
self.speech.sound_effect = self.chat_gpt_service.sound_effect
text_iterator = self.chat_gpt_service.send_to_chat_gpt(transcript, image, image_name)
if text_iterator is None:
append2log(f"{assistant_name}: Something went wrong.")
self.something_went_wrong()
return
socketio.emit('chat_response_ready', {'status': 'ready'})
self.handle_led_event("VoiceStarted")
if isinstance(text_iterator, str):
text_iterator = [text_iterator]
elif isinstance(text_iterator, Iterable):
text_iterator = text_iterator
else:
raise ValueError("Invalid input type: text_input must be a string or an iterable")
for text in text_iterator:
if text.strip():
self.speech.speak(text)
end_time = time.time()
print(f"Total Time: {end_time - start_time} seconds")
finally:
self._init_mic_stream()
def run(self):
try:
self.process_audio()
except KeyboardInterrupt:
pass
finally:
self.cleanup()
def cleanup(self):
print("Cleaning up detector...")
self.is_running = False
if self.speech is not None:
self.speech.stop()
current_thread = threading.current_thread()
if self.consumer_thread.is_alive() and self.consumer_thread != current_thread:
self.consumer_thread.join()
if self.mic_stream is not None:
self.mic_stream.stop_stream()
self.mic_stream.close()
if self.pa is not None:
self.pa.terminate()
self.mic_stream = None
self.pa = None
self.speech = None
self.sound_effect = None
self.chat_gpt_service = None
self.listener = None
self.handle = None
self.audio_queue = None
@app.template_filter('find_url')
def find_url_filter(text):
pattern = re.compile(r'(https?:\/\/[^\s]+\.(jpg|jpeg|png|gif))')
#grab the first url found in the text
url = pattern.search(text)[0] if pattern.search(text) else None
return url if url else None
# Function to get chat logs for a specific date
def get_chat_log_for_date(dateStr):
chatlog_filename = getChatFilename(dateStr)
filename = chatlog_filename.split("-")[0] + f"-{dateStr}.txt"
print(f"Getting chat log for {filename}...")
try:
with open(filename, 'r', encoding='utf-8') as f:
# read a line until you reach the end or You: or assistant_name:
chatlog = []
for line in f:
if line.startswith("You: ") or line.startswith(f"{assistant_name}: "):
chatlog.append(line)
else:
if chatlog:
chatlog[-1] += line
else:
chatlog.append(line)
except FileNotFoundError:
chatlog = []
chatlog = [{"message": message.strip()} for message in chatlog]
return chatlog
# @app.route('/set_alarm', methods=['POST'])
# def set_alarm():
# data = request.json
# alarm_time = datetime.strptime(data['time'], '%Y-%m-%d %H:%M:%S')
# alarm_timer_service.add_alarm(alarm_time, alarm_callback)
# return jsonify({'status': 'alarm set'})
# @app.route('/set_timer', methods=['POST'])
# def set_timer():
# data = request.json
# duration = int(data['duration'])
# alarm_timer_service.add_timer(duration, timer_callback)
# return jsonify({'status': 'timer set'})
# @app.route('/delete_all_jobs', methods=['POST'])
# def delete_all_jobs():
# alarm_timer_service.delete_all_jobs()
# return jsonify({'status': 'all jobs deleted'})
@app.route('/chatlog/<date>', methods=['GET'])
def chatlog(date):
chatlog = get_chat_log_for_date(date)
return jsonify(chatlog)
@app.route('/')
def index():
today = str(date.today())
chatlog = get_chat_log_for_date(today)
return render_template('index.html', vad_threshold=vad_threshold,
max_threshold=max_threshold,
assistants=assistants,
assistant_dict=assistant,
images_disabled=config["use_groq"],
chatlog=json.dumps(chatlog),
radio_playing=(radio_player is not None and radio_player.running)
)
@socketio.on("file_chunk")
def handle_file_chunk(data):
use_imgur = config["use_imgur"]
socketio.emit('prompt_received', {'status': 'ready'})
detector = app.config['detector']
# Extracting the chunk data
file_id = data.get("fileId")
chunk_index = data.get("chunkIndex") or 0
total_chunks = data.get("totalChunks") or 0
if use_imgur:
chunk_data = data.get("chunkData") if data.get("chunkData") else None
else:
chunk_data = base64.b64decode(data.get("chunkData")) if data.get("chunkData") else None
file_name = secure_filename(data.get("fileName")) if data.get("fileName") else None
text_prompt = data.get("prompt") if data.get("prompt") else ""
if file_id:
print(f"Received chunk {chunk_index + 1} of {total_chunks} for file {file_id}")
# Initialize the file's chunk list if not already
if file_id not in file_chunks:
file_chunks[file_id] = [None] * total_chunks
# Store the chunk data
file_chunks[file_id][chunk_index] = chunk_data
# Check if all chunks have been received
if all(chunk is not None for chunk in file_chunks[file_id]):
# combine the chunks in memory
print(f"Received all chunks for file {file_id}.")
if not use_imgur:
file_data = b"".join(file_chunks[file_id])
upload_path = os.path.join(script_dir, config['upload_folder'])
if not os.path.exists(upload_path):
os.makedirs(upload_path)
# Sanitize the file_name or ensure it's safe before appending it to the path
safe_file_name = os.path.join(upload_path, f"{time.time()}_{os.path.basename(file_name)}")
# Reassemble the file
with open(safe_file_name, "wb") as file:
for chunk in file_chunks[file_id]:
file.write(chunk)
file_name = f"http://{request.host}/{safe_file_name}"
file_data = base64.b64encode(file_data).decode("utf-8")
else:
file_data = "".join(file_chunks[file_id])
# convert to base64
response = detector.process_transcript(text_prompt, file_data, file_name)
# delete the chunks from memory
del file_chunks[file_id]
else:
detector.is_awoken = True
response = detector.process_transcript(text_prompt)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(config['upload_folder'], filename)
@app.route('/history')
def history():
today = str(date.today())
chatlog = get_chat_log_for_date(today)
return render_template('history.html', assistant_dict=assistant, chatlog=json.dumps(chatlog))
@app.route('/settings', methods=['GET', 'POST'])
def settings():
global config
if request.method == 'POST':
config['openai_model'] = request.form['openai_model']
config['groq_model'] = request.form['groq_model']
config['use_groq'] = 'use_groq' in request.form
config['radio_stream_url'] = request.form['radio_stream_url']
config['kids_radio_stream_url'] = request.form['kids_radio_stream_url']
config['elevenlabs_key'] = request.form['elevenlabs_key']
config['use_elevenlabs'] = 'use_elevenlabs' in request.form
config['use_gtts'] = 'use_gtts' in request.form
config['imgur_client_id'] = request.form['imgur_client_id']
config['imgur_client_secret'] = request.form['imgur_client_secret']
config['max_threshold'] = int(request.form['max_threshold'])
config['led_brightness'] = int(request.form['led_brightness'])
# Save the updated config to the file
with open('config.json', 'w') as f:
json.dump(config, f, indent=4)
restart_app()
return jsonify({"status": "ok"}), 200
return render_template('settings.html', config=config)
@app.route('/play_radio', methods=['POST'])
def play_radio():
global radio_player
if radio_player:
data = request.get_json()
print(data)
if data and data.get('radio') == 'kid':
stream_url = config["kids_radio_stream_url"]
else:
stream_url = config["radio_stream_url"]
radio_player.start(stream_url)
if radio_player.running:
return jsonify({"status": "done"}), 200
return jsonify({"status": "error"}), 500
@app.route('/stop_radio', methods=['POST'])
def stop_radio():
global radio_player
if radio_player:
radio_player.stop()
if not radio_player.running:
return jsonify({"status": "done"}), 200
return jsonify({"status": "error"}), 500
@socketio.on('change_vad_threshold')
def change_vad_threshold(data):
global vad_threshold, config
new_threshold = int(data.get('vad_threshold'))
if new_threshold:
vad_threshold = new_threshold
with open(config_file, 'r+') as f:
config = json.load(f)
config['vad_threshold'] = new_threshold
f.seek(0)
json.dump(config, f, indent=4)
f.truncate()
print(f"VAD threshold changed to {new_threshold}.")
socketio.emit('vad_threshold_changed', {'vad_threshold': new_threshold})
return socketio.emit('vad_threshold_changed', {'vad_threshold': None})
@socketio.on('change_assistant')
def change_assistant(data):
global config, assistant_name, assistant_acronym, assistant, chatlog_filename
new_assistant = data.get('assistant')
if new_assistant and new_assistant in assistants:
with open(config_file, 'r+') as f:
config = json.load(f)
old_assistant = config['assistant']
config['assistant'] = new_assistant
f.seek(0)
json.dump(config, f, indent=4)
f.truncate()
print(f"Assistant changed from {old_assistant} to {new_assistant}.")
config["old_assistant"] = old_assistant
config["assistant_dict"] = assistants[new_assistant]
assistant = assistants[config["assistant"]]
assistant_name = assistant["name"]
assistant_acronym = assistant["acronym"]
chatlog_filename = chatlog_filename = getChatFilename(str(date.today()))
socketio.emit('assistant_changed', {'assistant': new_assistant})
restart_app()
return
return socketio.emit('assistant_changed', {'assistant': None})
def run_flask_app():
socketio.run(app, debug=False, use_reloader=False, allow_unsafe_werkzeug=True, host="0.0.0.0")
def restart_app():
if detector is not None:
detector.restart_app = True
detector.cleanup()
if shairport_handler is not None:
shairport_handler.cleanup()
if radio_player is not None:
radio_player.cleanup()
if alarm_timer_service is not None:
alarm_timer_service.cleanup()
print("restart_app() complete.")
def runApp():
global detector, shairport_handler, loading_sound, radio_player, alarm_timer_service
while not is_exiting:
loading_sound = SoundEffectService(config).play_loop("loading")
detector = WakeWordDetector()
radio_player = RadioPlayer(detector)
alarm_timer_service = AlarmTimerService()
if is_rpi and config["use_shairport-sync"]:
shairport_handler = ShairportSyncHandler(detector, radio_player)
app.config['detector'] = detector # Attach detector to the Flask app config
detector.run()
if not detector.restart_app:
break
else:
print("Restarting app...")
detector = None
shairport_handler = None
radio_player = None
alarm_timer_service = None
loading_sound = None
gc.collect()
time.sleep(0.5)
print("Detector exited.")
def signal_handler(sig, frame):
print('Signal received: ', sig)
global is_exiting