Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- api/startup.py +44 -1
api/startup.py
CHANGED
|
@@ -6,6 +6,7 @@
|
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
import time
|
|
|
|
| 9 |
import torch
|
| 10 |
import clip
|
| 11 |
|
|
@@ -130,34 +131,67 @@ def load_all():
|
|
| 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:
|
|
@@ -168,14 +202,23 @@ def load_all():
|
|
| 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)
|
|
@@ -192,4 +235,4 @@ def load_all():
|
|
| 192 |
def get_uptime() -> float:
|
| 193 |
if STARTUP_TIME is None:
|
| 194 |
return 0.0
|
| 195 |
-
return time.time() - STARTUP_TIME
|
|
|
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
+
import sys
|
| 10 |
import torch
|
| 11 |
import clip
|
| 12 |
|
|
|
|
| 131 |
print(f"ERROR loading CLIP: {e}", flush=True)
|
| 132 |
raise
|
| 133 |
|
| 134 |
+
# DEBUG: Aggressive output buffer flushing after CLIP
|
| 135 |
+
sys.stdout.write("[DEBUG] Point 1: After CLIP load\n")
|
| 136 |
+
sys.stdout.flush()
|
| 137 |
+
|
| 138 |
print("Loading thresholds...", flush=True)
|
| 139 |
+
sys.stdout.write("[DEBUG] Point 2: After thresholds print\n")
|
| 140 |
+
sys.stdout.flush()
|
| 141 |
+
|
| 142 |
+
sys.stdout.write("[DEBUG] Point 2a: Building thresholds path\n")
|
| 143 |
+
sys.stdout.flush()
|
| 144 |
thresholds_path = os.path.join(
|
| 145 |
os.environ.get("DATA_DIR", "data"), "thresholds.json"
|
| 146 |
)
|
| 147 |
+
sys.stdout.write(f"[DEBUG] Point 2b: Checking if {thresholds_path} exists\n")
|
| 148 |
+
sys.stdout.flush()
|
| 149 |
+
|
| 150 |
if os.path.exists(thresholds_path):
|
| 151 |
+
sys.stdout.write("[DEBUG] Point 2c: File exists, opening\n")
|
| 152 |
+
sys.stdout.flush()
|
| 153 |
with open(thresholds_path) as f:
|
| 154 |
+
sys.stdout.write("[DEBUG] Point 2d: File opened, loading JSON\n")
|
| 155 |
+
sys.stdout.flush()
|
| 156 |
thresholds = json.load(f)
|
| 157 |
print(f"Thresholds loaded β {len(thresholds)} categories", flush=True)
|
| 158 |
else:
|
| 159 |
thresholds = {}
|
| 160 |
print("WARNING: thresholds.json not found β using score > 0.5 fallback", flush=True)
|
| 161 |
|
| 162 |
+
sys.stdout.write("[DEBUG] Point 3: After thresholds loading\n")
|
| 163 |
+
sys.stdout.flush()
|
| 164 |
+
|
| 165 |
# ββ GradCAM++ βββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
+
sys.stdout.write("[DEBUG] Point 4: Before GradCAM load\n")
|
| 167 |
+
sys.stdout.flush()
|
| 168 |
print("Loading GradCAM++...", flush=True)
|
| 169 |
try:
|
| 170 |
+
sys.stdout.write("[DEBUG] Point 4a: Inside GradCAM load try\n")
|
| 171 |
+
sys.stdout.flush()
|
| 172 |
gradcam.load()
|
| 173 |
print("GradCAM++ loaded β", flush=True)
|
| 174 |
except Exception as e:
|
| 175 |
print(f"WARNING: GradCAM++ load failed: {e}", flush=True)
|
| 176 |
print("Forensics mode will run without GradCAM++", flush=True)
|
| 177 |
|
| 178 |
+
sys.stdout.write("[DEBUG] Point 5: After GradCAM load\n")
|
| 179 |
+
sys.stdout.flush()
|
| 180 |
+
|
| 181 |
# ββ SHAP background βββββββββββββββββββββββββββββββββββββββ
|
| 182 |
+
sys.stdout.write("[DEBUG] Point 6: Before SHAP load\n")
|
| 183 |
+
sys.stdout.flush()
|
| 184 |
print("Loading SHAP background...", flush=True)
|
| 185 |
+
sys.stdout.write("[DEBUG] Point 6a: After SHAP print\n")
|
| 186 |
+
sys.stdout.flush()
|
| 187 |
+
|
| 188 |
bg_path = os.path.join(
|
| 189 |
os.environ.get("DATA_DIR", "data"), "shap_background.npy"
|
| 190 |
)
|
| 191 |
try:
|
| 192 |
if os.path.exists(bg_path):
|
| 193 |
+
sys.stdout.write("[DEBUG] Point 6b: SHAP file exists, loading\n")
|
| 194 |
+
sys.stdout.flush()
|
| 195 |
shap_explainer.load_background(bg_path)
|
| 196 |
print("SHAP background loaded β", flush=True)
|
| 197 |
else:
|
|
|
|
| 202 |
print("SHAP explanations will use default background", flush=True)
|
| 203 |
|
| 204 |
# ββ Inject into orchestrator ββββββββββββββββββββββββββββββ
|
| 205 |
+
sys.stdout.write("[DEBUG] Point 7: Before orchestrator init\n")
|
| 206 |
+
sys.stdout.flush()
|
| 207 |
print("Initializing orchestrator...", flush=True)
|
| 208 |
+
sys.stdout.write("[DEBUG] Point 7a: About to call init_orchestrator\n")
|
| 209 |
+
sys.stdout.flush()
|
| 210 |
try:
|
| 211 |
init_orchestrator(clip_model, clip_preprocess, thresholds)
|
| 212 |
+
sys.stdout.write("[DEBUG] Point 7b: init_orchestrator returned\n")
|
| 213 |
+
sys.stdout.flush()
|
| 214 |
print("Orchestrator initialized β", flush=True)
|
| 215 |
except Exception as e:
|
| 216 |
print(f"ERROR initializing orchestrator: {e}", flush=True)
|
| 217 |
raise
|
| 218 |
|
| 219 |
+
sys.stdout.write("[DEBUG] Point 8: After orchestrator init β about to print completion\n")
|
| 220 |
+
sys.stdout.flush()
|
| 221 |
+
|
| 222 |
elapsed = time.time() - STARTUP_TIME
|
| 223 |
print("=" * 50, flush=True)
|
| 224 |
print(f"Startup complete in {elapsed:.1f}s β", flush=True)
|
|
|
|
| 235 |
def get_uptime() -> float:
|
| 236 |
if STARTUP_TIME is None:
|
| 237 |
return 0.0
|
| 238 |
+
return time.time() - STARTUP_TIME
|