Update tools/startup_selfcheck.py
Browse files- tools/startup_selfcheck.py +67 -33
tools/startup_selfcheck.py
CHANGED
|
@@ -1,54 +1,88 @@
|
|
| 1 |
#!/usr/bin/env python3
|
|
|
|
| 2 |
"""
|
| 3 |
-
Startup Self-Check for Hugging Face Space
|
| 4 |
-
- Verifies SAM2 + MatAnyone load and run
|
| 5 |
-
-
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import logging
|
|
|
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import torch
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
if not
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
|
| 19 |
-
def run_selfcheck():
|
| 20 |
-
results
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
img = np.zeros((64, 64, 3), dtype=np.uint8)
|
|
|
|
| 24 |
|
| 25 |
# --- SAM2 ---
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
sam
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# --- MatAnyone ---
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
# tools/startup_selfcheck.py
|
| 3 |
"""
|
| 4 |
+
Quiet Startup Self-Check for Hugging Face Space
|
| 5 |
+
- Verifies SAM2 + MatAnyone can load and run a tiny inference
|
| 6 |
+
- Defaults to ASYNC so UI launches immediately; logs results
|
| 7 |
+
- Switch to SYNC by setting env SELF_CHECK_MODE=sync
|
| 8 |
"""
|
| 9 |
|
| 10 |
import logging
|
| 11 |
+
import os
|
| 12 |
+
import threading
|
| 13 |
import numpy as np
|
| 14 |
import torch
|
| 15 |
|
| 16 |
+
# Keep imports lazy so a busted loader doesn't break the Space
|
| 17 |
+
def _safe_imports():
|
| 18 |
+
from models.loaders.sam2_loader import SAM2Loader
|
| 19 |
+
from models.loaders.matanyone_loader import MatAnyoneLoader
|
| 20 |
+
return SAM2Loader, MatAnyoneLoader
|
| 21 |
|
| 22 |
+
log = logging.getLogger("selfcheck")
|
| 23 |
+
if not log.handlers:
|
| 24 |
logging.basicConfig(level=logging.INFO)
|
| 25 |
|
| 26 |
+
def run_selfcheck() -> None:
|
| 27 |
+
"""Do the actual check; log results. Non-throwing."""
|
| 28 |
try:
|
| 29 |
+
SAM2Loader, MatAnyoneLoader = _safe_imports()
|
| 30 |
+
except Exception as e:
|
| 31 |
+
log.error(f"β Self-check import failure: {e}", exc_info=True)
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# tiny dummy frame + mask
|
| 36 |
img = np.zeros((64, 64, 3), dtype=np.uint8)
|
| 37 |
+
mask = np.ones((64, 64), dtype=np.float32)
|
| 38 |
|
| 39 |
# --- SAM2 ---
|
| 40 |
+
try:
|
| 41 |
+
sam_loader = SAM2Loader(device="cuda" if torch.cuda.is_available() else "cpu")
|
| 42 |
+
sam = sam_loader.load("tiny")
|
| 43 |
+
if sam:
|
| 44 |
+
sam.set_image(img)
|
| 45 |
+
out = sam.predict(point_coords=None, point_labels=None)
|
| 46 |
+
m = out["masks"]
|
| 47 |
+
log.info(f"β
SAM2 OK β masks {m.shape} {m.dtype} [{m.min():.3f},{m.max():.3f}]")
|
| 48 |
+
else:
|
| 49 |
+
log.error("β SAM2 failed to load (adapter is None)")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
log.error(f"β SAM2 self-check error: {e}", exc_info=True)
|
| 52 |
|
| 53 |
# --- MatAnyone ---
|
| 54 |
+
try:
|
| 55 |
+
mat_loader = MatAnyoneLoader(device="cuda" if torch.cuda.is_available() else "cpu")
|
| 56 |
+
session = mat_loader.load()
|
| 57 |
+
if session:
|
| 58 |
+
alpha = session(img, mask) # first frame needs a mask (fallback ok)
|
| 59 |
+
log.info(f"β
MatAnyone OK β alpha {alpha.shape} {alpha.dtype} [{alpha.min():.3f},{alpha.max():.3f}]")
|
| 60 |
+
else:
|
| 61 |
+
log.error("β MatAnyone failed to load (session is None)")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
log.error(f"β MatAnyone self-check error: {e}", exc_info=True)
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
+
log.error(f"β Self-check unexpected error: {e}", exc_info=True)
|
| 67 |
+
|
| 68 |
+
def schedule_startup_selfcheck(mode: str = "async") -> None:
|
| 69 |
+
"""
|
| 70 |
+
mode: "async" (default) β run in a daemon thread; UI doesn't wait
|
| 71 |
+
"sync" β block until check completes (slower startup)
|
| 72 |
+
"""
|
| 73 |
+
mode = (mode or "async").lower()
|
| 74 |
+
if mode == "sync":
|
| 75 |
+
log.info("π Running model self-check (sync)β¦")
|
| 76 |
+
run_selfcheck()
|
| 77 |
+
return
|
| 78 |
|
| 79 |
+
def _worker():
|
| 80 |
+
try:
|
| 81 |
+
log.info("π Running model self-check (async)β¦")
|
| 82 |
+
run_selfcheck()
|
| 83 |
+
except Exception:
|
| 84 |
+
# Make absolutely sure this never crashes the app
|
| 85 |
+
log.exception("Self-check thread crashed")
|
| 86 |
|
| 87 |
+
t = threading.Thread(target=_worker, name="startup-selfcheck", daemon=True)
|
| 88 |
+
t.start()
|