# ── Stage 1: base ───────────────────────────────────────────────────────────── FROM python:3.11-slim AS base ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 WORKDIR /app # System deps for Pillow + diffusers RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ libgomp1 \ curl \ && rm -rf /var/lib/apt/lists/* # ── Stage 2: dependencies ────────────────────────────────────────────────────── FROM base AS deps COPY requirements.txt . # PyTorch CPU-only wheel first (avoids pulling the massive CUDA build) RUN pip install torch torchvision \ --index-url https://download.pytorch.org/whl/cpu \ && pip install -r requirements.txt # ── Stage 3: final image ─────────────────────────────────────────────────────── FROM base AS final # Copy installed packages from deps stage COPY --from=deps /usr/local/lib/python3.11 /usr/local/lib/python3.11 COPY --from=deps /usr/local/bin /usr/local/bin # Copy application source COPY . . # Create runtime directories RUN mkdir -p outputs model_cache # HF Spaces runs containers as UID 1000 — match it RUN useradd -m -u 1000 appuser \ && chown -R appuser:appuser /app USER appuser # ── Port 7860 is REQUIRED by Hugging Face Spaces ────────────────────────────── EXPOSE 7860 # Health check — longer start_period for model download on cold start HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=5 \ CMD curl -f http://localhost:7860/health || exit 1 # Default env (override via HF Spaces Secrets or docker-compose) ENV HOST=0.0.0.0 \ PORT=7860 \ MODEL_ID=nota-ai/bk-sdm-small \ MODEL_CACHE_DIR=/app/model_cache \ AUTO_LOAD_MODEL=true \ CPU_THREADS=0 \ ENABLE_ATTENTION_SLICING=true \ DEFAULT_STEPS=20 \ MAX_STEPS=50 \ DEFAULT_WIDTH=512 \ DEFAULT_HEIGHT=512 \ MAX_WIDTH=768 \ MAX_HEIGHT=768 \ DEFAULT_GUIDANCE_SCALE=7.5 \ OUTPUTS_DIR=/app/outputs \ JOB_TTL_SECONDS=0 CMD ["python", "-m", "uvicorn", "main:app", \ "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]