Spaces:
Sleeping
Sleeping
| 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) | |
| def startup(): | |
| load_model() | |
| def health(): | |
| return {"status": "ok", "model": WHISPER_MODEL} | |
| 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() | |