File size: 2,029 Bytes
93badc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# transcription_routes.py

import os
import io
from fastapi import APIRouter, File, UploadFile, HTTPException, Request
from fastapi.responses import JSONResponse
from groq import Groq
from groq.errors import GroqError

router = APIRouter()

# Load your Groq API key
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    raise ValueError("GROQ_API_KEY environment variable is not set")

client = Groq(api_key=GROQ_API_KEY)

@router.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=500, content={"detail": str(exc)}
    )

@router.post(
    "/transcribe",
    summary="Upload an audio file and return its transcription",
    response_description="Returns transcribed text as JSON"
)
async def transcribe_audio(file: UploadFile = File(...)):
    if not file.filename:
        raise HTTPException(status_code=400, detail="No file provided")

    # Only allow common audio formats
    allowed_exts = (".mp3", ".wav", ".m4a", ".flac")
    if not file.filename.lower().endswith(allowed_exts):
        raise HTTPException(
            status_code=415,
            detail=f"Unsupported file type. Allowed: {', '.join(allowed_exts)}"
        )

    data = await file.read()
    try:
        # send to Groq Whisper model
        resp = client.audio.transcriptions.create(
            file=(file.filename, data),
            model="whisper-large-v3",
            response_format="verbose_json",
        )
        # the library returns an object with a `.text` attribute
        transcript = getattr(resp, "text", None)
        if transcript is None:
            # fallback if resp is dict-like
            transcript = resp.get("text")  # type: ignore
    except GroqError as e:
        raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")

    return JSONResponse(content={"transcript": transcript})