File size: 2,415 Bytes
40787fc
 
 
 
3e805ab
 
 
40787fc
 
 
ae5af92
 
40787fc
3e805ab
40787fc
3e805ab
40787fc
3e805ab
 
40787fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e805ab
 
 
40787fc
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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