Download rsm_rise/scripts/hf_push/finalize.py from rli14/rsm_rise_code: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/rli14/rsm_rise_code/resolve/main/rsm_rise/scripts/hf_push/finalize.py
- Command line
-
hf download hf://rli14/rsm_rise_code/rsm_rise/scripts/hf_push/finalize.py
-
curl -L -o finalize.py https://huggingface.co/rli14/rsm_rise_code/resolve/main/rsm_rise/scripts/hf_push/finalize.py
2.27 kB
| #!/usr/bin/env python | |
| """HF_PUSH_ALL finalize: usage finalize.py <repo> <card.md> <results_glob> | |
| Merge results/*.json -> manifest.tsv (sha256 = Hub LFS sha256; small non-LFS files hashed locally), spot-check | |
| SPOT random LFS files by hashing the cluster source, upload manifest.tsv + README.md, write results/final_<repo>.json.""" | |
| import os, sys, json, glob, hashlib, random | |
| from huggingface_hub import HfApi | |
| repo, card, rg = sys.argv[1:4] | |
| api = HfApi(token=os.environ["HF_TOKEN"]) | |
| def sha(p): | |
| h = hashlib.sha256() | |
| with open(p, "rb") as f: | |
| for b in iter(lambda: f.read(16 << 20), b""): h.update(b) | |
| return h.hexdigest() | |
| rows, bad = [], [] | |
| for j in sorted(glob.glob(rg)): | |
| d = json.load(open(j)); bad += d["bad"] | |
| for r in d["rows"]: | |
| s = r["sha256"] or sha(r["source_path"]) | |
| rows.append([r["hf_path"], r["source_path"], r["bytes"], s, r["n_cells"], r["example_cell"], r["kind"], r["sha256"] is not None]) | |
| rows.sort() | |
| random.seed(0) | |
| spot = random.sample([r for r in rows if r[7]], min(int(os.environ.get("SPOT", "5")), sum(r[7] for r in rows))) | |
| spot_res = [(r[0], sha(r[1]) == r[3]) for r in spot] | |
| print("[fin] spot-check", spot_res, flush=True) | |
| out = os.path.join(os.path.dirname(card), "manifest_" + repo.split("/")[1] + ".tsv") | |
| with open(out, "w") as f: | |
| f.write("hf_path\tsource_path\tbytes\tsha256\tn_cells\texample_cell\tkind\n") | |
| for r in rows: f.write("\t".join(map(str, r[:7])) + "\n") | |
| tot = sum(int(r[2]) for r in rows) | |
| txt = open(card).read().replace("@@NFILES@@", str(len(rows))).replace("@@TIB@@", f"{tot/2**30:.1f} GiB") | |
| cp = out + ".README.md"; open(cp, "w").write(txt) | |
| api.upload_file(path_or_fileobj=out, path_in_repo="manifest.tsv", repo_id=repo, repo_type="model", commit_message="manifest.tsv") | |
| api.upload_file(path_or_fileobj=cp, path_in_repo="README.md", repo_id=repo, repo_type="model", commit_message="model card") | |
| res = {"repo": repo, "files": len(rows), "bytes": tot, "size_bad": bad, "spot_sha_ok": spot_res, | |
| "all_ok": not bad and all(ok for _, ok in spot_res)} | |
| json.dump(res, open(os.path.join(os.path.dirname(rg), "final_" + repo.split("/")[1] + ".json"), "w"), indent=1) | |
| print("[fin]", {k: v for k, v in res.items() if k != "spot_sha_ok"}, flush=True) | |
| sys.exit(0 if res["all_ok"] else 1) | |