Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- api/startup.py +30 -7
api/startup.py
CHANGED
|
@@ -120,41 +120,64 @@ def load_all():
|
|
| 120 |
# ββ CLIP model ββββββββββββββββββββββββββββββββββββββββββββ
|
| 121 |
# Loaded here, injected into orchestrator
|
| 122 |
print("Loading CLIP ViT-B/32...")
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# ββ Thresholds ββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 128 |
thresholds_path = os.path.join(
|
| 129 |
os.environ.get("DATA_DIR", "data"), "thresholds.json"
|
| 130 |
)
|
| 131 |
if os.path.exists(thresholds_path):
|
| 132 |
with open(thresholds_path) as f:
|
| 133 |
thresholds = json.load(f)
|
| 134 |
-
print(f"Thresholds loaded
|
| 135 |
else:
|
| 136 |
thresholds = {}
|
| 137 |
print("WARNING: thresholds.json not found β using score > 0.5 fallback")
|
| 138 |
|
| 139 |
# ββ GradCAM++ βββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 140 |
try:
|
| 141 |
gradcam.load()
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
print(f"WARNING: GradCAM++ load failed: {e}")
|
| 144 |
print("Forensics mode will run without GradCAM++")
|
| 145 |
|
| 146 |
# ββ SHAP background βββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 147 |
bg_path = os.path.join(
|
| 148 |
os.environ.get("DATA_DIR", "data"), "shap_background.npy"
|
| 149 |
)
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
# ββ Inject into orchestrator ββββββββββββββββββββββββββββββ
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
elapsed = time.time() - STARTUP_TIME
|
| 156 |
print("=" * 50)
|
| 157 |
-
print(f"Startup complete in {elapsed:.1f}s")
|
| 158 |
print(f"Model version: {MODEL_VERSION}")
|
| 159 |
print("=" * 50)
|
| 160 |
|
|
|
|
| 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 |
# ββ Thresholds ββββββββββββββββββββββββββββββββββββββββββββ
|
| 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 |
|