import whisper import tempfile import os from fastapi import FastAPI, UploadFile, File, Form from fastapi.responses import JSONResponse, HTMLResponse from fastapi.middleware.cors import CORSMiddleware import uvicorn app = FastAPI(title="Whisper Transcription API") print("Loading Whisper model...") model = whisper.load_model("base") print("Model loaded.") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.get("/", response_class=HTMLResponse) async def root(): return """
Status: Running
Endpoints:
POST /transcribe — Upload audio file, returns textGET /health — Health checkExample curl:
curl -X POST /transcribe \\
-F "file=@audio.wav" \\
-F "language=auto"
"""
@app.post("/transcribe")
async def transcribe(
file: UploadFile = File(...),
language: str = Form(default="auto")
):
suffix = os.path.splitext(file.filename)[-1] if file.filename else ".wav"
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
content = await file.read()
tmp.write(content)
tmp_path = tmp.name
try:
lang = language if language != "auto" else None
result = model.transcribe(tmp_path, language=lang)
return JSONResponse({
"text": result["text"].strip(),
"language": result.get("language", "unknown"),
"segments": [
{
"start": round(s["start"], 2),
"end": round(s["end"], 2),
"text": s["text"].strip()
}
for s in result.get("segments", [])
]
})
except Exception as e:
return JSONResponse({"error": str(e)}, status_code=500)
finally:
os.unlink(tmp_path)
@app.get("/health")
async def health():
return {"status": "ok", "model": "whisper-base"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)