Add /api/transcribe endpoint with Whisper
This commit is contained in:
+55
-1
@@ -2,10 +2,15 @@
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, File, HTTPException, UploadFile
|
||||
|
||||
from app.services.transcriber import transcribe_bytes
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
# File size limit: 10 MB
|
||||
MAX_UPLOAD_SIZE = 10 * 1024 * 1024
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health():
|
||||
@@ -16,3 +21,52 @@ async def health():
|
||||
"version": "0.1.0",
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/transcribe")
|
||||
async def transcribe_audio(
|
||||
file: UploadFile = File(...),
|
||||
model: str = "base",
|
||||
):
|
||||
"""Transcribe an audio file using Whisper.
|
||||
|
||||
Supported formats: wav, mp3, m4a, ogg, flac, webm.
|
||||
Model options: tiny, base, small, medium (default: base).
|
||||
"""
|
||||
# Validate model
|
||||
valid_models = {"tiny", "base", "small", "medium"}
|
||||
if model not in valid_models:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Invalid model '{model}'. Use: {', '.join(sorted(valid_models))}",
|
||||
)
|
||||
|
||||
# Validate file type
|
||||
allowed = {"audio/wav", "audio/mpeg", "audio/mp4", "audio/x-m4a",
|
||||
"audio/ogg", "audio/flac", "audio/webm", "audio/x-wav"}
|
||||
if file.content_type and file.content_type not in allowed:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Unsupported format: {file.content_type}. Supported: wav, mp3, m4a, ogg, flac, webm",
|
||||
)
|
||||
|
||||
# Read file
|
||||
contents = await file.read()
|
||||
if len(contents) > MAX_UPLOAD_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"File too large. Max {MAX_UPLOAD_SIZE // (1024*1024)} MB.",
|
||||
)
|
||||
if len(contents) == 0:
|
||||
raise HTTPException(400, detail="Empty file.")
|
||||
|
||||
# Transcribe
|
||||
try:
|
||||
result = transcribe_bytes(contents, model_name=model)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|
||||
|
||||
return {
|
||||
"filename": file.filename,
|
||||
**result,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user