Download app/stt/sarvam_client.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 2.13 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/stt/sarvam_client.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/stt/sarvam_client.py
-
curl -L -o sarvam_client.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/stt/sarvam_client.py
2.13 kB
| import logging | |
| import httpx | |
| from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type | |
| from app.config import settings | |
| logger = logging.getLogger(__name__) | |
| MAX_AUDIO_SIZE_BYTES = 10 * 1024 * 1024 # 10 MB | |
| async def transcribe_audio(audio_bytes: bytes, language: str = "gu-In") -> dict: | |
| """ | |
| Send audio to Sarvam AI Saaras v3 API and return the transcript. | |
| Args: | |
| audio_bytes: Raw audio bytes (WebM/Opus from MediaRecorder). | |
| language: Language hint (e.g. 'gu-In', 'hi-In', 'en-In'). | |
| Returns: | |
| dict with 'transcript' and 'language_detected' keys. | |
| """ | |
| if len(audio_bytes) > MAX_AUDIO_SIZE_BYTES: | |
| raise ValueError(f"Audio file too large: {len(audio_bytes)} bytes (max {MAX_AUDIO_SIZE_BYTES})") | |
| async with httpx.AsyncClient(timeout=30.0) as client: | |
| response = await client.post( | |
| settings.sarvam_api_url, | |
| headers={"api-subscription-key": settings.sarvam_api_key}, | |
| files={"file": ("audio.webm", audio_bytes, "audio/webm")}, | |
| data={ | |
| "model": "saaras:v3", | |
| "mode": "transcribe", | |
| "language_code": language, | |
| }, | |
| ) | |
| if response.status_code == 400: | |
| logger.warning("Sarvam API 400: %s", response.text) | |
| raise httpx.HTTPStatusError( | |
| f"Sarvam API error: {response.text}", | |
| request=response.request, | |
| response=response, | |
| ) | |
| response.raise_for_status() | |
| result = response.json() | |
| transcript = result.get("transcript", "") | |
| lang_detected = result.get("language_code", language) | |
| logger.info("Sarvam STT: '%s...' (lang=%s)", transcript[:60], lang_detected) | |
| return { | |
| "transcript": transcript, | |
| "language_detected": lang_detected, | |
| } | |