okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
2.3 KiB55 linesraw
1#!/usr/bin/env python3
2"""Print the size and SHA-256 Hugging Face's API metadata gives for the files of a repository at a pinned commit.
3Nothing is downloaded: https://huggingface.co/api/models/<repo>/tree/<revision>?recursive=1 lists each LFS file with
4`lfs.oid` (the SHA-256) and `size`.
5
6    python tools/hf_pin_fetch.py ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF ed59f92082b1e93c0e96d60a8b11aab089b52f09
7    python tools/hf_pin_fetch.py --json <repo> <revision>     # {path: [size, sha256]} as JSON
8    python tools/hf_pin_fetch.py --setup                      # every repository in setup.py's HF_REVISIONS, as JSON
9"""
10from __future__ import annotations
11
12import json
13import sys
14import urllib.request
15from pathlib import Path
16
17sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
18from strata_control import tls  # noqa: E402
19
20tls.install()       # #3722: the operating system's certificates
21
22
23def tree(repo: str, revision: str) -> dict:
24    """{path: (size, sha256)} of the repository's LFS files at `revision` (the API pages its answer with Link headers)."""
25    out = {}
26    url = f"https://huggingface.co/api/models/{repo}/tree/{revision}?recursive=1&expand=0"
27    while url:
28        with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "strata-pin"}), timeout=60) as r:
29            for e in json.load(r):
30                if e.get("type") == "file" and e.get("lfs"):
31                    out[e["path"]] = (int(e["lfs"]["size"]), e["lfs"]["oid"])
32            link = r.headers.get("Link", "")
33        url = next((p.split(";")[0].strip(" <>") for p in link.split(",") if 'rel="next"' in p), None)
34    return out
35
36
37def main(argv: list) -> int:
38    if argv and argv[0] == "--setup":
39        sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
40        import setup
41        print(json.dumps({r: {p: list(v) for p, v in tree(r, rev).items()} for r, rev in setup.HF_REVISIONS.items()}, indent=1))
42        return 0
43    as_json = bool(argv) and argv[0] == "--json"
44    repo, revision = argv[1:3] if as_json else argv[:2]
45    t = tree(repo, revision)
46    if as_json:
47        print(json.dumps({p: list(v) for p, v in t.items()}, indent=1))
48    else:
49        for p, (size, sha) in sorted(t.items()):
50            print(f"{size:>14,}  {sha}  {p}")
51    return 0
52
53
54if __name__ == "__main__":
55    sys.exit(main(sys.argv[1:]))