FROM python:3.10-slim-bookworm AS builder # Install system dependencies for compiling audio tools and Python packages RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ g++ \ ffmpeg \ libsndfile1 \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies. # Ensure we use a CPU-only version of PyTorch to avoid CUDA bloat. # If your requirements.txt already pulls torch without CUDA, you can keep the simple install. RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt \ --extra-index-url https://download.pytorch.org/whl/cpu # Final stage FROM python:3.10-slim-bookworm # Install runtime system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ ffmpeg \ libsndfile1 \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Copy installed packages from builder COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Copy application code COPY app.py . # Create non-root user for security RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app USER appuser # Environment variables with defaults ENV MODEL_SIZE=small \ MAX_FILE_SIZE_MB=200 \ LOG_LEVEL=INFO \ PORT=7860 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:7860/health || exit 1 EXPOSE 7860 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]