turkic-ocr-code / scripts /r5_vlmb /vlmb_fetch.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
3.17 kB
"""VLMB step 1: download each maker repository at its pinned revision (weights, configs, tokenizer AND the
repository's own .py files) into its own folder. Nothing downloaded is imported or executed here.
Skipped on purpose: pickle-format weights (.bin .pt .pth .ckpt .pkl) and media files (images, PDFs, video).
No token is used. Writes OUT_DIR/<short>.json with the file list, sizes and sha256 of every downloaded file.
usage: vlmb_fetch.py MODELS_TSV MODELS_DIR OUT_DIR short [short ...]
"""
import hashlib
import json
import os
import sys
import time
os.environ.setdefault("HF_HUB_DISABLE_TELEMETRY", "1")
for k in ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN"):
os.environ.pop(k, None)
models_tsv, models_dir, out_dir = sys.argv[1:4]
shorts = sys.argv[4:]
os.makedirs(out_dir, exist_ok=True)
from huggingface_hub import HfApi, snapshot_download # noqa: E402
SKIP_EXT = (".bin", ".pt", ".pth", ".ckpt", ".pkl", ".pickle", ".png", ".jpg", ".jpeg", ".gif", ".webp",
".pdf", ".mp4", ".svg", ".pyc", ".pyo", ".so") # compiled code is never downloaded (added 2026-11-20 22:50)
REPO, REV = {}, {}
for line in open(models_tsv, encoding="utf-8").read().splitlines()[1:]:
s, r, v = line.split("\t")
REPO[s], REV[s] = r, v
def sha256(path):
h = hashlib.sha256()
with open(path, "rb") as f:
for b in iter(lambda: f.read(1 << 22), b""):
h.update(b)
return h.hexdigest()
api = HfApi(token=False)
for short in shorts:
rec = {"short": short, "repo": REPO[short], "pinned_revision": REV[short], "errors": []}
t0 = time.time()
try:
info = api.model_info(REPO[short], revision=REV[short], files_metadata=True, token=False)
rec["revision_resolved"] = info.sha
assert info.sha == REV[short], "resolved revision differs from the pin"
allf = [(s.rfilename, s.size) for s in info.siblings]
rec["repo_files"] = [f"{n}:{z}" for n, z in allf]
want = [n for n, _ in allf if not n.lower().endswith(SKIP_EXT)]
rec["skipped_files"] = [f"{n}:{z}" for n, z in allf if n.lower().endswith(SKIP_EXT)]
local = os.path.join(models_dir, short)
snapshot_download(REPO[short], revision=REV[short], local_dir=local, allow_patterns=want, token=False)
files = []
for r, _, fs in os.walk(local):
for fn in fs:
p = os.path.join(r, fn)
rel = os.path.relpath(p, local)
if rel.startswith(".cache"):
continue
files.append({"path": rel, "bytes": os.path.getsize(p), "sha256": sha256(p)})
rec["files"] = sorted(files, key=lambda d: d["path"])
rec["py_files"] = [d["path"] for d in rec["files"] if d["path"].endswith(".py")]
rec["download_seconds"] = round(time.time() - t0, 1)
except Exception as e: # noqa: BLE001
rec["errors"].append("fetch: " + repr(e)[:500])
json.dump(rec, open(os.path.join(out_dir, short + ".json"), "w"), indent=1, ensure_ascii=False)
print(short, "files", len(rec.get("files", [])), "py", rec.get("py_files"), "errors", rec["errors"], flush=True)
print("[vlmb_fetch] done", flush=True)