Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- api/startup.py +25 -24
api/startup.py
CHANGED
|
@@ -119,67 +119,68 @@ def load_all():
|
|
| 119 |
|
| 120 |
# ββ CLIP model ββββββββββββββββββββββββββββββββββββββββββββ
|
| 121 |
# Loaded here, injected into orchestrator
|
| 122 |
-
print("Loading CLIP ViT-B/32...")
|
| 123 |
try:
|
|
|
|
| 124 |
clip_model, clip_preprocess = clip.load("ViT-B/32", device="cpu")
|
|
|
|
| 125 |
clip_model.eval()
|
| 126 |
-
print("CLIP loaded β")
|
| 127 |
except Exception as e:
|
| 128 |
-
print(f"ERROR loading CLIP: {e}")
|
| 129 |
raise
|
| 130 |
|
| 131 |
-
|
| 132 |
-
print("Loading thresholds...")
|
| 133 |
thresholds_path = os.path.join(
|
| 134 |
os.environ.get("DATA_DIR", "data"), "thresholds.json"
|
| 135 |
)
|
| 136 |
if os.path.exists(thresholds_path):
|
| 137 |
with open(thresholds_path) as f:
|
| 138 |
thresholds = json.load(f)
|
| 139 |
-
print(f"Thresholds loaded β {len(thresholds)} categories")
|
| 140 |
else:
|
| 141 |
thresholds = {}
|
| 142 |
-
print("WARNING: thresholds.json not found β using score > 0.5 fallback")
|
| 143 |
|
| 144 |
# ββ GradCAM++ βββββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
-
print("Loading GradCAM++...")
|
| 146 |
try:
|
| 147 |
gradcam.load()
|
| 148 |
-
print("GradCAM++ loaded β")
|
| 149 |
except Exception as e:
|
| 150 |
-
print(f"WARNING: GradCAM++ load failed: {e}")
|
| 151 |
-
print("Forensics mode will run without GradCAM++")
|
| 152 |
|
| 153 |
# ββ SHAP background βββββββββββββββββββββββββββββββββββββββ
|
| 154 |
-
print("Loading SHAP background...")
|
| 155 |
bg_path = os.path.join(
|
| 156 |
os.environ.get("DATA_DIR", "data"), "shap_background.npy"
|
| 157 |
)
|
| 158 |
try:
|
| 159 |
if os.path.exists(bg_path):
|
| 160 |
shap_explainer.load_background(bg_path)
|
| 161 |
-
print("SHAP background loaded β")
|
| 162 |
else:
|
| 163 |
-
print(f"WARNING: SHAP background not found at {bg_path}")
|
| 164 |
-
print("SHAP explanations will use default background")
|
| 165 |
except Exception as e:
|
| 166 |
-
print(f"WARNING: SHAP background load failed: {e}")
|
| 167 |
-
print("SHAP explanations will use default background")
|
| 168 |
|
| 169 |
# ββ Inject into orchestrator ββββββββββββββββββββββββββββββ
|
| 170 |
-
print("Initializing orchestrator...")
|
| 171 |
try:
|
| 172 |
init_orchestrator(clip_model, clip_preprocess, thresholds)
|
| 173 |
-
print("Orchestrator initialized β")
|
| 174 |
except Exception as e:
|
| 175 |
-
print(f"ERROR initializing orchestrator: {e}")
|
| 176 |
raise
|
| 177 |
|
| 178 |
elapsed = time.time() - STARTUP_TIME
|
| 179 |
-
print("=" * 50)
|
| 180 |
-
print(f"Startup complete in {elapsed:.1f}s β")
|
| 181 |
-
print(f"Model version: {MODEL_VERSION}")
|
| 182 |
-
print("=" * 50)
|
| 183 |
|
| 184 |
return {
|
| 185 |
"clip_model": clip_model,
|
|
|
|
| 119 |
|
| 120 |
# ββ CLIP model ββββββββββββββββββββββββββββββββββββββββββββ
|
| 121 |
# Loaded here, injected into orchestrator
|
| 122 |
+
print("Loading CLIP ViT-B/32...", flush=True)
|
| 123 |
try:
|
| 124 |
+
print(" [Downloading CLIP weights...]", flush=True)
|
| 125 |
clip_model, clip_preprocess = clip.load("ViT-B/32", device="cpu")
|
| 126 |
+
print(" [CLIP weights loaded, setting eval mode...]", flush=True)
|
| 127 |
clip_model.eval()
|
| 128 |
+
print("CLIP loaded β", flush=True)
|
| 129 |
except Exception as e:
|
| 130 |
+
print(f"ERROR loading CLIP: {e}", flush=True)
|
| 131 |
raise
|
| 132 |
|
| 133 |
+
print("Loading thresholds...", flush=True)
|
|
|
|
| 134 |
thresholds_path = os.path.join(
|
| 135 |
os.environ.get("DATA_DIR", "data"), "thresholds.json"
|
| 136 |
)
|
| 137 |
if os.path.exists(thresholds_path):
|
| 138 |
with open(thresholds_path) as f:
|
| 139 |
thresholds = json.load(f)
|
| 140 |
+
print(f"Thresholds loaded β {len(thresholds)} categories", flush=True)
|
| 141 |
else:
|
| 142 |
thresholds = {}
|
| 143 |
+
print("WARNING: thresholds.json not found β using score > 0.5 fallback", flush=True)
|
| 144 |
|
| 145 |
# ββ GradCAM++ βββββββββββββββββββββββββββββββββββββββββββββ
|
| 146 |
+
print("Loading GradCAM++...", flush=True)
|
| 147 |
try:
|
| 148 |
gradcam.load()
|
| 149 |
+
print("GradCAM++ loaded β", flush=True)
|
| 150 |
except Exception as e:
|
| 151 |
+
print(f"WARNING: GradCAM++ load failed: {e}", flush=True)
|
| 152 |
+
print("Forensics mode will run without GradCAM++", flush=True)
|
| 153 |
|
| 154 |
# ββ SHAP background βββββββββββββββββββββββββββββββββββββββ
|
| 155 |
+
print("Loading SHAP background...", flush=True)
|
| 156 |
bg_path = os.path.join(
|
| 157 |
os.environ.get("DATA_DIR", "data"), "shap_background.npy"
|
| 158 |
)
|
| 159 |
try:
|
| 160 |
if os.path.exists(bg_path):
|
| 161 |
shap_explainer.load_background(bg_path)
|
| 162 |
+
print("SHAP background loaded β", flush=True)
|
| 163 |
else:
|
| 164 |
+
print(f"WARNING: SHAP background not found at {bg_path}", flush=True)
|
| 165 |
+
print("SHAP explanations will use default background", flush=True)
|
| 166 |
except Exception as e:
|
| 167 |
+
print(f"WARNING: SHAP background load failed: {e}", flush=True)
|
| 168 |
+
print("SHAP explanations will use default background", flush=True)
|
| 169 |
|
| 170 |
# ββ Inject into orchestrator ββββββββββββββββββββββββββββββ
|
| 171 |
+
print("Initializing orchestrator...", flush=True)
|
| 172 |
try:
|
| 173 |
init_orchestrator(clip_model, clip_preprocess, thresholds)
|
| 174 |
+
print("Orchestrator initialized β", flush=True)
|
| 175 |
except Exception as e:
|
| 176 |
+
print(f"ERROR initializing orchestrator: {e}", flush=True)
|
| 177 |
raise
|
| 178 |
|
| 179 |
elapsed = time.time() - STARTUP_TIME
|
| 180 |
+
print("=" * 50, flush=True)
|
| 181 |
+
print(f"Startup complete in {elapsed:.1f}s β", flush=True)
|
| 182 |
+
print(f"Model version: {MODEL_VERSION}", flush=True)
|
| 183 |
+
print("=" * 50, flush=True)
|
| 184 |
|
| 185 |
return {
|
| 186 |
"clip_model": clip_model,
|