# ── Stage 1: dependency builder ─────────────────────────────────────────────── FROM python:3.11-slim AS builder WORKDIR /build # Install build tools RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ && rm -rf /var/lib/apt/lists/* COPY requirements.txt . RUN pip install --upgrade pip \ && pip install --prefix=/install --no-cache-dir -r requirements.txt # ── Stage 2: runtime ────────────────────────────────────────────────────────── FROM python:3.11-slim AS runtime # Non-root user (required on Hugging Face Spaces) RUN useradd -m -u 1000 appuser WORKDIR /app # Copy installed packages from builder COPY --from=builder /install /usr/local # Copy application source COPY --chown=appuser:appuser app/ app/ COPY --chown=appuser:appuser data/ data/ # Create writable output directories RUN mkdir -p /app/data /app/models && chown -R appuser:appuser /app USER appuser # Hugging Face Spaces requires port 7860 EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/health')" CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]