fix: improve streaming, alarms, and AI TTS
This commit is contained in:
68
app/main.py
68
app/main.py
@@ -2,14 +2,9 @@
|
||||
Smart Speaker - Main Application
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import os
|
||||
import queue
|
||||
import re
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
|
||||
@@ -566,38 +561,8 @@ def main():
|
||||
# AI запрос
|
||||
chat_history.append({"role": "user", "content": user_text})
|
||||
|
||||
# Очередь для TTS
|
||||
tts_q = queue.Queue()
|
||||
interrupt_event = threading.Event()
|
||||
|
||||
def tts_worker():
|
||||
"""Фоновый поток для озвучки."""
|
||||
while True:
|
||||
item = tts_q.get()
|
||||
if item is None:
|
||||
tts_q.task_done()
|
||||
break
|
||||
|
||||
text, lang = item
|
||||
|
||||
if interrupt_event.is_set():
|
||||
tts_q.task_done()
|
||||
continue
|
||||
|
||||
completed = speak(
|
||||
text, check_interrupt=check_wakeword_once, language=lang
|
||||
)
|
||||
|
||||
if not completed:
|
||||
interrupt_event.set()
|
||||
|
||||
tts_q.task_done()
|
||||
|
||||
worker_thread = threading.Thread(target=tts_worker, daemon=True)
|
||||
worker_thread.start()
|
||||
|
||||
full_response = ""
|
||||
buffer = ""
|
||||
interrupted = False
|
||||
|
||||
try:
|
||||
# Streaming от AI
|
||||
@@ -606,33 +571,20 @@ def main():
|
||||
print("🤖 AI: ", end="", flush=True)
|
||||
|
||||
for chunk in stream_generator:
|
||||
if interrupt_event.is_set():
|
||||
break
|
||||
|
||||
buffer += chunk
|
||||
full_response += chunk
|
||||
print(chunk, end="", flush=True)
|
||||
|
||||
# Конец предложения
|
||||
if re.search(r"[.!?\n]+(?:\s|$)", buffer):
|
||||
clean_chunk = clean_response(buffer, language="ru")
|
||||
if clean_chunk.strip():
|
||||
tts_q.put((clean_chunk, "ru"))
|
||||
buffer = ""
|
||||
|
||||
# Остаток
|
||||
if buffer.strip() and not interrupt_event.is_set():
|
||||
clean_chunk = clean_response(buffer, language="ru")
|
||||
if clean_chunk.strip():
|
||||
tts_q.put((clean_chunk, "ru"))
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Ошибка: {e}")
|
||||
speak("Произошла ошибка при получении ответа.")
|
||||
|
||||
# Ждем окончания озвучки
|
||||
tts_q.put(None)
|
||||
worker_thread.join()
|
||||
else:
|
||||
clean_ai_response = clean_response(full_response, language="ru")
|
||||
if clean_ai_response.strip():
|
||||
interrupted = not speak(
|
||||
clean_ai_response,
|
||||
check_interrupt=check_wakeword_once,
|
||||
language="ru",
|
||||
)
|
||||
|
||||
print()
|
||||
|
||||
@@ -643,7 +595,7 @@ def main():
|
||||
stop_wakeword_monitoring()
|
||||
skip_wakeword = True
|
||||
|
||||
if interrupt_event.is_set():
|
||||
if interrupted:
|
||||
print("⏹️ Ответ прерван")
|
||||
|
||||
print()
|
||||
|
||||
Reference in New Issue
Block a user