-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
98 lines (77 loc) · 3.02 KB
/
app.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
"""
app.py
Main entry point for the TARS-AI application.
Initializes modules, loads configuration, and manages key threads for functionality such as:
- Speech-to-text (STT)
- Text-to-speech (TTS)
- Bluetooth control
- AI response generation
Run this script directly to start the application.
"""
# === Standard Libraries ===
import os
import sys
import threading
from datetime import datetime
# === Custom Modules ===
from module_config import load_config
from module_character import CharacterManager
from module_memory import MemoryManager
from module_stt import STTManager
from module_tts import update_tts_settings
from module_btcontroller import *
from module_main import initialize_managers, wake_word_callback, utterance_callback, post_utterance_callback, start_bt_controller_thread
from module_vision import initialize_blip
# === Constants and Globals ===
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(BASE_DIR)
sys.path.insert(0, BASE_DIR)
sys.path.append(os.getcwd())
CONFIG = load_config()
# === Helper Functions ===
def init_app():
"""
Performs initial setup for the application
"""
print(f"LOAD: Script running from: {BASE_DIR}")
#print(f"DEBUG: init_app() called")
# Load the configuration
CONFIG = load_config()
if CONFIG['TTS']['ttsoption'] == 'xttsv2':
update_tts_settings(CONFIG['TTS']['ttsurl'])
# === Main Application Logic ===
if __name__ == "__main__":
# Perform initial setup
init_app()
# Create a shutdown event for global threads
shutdown_event = threading.Event()
# Initialize CharacterManager, MemoryManager
char_manager = CharacterManager(config=CONFIG)
memory_manager = MemoryManager(config=CONFIG, char_name=char_manager.char_name, char_greeting=char_manager.char_greeting)
# Initialize STTManager
stt_manager = STTManager(config=CONFIG, shutdown_event=shutdown_event)
stt_manager.set_wake_word_callback(wake_word_callback)
stt_manager.set_utterance_callback(utterance_callback)
stt_manager.set_post_utterance_callback(post_utterance_callback)
# Pass managers to main module
initialize_managers(memory_manager, char_manager, stt_manager)
# Start necessary threads
bt_controller_thread = threading.Thread(target=start_bt_controller_thread, name="BTControllerThread", daemon=True)
bt_controller_thread.start()
# Initilize BLIP to speed up initial image capture
if not CONFIG['VISION']['server_hosted']:
initialize_blip()
try:
print(f"LOAD: TARS-AI v1.00 running.")
# Start the STT thread
stt_manager.start()
while not shutdown_event.is_set():
time.sleep(0.1) # Sleep to reduce CPU usage
except KeyboardInterrupt:
print(f"INFO: Stopping all threads and shutting down executor...")
shutdown_event.set() # Signal global threads to shutdown
# executor.shutdown(wait=True)
finally:
stt_manager.stop()
bt_controller_thread.join()
print(f"INFO: All threads and executor stopped gracefully.")