Spaces:
Sleeping
Sleeping
fix: Build failing
Browse files- Dockerfile +7 -13
- src/models.py +11 -8
Dockerfile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# Dockerfile — Enterprise Lens V3
|
| 2 |
-
# InsightFace
|
| 3 |
-
#
|
| 4 |
|
| 5 |
FROM python:3.10-slim
|
| 6 |
|
|
@@ -14,7 +14,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 14 |
wget ca-certificates \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
-
# ── Step 1:
|
| 18 |
RUN pip install --no-cache-dir \
|
| 19 |
"numpy<2.0" \
|
| 20 |
"setuptools>=65" \
|
|
@@ -26,7 +26,7 @@ RUN pip install --no-cache-dir \
|
|
| 26 |
# ── Step 2: onnxruntime (MUST be before insightface) ─────────────
|
| 27 |
RUN pip install --no-cache-dir onnxruntime
|
| 28 |
|
| 29 |
-
# ── Step 3: insightface
|
| 30 |
RUN pip install --no-cache-dir --prefer-binary insightface
|
| 31 |
|
| 32 |
# ── Step 4: Remaining requirements ───────────────────────────────
|
|
@@ -37,7 +37,8 @@ RUN pip install --no-cache-dir --prefer-binary -r requirements.txt
|
|
| 37 |
COPY . .
|
| 38 |
RUN mkdir -p temp_uploads saved_images && chmod -R 777 temp_uploads saved_images
|
| 39 |
|
| 40 |
-
# ── Pre-download
|
|
|
|
| 41 |
RUN python - <<'EOF'
|
| 42 |
import os
|
| 43 |
os.environ["TRANSFORMERS_VERBOSITY"] = "error"
|
|
@@ -59,14 +60,7 @@ from ultralytics import YOLO
|
|
| 59 |
YOLO("yolo11n-seg.pt")
|
| 60 |
print("YOLO done")
|
| 61 |
|
| 62 |
-
print("
|
| 63 |
-
from insightface.app import FaceAnalysis
|
| 64 |
-
app = FaceAnalysis(name="buffalo_sc", providers=["CPUExecutionProvider"])
|
| 65 |
-
app.prepare(ctx_id=-1, det_size=(640, 640))
|
| 66 |
-
del app
|
| 67 |
-
print("InsightFace done")
|
| 68 |
-
|
| 69 |
-
print("All V3 models cached!")
|
| 70 |
EOF
|
| 71 |
|
| 72 |
EXPOSE 7860
|
|
|
|
| 1 |
# Dockerfile — Enterprise Lens V3
|
| 2 |
+
# InsightFace models download on first run (not at build time)
|
| 3 |
+
# This avoids build timeout and network issues during Docker build
|
| 4 |
|
| 5 |
FROM python:3.10-slim
|
| 6 |
|
|
|
|
| 14 |
wget ca-certificates \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
+
# ── Step 1: Build tools (MUST be before insightface) ─────────────
|
| 18 |
RUN pip install --no-cache-dir \
|
| 19 |
"numpy<2.0" \
|
| 20 |
"setuptools>=65" \
|
|
|
|
| 26 |
# ── Step 2: onnxruntime (MUST be before insightface) ─────────────
|
| 27 |
RUN pip install --no-cache-dir onnxruntime
|
| 28 |
|
| 29 |
+
# ── Step 3: insightface ───────────────────────────────────────────
|
| 30 |
RUN pip install --no-cache-dir --prefer-binary insightface
|
| 31 |
|
| 32 |
# ── Step 4: Remaining requirements ───────────────────────────────
|
|
|
|
| 37 |
COPY . .
|
| 38 |
RUN mkdir -p temp_uploads saved_images && chmod -R 777 temp_uploads saved_images
|
| 39 |
|
| 40 |
+
# ── Pre-download ONLY transformers + YOLO at build time ──────────
|
| 41 |
+
# InsightFace models download on first startup (cached after that)
|
| 42 |
RUN python - <<'EOF'
|
| 43 |
import os
|
| 44 |
os.environ["TRANSFORMERS_VERBOSITY"] = "error"
|
|
|
|
| 60 |
YOLO("yolo11n-seg.pt")
|
| 61 |
print("YOLO done")
|
| 62 |
|
| 63 |
+
print("Build complete! InsightFace models download on first startup.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
EOF
|
| 65 |
|
| 66 |
EXPOSE 7860
|
src/models.py
CHANGED
|
@@ -19,6 +19,7 @@ import io
|
|
| 19 |
|
| 20 |
import cv2
|
| 21 |
import numpy as np
|
|
|
|
| 22 |
import torch
|
| 23 |
import torch.nn.functional as F
|
| 24 |
from PIL import Image
|
|
@@ -113,14 +114,12 @@ class AIModelManager:
|
|
| 113 |
# buffalo_sc = small+fast model (CPU optimised)
|
| 114 |
# buffalo_l = large+accurate (use if GPU available)
|
| 115 |
model_name = "buffalo_l" if self.device == "cuda" else "buffalo_sc"
|
| 116 |
-
self.face_app = FaceAnalysis(
|
| 117 |
-
name=model_name,
|
| 118 |
-
providers=["CUDAExecutionProvider"] if self.device == "cuda"
|
| 119 |
-
else ["CPUExecutionProvider"],
|
| 120 |
-
)
|
| 121 |
# det_size controls detection resolution — larger = finds smaller faces
|
| 122 |
-
self.face_app.prepare(
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
print(f"✅ InsightFace ({model_name}) loaded — ArcFace face lane active")
|
| 125 |
except Exception as e:
|
| 126 |
print(f"⚠️ InsightFace init failed: {e} — face lane disabled")
|
|
@@ -130,6 +129,9 @@ class AIModelManager:
|
|
| 130 |
|
| 131 |
self._cache = {}
|
| 132 |
self._cache_maxsize = 128
|
|
|
|
|
|
|
|
|
|
| 133 |
print("✅ Models ready!")
|
| 134 |
|
| 135 |
# ── Object Lane batched embedding ────────────────────────────
|
|
@@ -183,7 +185,8 @@ class AIModelManager:
|
|
| 183 |
else:
|
| 184 |
bgr = img_np.copy()
|
| 185 |
|
| 186 |
-
|
|
|
|
| 187 |
results = []
|
| 188 |
|
| 189 |
for idx, face in enumerate(faces):
|
|
|
|
| 19 |
|
| 20 |
import cv2
|
| 21 |
import numpy as np
|
| 22 |
+
import threading
|
| 23 |
import torch
|
| 24 |
import torch.nn.functional as F
|
| 25 |
from PIL import Image
|
|
|
|
| 114 |
# buffalo_sc = small+fast model (CPU optimised)
|
| 115 |
# buffalo_l = large+accurate (use if GPU available)
|
| 116 |
model_name = "buffalo_l" if self.device == "cuda" else "buffalo_sc"
|
| 117 |
+
self.face_app = FaceAnalysis(name=model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# det_size controls detection resolution — larger = finds smaller faces
|
| 119 |
+
self.face_app.prepare(
|
| 120 |
+
ctx_id=0 if self.device == "cuda" else -1,
|
| 121 |
+
det_size=(640, 640),
|
| 122 |
+
)
|
| 123 |
print(f"✅ InsightFace ({model_name}) loaded — ArcFace face lane active")
|
| 124 |
except Exception as e:
|
| 125 |
print(f"⚠️ InsightFace init failed: {e} — face lane disabled")
|
|
|
|
| 129 |
|
| 130 |
self._cache = {}
|
| 131 |
self._cache_maxsize = 128
|
| 132 |
+
# InsightFace ONNX runtime is NOT thread-safe
|
| 133 |
+
# This lock ensures only one inference runs at a time
|
| 134 |
+
self._face_lock = threading.Lock()
|
| 135 |
print("✅ Models ready!")
|
| 136 |
|
| 137 |
# ── Object Lane batched embedding ────────────────────────────
|
|
|
|
| 185 |
else:
|
| 186 |
bgr = img_np.copy()
|
| 187 |
|
| 188 |
+
with self._face_lock:
|
| 189 |
+
faces = self.face_app.get(bgr)
|
| 190 |
results = []
|
| 191 |
|
| 192 |
for idx, face in enumerate(faces):
|