SearingShot's picture
Deploy SignApp Whisper service
68b43ce
Raw
History Blame Contribute Delete
1.21 kB
import os
import shutil
from pathlib import Path
from fastapi import FastAPI, File, HTTPException, UploadFile
WHISPER_MODEL = os.getenv("WHISPER_MODEL", "small")
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
app = FastAPI(title="SignApp Whisper STT")
model = None
def load_model():
global model
if model is None:
import whisper
model = whisper.load_model(WHISPER_MODEL)
@app.on_event("startup")
def startup():
load_model()
@app.get("/health")
def health():
return {"status": "ok", "model": WHISPER_MODEL}
@app.post("/transcribe/")
def transcribe(file: UploadFile = File(...)):
file_path = UPLOAD_DIR / (file.filename or "recording.webm")
try:
with file_path.open("wb") as audio_file:
shutil.copyfileobj(file.file, audio_file)
load_model()
result = model.transcribe(str(file_path), language="en")
return {
"text": result.get("text", "").strip(),
"language": result.get("language", "en"),
}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
finally:
if file_path.exists():
file_path.unlink()