Download task_036/solution/code/dataloader_demo.py from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_036/solution/code/dataloader_demo.py
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_036/solution/code/dataloader_demo.py
-
curl -L -o dataloader_demo.py https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_036/solution/code/dataloader_demo.py
1.14 kB
| #!/usr/bin/env python3 | |
| """Read all tar shards and print globally ordered sample-key batches.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import tarfile | |
| def sample_keys(): | |
| keys = [] | |
| for path in sorted(Path("shards").glob("dataset-*.tar")): | |
| with tarfile.open(path) as archive: | |
| names = {member.name for member in archive.getmembers() if member.isfile()} | |
| for key in sorted({name[:-4] for name in names if name.endswith(".jpg")}): | |
| if f"{key}.txt" in names: | |
| assert archive.extractfile(f"{key}.jpg").read() | |
| assert archive.extractfile(f"{key}.txt").read().strip() | |
| keys.append(key) | |
| return sorted(keys) | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--batch-size", type=int, default=4) | |
| args = parser.parse_args() | |
| if args.batch_size < 1: | |
| parser.error("batch size must be positive") | |
| keys = sample_keys() | |
| for offset in range(0, len(keys), args.batch_size): | |
| print(json.dumps(keys[offset:offset + args.batch_size])) | |
| if __name__ == "__main__": | |
| main() | |