Arseniy Savin commited on
Commit
1ecbdb7
·
1 Parent(s): 8feef52

Refactor Dockerfile for Whisper.cpp application build process

Browse files

Revised the Dockerfile to utilize a multi-stage build approach, separating the build and runtime environments. This change improves efficiency by reducing the final image size and ensuring only necessary dependencies are included in the runtime stage. The Whisper.cpp source code is now fetched and built during the build stage, with the model downloaded as part of the process.

Files changed (1) hide show
  1. Dockerfile +33 -38
Dockerfile CHANGED
@@ -1,39 +1,34 @@
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 and a stable cache path
24
- ENV WHISPER_MODEL=tiny.en \
25
- WHISPER_THREADS=4 \
26
- XDG_DATA_HOME=/home/user/.local/share
27
-
28
- # Pre-download the whisper.cpp model at build time so it is baked into the image
29
- # and won't be re-downloaded on each container start
30
- RUN python - <<'PY'
31
- import os
32
- from pywhispercpp.model import Model
33
- model = os.environ.get('WHISPER_MODEL','tiny.en')
34
- print(f"Pre-fetching pywhispercpp model: {model}")
35
- Model(model)
36
- PY
37
-
38
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
23
+ RUN bash ./models/download-ggml-model.sh tiny.en
24
+
25
+ FROM alpine:3.21 AS runtime
26
+
27
+ RUN apk add --no-cache libstdc++ libgcc libgomp ffmpeg
28
+
29
+ WORKDIR /app
30
+
31
+ COPY --from=builder /build/whisper.cpp/build/ /build/whisper.cpp/build/
32
+ COPY --from=builder /build/whisper.cpp/models/ ./models/
33
+
34
+ CMD ["/bin/sh", "-c", "THREADS=$(nproc) && PROCESSORS=$(nproc) && echo \"Using $THREADS threads and $PROCESSORS processors with streaming enabled\" && exec /build/whisper.cpp/build/bin/whisper-server -m /app/models/ggml-tiny.en.bin --host 0.0.0.0 --port 8080 -t $THREADS -p $PROCESSORS --convert"]