Datasets:
Download download.py from Zixia3/CaScenes: direct link, hf CLI and curl.
- Browser
- Download file 4.61 kB
-
https://huggingface.co/datasets/Zixia3/CaScenes/resolve/main/download.py
- Command line
-
hf download hf://datasets/Zixia3/CaScenes/download.py
-
curl -L -o download.py https://huggingface.co/datasets/Zixia3/CaScenes/resolve/main/download.py
4.61 kB
| """Download and extract CaScenes from the Hugging Face Hub. | |
| Usage: | |
| python download.py --out ./workspace | |
| python download.py --out ./workspace --split test # download only test | |
| python download.py --out ./workspace --no-extract # download tarballs only | |
| Requirements: | |
| pip install -U huggingface_hub | |
| """ | |
| import argparse | |
| import hashlib | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| from huggingface_hub import HfApi, hf_hub_download | |
| REPO_ID = "Zixia3/CaScenes" | |
| REPO_TYPE = "dataset" | |
| SPLITS = ("train", "test") | |
| def sha256_of(path: Path, chunk: int = 1 << 20) -> str: | |
| h = hashlib.sha256() | |
| with path.open("rb") as f: | |
| for buf in iter(lambda: f.read(chunk), b""): | |
| h.update(buf) | |
| return h.hexdigest() | |
| def load_expected_sums(sumfile: Path) -> dict[str, str]: | |
| sums: dict[str, str] = {} | |
| for line in sumfile.read_text().splitlines(): | |
| line = line.strip() | |
| if not line or line.startswith("#"): | |
| continue | |
| digest, _, name = line.partition(" ") | |
| sums[name.strip()] = digest.strip() | |
| return sums | |
| def verify(path: Path, expected: str) -> None: | |
| print(f" verifying {path.name} ...", flush=True) | |
| got = sha256_of(path) | |
| if got != expected: | |
| raise SystemExit( | |
| f"sha256 mismatch for {path.name}:\n expected {expected}\n got {got}" | |
| ) | |
| print(f" {path.name}: OK") | |
| def list_split_files(split: str) -> list[str]: | |
| """Return remote filenames belonging to a split, sorted in concat order.""" | |
| api = HfApi() | |
| files = api.list_repo_files(REPO_ID, repo_type=REPO_TYPE) | |
| if split == "train": | |
| # multi-part: train.tar.part-aa, train.tar.part-ab, ... | |
| parts = sorted(f for f in files if f.startswith("train.tar.part-")) | |
| if parts: | |
| return parts | |
| # fallback: single train.tar (if release ever switches back) | |
| if "train.tar" in files: | |
| return ["train.tar"] | |
| raise SystemExit("no train.tar.part-* or train.tar found in repo") | |
| if split == "test": | |
| if "test.tar" in files: | |
| return ["test.tar"] | |
| raise SystemExit("test.tar not found in repo") | |
| raise ValueError(split) | |
| def extract_split(split: str, local_files: list[Path], out_dir: Path) -> None: | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| print(f" extracting {split} ({len(local_files)} file(s)) -> {out_dir} ...", flush=True) | |
| if len(local_files) == 1: | |
| rc = subprocess.call(["tar", "-xf", str(local_files[0]), "-C", str(out_dir)]) | |
| if rc != 0: | |
| raise SystemExit(f"tar -xf failed for {local_files[0].name}") | |
| return | |
| # multi-part: cat parts | tar -xf - | |
| cat = subprocess.Popen(["cat", *map(str, local_files)], stdout=subprocess.PIPE) | |
| tar = subprocess.Popen(["tar", "-xf", "-", "-C", str(out_dir)], stdin=cat.stdout) | |
| if cat.stdout is not None: | |
| cat.stdout.close() | |
| tar_rc = tar.wait() | |
| cat.wait() | |
| if cat.returncode != 0 or tar_rc != 0: | |
| raise SystemExit(f"extraction failed (cat={cat.returncode}, tar={tar_rc})") | |
| def main() -> None: | |
| ap = argparse.ArgumentParser(description="Download & extract CaScenes") | |
| ap.add_argument("--out", required=True, type=Path, help="workspace root; CaScenes/ will land inside") | |
| ap.add_argument("--split", choices=SPLITS, action="append", | |
| help="restrict to these splits (repeatable). Default: both.") | |
| ap.add_argument("--cache", type=Path, default=None, | |
| help="huggingface_hub cache dir (default: HF_HOME or ~/.cache/huggingface)") | |
| ap.add_argument("--no-extract", action="store_true", help="download tarballs only, skip extraction") | |
| args = ap.parse_args() | |
| splits = tuple(args.split) if args.split else SPLITS | |
| args.out.mkdir(parents=True, exist_ok=True) | |
| sums_path = Path(hf_hub_download(REPO_ID, "SHA256SUMS", repo_type=REPO_TYPE, cache_dir=args.cache)) | |
| expected = load_expected_sums(sums_path) | |
| for split in splits: | |
| remote_files = list_split_files(split) | |
| local_files: list[Path] = [] | |
| for rf in remote_files: | |
| local = Path(hf_hub_download(REPO_ID, rf, repo_type=REPO_TYPE, cache_dir=args.cache)) | |
| if rf in expected: | |
| verify(local, expected[rf]) | |
| else: | |
| print(f"warning: {rf} not listed in SHA256SUMS; skipping verification", file=sys.stderr) | |
| local_files.append(local) | |
| if not args.no_extract: | |
| extract_split(split, local_files, args.out) | |
| print("done.") | |
| if __name__ == "__main__": | |
| main() | |