"""Speech-to-text: faster-whisper on the tower-of-joy GPU. Import of faster_whisper is deferred so the gateway can run (health checks, protocol tests) without the heavy speech extras installed. """ from .config import settings _model = None def transcribe(pcm: bytes, sample_rate: int | None = None) -> str: """Transcribe raw s16le mono PCM to text.""" global _model if _model is None: from faster_whisper import WhisperModel _model = WhisperModel(settings.stt_model, device=settings.stt_device) import numpy as np audio = np.frombuffer(pcm, dtype=np.int16).astype(np.float32) / 32768.0 segments, _info = _model.transcribe(audio, language="en") return " ".join(segment.text.strip() for segment in segments).strip()