Datasets:
scripts: add resumable downloader with sha256 verification
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980#!/usr/bin/env python3"""Download the BrickMore-48 archives from Hugging Face and verify every SHA-256.
Examples-------- pip install huggingface_hub python scripts/download_from_hf.py --output brickmore80k python scripts/download_from_hf.py --output brickmore80k --shard A --extract-metadata
The repository is private, so log in first (`huggingface-cli login`) or pass --token,or set the HF_TOKEN environment variable."""import argparseimport hashlibimport jsonimport osimport sysimport zipfilefrom pathlib import Path
from huggingface_hub import hf_hub_download
REPO_ID = "VergilLLLL/BrickForma-DataSet"MANIFEST_IN_REPO = "checksums/SHA256_MANIFEST.json"
def sha256(path): digest = hashlib.sha256() with open(path, "rb") as handle: for chunk in iter(lambda: handle.read(1 << 20), b""): digest.update(chunk) return digest.hexdigest()
def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--output", type=Path, default=Path("brickmore80k")) parser.add_argument("--shard", choices=("all", "A", "B"), default="all") parser.add_argument("--extract-metadata", action="store_true", help="unzip the two metadata archives") parser.add_argument("--token", default=os.environ.get("HF_TOKEN")) parser.add_argument("--revision", default=None) args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=True)
manifest_path = Path( hf_hub_download(REPO_ID, MANIFEST_IN_REPO, repo_type="dataset", token=args.token, revision=args.revision, local_dir=args.output) ) manifest = json.loads(manifest_path.read_text(encoding="utf-8")) wanted = [entry for entry in manifest["files"] if args.shard == "all" or entry["path"].split("/")[1] == f"AI_{args.shard}"] total = sum(entry["bytes"] for entry in wanted) print(f"{len(wanted)} archives, {total / 1e9:.2f} GB, from {REPO_ID}")
for index, entry in enumerate(wanted, 1): target = Path(hf_hub_download(REPO_ID, entry["path"], repo_type="dataset", token=args.token, revision=args.revision, local_dir=args.output)) size = target.stat().st_size if size != entry["bytes"]: sys.exit(f"size mismatch for {entry['path']}: {size} != {entry['bytes']}") digest = sha256(target) if digest != entry["sha256"]: sys.exit(f"sha256 mismatch for {entry['path']}") print(f"[{index}/{len(wanted)}] verified {entry['path']} ({size / 1e6:.0f} MB)")
if args.extract_metadata: for archive in ("data/AI_A/metadata.zip", "data/AI_B/B_metadata.zip"): path = args.output / archive if not path.exists(): continue destination = args.output / "metadata" / archive.split("/")[1] destination.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(path) as bundle: bundle.extractall(destination) print(f"extracted {archive} -> {destination}") print("all archives verified")
if __name__ == "__main__": main()scripts/download_from_hf.pyAdded+80