Spaces:
Runtime error
Runtime error
Create speech.py
Browse files
speech.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio, threading
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
from TTS.api import TTS
|
| 4 |
+
from logger import logger
|
| 5 |
+
|
| 6 |
+
# Load TTS
|
| 7 |
+
from config import cfg
|
| 8 |
+
tts = TTS(cfg["tts_model"])
|
| 9 |
+
|
| 10 |
+
# Async wrapper for TTS
|
| 11 |
+
def tts_speak(text: str, filename: str):
|
| 12 |
+
try:
|
| 13 |
+
tts.tts_to_file(text=text, file_path=filename)
|
| 14 |
+
logger.info(f"TTS generated at {filename}")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
logger.error("TTS error", exc_info=e)
|
| 17 |
+
|
| 18 |
+
def speak_async(text: str, filename="response.wav"):
|
| 19 |
+
thread = threading.Thread(target=tts_speak, args=(text, filename), daemon=True)
|
| 20 |
+
thread.start()
|
| 21 |
+
|
| 22 |
+
# Speech-to-text
|
| 23 |
+
def listen() -> str:
|
| 24 |
+
recognizer = sr.Recognizer()
|
| 25 |
+
with sr.Microphone() as source:
|
| 26 |
+
logger.info("Listening for scenario...")
|
| 27 |
+
audio = recognizer.listen(source)
|
| 28 |
+
try:
|
| 29 |
+
text = recognizer.recognize_google(audio)
|
| 30 |
+
logger.info(f"Transcribed audio: {text}")
|
| 31 |
+
return text
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logger.error("STT error", exc_info=e)
|
| 34 |
+
return ""
|