feat: download checkpoints from HF model repo on startup
Browse files- formscout/startup.py +48 -0
formscout/startup.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Checkpoint bootstrap — downloads missing model files from HF model repo on first run.
|
| 3 |
+
Called once at app startup before build_app(); no-ops if files already present.
|
| 4 |
+
"""
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
import logging
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
CHECKPOINT_REPO = "silas-therapy/formscout-checkpoints"
|
| 13 |
+
ROOT = Path(__file__).parent.parent
|
| 14 |
+
|
| 15 |
+
_CHECKPOINTS = [
|
| 16 |
+
"checkpoints/yolo26/yolo26n-pose.pt",
|
| 17 |
+
"checkpoints/yolo26/yolo26s-pose.pt",
|
| 18 |
+
"checkpoints/yolo26/yolo26m-pose.pt",
|
| 19 |
+
"checkpoints/yolo26/yolo26l-pose.pt",
|
| 20 |
+
"checkpoints/yolo26/yolo26x-pose.pt",
|
| 21 |
+
"checkpoints/mediapipe/pose_landmarker_full.task",
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def ensure_checkpoints() -> None:
|
| 26 |
+
"""Download any missing checkpoints from silas-therapy/formscout-checkpoints."""
|
| 27 |
+
try:
|
| 28 |
+
from huggingface_hub import hf_hub_download
|
| 29 |
+
except ImportError:
|
| 30 |
+
logger.warning("huggingface_hub not installed — skipping checkpoint download")
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
for rel_path in _CHECKPOINTS:
|
| 34 |
+
local = ROOT / rel_path
|
| 35 |
+
if local.exists():
|
| 36 |
+
continue
|
| 37 |
+
logger.info("Downloading %s ...", rel_path)
|
| 38 |
+
try:
|
| 39 |
+
local.parent.mkdir(parents=True, exist_ok=True)
|
| 40 |
+
hf_hub_download(
|
| 41 |
+
repo_id=CHECKPOINT_REPO,
|
| 42 |
+
filename=rel_path,
|
| 43 |
+
local_dir=str(ROOT),
|
| 44 |
+
local_dir_use_symlinks=False,
|
| 45 |
+
)
|
| 46 |
+
logger.info("Downloaded %s", rel_path)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logger.warning("Failed to download %s: %s", rel_path, e)
|