Arseniy Savin commited on
Commit
740950d
·
1 Parent(s): f896d57

Refactor Dockerfile and main.py for faster-whisper integration

Browse files

Updated 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.

Files changed (3) hide show
  1. Dockerfile +20 -26
  2. main.py +139 -145
  3. requirements.txt +4 -8
Dockerfile CHANGED
@@ -1,33 +1,27 @@
1
- FROM alpine:3.21 AS builder
2
 
3
- WORKDIR /build
 
 
 
4
 
5
- # Fetch whisper.cpp source code
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
- FROM alpine:3.21 AS runtime
 
 
25
 
26
- RUN apk add --no-cache libstdc++ libgcc libgomp ffmpeg
 
27
 
28
- WORKDIR /app
 
 
 
 
 
 
29
 
30
- COPY --from=builder /build/whisper.cpp/build/ /build/whisper.cpp/build/
31
- COPY --from=builder /build/whisper.cpp/models/ ./models/
32
 
33
- CMD sh -c "/build/whisper.cpp/build/bin/whisper-server -m /app/models/ggml-small.en.bin --host 0.0.0.0 --port ${PORT:-7860} --flash-attn"
 
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 hashlib
 
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 ( # type: ignore[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
- # Bearer token auth: compare SHA-256(token) to a hash stored in code
55
- # Change this value and keep the raw token secret off-repo.
56
- DEFAULT_TOKEN_SHA256 = (
57
- "1f72b9a1bfe2da0c5f71d61f6e84f8a9a15d3f2a3c7b384e8f8b1efc7d7a3d8a"
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
- @app.on_event("startup")
76
- def _startup() -> None:
77
- global WHISPER_MODEL_INSTANCE
78
- WHISPER_MODEL_INSTANCE = load_model()
 
 
 
 
 
 
 
79
 
80
 
81
  @app.get("/health")
82
- def health(_auth: None = Depends(_require_bearer)) -> Dict[str, str]:
83
  return {"status": "ok"}
84
 
85
 
86
- def _decode_audio_to_wav_bytes(
87
- file_bytes: bytes, sample_rate: int = 16000
88
- ) -> bytes:
 
 
 
 
 
89
  try:
90
- out, _ = (
91
- ffmpeg.input("pipe:0")
92
- .output(
93
- "pipe:1",
94
- format="s16le",
95
- acodec="pcm_s16le",
96
- ac=1,
97
- ar=str(sample_rate),
98
- )
99
- .run(
100
- input=file_bytes,
101
- cmd=["ffmpeg", "-nostdin"],
102
- capture_stdout=True,
103
- capture_stderr=True,
104
- )
105
- )
106
- return out
107
- except ffmpeg.Error as e:
108
- detail = f"ffmpeg decode error: {e.stderr.decode()}"
109
- raise HTTPException(status_code=400, detail=detail) from e
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
- # Threads hint
133
- threads_env = os.getenv("WHISPER_THREADS")
134
  try:
135
- n_threads = int(threads_env) if threads_env else None
136
- except ValueError:
137
- n_threads = None
138
-
139
- # Run transcription (pywhispercpp uses n_threads, not num_proc)
140
- transcribe_kwargs: Dict[str, int] = {}
141
- if n_threads:
142
- transcribe_kwargs["n_threads"] = n_threads
143
- segments = WHISPER_MODEL_INSTANCE.transcribe(audio, **transcribe_kwargs)
144
-
145
- seg_models: List[Segment] = []
146
- full_text_parts: List[str] = []
147
- for idx, seg in enumerate(segments):
148
- # pywhispercpp returns objects with .text/.t0/.t1 timestamps in 0.01s
149
- # units; normalize to seconds if present
150
- start_s = getattr(seg, "start", None)
151
- end_s = getattr(seg, "end", None)
152
- text = getattr(seg, "text", "")
153
- # Some versions expose t0/t1 in 0.01s ticks
154
- if start_s is None and hasattr(seg, "t0"):
155
- start_s = float(seg.t0) / 100.0
156
- if end_s is None and hasattr(seg, "t1"):
157
- end_s = float(seg.t1) / 100.0
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
- resp = TranscribeResponse(
170
- model=os.getenv("WHISPER_MODEL", "tiny.en"),
171
- text="".join(full_text_parts).strip(),
172
- segments=seg_models,
173
- )
174
- return resp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.110.0
2
- uvicorn[standard]==0.27.1
3
- python-multipart==0.0.9
4
- pywhispercpp==1.3.3
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
+