Spaces:
Sleeping
Sleeping
Arseniy Savin commited on
Commit ·
740950d
1
Parent(s): f896d57
Refactor Dockerfile and main.py for faster-whisper integration
Browse filesUpdated the Dockerfile to switch from alpine to python:3.11-slim, simplifying the build process and removing unnecessary build steps. Added installation of system dependencies and Python packages. In main.py, refactored the transcription logic to utilize the faster-whisper library, enhancing performance and simplifying the model loading process. Removed unused imports and improved error handling in the transcription endpoint.
- Dockerfile +20 -26
- main.py +139 -145
- requirements.txt +4 -8
Dockerfile
CHANGED
|
@@ -1,33 +1,27 @@
|
|
| 1 |
-
FROM
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
RUN apk add --no-cache git
|
| 7 |
-
|
| 8 |
-
RUN git clone https://github.com/ggerganov/whisper.cpp.git
|
| 9 |
-
|
| 10 |
-
RUN cd whisper.cpp && git checkout v1.7.6
|
| 11 |
-
|
| 12 |
-
# Build whisper.cpp
|
| 13 |
-
RUN apk add --no-cache g++ make sdl2-dev wget bash
|
| 14 |
-
RUN apk add --no-cache cmake
|
| 15 |
-
|
| 16 |
-
WORKDIR /build/whisper.cpp
|
| 17 |
-
|
| 18 |
-
RUN make \
|
| 19 |
-
-E 'CFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE' \
|
| 20 |
-
-E 'CXXFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE'
|
| 21 |
-
|
| 22 |
-
RUN bash ./models/download-ggml-model.sh small.en
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
COPY --from=builder /build/whisper.cpp/models/ ./models/
|
| 32 |
|
| 33 |
-
CMD sh -c "
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
|
| 3 |
+
# Install system dependencies
|
| 4 |
+
RUN apt-get update \
|
| 5 |
+
&& apt-get install -y --no-install-recommends ffmpeg \
|
| 6 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 7 |
|
| 8 |
+
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Python dependencies
|
| 11 |
+
COPY requirements.txt /app/requirements.txt
|
| 12 |
+
RUN pip install --no-cache-dir -r /app/requirements.txt
|
| 13 |
|
| 14 |
+
# App source
|
| 15 |
+
COPY main.py /app/main.py
|
| 16 |
|
| 17 |
+
# Defaults (overridable at runtime)
|
| 18 |
+
ENV WHISPER_MODEL=small \
|
| 19 |
+
WHISPER_DEVICE=cpu \
|
| 20 |
+
WHISPER_COMPUTE_TYPE=int8 \
|
| 21 |
+
WHISPER_BEAM_SIZE=5 \
|
| 22 |
+
WHISPER_VAD=false \
|
| 23 |
+
WHISPER_WORD_TIMESTAMPS=false
|
| 24 |
|
| 25 |
+
EXPOSE 7860
|
|
|
|
| 26 |
|
| 27 |
+
CMD sh -c "uvicorn main:app --host 0.0.0.0 --port \${PORT:-7860}"
|
main.py
CHANGED
|
@@ -1,174 +1,168 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from typing import Any, Dict, List, Optional
|
| 4 |
|
| 5 |
-
import numpy as np # type: ignore[import]
|
| 6 |
-
import orjson # type: ignore[import]
|
| 7 |
from fastapi import ( # type: ignore[import]
|
| 8 |
FastAPI,
|
| 9 |
File,
|
| 10 |
UploadFile,
|
| 11 |
HTTPException,
|
| 12 |
-
Header,
|
| 13 |
-
Depends,
|
| 14 |
)
|
| 15 |
-
from fastapi.responses import
|
| 16 |
-
JSONResponse,
|
| 17 |
-
)
|
| 18 |
-
from pydantic import BaseModel # type: ignore[import]
|
| 19 |
-
import ffmpeg # type: ignore[import]
|
| 20 |
-
|
| 21 |
-
from pywhispercpp.model import Model # type: ignore[import]
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def json_dumps(v: Any, *, default: Optional[Any] = None) -> str:
|
| 25 |
-
return orjson.dumps(v, default=default).decode()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
class Segment(BaseModel):
|
| 29 |
-
id: int
|
| 30 |
-
start: float
|
| 31 |
-
end: float
|
| 32 |
-
text: str
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
class TranscribeResponse(BaseModel):
|
| 36 |
-
model: str
|
| 37 |
-
language: Optional[str] = None
|
| 38 |
-
duration: Optional[float] = None
|
| 39 |
-
text: str
|
| 40 |
-
segments: List[Segment]
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
app = FastAPI(title="Whisper.cpp FastAPI", default_response_class=JSONResponse)
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
-
def load_model() -> Model:
|
| 47 |
-
model_name = os.getenv("WHISPER_MODEL", "tiny.en")
|
| 48 |
-
return Model(model_name)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
WHISPER_MODEL_INSTANCE: Optional[Model] = None
|
| 52 |
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def _expected_token_hash() -> str:
|
| 62 |
-
env_hash = os.getenv("AUTH_TOKEN_SHA256")
|
| 63 |
-
return (env_hash or DEFAULT_TOKEN_SHA256).lower()
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
def _require_bearer(authorization: Optional[str] = Header(None)) -> None:
|
| 67 |
-
if not authorization or not authorization.startswith("Bearer "):
|
| 68 |
-
raise HTTPException(status_code=401, detail="missing bearer token")
|
| 69 |
-
token = authorization.split(" ", 1)[1].strip()
|
| 70 |
-
token_hash = hashlib.sha256(token.encode("utf-8")).hexdigest()
|
| 71 |
-
if token_hash != _expected_token_hash():
|
| 72 |
-
raise HTTPException(status_code=401, detail="invalid token")
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
@app.get("/health")
|
| 82 |
-
def health(
|
| 83 |
return {"status": "ok"}
|
| 84 |
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def _bytes_to_float32_mono(arr_bytes: bytes) -> np.ndarray:
|
| 113 |
-
int16 = np.frombuffer(arr_bytes, dtype=np.int16)
|
| 114 |
-
return (int16.astype(np.float32) / 32768.0).flatten()
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
@app.post("/v1/transcribe", response_model=TranscribeResponse)
|
| 118 |
-
async def transcribe(
|
| 119 |
-
file: UploadFile = File(...), _auth: None = Depends(_require_bearer)
|
| 120 |
-
) -> TranscribeResponse:
|
| 121 |
-
if WHISPER_MODEL_INSTANCE is None:
|
| 122 |
-
raise HTTPException(status_code=503, detail="model not loaded")
|
| 123 |
-
|
| 124 |
-
file_bytes = await file.read()
|
| 125 |
-
if not file_bytes:
|
| 126 |
-
raise HTTPException(status_code=400, detail="empty file")
|
| 127 |
-
|
| 128 |
-
# Decode with ffmpeg to 16k mono PCM
|
| 129 |
-
pcm_bytes = _decode_audio_to_wav_bytes(file_bytes, sample_rate=16000)
|
| 130 |
-
audio = _bytes_to_float32_mono(pcm_bytes)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
threads_env = os.getenv("WHISPER_THREADS")
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
seg_models.append(
|
| 159 |
-
Segment(
|
| 160 |
-
id=idx,
|
| 161 |
-
start=float(start_s or 0.0),
|
| 162 |
-
end=float(end_s or 0.0),
|
| 163 |
-
text=text,
|
| 164 |
-
)
|
| 165 |
)
|
| 166 |
-
if text:
|
| 167 |
-
full_text_parts.append(text)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import tempfile
|
| 4 |
from typing import Any, Dict, List, Optional
|
| 5 |
|
|
|
|
|
|
|
| 6 |
from fastapi import ( # type: ignore[import]
|
| 7 |
FastAPI,
|
| 8 |
File,
|
| 9 |
UploadFile,
|
| 10 |
HTTPException,
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
+
from fastapi.responses import JSONResponse # type: ignore[import]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
# Import lazily; model will be instantiated on first use
|
| 16 |
+
from faster_whisper import WhisperModel # type: ignore
|
| 17 |
+
except Exception as import_error: # pragma: no cover
|
| 18 |
+
WhisperModel = None # type: ignore
|
| 19 |
+
_import_error: Optional[Exception] = import_error
|
| 20 |
+
else:
|
| 21 |
+
_import_error = None
|
| 22 |
|
|
|
|
| 23 |
|
| 24 |
+
app = FastAPI(title="faster-whisper REST API")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def _get_bool_env(name: str, default: bool = False) -> bool:
|
| 28 |
+
val = os.getenv(name)
|
| 29 |
+
if val is None:
|
| 30 |
+
return default
|
| 31 |
+
return val.strip().lower() in {"1", "true", "yes", "y", "on"}
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
+
class _ModelHolder:
|
| 35 |
+
_model: Optional[WhisperModel] = None # type: ignore
|
| 36 |
|
| 37 |
+
@classmethod
|
| 38 |
+
def get_model(cls) -> "WhisperModel": # type: ignore
|
| 39 |
+
if _import_error is not None:
|
| 40 |
+
raise RuntimeError(
|
| 41 |
+
f"Failed to import faster-whisper: {_import_error}"
|
| 42 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
if cls._model is None:
|
| 45 |
+
model_size = os.getenv("WHISPER_MODEL", "small")
|
| 46 |
+
device = os.getenv("WHISPER_DEVICE", "cpu")
|
| 47 |
+
compute_type = os.getenv("WHISPER_COMPUTE_TYPE", "int8")
|
| 48 |
+
# Construct model once and reuse across requests
|
| 49 |
+
cls._model = WhisperModel(
|
| 50 |
+
model_size,
|
| 51 |
+
device=device,
|
| 52 |
+
compute_type=compute_type,
|
| 53 |
+
) # type: ignore
|
| 54 |
+
return cls._model
|
| 55 |
|
| 56 |
|
| 57 |
@app.get("/health")
|
| 58 |
+
def health() -> Dict[str, Any]:
|
| 59 |
return {"status": "ok"}
|
| 60 |
|
| 61 |
|
| 62 |
+
@app.post("/inference")
|
| 63 |
+
def transcribe(
|
| 64 |
+
file: UploadFile = File(...),
|
| 65 |
+
language: Optional[str] = None,
|
| 66 |
+
beam_size: Optional[int] = None,
|
| 67 |
+
vad: Optional[bool] = None,
|
| 68 |
+
word_timestamps: Optional[bool] = None,
|
| 69 |
+
) -> JSONResponse:
|
| 70 |
try:
|
| 71 |
+
model = _ModelHolder.get_model()
|
| 72 |
+
except Exception as e: # pragma: no cover
|
| 73 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 74 |
+
|
| 75 |
+
# Fallback to env-configurable defaults
|
| 76 |
+
effective_beam_size = (
|
| 77 |
+
int(os.getenv("WHISPER_BEAM_SIZE", "5"))
|
| 78 |
+
if beam_size is None
|
| 79 |
+
else beam_size
|
| 80 |
+
)
|
| 81 |
+
effective_vad = (
|
| 82 |
+
_get_bool_env("WHISPER_VAD", False)
|
| 83 |
+
if vad is None
|
| 84 |
+
else vad
|
| 85 |
+
)
|
| 86 |
+
effective_word_ts = (
|
| 87 |
+
_get_bool_env("WHISPER_WORD_TIMESTAMPS", False)
|
| 88 |
+
if word_timestamps is None
|
| 89 |
+
else word_timestamps
|
| 90 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Persist upload to a temporary file so ffmpeg/backends can access it
|
|
|
|
| 93 |
try:
|
| 94 |
+
with tempfile.NamedTemporaryFile(
|
| 95 |
+
delete=False,
|
| 96 |
+
suffix=os.path.splitext(file.filename or "audio")[1],
|
| 97 |
+
) as tmp:
|
| 98 |
+
shutil.copyfileobj(file.file, tmp)
|
| 99 |
+
tmp_path = tmp.name
|
| 100 |
+
except Exception as e:
|
| 101 |
+
raise HTTPException(
|
| 102 |
+
status_code=400,
|
| 103 |
+
detail=f"Failed to read uploaded file: {e}",
|
| 104 |
+
)
|
| 105 |
+
finally:
|
| 106 |
+
try:
|
| 107 |
+
file.file.close()
|
| 108 |
+
except Exception:
|
| 109 |
+
pass
|
| 110 |
+
try:
|
| 111 |
+
segments_iter, info = model.transcribe(
|
| 112 |
+
tmp_path,
|
| 113 |
+
beam_size=effective_beam_size,
|
| 114 |
+
language=language,
|
| 115 |
+
vad_filter=effective_vad,
|
| 116 |
+
word_timestamps=effective_word_ts,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
)
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
segments = list(segments_iter)
|
| 120 |
+
|
| 121 |
+
# Build response
|
| 122 |
+
all_text = "".join(seg.text for seg in segments)
|
| 123 |
+
|
| 124 |
+
segments_json: List[Dict[str, Any]] = []
|
| 125 |
+
for seg in segments:
|
| 126 |
+
seg_obj: Dict[str, Any] = {
|
| 127 |
+
"id": seg.id,
|
| 128 |
+
"start": seg.start,
|
| 129 |
+
"end": seg.end,
|
| 130 |
+
"text": seg.text,
|
| 131 |
+
"avg_log_prob": seg.avg_logprob,
|
| 132 |
+
"compression_ratio": seg.compression_ratio,
|
| 133 |
+
"no_speech_prob": seg.no_speech_prob,
|
| 134 |
+
}
|
| 135 |
+
if effective_word_ts and getattr(seg, "words", None):
|
| 136 |
+
seg_obj["words"] = [
|
| 137 |
+
{
|
| 138 |
+
"start": w.start,
|
| 139 |
+
"end": w.end,
|
| 140 |
+
"word": w.word,
|
| 141 |
+
"probability": getattr(w, "probability", None),
|
| 142 |
+
}
|
| 143 |
+
for w in seg.words
|
| 144 |
+
]
|
| 145 |
+
segments_json.append(seg_obj)
|
| 146 |
+
|
| 147 |
+
result: Dict[str, Any] = {
|
| 148 |
+
"text": all_text.strip(),
|
| 149 |
+
"segments": segments_json,
|
| 150 |
+
"info": {
|
| 151 |
+
"language": getattr(info, "language", None),
|
| 152 |
+
"language_probability": getattr(
|
| 153 |
+
info, "language_probability", None
|
| 154 |
+
),
|
| 155 |
+
"duration": getattr(info, "duration", None),
|
| 156 |
+
},
|
| 157 |
+
}
|
| 158 |
+
return JSONResponse(result)
|
| 159 |
+
except Exception as e:
|
| 160 |
+
raise HTTPException(
|
| 161 |
+
status_code=500,
|
| 162 |
+
detail=f"Transcription error: {e}",
|
| 163 |
+
)
|
| 164 |
+
finally:
|
| 165 |
+
try:
|
| 166 |
+
os.unlink(tmp_path)
|
| 167 |
+
except Exception:
|
| 168 |
+
pass
|
requirements.txt
CHANGED
|
@@ -1,8 +1,4 @@
|
|
| 1 |
-
fastapi==0.
|
| 2 |
-
uvicorn[standard]==0.
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
ffmpeg-python==0.2.0
|
| 6 |
-
numpy==1.26.4
|
| 7 |
-
pydantic==2.6.3
|
| 8 |
-
orjson==3.9.15
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn[standard]==0.30.0
|
| 3 |
+
faster-whisper==1.0.3
|
| 4 |
+
|
|
|
|
|
|
|
|
|
|
|
|