File size: 2,575 Bytes
1c2dd4b
 
 
 
 
 
a2d1173
 
 
 
 
 
 
 
46f25fd
 
a2d1173
 
 
1c2dd4b
 
 
 
a2d1173
1c2dd4b
 
cec66cb
 
 
 
 
1c2dd4b
 
 
 
 
 
 
 
 
 
 
 
cec66cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c2dd4b
 
 
a2d1173
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Download the fine-tuned GGUF model from Hugging Face.

Robust download: returns the expected local path even on failure so the
rest of the app can report a clear "model not found" error instead of
crashing at startup. Tries with the token first, then anonymously
(works for public repos)."""

import os
from pathlib import Path

from dotenv import load_dotenv

load_dotenv()

MODEL_REPO = os.getenv("MODEL_REPO", "sankalphs/retro-alpha-nemotron-gguf")
MODEL_FILE = os.getenv("MODEL_FILE", "NVIDIA-Nemotron-3-Nano-4B.Q4_K_M.gguf")
MODEL_DIR = Path(__file__).resolve().parent / "models"


def _local_path() -> Path:
    return MODEL_DIR / MODEL_FILE


def download() -> str:
    """Ensure the GGUF model is available locally. Returns the local path
    as a string. Never raises — returns the expected path even on
    failure so callers can surface a precise error to the user.

    Retries each download mode up to 3 times with exponential backoff
    to survive transient network failures on cold starts."""
    import time
    MODEL_DIR.mkdir(parents=True, exist_ok=True)
    local = _local_path()

    if local.exists() and local.stat().st_size > 100_000_000:
        size_gb = local.stat().st_size / 1e9
        print(f"Model already present: {local} ({size_gb:.2f} GB)")
        return str(local)

    token = os.getenv("HF_TOKEN")
    for attempt_token, label in [(token, "with token"), (None, "anonymously")]:
        if attempt_token == "":
            attempt_token = None
        for attempt in range(1, 4):
            try:
                print(f"Downloading {MODEL_FILE} from {MODEL_REPO} ({label}, attempt {attempt}/3)...")
                from huggingface_hub import hf_hub_download
                path = hf_hub_download(
                    repo_id=MODEL_REPO,
                    filename=MODEL_FILE,
                    local_dir=str(MODEL_DIR),
                    local_dir_use_symlinks=False,
                    token=attempt_token,
                )
                print(f"Download complete: {path}")
                return str(path)
            except Exception as e:
                print(f"Download attempt {attempt} failed ({label}): {type(e).__name__}: {e}")
                if attempt < 3:
                    time.sleep(2 ** attempt)  # 2s, 4s
                else:
                    break
        # If anonymous mode also failed, no point retrying it
        if attempt_token is None:
            break

    print(f"Model download failed. Expected at: {local}")
    return str(local)


if __name__ == "__main__":
    print(download())