File size: 1,021 Bytes
1b64148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)