-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathagent.py
More file actions
1081 lines (901 loc) · 43.6 KB
/
Copy pathagent.py
File metadata and controls
1081 lines (901 loc) · 43.6 KB
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
# =========================================================================
# Be More Agent 🤖
# A Local, Offline-First AI Agent for Raspberry Pi
#
# Copyright (c) 2026 brenpoly
# Licensed under the MIT License
# Source: https://github.com/brenpoly/be-more-agent
#
# DISCLAIMER:
# This software is provided "as is", without warranty of any kind.
# This project is a generic framework and includes no copyrighted assets.
# =========================================================================
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import threading
import time
import json
import os
import subprocess
import random
import re
import sys
import select
import traceback
import atexit
import datetime
import warnings
import wave
import struct
# Suppress harmless library warnings
warnings.filterwarnings("ignore", category=RuntimeWarning, module="duckduckgo_search")
# Core dependencies
import sounddevice as sd
import numpy as np
import scipy.signal
# --- AI ENGINES ---
import openwakeword
from openwakeword.model import Model
import ollama
# --- WEB SEARCH (Using your working import) ---
from duckduckgo_search import DDGS
# =========================================================================
# 1. CONFIGURATION & CONSTANTS
# =========================================================================
CONFIG_FILE = "config.json"
MEMORY_FILE = "memory.json"
BMO_IMAGE_FILE = "current_image.jpg"
WAKE_WORD_MODEL = "./wakeword.onnx"
WAKE_WORD_THRESHOLD = 0.5
# HARDWARE SETTINGS
INPUT_DEVICE_NAME = None
DEFAULT_CONFIG = {
"text_model": "gemma3:1b",
"vision_model": "moondream",
"voice_model": "piper/en_GB-semaine-medium.onnx",
"chat_memory": True,
"camera_rotation": 0,
"system_prompt_extras": "",
"input_device": None,
"input_sample_rate": None
}
# LLM SETTINGS
OLLAMA_OPTIONS = {
'keep_alive': '-1',
'num_thread': 4,
'temperature': 0.7,
'top_k': 40,
'top_p': 0.9
}
def load_config():
config = DEFAULT_CONFIG.copy()
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, "r") as f:
user_config = json.load(f)
config.update(user_config)
except Exception as e:
print(f"Config Error: {e}. Using defaults.")
return config
CURRENT_CONFIG = load_config()
TEXT_MODEL = CURRENT_CONFIG["text_model"]
VISION_MODEL = CURRENT_CONFIG["vision_model"]
def resolve_input_device(config):
requested = config.get("input_device")
if requested in (None, "", "default"):
return None
try:
devices = sd.query_devices()
except Exception as e:
print(f"[AUDIO] Device query failed: {e}", flush=True)
return None
if isinstance(requested, int) or (isinstance(requested, str) and requested.isdigit()):
index = int(requested)
if 0 <= index < len(devices):
return index
print(f"[AUDIO] Input device index not found: {index}", flush=True)
return None
requested_lower = str(requested).lower()
for idx, dev in enumerate(devices):
print(f"[AUDIO DEBUG] Index {idx}: {dev.get('name')} (In: {dev.get('max_input_channels')})", flush=True) # DEBUG LINE
if dev.get("max_input_channels", 0) > 0 and requested_lower in dev.get("name", "").lower():
return idx
print(f"[AUDIO] Input device name not found: {requested}", flush=True)
return None
INPUT_DEVICE_NAME = resolve_input_device(CURRENT_CONFIG)
if INPUT_DEVICE_NAME is not None:
try:
device_info = sd.query_devices(INPUT_DEVICE_NAME)
print(f"[AUDIO] Using input device: {device_info.get('name', INPUT_DEVICE_NAME)}", flush=True)
except Exception:
print(f"[AUDIO] Using input device index: {INPUT_DEVICE_NAME}", flush=True)
def choose_input_samplerate(device, preferred=None):
candidates = []
if preferred:
candidates.append(preferred)
try:
device_info = sd.query_devices(device)
print(f"[AUDIO DEBUG] Device Info: {device_info}", flush=True) # DEBUG
if "default_samplerate" in device_info:
candidates.append(int(device_info["default_samplerate"]))
except Exception as e:
print(f"[AUDIO DEBUG] Query failed: {e}", flush=True)
pass
candidates.extend([48000, 44100, 32000, 16000])
seen = set()
for rate in candidates:
if not rate or rate in seen:
continue
seen.add(rate)
try:
sd.check_input_settings(device=device, samplerate=rate, channels=1, dtype="int16")
return rate
except Exception:
continue
return int(candidates[0]) if candidates else 44100
class BotStates:
IDLE = "idle"
LISTENING = "listening"
THINKING = "thinking"
SPEAKING = "speaking"
ERROR = "error"
CAPTURING = "capturing"
WARMUP = "warmup"
# --- SYSTEM PROMPT ---
BASE_SYSTEM_PROMPT = """You are a helpful robot assistant running on a Raspberry Pi.
Personality: Cute, helpful, robot.
Style: Short sentences. Enthusiastic.
INSTRUCTIONS:
- If the user asks for a physical action (time, search, photo), output JSON.
- If the user just wants to chat, reply with NORMAL TEXT.
### EXAMPLES ###
User: What time is it?
You: {"action": "get_time", "value": "now"}
User: Hello!
You: Hi! I am ready to help!
User: Search for news about robots.
You: {"action": "search_web", "value": "robots news"}
User: What do you see right now?
You: {"action": "capture_image", "value": "environment"}
### END EXAMPLES ###
"""
SYSTEM_PROMPT = BASE_SYSTEM_PROMPT + "\n\n" + CURRENT_CONFIG.get("system_prompt_extras", "")
# Sound Directories
greeting_sounds_dir = "sounds/greeting_sounds"
ack_sounds_dir = "sounds/ack_sounds"
thinking_sounds_dir = "sounds/thinking_sounds"
error_sounds_dir = "sounds/error_sounds"
# =========================================================================
# 2. GUI CLASS
# =========================================================================
class BotGUI:
BG_WIDTH, BG_HEIGHT = 800, 480
OVERLAY_WIDTH, OVERLAY_HEIGHT = 400, 300
def __init__(self, master):
self.master = master
master.title("Pi Assistant")
master.attributes('-fullscreen', True)
master.bind('<Escape>', self.exit_fullscreen)
# Inputs
master.bind('<Return>', self.handle_ptt_toggle)
master.bind('<space>', self.handle_speaking_interrupt)
atexit.register(self.safe_exit)
# State
self.current_state = BotStates.WARMUP
self.current_volume = 0
self.animations = {}
self.current_frame_index = 0
self.current_overlay_image = None
self.permanent_memory = self.load_chat_history()
self.session_memory = []
self.thinking_sound_active = threading.Event()
self.last_ptt_time = 0
self.ptt_event = threading.Event()
self.recording_active = threading.Event()
self.interrupted = threading.Event()
self.tts_queue = []
self.tts_queue_lock = threading.Lock()
self.tts_thread = None
self.tts_active = threading.Event()
self.current_audio_process = None
self.exiting = False
# --- WAKE WORD INITIALIZATION ---
print("[INIT] Loading Wake Word...", flush=True)
self.oww_model = None
if os.path.exists(WAKE_WORD_MODEL):
try:
self.oww_model = Model(wakeword_model_paths=[WAKE_WORD_MODEL])
print("[INIT] Wake Word Loaded.", flush=True)
except TypeError:
try:
self.oww_model = Model(wakeword_models=[WAKE_WORD_MODEL])
print("[INIT] Wake Word Loaded (New API).", flush=True)
except Exception as e:
print(f"[CRITICAL] Failed to load model: {e}")
except Exception as e:
print(f"[CRITICAL] Failed to load model: {e}")
else:
print(f"[CRITICAL] Model not found: {WAKE_WORD_MODEL}")
# GUI Setup
self.background_label = tk.Label(master)
self.background_label.place(x=0, y=0, width=self.BG_WIDTH, height=self.BG_HEIGHT)
self.background_label.bind('<Button-1>', self.toggle_hud_visibility)
self.overlay_label = tk.Label(master, bg='black')
self.overlay_label.bind('<Button-1>', self.toggle_hud_visibility)
self.response_text = tk.Text(master, height=6, width=60, wrap=tk.WORD,
state=tk.DISABLED, bg="#ffffff", fg="#000000", font=('Arial', 12))
self.status_var = tk.StringVar(value="Initializing...")
self.status_label = ttk.Label(master, textvariable=self.status_var, background="#2e2e2e", foreground="white")
self.exit_button = ttk.Button(master, text="Exit & Save", command=self.safe_exit)
self.load_animations()
self.update_animation()
threading.Thread(target=self.safe_main_execution, daemon=True).start()
# --- HELPERS ---
def extract_json_from_text(self, text):
try:
match = re.search(r'\{.*\}', text, re.DOTALL)
if match:
return json.loads(match.group(0))
return None
except: return None
def safe_exit(self):
if self.exiting:
return
self.exiting = True
print("\n--- SHUTDOWN SEQUENCE ---", flush=True)
if self.current_audio_process:
try:
self.current_audio_process.terminate()
self.current_audio_process.wait(timeout=1)
except: pass
self.recording_active.clear()
self.thinking_sound_active.clear()
self.tts_active.clear()
self.save_chat_history()
try:
ollama.generate(model=TEXT_MODEL, prompt="", keep_alive=0)
except: pass
try:
sd.stop()
except: pass
try:
self.master.quit()
except Exception:
pass
def exit_fullscreen(self, event=None):
self.master.attributes('-fullscreen', False)
self.safe_exit()
def toggle_hud_visibility(self, event=None):
try:
if self.response_text.winfo_ismapped():
self.response_text.place_forget()
self.status_label.place_forget()
self.exit_button.place_forget()
else:
self.response_text.place(relx=0.5, rely=0.82, anchor=tk.S)
self.status_label.place(relx=0.5, rely=1.0, anchor=tk.S, relwidth=1)
self.exit_button.place(x=10, y=10)
except tk.TclError: pass
def handle_ptt_toggle(self, event=None):
current_time = time.time()
if current_time - self.last_ptt_time < 0.5:
return
self.last_ptt_time = current_time
if self.recording_active.is_set():
print("[PTT] Toggle OFF", flush=True)
self.recording_active.clear()
else:
if self.current_state == BotStates.IDLE or "Wait" in self.status_var.get():
print("[PTT] Toggle ON", flush=True)
self.recording_active.set()
self.ptt_event.set()
def handle_speaking_interrupt(self, event=None):
if self.current_state == BotStates.SPEAKING or self.current_state == BotStates.THINKING:
self.interrupted.set()
self.thinking_sound_active.clear()
with self.tts_queue_lock:
self.tts_queue.clear()
if self.current_audio_process:
try: self.current_audio_process.terminate()
except: pass
self.set_state(BotStates.IDLE, "Interrupted.")
def load_animations(self):
base_path = "faces"
states = ["idle", "listening", "thinking", "speaking", "error", "capturing", "warmup"]
for state in states:
folder = os.path.join(base_path, state)
self.animations[state] = []
if os.path.exists(folder):
files = sorted([f for f in os.listdir(folder) if f.lower().endswith('.png')])
for f in files:
img = Image.open(os.path.join(folder, f)).resize((self.BG_WIDTH, self.BG_HEIGHT))
self.animations[state].append(ImageTk.PhotoImage(img))
if not self.animations[state]:
if state in self.animations.get("idle", []):
self.animations[state] = self.animations["idle"]
else:
# Blue screen fallback
blank = Image.new('RGB', (self.BG_WIDTH, self.BG_HEIGHT), color='#0000FF')
self.animations[state].append(ImageTk.PhotoImage(blank))
def update_animation(self):
frames = self.animations.get(self.current_state, []) or self.animations.get(BotStates.IDLE, [])
if not frames:
self.master.after(500, self.update_animation)
return
if self.current_state == BotStates.SPEAKING:
if len(frames) > 1:
self.current_frame_index = random.randint(1, len(frames) - 1)
else:
self.current_frame_index = 0
else:
self.current_frame_index = (self.current_frame_index + 1) % len(frames)
self.background_label.config(image=frames[self.current_frame_index])
speed = 50 if self.current_state == BotStates.SPEAKING else 500
self.master.after(speed, self.update_animation)
def set_state(self, state, msg="", cam_path=None):
def _update():
if msg: print(f"[STATE] {state.upper()}: {msg}", flush=True)
if self.current_state != state:
self.current_state = state
self.current_frame_index = 0
if msg: self.status_var.set(msg)
if cam_path and os.path.exists(cam_path) and state in [BotStates.THINKING, BotStates.SPEAKING]:
try:
img = Image.open(cam_path).resize((self.OVERLAY_WIDTH, self.OVERLAY_HEIGHT))
self.current_overlay_image = ImageTk.PhotoImage(img)
self.overlay_label.config(image=self.current_overlay_image)
self.overlay_label.place(x=200, y=90)
except: pass
else:
self.overlay_label.place_forget()
self.master.after(0, _update)
def append_to_text(self, text, newline=True):
def _update():
self.response_text.config(state=tk.NORMAL)
if newline:
self.response_text.insert(tk.END, text + "\n")
else:
self.response_text.insert(tk.END, text)
self.response_text.see(tk.END)
self.response_text.config(state=tk.DISABLED)
self.master.after(0, _update)
def _stream_to_text(self, chunk):
def update_text_stream():
self.response_text.config(state=tk.NORMAL)
self.response_text.insert(tk.END, chunk)
self.response_text.see(tk.END)
self.response_text.config(state=tk.DISABLED)
self.master.after(0, update_text_stream)
# =========================================================================
# 3. ACTION ROUTER
# =========================================================================
def execute_action_and_get_result(self, action_data):
raw_action = action_data.get("action", "").lower().strip()
value = action_data.get("value") or action_data.get("query")
VALID_TOOLS = {
"get_time", "search_web", "capture_image"
}
ALIASES = {
"google": "search_web", "browser": "search_web", "news": "search_web",
"search_news": "search_web", "look": "capture_image", "see": "capture_image",
"check_time": "get_time"
}
action = ALIASES.get(raw_action, raw_action)
print(f"ACTION: {raw_action} -> {action}", flush=True)
if action not in VALID_TOOLS:
if value and isinstance(value, str) and len(value.split()) > 1:
return f"CHAT_FALLBACK::{value}"
return "INVALID_ACTION"
if action == "get_time":
now = datetime.datetime.now().strftime("%I:%M %p")
return f"The current time is {now}."
elif action == "search_web":
print(f"Searching web for: {value}...", flush=True)
try:
# 'us-en' region is often more stable for CLI queries
with DDGS() as ddgs:
results = []
# 1. News search
try:
results = list(ddgs.news(value, region='us-en', max_results=1))
if results:
print(f"[DEBUG] Found News: {results[0].get('title')}", flush=True)
except Exception as e:
print(f"[DEBUG] News Search Error: {e}", flush=True)
# 2. Text fallback
if not results:
print("[DEBUG] No news found, trying text search...", flush=True)
try:
results = list(ddgs.text(value, region='us-en', max_results=1))
if results:
print(f"[DEBUG] Found Text: {results[0].get('title')}", flush=True)
except Exception as e:
print(f"[DEBUG] Text Search Error: {e}", flush=True)
if results:
r = results[0]
# Safe get
title = r.get('title', 'No Title')
body = r.get('body', r.get('snippet', 'No Body'))
return f"SEARCH RESULTS for '{value}':\nTitle: {title}\nSnippet: {body[:300]}"
else:
print(f"[DEBUG] Search returned 0 results.", flush=True)
return "SEARCH_EMPTY"
except Exception as e:
print(f"[DEBUG] Connection/Library Error: {e}", flush=True)
return "SEARCH_ERROR"
elif action == "capture_image":
return "IMAGE_CAPTURE_TRIGGERED"
return None
# =========================================================================
# 4. CORE LOGIC
# =========================================================================
def safe_main_execution(self):
try:
self.warm_up_logic()
self.tts_active.set()
self.tts_thread = threading.Thread(target=self._tts_worker, daemon=True)
self.tts_thread.start()
while True:
trigger_source = self.detect_wake_word_or_ptt()
if self.interrupted.is_set():
self.interrupted.clear()
self.set_state(BotStates.IDLE, "Resetting...")
continue
self.set_state(BotStates.LISTENING, "I'm listening!")
audio_file = None
if trigger_source == "PTT":
audio_file = self.record_voice_ptt()
else:
audio_file = self.record_voice_adaptive()
if not audio_file:
self.set_state(BotStates.IDLE, "Heard nothing.")
continue
user_text = self.transcribe_audio(audio_file)
if not user_text:
self.set_state(BotStates.IDLE, "Transcription empty.")
continue
self.append_to_text(f"YOU: {user_text}")
self.interrupted.clear()
self.chat_and_respond(user_text, img_path=None)
except Exception as e:
traceback.print_exc()
self.set_state(BotStates.ERROR, f"Fatal Error: {str(e)[:40]}")
def warm_up_logic(self):
self.set_state(BotStates.WARMUP, "Warming up brains...")
try:
ollama.generate(model=TEXT_MODEL, prompt="", keep_alive=-1)
except Exception as e:
print(f"Failed to load {TEXT_MODEL}: {e}", flush=True)
self.play_sound(self.get_random_sound(greeting_sounds_dir))
print("Models loaded.", flush=True)
def detect_wake_word_or_ptt(self):
self.set_state(BotStates.IDLE, "Waiting...")
self.ptt_event.clear()
if self.oww_model: self.oww_model.reset()
if self.oww_model is None:
self.ptt_event.wait()
self.ptt_event.clear()
return "PTT"
CHUNK_SIZE = 1280
OWW_SAMPLE_RATE = 16000
input_rate = choose_input_samplerate(INPUT_DEVICE_NAME, CURRENT_CONFIG.get("input_sample_rate"))
use_resampling = (input_rate != OWW_SAMPLE_RATE)
input_chunk_size = int(CHUNK_SIZE * (input_rate / OWW_SAMPLE_RATE)) if use_resampling else CHUNK_SIZE
stream_args = {
"samplerate": input_rate,
"channels": 1,
"dtype": 'int16',
"blocksize": input_chunk_size,
"device": INPUT_DEVICE_NAME
}
# Try to find a compatible block size and sample rate
try:
# First attempt: standard settings
self._listen_loop(stream_args, input_chunk_size, CHUNK_SIZE, use_resampling)
except StopIteration as si:
return str(si)
except Exception as e:
print(f"[AUDIO] Stream failed with defaults: {e}. Retrying with loose settings...", flush=True)
try:
# Second attempt: Let PortAudio decide blocksize (0) and latency
stream_args["blocksize"] = 0
stream_args["latency"] = "high"
# If blocksize is variable, we must read specific amounts manually or handle buffering.
# Simplest fallback: Just attempt small fixed block
stream_args["blocksize"] = 1024
use_resampling = True
self._listen_loop(stream_args, 1024, CHUNK_SIZE, use_resampling)
except StopIteration as si:
return str(si)
except Exception as e2:
print(f"[CRITICAL] Wake Word Stream Error: {e2}")
self.ptt_event.wait()
return "PTT"
return "WAKE"
def _listen_loop(self, stream_args, input_chunk_size, target_chunk_size, use_resampling):
# Force software backend (no mmap) via environment variable if possible,
# but here we can try to hint loop settings.
# However, the most effective fix for ALSA mmap issues is often just asking for 'blocksize=0'
# and letting portaudio manage the buffering, OR very small chunks.
# Let's try to be less aggressive with reads.
with sd.InputStream(**stream_args) as stream:
print(f"[AUDIO] Listening with rate {stream_args['samplerate']} and block {stream_args['blocksize']}", flush=True)
# Pre-allocate buffer for speed
# If blocksize is 0, we read what is available.
while True:
if self.ptt_event.is_set():
self.ptt_event.clear()
raise StopIteration("PTT")
rlist, _, _ = select.select([sys.stdin], [], [], 0.001)
if rlist:
sys.stdin.readline()
raise StopIteration("CLI")
# If fallback mode (blocksize 0), read fixed amount
read_size = input_chunk_size
if stream_args.get('blocksize') == 0:
read_size = 1024 # Safe small read
try:
data, overflow = stream.read(read_size)
if overflow:
print("!", end="", flush=True)
# If we overflow excessively, raise error to trigger fallback to SAFE MODE (PulseAudio/Software)
# We can use a simple counter attached to the function or object, but here raising immediately
# after a few in a row is safest.
raise RuntimeError("Audio Buffer Overflow - Triggering Safe Mode")
except Exception as e:
# Convert uncatchable PaErrorCode wrapper to standard Exception if needed
# But honestly, `raise e` should work... unless it's a SystemExit?
# Let's wrap it in a new exception to be sure it bubbles up
raise RuntimeError(f"Audio read failed: {e}")
audio_data = np.frombuffer(data, dtype=np.int16)
# Ensure flattening for openwakeword compatibility
if audio_data.ndim > 1:
audio_data = audio_data.flatten()
if use_resampling:
# FAST RESAMPLING: Nearest-neighbor slicing instead of scipy.signal.resample
# This avoids the CPU bottleneck that causes overflow (!!!!!!!) on Raspberry Pi
step = len(audio_data) / target_chunk_size
indices = np.arange(0, len(audio_data), step)[:target_chunk_size].astype(int)
audio_data = audio_data[indices]
# Convert to float for model prediction without needing heavy resampling logic
# The wake word model needs 16000, which we just faked above.
# Debug volume occasionally
current_max = np.max(np.abs(audio_data))
# Only predict if volume is significant to save CPU
if current_max > 200:
prediction = self.oww_model.predict(audio_data)
for mdl in self.oww_model.prediction_buffer.keys():
score = list(self.oww_model.prediction_buffer[mdl])[-1]
if score > 0.1: # Show potential triggers
print(f"\r[Oww] Score: {score:.3f} | Vol: {current_max} ", end="", flush=True)
if score > WAKE_WORD_THRESHOLD:
print(f"\n[WAKE] Triggered on '{mdl}' with score: {score:.2f}", flush=True)
self.oww_model.reset()
return # Success
def record_voice_adaptive(self, filename="input.wav"):
print("Recording (Adaptive)...", flush=True)
time.sleep(0.5)
samplerate = choose_input_samplerate(INPUT_DEVICE_NAME, CURRENT_CONFIG.get("input_sample_rate"))
silence_threshold = 0.006
silence_duration = 1.5
max_record_time = 30.0
buffer = []
silent_chunks = 0
chunk_duration = 0.05
chunk_size = int(samplerate * chunk_duration)
num_silent_chunks = int(silence_duration / chunk_duration)
max_chunks = int(max_record_time / chunk_duration)
recorded_chunks = 0
silence_started = False
def callback(indata, frames, time_info, status):
nonlocal silent_chunks, recorded_chunks, silence_started
volume_norm = np.linalg.norm(indata) / np.sqrt(len(indata))
buffer.append(indata.copy())
recorded_chunks += 1
if recorded_chunks < 5: return
if volume_norm < silence_threshold:
silent_chunks += 1
if silent_chunks >= num_silent_chunks: silence_started = True
else: silent_chunks = 0
try:
# Explicitly close stream if it exists to free hardware
sd.stop()
time.sleep(0.2)
with sd.InputStream(samplerate=samplerate, channels=1, callback=callback,
device=INPUT_DEVICE_NAME, blocksize=chunk_size):
while not silence_started and recorded_chunks < max_chunks:
sd.sleep(int(chunk_duration * 1000))
except Exception as e:
print(f"[AUDIO ERROR] Adaptive Recording Failed: {e}", flush=True)
return None
return self.save_audio_buffer(buffer, filename, samplerate)
def record_voice_ptt(self, filename="input.wav"):
print("Recording (PTT)...", flush=True)
time.sleep(0.5)
samplerate = choose_input_samplerate(INPUT_DEVICE_NAME, CURRENT_CONFIG.get("input_sample_rate"))
buffer = []
def callback(indata, frames, time_info, status): buffer.append(indata.copy())
try:
# Explicitly close stream if it exists to free hardware
# This is critical on Pi 5 where hardware contention causes freezes
sd.stop()
time.sleep(0.2)
with sd.InputStream(samplerate=samplerate, channels=1, callback=callback, device=INPUT_DEVICE_NAME):
while self.recording_active.is_set():
sd.sleep(50)
except Exception as e:
print(f"[AUDIO ERROR] PTT Recording Failed: {e}", flush=True)
return None
return self.save_audio_buffer(buffer, filename, samplerate)
def save_audio_buffer(self, buffer, filename, samplerate=16000):
if not buffer: return None
audio_data = np.concatenate(buffer, axis=0).flatten()
audio_data = np.nan_to_num(audio_data, nan=0.0, posinf=0.0, neginf=0.0)
audio_data = (audio_data * 32767).astype(np.int16)
with wave.open(filename, "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(samplerate)
wf.writeframes(audio_data.tobytes())
self.play_sound(self.get_random_sound(ack_sounds_dir))
return filename
def transcribe_audio(self, filename):
print("Transcribing...", flush=True)
try:
result = subprocess.run(
["./whisper.cpp/build/bin/whisper-cli", "-m", "./whisper.cpp/models/ggml-base.en.bin", "-l", "en", "-t", "4", "-f", filename],
capture_output=True, text=True
)
transcription_lines = result.stdout.strip().split('\n')
if transcription_lines and transcription_lines[-1].strip():
last_line = transcription_lines[-1].strip()
if ']' in last_line: transcription = last_line.split("]")[1].strip()
else: transcription = last_line
else: transcription = ""
print(f"Heard: '{transcription}'", flush=True)
return transcription.strip()
except Exception as e:
print(f"Transcription Error: {e}")
return ""
def capture_image(self):
self.set_state(BotStates.CAPTURING, "Watching...")
try:
subprocess.run(["rpicam-still", "-t", "500", "-n", "--width", "640", "--height", "480", "-o", BMO_IMAGE_FILE], check=True)
rotation = CURRENT_CONFIG.get("camera_rotation", 0)
if rotation != 0:
img = Image.open(BMO_IMAGE_FILE)
img = img.rotate(rotation, expand=True)
img.save(BMO_IMAGE_FILE)
return BMO_IMAGE_FILE
except Exception as e:
print(f"Camera Error: {e}")
return None
# =========================================================================
# 5. CHAT & RESPOND
# =========================================================================
def chat_and_respond(self, text, img_path=None):
if "forget everything" in text.lower() or "reset memory" in text.lower():
self.session_memory = []
self.permanent_memory = [{"role": "system", "content": SYSTEM_PROMPT}]
self.save_chat_history()
with self.tts_queue_lock:
self.tts_queue.append("Okay. Memory wiped.")
self.set_state(BotStates.IDLE, "Memory Wiped")
return
model_to_use = VISION_MODEL if img_path else TEXT_MODEL
self.set_state(BotStates.THINKING, "Thinking...", cam_path=img_path)
messages = []
if img_path:
messages = [{"role": "user", "content": text, "images": [img_path]}]
else:
user_msg = {"role": "user", "content": text}
messages = self.permanent_memory + self.session_memory + [user_msg]
self.thinking_sound_active.set()
threading.Thread(target=self._run_thinking_sound_loop, daemon=True).start()
full_response_buffer = ""
sentence_buffer = ""
try:
stream = ollama.chat(model=model_to_use, messages=messages, stream=True, options=OLLAMA_OPTIONS)
is_action_mode = False
for chunk in stream:
if self.interrupted.is_set(): break
content = chunk['message']['content']
full_response_buffer += content
if '{"' in content or "action:" in content.lower():
is_action_mode = True
self.thinking_sound_active.clear()
continue
if is_action_mode: continue
self.thinking_sound_active.clear()
if self.current_state != BotStates.SPEAKING:
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self._stream_to_text(content)
sentence_buffer += content
if any(punct in content for punct in ".!?\n"):
clean_sentence = sentence_buffer.strip()
if clean_sentence and re.search(r'[a-zA-Z0-9]', clean_sentence):
with self.tts_queue_lock: self.tts_queue.append(clean_sentence)
sentence_buffer = ""
if is_action_mode:
action_data = self.extract_json_from_text(full_response_buffer)
if action_data:
tool_result = self.execute_action_and_get_result(action_data)
if tool_result and tool_result.startswith("CHAT_FALLBACK::"):
chat_text = tool_result.split("::", 1)[1]
self.thinking_sound_active.clear()
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self.append_to_text(chat_text, newline=True)
with self.tts_queue_lock: self.tts_queue.append(chat_text)
self.session_memory.append({"role": "assistant", "content": chat_text})
self.wait_for_tts()
self.set_state(BotStates.IDLE, "Ready")
return
if tool_result == "IMAGE_CAPTURE_TRIGGERED":
new_img_path = self.capture_image()
if new_img_path:
self.chat_and_respond(text, img_path=new_img_path)
return
elif tool_result == "INVALID_ACTION":
fallback_text = "I am not sure how to do that."
self.thinking_sound_active.clear()
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self.append_to_text(fallback_text, newline=True)
with self.tts_queue_lock: self.tts_queue.append(fallback_text)
elif tool_result == "SEARCH_EMPTY":
fallback_text = "I searched, but I couldn't find any news about that."
self.thinking_sound_active.clear()
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self.append_to_text(fallback_text, newline=True)
with self.tts_queue_lock: self.tts_queue.append(fallback_text)
elif tool_result == "SEARCH_ERROR":
fallback_text = "I cannot reach the internet right now."
self.thinking_sound_active.clear()
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self.append_to_text(fallback_text, newline=True)
with self.tts_queue_lock: self.tts_queue.append(fallback_text)
elif tool_result:
summary_prompt = [
{"role": "system", "content": "Summarize this result in one short sentence."},
{"role": "user", "content": f"RESULT: {tool_result}\nUser Question: {text}"}
]
self.set_state(BotStates.THINKING, "Reading...")
self.thinking_sound_active.set()
final_resp = ollama.chat(model=model_to_use, messages=summary_prompt, stream=False, options=OLLAMA_OPTIONS)
final_text = final_resp['message']['content']
self.thinking_sound_active.clear()
self.set_state(BotStates.SPEAKING, "Speaking...", cam_path=img_path)
self.append_to_text("BOT: ", newline=False)
self.append_to_text(final_text, newline=True)
with self.tts_queue_lock: self.tts_queue.append(final_text)
self.session_memory.append({"role": "assistant", "content": final_text})
else:
self.append_to_text("")
self.session_memory.append({"role": "assistant", "content": full_response_buffer})
self.wait_for_tts()
self.set_state(BotStates.IDLE, "Ready")
except Exception as e:
print(f"LLM Error: {e}")
self.set_state(BotStates.ERROR, "Brain Freeze!")
def wait_for_tts(self):
while self.tts_queue or self.tts_active.is_set():
if self.interrupted.is_set(): break
time.sleep(0.1)
def _tts_worker(self):
while True:
text = None
with self.tts_queue_lock:
if self.tts_queue:
text = self.tts_queue.pop(0)
self.tts_active.set()
if text:
self.speak(text)
self.tts_active.clear()
else: time.sleep(0.05)
def speak(self, text):
clean = re.sub(r"[^\w\s,.!?:-]", "", text)
if not clean.strip(): return
print(f"[PIPER SPEAKING] '{clean}'", flush=True)
voice_model = CURRENT_CONFIG.get("voice_model", "piper/en_GB-semaine-medium.onnx")
try:
self.current_audio_process = subprocess.Popen(
["./piper/piper", "--model", voice_model, "--output-raw"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
self.current_audio_process.stdin.write(clean.encode() + b'\n')
self.current_audio_process.stdin.close()
try:
device_info = sd.query_devices(kind='output')
native_rate = int(device_info['default_samplerate'])
except:
native_rate = 48000
PIPER_RATE = 22050
use_native_rate = False
try:
sd.check_output_settings(device=None, samplerate=PIPER_RATE)
except:
use_native_rate = True
with sd.RawOutputStream(samplerate=native_rate if use_native_rate else PIPER_RATE,
channels=1, dtype='int16',
device=None, latency='low', blocksize=2048) as stream:
while True:
if self.interrupted.is_set(): break
data = self.current_audio_process.stdout.read(4096)
if not data: break