Arseniy Savin commited on
Commit
5aea4af
·
1 Parent(s): 9e5239c

Implement initial setup for Whisper.cpp FastAPI application with Docker support. Added Dockerfile, main application logic, requirements, and updated README with usage instructions and environment variables.

Browse files
Files changed (4) hide show
  1. Dockerfile +28 -0
  2. README.md +25 -2
  3. main.py +147 -0
  4. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ FROM python:3.10-slim
3
+
4
+ # Create non-root user per Spaces guidance
5
+ RUN useradd -m -u 1000 user
6
+ ENV HOME=/home/user \
7
+ PATH=/home/user/.local/bin:$PATH
8
+ WORKDIR $HOME/app
9
+
10
+ # System deps: ffmpeg for non-wav inputs
11
+ RUN apt-get update && apt-get install -y --no-install-recommends \
12
+ ffmpeg \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Python deps
16
+ COPY --chown=user requirements.txt ./requirements.txt
17
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
18
+
19
+ # App
20
+ COPY --chown=user . .
21
+ USER user
22
+
23
+ # Default env tuned for CPU Basic
24
+ ENV WHISPER_MODEL=tiny.en \
25
+ WHISPER_THREADS=4
26
+
27
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
28
+
README.md CHANGED
@@ -4,7 +4,30 @@ emoji: 🦀
4
  colorFrom: indigo
5
  colorTo: blue
6
  sdk: docker
7
- pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  colorFrom: indigo
5
  colorTo: blue
6
  sdk: docker
7
+ app_port: 7860
8
  ---
9
 
10
+ Minimal REST API for Whisper.cpp on free CPU Spaces.
11
+
12
+ Endpoints
13
+
14
+ - GET /health: liveness check
15
+ - POST /v1/transcribe: multipart file upload (audio/video). Returns JSON with text and per-segment metadata.
16
+
17
+ Quick use
18
+
19
+ ```bash
20
+ curl -s -X POST \
21
+ -F "file=@sample.mp3" \
22
+ https://<your-space-subdomain>.hf.space/v1/transcribe | jq
23
+ ```
24
+
25
+ Environment
26
+
27
+ - WHISPER_MODEL: model name (e.g. tiny.en, base.en, small). Default tiny.en.
28
+ - WHISPER_THREADS: number of CPU threads to use. Default 4.
29
+
30
+ Notes
31
+
32
+ - Uses pywhispercpp wheels (no compile) + ffmpeg to handle non-WAV inputs.
33
+ - Weights cache in ephemeral storage. For persistence, add a storage upgrade and set HF_HOME=/data/.huggingface.
main.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Any, Dict, List, Optional
3
+
4
+ import numpy as np # type: ignore[import]
5
+ import orjson # type: ignore[import]
6
+ from fastapi import ( # type: ignore[import]
7
+ FastAPI,
8
+ File,
9
+ UploadFile,
10
+ HTTPException,
11
+ )
12
+ from fastapi.responses import ( # type: ignore[import]
13
+ JSONResponse,
14
+ )
15
+ from pydantic import BaseModel # type: ignore[import]
16
+ import ffmpeg # type: ignore[import]
17
+
18
+ from pywhispercpp.model import Model # type: ignore[import]
19
+
20
+
21
+ def json_dumps(v: Any, *, default: Optional[Any] = None) -> str:
22
+ return orjson.dumps(v, default=default).decode()
23
+
24
+
25
+ class Segment(BaseModel):
26
+ id: int
27
+ start: float
28
+ end: float
29
+ text: str
30
+
31
+
32
+ class TranscribeResponse(BaseModel):
33
+ model: str
34
+ language: Optional[str] = None
35
+ duration: Optional[float] = None
36
+ text: str
37
+ segments: List[Segment]
38
+
39
+
40
+ app = FastAPI(title="Whisper.cpp FastAPI", default_response_class=JSONResponse)
41
+
42
+
43
+ def load_model() -> Model:
44
+ model_name = os.getenv("WHISPER_MODEL", "tiny.en")
45
+ return Model(model_name)
46
+
47
+
48
+ WHISPER_MODEL_INSTANCE: Optional[Model] = None
49
+
50
+
51
+ @app.on_event("startup")
52
+ def _startup() -> None:
53
+ global WHISPER_MODEL_INSTANCE
54
+ WHISPER_MODEL_INSTANCE = load_model()
55
+
56
+
57
+ @app.get("/health")
58
+ def health() -> Dict[str, str]:
59
+ return {"status": "ok"}
60
+
61
+
62
+ def _decode_audio_to_wav_bytes(
63
+ file_bytes: bytes, sample_rate: int = 16000
64
+ ) -> bytes:
65
+ try:
66
+ out, _ = (
67
+ ffmpeg.input("pipe:0")
68
+ .output(
69
+ "pipe:1",
70
+ format="s16le",
71
+ acodec="pcm_s16le",
72
+ ac=1,
73
+ ar=str(sample_rate),
74
+ )
75
+ .run(
76
+ input=file_bytes,
77
+ cmd=["ffmpeg", "-nostdin"],
78
+ capture_stdout=True,
79
+ capture_stderr=True,
80
+ )
81
+ )
82
+ return out
83
+ except ffmpeg.Error as e:
84
+ detail = f"ffmpeg decode error: {e.stderr.decode()}"
85
+ raise HTTPException(status_code=400, detail=detail) from e
86
+
87
+
88
+ def _bytes_to_float32_mono(arr_bytes: bytes) -> np.ndarray:
89
+ int16 = np.frombuffer(arr_bytes, dtype=np.int16)
90
+ return (int16.astype(np.float32) / 32768.0).flatten()
91
+
92
+
93
+ @app.post("/v1/transcribe", response_model=TranscribeResponse)
94
+ async def transcribe(file: UploadFile = File(...)) -> TranscribeResponse:
95
+ if WHISPER_MODEL_INSTANCE is None:
96
+ raise HTTPException(status_code=503, detail="model not loaded")
97
+
98
+ file_bytes = await file.read()
99
+ if not file_bytes:
100
+ raise HTTPException(status_code=400, detail="empty file")
101
+
102
+ # Decode with ffmpeg to 16k mono PCM
103
+ pcm_bytes = _decode_audio_to_wav_bytes(file_bytes, sample_rate=16000)
104
+ audio = _bytes_to_float32_mono(pcm_bytes)
105
+
106
+ # Threads hint
107
+ threads_env = os.getenv("WHISPER_THREADS")
108
+ try:
109
+ n_threads = int(threads_env) if threads_env else None
110
+ except ValueError:
111
+ n_threads = None
112
+
113
+ # Run transcription
114
+ segments = WHISPER_MODEL_INSTANCE.transcribe(
115
+ audio, num_proc=n_threads or 1
116
+ )
117
+
118
+ seg_models: List[Segment] = []
119
+ full_text_parts: List[str] = []
120
+ for idx, seg in enumerate(segments):
121
+ # pywhispercpp returns objects with .text/.t0/.t1 timestamps in 0.01s
122
+ # units; normalize to seconds if present
123
+ start_s = getattr(seg, "start", None)
124
+ end_s = getattr(seg, "end", None)
125
+ text = getattr(seg, "text", "")
126
+ # Some versions expose t0/t1 in 0.01s ticks
127
+ if start_s is None and hasattr(seg, "t0"):
128
+ start_s = float(seg.t0) / 100.0
129
+ if end_s is None and hasattr(seg, "t1"):
130
+ end_s = float(seg.t1) / 100.0
131
+ seg_models.append(
132
+ Segment(
133
+ id=idx,
134
+ start=float(start_s or 0.0),
135
+ end=float(end_s or 0.0),
136
+ text=text,
137
+ )
138
+ )
139
+ if text:
140
+ full_text_parts.append(text)
141
+
142
+ resp = TranscribeResponse(
143
+ model=os.getenv("WHISPER_MODEL", "tiny.en"),
144
+ text="".join(full_text_parts).strip(),
145
+ segments=seg_models,
146
+ )
147
+ return resp
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
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