Download api/routes_audio.py from ZeloTech/zelotech: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/ZeloTech/zelotech/resolve/main/api/routes_audio.py
- Command line
-
hf download hf://spaces/ZeloTech/zelotech/api/routes_audio.py
-
curl -L -o routes_audio.py https://huggingface.co/spaces/ZeloTech/zelotech/resolve/main/api/routes_audio.py
1.02 kB
| from fastapi import APIRouter, Depends, File, HTTPException, UploadFile | |
| from api.deps import get_transcriber | |
| from domain.transcriber import Transcriber | |
| from services.audio_utils import normalize_to_wav | |
| from services.files import cleanup, save_upload_to_temp | |
| router = APIRouter(tags=["audio"]) | |
| async def transcribe( | |
| file: UploadFile = File(...), | |
| transcriber: Transcriber = Depends(get_transcriber), | |
| ): | |
| """Recebe audio (ogg/opus do WhatsApp, mp3, m4a, wav...) e devolve a transcricao em PT-BR.""" | |
| src = await save_upload_to_temp(file) | |
| wav = None | |
| try: | |
| try: | |
| wav = normalize_to_wav(src) | |
| except RuntimeError: | |
| raise HTTPException( | |
| status_code=422, | |
| detail="Nao foi possivel decodificar o audio. Envie ogg/opus, mp3, m4a ou wav.", | |
| ) | |
| text = transcriber.transcribe(wav, language="pt") | |
| return {"text": text} | |
| finally: | |
| cleanup(src, wav) | |