Spaces:
Running
Running
File size: 1,728 Bytes
93badc8 4536d7e 93badc8 4536d7e 93badc8 4536d7e 93badc8 4536d7e ae9d8c1 4536d7e 93badc8 4536d7e ae9d8c1 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 |
# transcription_routes.py
import os
from fastapi import APIRouter, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from groq import Groq
router = APIRouter(prefix="/transcribe", tags=["transcription"])
# 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.post(
"/audio",
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:
resp = client.audio.transcriptions.create(
file=(file.filename, data),
model="whisper-large-v3",
response_format="verbose_json",
)
# The client returns .text or, if dict-like, resp.get("text")
transcript = getattr(resp, "text", None) or resp.get("text")
if transcript is None:
raise ValueError("No transcript returned by service")
except Exception as e:
# All errors return 502 with the exception message
raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
return JSONResponse(content={"transcript": transcript})
|