Datasets:
Download scripts/shard_bouncing_balls.py from jimchen2/bouncing-balls: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/datasets/jimchen2/bouncing-balls/resolve/main/scripts/shard_bouncing_balls.py
- Command line
-
hf download hf://datasets/jimchen2/bouncing-balls/scripts/shard_bouncing_balls.py
-
curl -L -o shard_bouncing_balls.py https://huggingface.co/datasets/jimchen2/bouncing-balls/resolve/main/scripts/shard_bouncing_balls.py
2.15 kB
| """Package generated MP4 videos and metadata into Parquet shards.""" | |
| import argparse | |
| import json | |
| import math | |
| from pathlib import Path | |
| from datasets import Dataset, Video | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--source", type=Path, default=Path("bouncing-balls")) | |
| parser.add_argument("--output", type=Path, default=Path("bouncing-balls-sharded")) | |
| parser.add_argument("--shards", type=int, default=10) | |
| args = parser.parse_args() | |
| metadata = [ | |
| json.loads(line) | |
| for line in (args.source / "metadata.jsonl").read_text().splitlines() | |
| if line.strip() | |
| ] | |
| if not metadata: | |
| raise ValueError(f"No records found in {args.source / 'metadata.jsonl'}") | |
| if args.shards < 1: | |
| raise ValueError("--shards must be at least 1") | |
| data_dir = args.output / "data" | |
| data_dir.mkdir(parents=True, exist_ok=True) | |
| shard_size = math.ceil(len(metadata) / args.shards) | |
| for shard_idx in range(args.shards): | |
| start = shard_idx * shard_size | |
| shard_metadata = metadata[start:start + shard_size] | |
| if not shard_metadata: | |
| break | |
| records = [] | |
| for record in shard_metadata: | |
| video_path = args.source / record["file_name"] | |
| records.append({ | |
| "video": { | |
| "bytes": video_path.read_bytes(), | |
| "path": video_path.name, | |
| }, | |
| **{ | |
| key: value | |
| for key, value in record.items() | |
| if key != "file_name" | |
| }, | |
| }) | |
| shard = Dataset.from_list(records).cast_column("video", Video()) | |
| shard_path = data_dir / ( | |
| f"train-{shard_idx:05d}-of-{args.shards:05d}.parquet" | |
| ) | |
| shard.to_parquet(shard_path) | |
| print( | |
| f"Wrote {shard_path.name}: {len(shard)} videos, " | |
| f"{shard_path.stat().st_size / 1024 / 1024:.2f} MiB", | |
| flush=True, | |
| ) | |
| print(f"Created {args.shards} shards in {args.output}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |