whisper.cpp / main.py
Arseniy Savin
Enhance Dockerfile and main.py for improved caching and health check endpoint
d7ee472
Raw History Blame Contribute Delete
5.09 kB
import os
import shutil
import tempfile
from typing import Any, Dict, List, Optional
from fastapi import ( # type: ignore[import]
FastAPI,
File,
UploadFile,
HTTPException,
)
from fastapi.responses import JSONResponse # type: ignore[import]
try:
# Import lazily; model will be instantiated on first use
from faster_whisper import WhisperModel # type: ignore
except Exception as import_error: # pragma: no cover
WhisperModel = None # type: ignore
_import_error: Optional[Exception] = import_error
else:
_import_error = None
app = FastAPI(title="faster-whisper REST API")
def _get_bool_env(name: str, default: bool = False) -> bool:
val = os.getenv(name)
if val is None:
return default
return val.strip().lower() in {"1", "true", "yes", "y", "on"}
class _ModelHolder:
_model: Optional[WhisperModel] = None # type: ignore
@classmethod
def get_model(cls) -> "WhisperModel": # type: ignore
if _import_error is not None:
raise RuntimeError(
f"Failed to import faster-whisper: {_import_error}"
)
if cls._model is None:
model_size = os.getenv("WHISPER_MODEL", "small")
device = os.getenv("WHISPER_DEVICE", "cpu")
compute_type = os.getenv("WHISPER_COMPUTE_TYPE", "int8")
# Construct model once and reuse across requests
cls._model = WhisperModel(
model_size,
device=device,
compute_type=compute_type,
) # type: ignore
return cls._model
@app.get("/")
def root() -> Dict[str, Any]:
return {"status": "ok"}
@app.get("/health")
def health() -> Dict[str, Any]:
return {"status": "ok"}
@app.post("/inference")
def transcribe(
file: UploadFile = File(...),
language: Optional[str] = None,
beam_size: Optional[int] = None,
vad: Optional[bool] = None,
word_timestamps: Optional[bool] = None,
) -> JSONResponse:
try:
model = _ModelHolder.get_model()
except Exception as e: # pragma: no cover
raise HTTPException(status_code=500, detail=str(e))
# Fallback to env-configurable defaults
effective_beam_size = (
int(os.getenv("WHISPER_BEAM_SIZE", "5"))
if beam_size is None
else beam_size
)
effective_vad = (
_get_bool_env("WHISPER_VAD", False)
if vad is None
else vad
)
effective_word_ts = (
_get_bool_env("WHISPER_WORD_TIMESTAMPS", False)
if word_timestamps is None
else word_timestamps
)
# Persist upload to a temporary file so ffmpeg/backends can access it
try:
with tempfile.NamedTemporaryFile(
delete=False,
suffix=os.path.splitext(file.filename or "audio")[1],
) as tmp:
shutil.copyfileobj(file.file, tmp)
tmp_path = tmp.name
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Failed to read uploaded file: {e}",
)
finally:
try:
file.file.close()
except Exception:
pass
try:
segments_iter, info = model.transcribe(
tmp_path,
beam_size=effective_beam_size,
language=language,
vad_filter=effective_vad,
word_timestamps=effective_word_ts,
)
segments = list(segments_iter)
# Build response
all_text = "".join(seg.text for seg in segments)
segments_json: List[Dict[str, Any]] = []
for seg in segments:
seg_obj: Dict[str, Any] = {
"id": seg.id,
"start": seg.start,
"end": seg.end,
"text": seg.text,
"avg_log_prob": seg.avg_logprob,
"compression_ratio": seg.compression_ratio,
"no_speech_prob": seg.no_speech_prob,
}
if effective_word_ts and getattr(seg, "words", None):
seg_obj["words"] = [
{
"start": w.start,
"end": w.end,
"word": w.word,
"probability": getattr(w, "probability", None),
}
for w in seg.words
]
segments_json.append(seg_obj)
result: Dict[str, Any] = {
"text": all_text.strip(),
"segments": segments_json,
"info": {
"language": getattr(info, "language", None),
"language_probability": getattr(
info, "language_probability", None
),
"duration": getattr(info, "duration", None),
},
}
return JSONResponse(result)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Transcription error: {e}",
)
finally:
try:
os.unlink(tmp_path)
except Exception:
pass