Spaces:
Sleeping
Sleeping
| import logging | |
| from deepgram import AsyncDeepgramClient | |
| from src.config import DEEPGRAM_API_KEY, DEEPGRAM_LANGUAGE | |
| logger = logging.getLogger(__name__) | |
| async def transcribe_audio(data: bytes, mimetype: str = "audio/wav") -> dict: | |
| """ | |
| Transcribes a full audio file using Deepgram pre-recorded API. | |
| Returns dict with 'text' (full transcript) and 'duration' (seconds). | |
| """ | |
| client = AsyncDeepgramClient(api_key=DEEPGRAM_API_KEY) | |
| response = await client.listen.v1.media.transcribe_file( | |
| request=data, | |
| model="nova-2", | |
| language=DEEPGRAM_LANGUAGE, | |
| smart_format=True, | |
| ) | |
| try: | |
| transcript = response.results.channels[0].alternatives[0].transcript | |
| except (AttributeError, IndexError): | |
| transcript = "" | |
| try: | |
| duration = response.metadata.duration | |
| except AttributeError: | |
| duration = None | |
| logger.info("STT transcript (%ss): %s", duration, transcript[:80]) | |
| return {"text": transcript, "language": DEEPGRAM_LANGUAGE, "duration": duration} | |