# ============================================================ # Stage 1: Builder — install Python dependencies # ============================================================ FROM python:3.11-slim AS builder WORKDIR /install # Build-time system deps (compilers, headers needed by grpcio, cffi, etc.) RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ wget \ libgl1 \ libglib2.0-0 \ libgomp1 \ libsm6 \ libxext6 \ libxrender1 \ && rm -rf /var/lib/apt/lists/* COPY requirements.txt . # Install all Python deps + gunicorn into an isolated prefix RUN pip install --no-cache-dir --prefix=/python-deps -r requirements.txt gunicorn # Pre-download the MediaPipe face-detector tflite model at build time # so the container never needs to reach out to Google Storage at runtime. RUN mkdir -p /models && \ wget -q -O /models/detector.tflite \ "https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite" # ============================================================ # Stage 2: Runtime image # ============================================================ FROM python:3.11-slim LABEL maintainer="undertone-classification" LABEL description="Skin undertone classification — Flask + TensorFlow + MediaPipe (HF Spaces)" # Runtime system libs required by OpenCV and MediaPipe # libgles2 → provides libGLESv2.so.2 (MediaPipe C bindings hard-require this) # libegl1 → provides libEGL.so.1 (MediaPipe GPU/EGL path) RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ libgomp1 \ libsm6 \ libxext6 \ libxrender1 \ libgles2 \ libegl1 \ && rm -rf /var/lib/apt/lists/* # Copy installed Python packages from the builder stage COPY --from=builder /python-deps /usr/local # ── Hugging Face Spaces requirement ────────────────────────── # HF Spaces runs containers with a non-root user (UID=1000, GID=1000). # We create that user and make /app writable so Flask can write uploads. RUN useradd -m -u 1000 appuser WORKDIR /app # Copy application source code COPY app.py . COPY templates/ templates/ COPY static/ static/ COPY models/ models/ # Copy the pre-downloaded tflite model into the models directory COPY --from=builder /models/detector.tflite models/detector.tflite # Ensure the uploads directory exists and is owned by the runtime user RUN mkdir -p static/uploads && \ chown -R appuser:appuser /app USER appuser # ── Port ────────────────────────────────────────────────────── # Hugging Face Spaces Docker SDK expects port 7860. EXPOSE 7860 ENV FLASK_ENV=production \ PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ # Silence unnecessary TF / oneDNN log noise TF_CPP_MIN_LOG_LEVEL=2 \ TF_ENABLE_ONEDNN_OPTS=0 # 1 worker → global ai_engine singleton is shared; 180 s timeout for # model load + first inference. CMD ["gunicorn", \ "--bind", "0.0.0.0:7860", \ "--workers", "1", \ "--timeout", "180", \ "--log-level", "info", \ "app:app"]