"""Download tracks from the shared dataset repo, verify manifests and extract images.""" import os import json import tarfile import hashlib import argparse from huggingface_hub import snapshot_download def sha256(path): h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def extract_if_needed(track_dir, manifest): layout = manifest.get("image_layout", "images/") if layout == "images/": return archive = os.path.join(track_dir, layout) if not os.path.exists(archive): return images_dir = os.path.join(track_dir, "images") os.makedirs(images_dir, exist_ok=True) with tarfile.open(archive) as tar: for member in tar.getmembers(): if member.isfile(): member.name = os.path.basename(member.name) tar.extract(member, images_dir) def main(): ap = argparse.ArgumentParser(description="Pull all tracks from the HF dataset repo.") ap.add_argument("--repo", default="comradev7/Scenic-Font-Analysis") ap.add_argument("--out", default="data") ap.add_argument("--tracks", default="", help="Comma list (a,b,c). Empty = all.") ap.add_argument("--no-extract", action="store_true", help="Skip archive extraction.") args = ap.parse_args() patterns = None if args.tracks.strip(): patterns = ["README.md", "schema.json", ".gitattributes"] for t in args.tracks.split(","): patterns.append(f"track_{t.strip().lower()}/**") root = snapshot_download( repo_id=args.repo, repo_type="dataset", local_dir=args.out, allow_patterns=patterns, ) print(f"Downloaded to {root}") for entry in sorted(os.listdir(root)): track_dir = os.path.join(root, entry) manifest_path = os.path.join(track_dir, "manifest.json") if not (entry.startswith("track_") and os.path.isdir(track_dir)): continue gt = os.path.join(track_dir, "ground_truth.csv") if os.path.exists(manifest_path): meta = json.load(open(manifest_path, encoding="utf-8")) ok = os.path.exists(gt) and sha256(gt) == meta.get("ground_truth_sha256") print(f"{entry}: images={meta.get('image_count')} " f"layout={meta.get('image_layout')} " f"styles={meta.get('style_unified_counts')} csv_ok={ok}") if not args.no_extract: extract_if_needed(track_dir, meta) print(f" extracted -> {os.path.join(track_dir, 'images')}") else: print(f"{entry}: (no manifest) csv_present={os.path.exists(gt)}") if __name__ == "__main__": main()