turkic-ocr-code / scripts /r5_testpass2 /tp2_kzb_fetch.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
3.72 kB
"""Test pass 2, owner-approved download (25 Nov 2026): Omar-2002/kz_benchmark_data, handwritten config only, no token.
huggingface_hub 2.4.0 (CODE/.venv_dl): HfApi.dataset_info / list_repo_tree (token=False), hf_hub_download, snapshot_download
(allow_patterns, local_dir, token=False). Downloaded files are untrusted data: nothing in them is executed.
usage: tp2_kzb_fetch.py card --dest DIR --out JSON # pin the revision, list the tree, fetch README.md only
tp2_kzb_fetch.py fetch --dest DIR --out JSON --listing JSON --pattern P [--pattern P ...]
"""
import argparse
import hashlib
import json
import os
import time
from huggingface_hub import HfApi, hf_hub_download, snapshot_download
REPO = "Omar-2002/kz_benchmark_data"
def sha(p):
h = hashlib.sha256()
with open(p, "rb") as f:
for b in iter(lambda: f.read(1 << 22), b""):
h.update(b)
return h.hexdigest()
ap = argparse.ArgumentParser()
ap.add_argument("mode", choices=["card", "fetch"])
ap.add_argument("--dest", required=True)
ap.add_argument("--out", required=True)
ap.add_argument("--listing", default="")
ap.add_argument("--pattern", action="append", default=[])
a = ap.parse_args()
api = HfApi()
t0 = time.time()
if a.mode == "card":
if os.path.exists(a.dest) and os.listdir(a.dest):
raise SystemExit("REFUSED: destination exists and is not empty: " + a.dest)
os.makedirs(a.dest, exist_ok=True)
info = api.dataset_info(REPO, token=False)
rev = info.sha
tree = []
for x in api.list_repo_tree(REPO, recursive=True, repo_type="dataset", revision=rev, token=False):
d = {"path": x.path, "type": type(x).__name__}
if hasattr(x, "size"):
d["size"] = x.size
lfs = getattr(x, "lfs", None)
if lfs is not None:
d["lfs_sha256"] = getattr(lfs, "sha256", None)
d["blob_id"] = getattr(x, "blob_id", None)
tree.append(d)
p = hf_hub_download(REPO, "README.md", repo_type="dataset", revision=rev, local_dir=a.dest, token=False)
try:
lic = info.card_data.to_dict().get("license")
except Exception as e: # noqa: BLE001
lic = "unknown (%r)" % e
res = {"repo": REPO, "revision": rev, "last_modified": str(getattr(info, "last_modified", "")), "gated": getattr(info, "gated", None),
"card_data_licence": lic, "tree": tree, "readme": p, "readme_sha256": sha(p),
"utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
else:
L = json.load(open(a.listing, encoding="utf-8"))
rev = L["revision"]
snapshot_download(REPO, repo_type="dataset", revision=rev, local_dir=a.dest, allow_patterns=a.pattern, token=False, max_workers=4)
lfs = {t["path"]: t.get("lfs_sha256") for t in L["tree"]}
size = {t["path"]: t.get("size") for t in L["tree"]}
files = []
for root, dirs, fns in os.walk(a.dest):
dirs[:] = [d for d in dirs if d != ".cache"]
for fn in sorted(fns):
p = os.path.join(root, fn)
rel = os.path.relpath(p, a.dest)
s = sha(p)
files.append({"path": rel, "bytes": os.path.getsize(p), "sha256": s, "listing_size": size.get(rel),
"lfs_sha256": lfs.get(rel), "lfs_match": (lfs.get(rel) == s) if lfs.get(rel) else None})
res = {"repo": REPO, "revision": rev, "patterns": a.pattern, "files": files, "n_files": len(files),
"all_lfs_match": all(f["lfs_match"] is not False for f in files), "utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
res["seconds"] = round(time.time() - t0, 1)
json.dump(res, open(a.out, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
print("done", a.mode, res.get("revision"), res["seconds"], "s")