forked from groxaxo/Qwen3-TTS-Openai-Fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
304 lines (246 loc) · 7.88 KB
/
Copy pathDockerfile
File metadata and controls
304 lines (246 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Qwen3-TTS OpenAI-Compatible API Server
# Multi-stage Dockerfile optimized for GPU/CUDA and CPU deployments
# =============================================================================
# Stage 1: Base image with system dependencies
# =============================================================================
ARG BASE_IMAGE=nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
FROM ${BASE_IMAGE} AS base
# Prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV NUMBA_CACHE_DIR=/tmp/numba_cache
# NVIDIA Container Runtime environment variables (required for PyTorch CUDA detection)
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/nvidia/lib:/usr/local/nvidia/lib64:${LD_LIBRARY_PATH}
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.11 \
python3.11-venv \
python3.11-dev \
python3-pip \
build-essential \
git \
curl \
ffmpeg \
libsndfile1 \
libsox-dev \
sox \
&& rm -rf /var/lib/apt/lists/* \
&& ln -sf /usr/bin/python3.11 /usr/bin/python3 \
&& ln -sf /usr/bin/python3 /usr/bin/python
# Set up Python virtual environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Upgrade pip
RUN pip install --no-cache-dir --upgrade pip setuptools wheel
# =============================================================================
# Stage 2: Builder with CUDA development tools for flash-attn
# =============================================================================
FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04 AS builder
# Install Python and build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.11 \
python3.11-venv \
python3.11-dev \
build-essential \
git \
curl \
ninja-build \
&& rm -rf /var/lib/apt/lists/* \
&& ln -sf /usr/bin/python3.11 /usr/bin/python3 \
&& ln -sf /usr/bin/python3 /usr/bin/python
# Set up Python virtual environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir --upgrade pip setuptools wheel
WORKDIR /build
# Copy dependency files
COPY pyproject.toml ./
COPY README.md ./
# Install Python dependencies
RUN pip install --no-cache-dir \
torch>=2.0.0 \
torchaudio>=2.0.0 \
--index-url https://download.pytorch.org/whl/cu121
# Install the main package dependencies
RUN pip install --no-cache-dir \
transformers>=4.40.0 \
accelerate>=1.0.0 \
librosa \
soundfile \
pydub \
numpy \
scipy \
einops \
onnxruntime-gpu
# Install FastAPI and server dependencies
RUN pip install --no-cache-dir \
fastapi>=0.109.0 \
uvicorn[standard]>=0.27.0 \
python-multipart \
pydantic>=2.0.0 \
inflect \
aiofiles
# Install ninja for faster flash-attn compilation
RUN pip install --no-cache-dir ninja packaging wheel
# Install flash-attention 2 for optimized attention (requires CUDA)
RUN pip install --no-cache-dir flash-attn --no-build-isolation
# =============================================================================
# Stage 3: Production image (official backend)
# =============================================================================
FROM base AS production
WORKDIR /app
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy application code
COPY . .
# Install the package in editable mode
RUN pip install --no-cache-dir -e .
# Create non-root user for security
RUN useradd --create-home --shell /bin/bash appuser \
&& mkdir -p /tmp/numba_cache \
&& chown -R appuser:appuser /app /tmp/numba_cache
USER appuser
# Environment variables
ENV HOST=0.0.0.0
ENV PORT=8880
ENV WORKERS=1
ENV PYTHONPATH=/app
ENV TTS_BACKEND=official
# Expose port
EXPOSE 8880
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:8880/health || exit 1
# Run the server
CMD ["python", "-m", "api.main"]
# =============================================================================
# Stage 4: vLLM-Omni backend (with vLLM dependencies)
# =============================================================================
FROM base AS vllm-builder
WORKDIR /build
# Copy dependency files
COPY pyproject.toml ./
COPY README.md ./
# Install base dependencies first
RUN pip install --no-cache-dir \
torch>=2.0.0 \
torchaudio>=2.0.0 \
--index-url https://download.pytorch.org/whl/cu121
# Install vLLM (this may take a while)
RUN pip install --no-cache-dir vllm>=0.4.0
# Install the main package dependencies
RUN pip install --no-cache-dir \
transformers>=4.40.0 \
accelerate>=1.0.0 \
librosa \
soundfile \
pydub \
numpy \
scipy \
einops \
onnxruntime-gpu
# Install FastAPI and server dependencies
RUN pip install --no-cache-dir \
fastapi>=0.109.0 \
uvicorn[standard]>=0.27.0 \
python-multipart \
pydantic>=2.0.0 \
inflect \
aiofiles
# Optional: Install flash-attention for better performance
RUN pip install --no-cache-dir flash-attn --no-build-isolation || true
# =============================================================================
# Stage 5: vLLM-Omni production image
# =============================================================================
FROM base AS vllm-production
WORKDIR /app
# Copy virtual environment from vllm-builder
COPY --from=vllm-builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy application code
COPY . .
# Install the package in editable mode with vllm extras
RUN pip install --no-cache-dir -e ".[vllm]"
# Create non-root user for security
RUN useradd --create-home --shell /bin/bash appuser \
&& mkdir -p /tmp/numba_cache \
&& chown -R appuser:appuser /app /tmp/numba_cache
USER appuser
# Environment variables
ENV HOST=0.0.0.0
ENV PORT=8880
ENV WORKERS=1
ENV PYTHONPATH=/app
ENV TTS_BACKEND=vllm_omni
# Expose port
EXPOSE 8880
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=90s --retries=3 \
CMD curl -f http://localhost:8880/health || exit 1
# Run the server
CMD ["python", "-m", "api.main"]
# =============================================================================
# CPU-only variant
# =============================================================================
FROM python:3.11-slim AS cpu-base
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV NUMBA_CACHE_DIR=/tmp/numba_cache
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
git \
curl \
ffmpeg \
libsndfile1 \
libsox-dev \
sox \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy dependency files first for better caching
COPY pyproject.toml README.md ./
# Install PyTorch (CPU version)
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
&& pip install --no-cache-dir \
torch>=2.0.0 \
torchaudio>=2.0.0 \
--index-url https://download.pytorch.org/whl/cpu
# Install Python dependencies
RUN pip install --no-cache-dir \
transformers>=4.40.0 \
accelerate>=1.0.0 \
librosa \
soundfile \
pydub \
numpy \
scipy \
einops \
onnxruntime \
fastapi>=0.109.0 \
uvicorn[standard]>=0.27.0 \
python-multipart \
pydantic>=2.0.0 \
inflect \
aiofiles
# Copy application code
COPY . .
# Install the package
RUN pip install --no-cache-dir -e .
# Create non-root user
RUN useradd --create-home --shell /bin/bash appuser \
&& mkdir -p /tmp/numba_cache \
&& chown -R appuser:appuser /app /tmp/numba_cache
USER appuser
# Environment variables
ENV HOST=0.0.0.0
ENV PORT=8880
ENV WORKERS=1
ENV PYTHONPATH=/app
EXPOSE 8880
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:8880/health || exit 1
CMD ["python", "-m", "api.main"]