"""Speech-to-text for the actual query, after the wake word fires. Deepgram, same as desk_client/bolt_desk.py. """ from __future__ import annotations import requests from .. import config from .mic import pcm_to_wav_bytes class SttError(Exception): pass def transcribe(pcm) -> str: if not config.DEEPGRAM_API_KEY: raise SttError("DEEPGRAM_API_KEY is not set") try: response = requests.post( "https://api.deepgram.com/v1/listen", params={"model": config.DEEPGRAM_MODEL, "language": "en", "smart_format": "true"}, headers={ "Authorization": f"Token {config.DEEPGRAM_API_KEY}", "Content-Type": "audio/wav", }, data=pcm_to_wav_bytes(pcm), timeout=30, ) response.raise_for_status() except Exception as exc: raise SttError(f"transcription request failed: {exc}") from exc try: return ( response.json() .get("results", {}).get("channels", [{}])[0] .get("alternatives", [{}])[0].get("transcript", "") ).strip() except Exception as exc: raise SttError(f"couldn't parse transcription response: {exc}") from exc