Spaces:
Running
Running
# 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) | |
async def global_exception_handler(request: Request, exc: Exception): | |
return JSONResponse( | |
status_code=500, content={"detail": str(exc)} | |
) | |
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}) | |