File size: 1,803 Bytes
3a66575
 
 
 
 
 
 
 
 
 
 
 
8c2f1b1
3a66575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
download_weights.py
Auto-downloads the P2PNet model weights from HuggingFace Hub if they are
not present locally. Called at FastAPI startup so the container always has
the weights without committing the 82 MB .pth file to Git.
"""

import os


HF_WEIGHTS_REPO = os.environ.get(
    "HF_WEIGHTS_REPO",
    "praveendatascience/crowd-counting-weights",
)
WEIGHTS_FILENAME = "SHTechA.pth"
WEIGHTS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "weights")
WEIGHTS_PATH = os.path.join(WEIGHTS_DIR, WEIGHTS_FILENAME)


def ensure_weights() -> None:
    """Download model weights from HuggingFace Hub if not present locally."""
    if os.path.exists(WEIGHTS_PATH):
        print(f"[Weights] Found at {WEIGHTS_PATH} - skipping download.")
        return

    repo_is_placeholder = (
        not HF_WEIGHTS_REPO
        or "YOUR_HF_USERNAME" in HF_WEIGHTS_REPO
        or "your-username" in HF_WEIGHTS_REPO.lower()
    )
    if repo_is_placeholder:
        print("[Weights] No valid HuggingFace weights repo configured - skipping download.")
        print("[Weights] The model will run without pretrained weights.")
        return

    print(
        f"[Weights] Not found locally. Downloading '{WEIGHTS_FILENAME}' "
        f"from HuggingFace Hub repo '{HF_WEIGHTS_REPO}' ..."
    )
    os.makedirs(WEIGHTS_DIR, exist_ok=True)

    try:
        from huggingface_hub import hf_hub_download

        downloaded = hf_hub_download(
            repo_id=HF_WEIGHTS_REPO,
            filename=WEIGHTS_FILENAME,
            local_dir=WEIGHTS_DIR,
        )
        print(f"[Weights] Downloaded successfully -> {downloaded}")
    except Exception as exc:
        print(f"[Weights] WARNING: Could not download weights - {exc}")
        print("[Weights] The model will run without pretrained weights.")