voice.py
Ricky/RAG_project/io/voice.py
"""
Voice input (Cantonese / Google STT) and console text helpers.
"""
import logging
from config.runtime import (
get_voice_language,
get_voice_timeout,
get_voice_phrase_time_limit,
)
logger = logging.getLogger(__name__)
try:
import speech_recognition as sr
except Exception:
sr = None # type: ignore
def to_console_text(text: str) -> str:
"""Encode to cp950 (Windows Traditional Chinese) to avoid console errors.
Falls back to the original string on non-Windows or unsupported characters.
"""
try:
return text.encode("cp950", errors="replace").decode("cp950")
except Exception:
return text
def init_voice_input():
"""Return (recognizer, microphone) or (None, None) if unavailable."""
if sr is None:
return None, None
try:
recognizer = sr.Recognizer()
microphone = sr.Microphone()
return recognizer, microphone
except Exception as e:
logger.warning("Voice input init failed: %s", e)
return None, None
def get_voice_question(recognizer, microphone) -> str | None:
"""Record audio and transcribe it; returns transcribed text or None."""
if not recognizer or not microphone:
return None
language = get_voice_language()
timeout = get_voice_timeout()
phrase_time_limit = get_voice_phrase_time_limit()
try:
with microphone as source:
recognizer.adjust_for_ambient_noise(source, duration=0.4)
print("[VOICE] Please speak your question now...")
audio = recognizer.listen(
source,
timeout=timeout,
phrase_time_limit=phrase_time_limit,
)
text = recognizer.recognize_google(audio, language=language).strip()
if text:
print(f"[VOICE] Recognized: {to_console_text(text)}")
return text
return None
except sr.WaitTimeoutError:
print("[WARN] Voice input timed out. Please try again.")
return None
except sr.UnknownValueError:
print("[WARN] Could not understand audio. Please try again.")
return None
except Exception as e:
logger.warning("Speech recognition failed: %s", e)
print(f"[WARN] Speech-to-text failed: {e}")
return None
関連記事
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
記事を読む →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
記事を読む →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
記事を読む →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
記事を読む →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
記事を読む →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
記事を読む →