zelotech / api /routes_audio.py
matheusalencar's picture
first commit
1b64148
Raw History Blame Contribute Delete
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"])
@router.post("/transcribe")
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)