sunghjung3's picture
Merge v2-dedup (77d73af) into main, part 12
46709a8 verified
Raw History Blame Contribute Delete
1.4 kB
"""Check every downloaded file against the sha256 Hugging Face records for it (LFS files) at the pinned revision.
usage: v2_sha.py <repo_id> <revision> <local_dir> [workers]"""
import hashlib, os, sys
from multiprocessing import Pool
from huggingface_hub import HfApi
repo, rev, local = sys.argv[1:4]; nw = int(sys.argv[4]) if len(sys.argv) > 4 else 32
def sha(p):
h = hashlib.sha256()
with open(p, "rb") as f:
for b in iter(lambda: f.read(1 << 24), b""): h.update(b)
return p, h.hexdigest(), os.path.getsize(p)
if __name__ == "__main__":
info = HfApi().dataset_info(repo, revision=rev, files_metadata=True)
want = {s.rfilename: (s.lfs.sha256 if s.lfs else None, s.size) for s in info.siblings}
files = [f for f in want if want[f][0]]
missing = [f for f in files if not os.path.exists(f"{local}/{f}")]
print(f"{len(want)} files at {rev}, {len(files)} LFS; missing locally {missing}", flush=True)
bad = []
with Pool(nw) as pool:
for n, (p, h, sz) in enumerate(pool.imap_unordered(sha, [f"{local}/{f}" for f in files if f not in missing]), 1):
f = os.path.relpath(p, local)
if h != want[f][0] or sz != want[f][1]: bad.append(f)
if n % 32 == 0: print(f" {n}/{len(files)}", flush=True)
print(f"sha256+size mismatches: {len(bad)} {bad}\n{'ALL FILES MATCH' if not bad and not missing else '*** MISMATCH ***'}")