File size: 3,662 Bytes
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
2c2a775
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Dockerfile — Enterprise Lens V3
# InsightFace models download on first run (not at build time)
# This avoids build timeout and network issues during Docker build

FROM python:3.10-slim

WORKDIR /app

# ── System deps ──────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 libglib2.0-0 libgomp1 git \
        build-essential cmake g++ \
        wget curl ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# ── Step 1: Build tools (MUST be before insightface) ─────────────
RUN pip install --no-cache-dir \
        "numpy<2.0" \
        "setuptools>=65" \
        wheel \
        cython \
        scikit-build \
        cmake

# ── Step 2: onnxruntime (MUST be before insightface) ─────────────
RUN pip install --no-cache-dir onnxruntime

# ── Step 3: insightface ───────────────────────────────────────────
RUN pip install --no-cache-dir --prefer-binary "insightface>=0.7.3"

# ── Step 4: Remaining requirements ───────────────────────────────
COPY requirements.txt .
RUN pip install --no-cache-dir --prefer-binary -r requirements.txt

# ── Copy app code ─────────────────────────────────────────────────
COPY . .
RUN mkdir -p temp_uploads saved_images && chmod -R 777 temp_uploads saved_images

# ── Hugging Face Auth Token ──────────────────────────────────────
# Define the argument so Docker accepts it during build
ARG HF_TOKEN
# Set it as an environment variable so Python/HuggingFace can see it
ENV HF_TOKEN=$HF_TOKEN

# ── Pre-download ONLY transformers + YOLO at build time ──────────
# InsightFace models download on first startup (cached after that)

RUN python - <<'EOF'
import os
os.environ["TRANSFORMERS_VERBOSITY"] = "error"


print("Pre-downloading SigLIP...")
from transformers import AutoProcessor, AutoModel
AutoProcessor.from_pretrained("google/siglip-base-patch16-224", use_fast=True)
AutoModel.from_pretrained("google/siglip-base-patch16-224")
print("SigLIP done")


print("Pre-downloading DINOv2...")
from transformers import AutoImageProcessor
AutoImageProcessor.from_pretrained("facebook/dinov2-base")
AutoModel.from_pretrained("facebook/dinov2-base")
print("DINOv2 done")

print("Pre-downloading YOLO seg...")

from ultralytics import YOLO
YOLO("yolo11n-seg.pt")
print("YOLO done")

print("Build complete! InsightFace models download on first startup.")

EOF

EXPOSE 7860
ENV WEB_CONCURRENCY=1


CMD uvicorn main:app \
        --host 0.0.0.0 \
        --port 7860 \

        # Add these environment variables to your existing Dockerfile.
# They significantly improve CPU inference throughput on HF free tier.

ENV OMP_NUM_THREADS=2
ENV MKL_NUM_THREADS=2
ENV OPENBLAS_NUM_THREADS=2
ENV NUMEXPR_NUM_THREADS=2
ENV TOKENIZERS_PARALLELISM=false

# Tell ONNX Runtime to use CPU optimizations aggressively
ENV ORT_DISABLE_ALL_OPTIMIZATIONS=0

# COPY the pre-converted ONNX models into the image.
# Run scripts/convert_to_onnx.py locally first, then commit onnx_models/
# to your Space repo.
COPY onnx_models/ /app/onnx_models/
ENV ONNX_MODELS_DIR=/app/onnx_models


CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]