#!/usr/bin/env python3 """Refresh weight sizes in llmhost/catalog.json from the Hugging Face API. python3 tools/refresh_catalog.py # report differences python3 tools/refresh_catalog.py --write # update the file """ import json import sys import urllib.request from pathlib import Path CATALOG = Path(__file__).resolve().parent.parent / "llmhost/catalog.json" def weights_gb(repo): with urllib.request.urlopen(f"https://huggingface.co/api/models/{repo}?blobs=true", timeout=30) as r: d = json.load(r) total = sum((s.get("size") or 0) for s in d.get("siblings", []) if s["rfilename"].endswith(".safetensors") and "/" not in s["rfilename"]) return round(total / 1e9, 1) def main(): write = "--write" in sys.argv text = CATALOG.read_text() cat = json.loads(text) changed = 0 for m in cat["models"]: for fmt, v in m["variants"].items(): try: gb = weights_gb(v["repo"]) except Exception as e: print(f"!! {v['repo']}: {e}") continue if abs(gb - v["gb"]) >= 0.1: print(f"{m['name']:18} {fmt:9} {v['gb']:>7} -> {gb:>7} GB {v['repo']}") old = f'"repo": "{v["repo"]}", "gb": {v["gb"]}' text = text.replace(old, f'"repo": "{v["repo"]}", "gb": {gb}') changed += 1 if write and changed: json.loads(text) CATALOG.write_text(text) print(f"{changed} size(s) {'updated' if write else 'differ'}") if __name__ == "__main__": main()