File size: 2,360 Bytes
414dc55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
"""Open-weights fetcher. Downloads the models once so the runtime can run locally:
- the LLM GGUF (Qwen2.5-1.5B-Instruct, Q4_K_M) into models/llm/;
- the Supertonic ONNX TTS model into its cache.

Run once locally (or it is invoked automatically at app startup if the GGUF is missing).

    python scripts/fetch_models.py
    python scripts/fetch_models.py --llm-only
"""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "src"))

LLM_DIR = ROOT / "models" / "llm"
# The single model the game runs on: Qwen2.5-1.5B (fast, <=4B Tiny Titan).
LLM_CANDIDATES = (
    ("Qwen/Qwen2.5-1.5B-Instruct-GGUF", "qwen2.5-1.5b-instruct-q4_k_m.gguf",
     LLM_DIR / "qwen2.5-1.5b-instruct-q4_k_m.gguf"),
)

def _prefetch_supertonic() -> bool:
    """Cache the Supertonic ONNX model so the runtime never downloads (off-grid)."""
    try:
        from supertonic import TTS

        TTS(auto_download=True)
        print("[fetch] Supertonic model cached")
        return True
    except Exception as exc:
        print(f"[fetch] Supertonic prefetch failed: {exc}", file=sys.stderr)
        return False


def _download(repo: str, filename: str, dest: Path) -> bool:
    from huggingface_hub import hf_hub_download

    try:
        path = hf_hub_download(repo_id=repo, filename=filename)
        dest.parent.mkdir(parents=True, exist_ok=True)
        dest.write_bytes(Path(path).read_bytes())
        print(f"[fetch] {repo}/{filename} -> {dest} ({dest.stat().st_size} bytes)")
        return True
    except Exception as exc:
        print(f"[fetch] FAILED {repo}/{filename}: {exc}", file=sys.stderr)
        return False


def main() -> int:
    parser = argparse.ArgumentParser(description="Fetch open weights for Case Zero")
    parser.add_argument("--llm-only", action="store_true")
    parser.add_argument("--tts-only", action="store_true")
    args = parser.parse_args()

    ok = True
    if not args.tts_only:
        llm_ok = False
        for repo, filename, dest in LLM_CANDIDATES:
            if _download(repo, filename, dest):
                llm_ok = True
                break
        ok &= llm_ok
    if not args.llm_only:
        ok &= _prefetch_supertonic()
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())