visual-search-api / Dockerfile
AdarshDRC's picture
Update Dockerfile
40787fc verified
# Dockerfile
FROM python:3.10-slim
WORKDIR /app
# ── System deps (OpenCV headless needs libGL) ────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
libgl1 libglib2.0-0 libgomp1 git \
&& rm -rf /var/lib/apt/lists/*
# ── Python deps ───────────────────────────────────────────────────
COPY requirements.txt .
RUN pip install --no-cache-dir --compile -r requirements.txt
# ── Copy application code ────────────────────────────────────────
COPY . .
RUN mkdir -p temp_uploads saved_images && chmod -R 777 temp_uploads saved_images
# ── Pre-download all AI models at BUILD time ─────────────────────
# This bakes the weights into the Docker image layer.
# Cold-start on HF Spaces goes from ~3-5 min → ~10 sec.
# Remove this block if your image size budget is tight (<5 GB limit on free HF).
RUN python - <<'EOF'
from transformers import AutoProcessor, AutoModel, AutoImageProcessor
from ultralytics import YOLO
from deepface import DeepFace
import numpy as np
print("Pre-downloading SigLIP …")
AutoProcessor.from_pretrained("google/siglip-base-patch16-224", use_fast=True)
AutoModel.from_pretrained("google/siglip-base-patch16-224")
print("Pre-downloading DINOv2 …")
AutoImageProcessor.from_pretrained("facebook/dinov2-base")
AutoModel.from_pretrained("facebook/dinov2-base")
print("Pre-downloading YOLO …")
YOLO("yolo11n.pt")
print("Pre-downloading GhostFaceNet + RetinaFace …")
dummy = np.zeros((112, 112, 3), dtype=np.uint8)
try:
DeepFace.represent(img_path=dummy, model_name="GhostFaceNet",
detector_backend="retinaface", enforce_detection=False)
except Exception:
pass # first run just downloads weights; inference error is fine here
print("✅ All models cached in image layer")
EOF
EXPOSE 7860
# ── Two uvicorn workers for true parallelism ─────────────────────
# WEB_CONCURRENCY can be overridden via HF Space env vars
ENV WEB_CONCURRENCY=2
CMD uvicorn main:app \
--host 0.0.0.0 \
--port 7860 \
--workers ${WEB_CONCURRENCY} \
--timeout-keep-alive 75