Datasets:
Download reader.py from nvidia/Synthetic-Charts-V1: direct link, hf CLI and curl.
- Browser
- Download file 880 Bytes
-
https://huggingface.co/datasets/nvidia/Synthetic-Charts-V1/resolve/e21759e2ed0adfab2c8a4aef052d375fd3616c41/reader.py
- Command line
-
hf download hf://datasets/nvidia/Synthetic-Charts-V1@e21759e2ed0adfab2c8a4aef052d375fd3616c41/reader.py
-
curl -L -o reader.py https://huggingface.co/datasets/nvidia/Synthetic-Charts-V1/resolve/e21759e2ed0adfab2c8a4aef052d375fd3616c41/reader.py
880 Bytes
| import json | |
| import tarfile | |
| def iter_records(path): | |
| """Yield one shard at a time without extracting files.""" | |
| current = None | |
| parts = {} | |
| with tarfile.open(path, mode="r|*") as archive: | |
| for member in archive: | |
| if not member.isfile(): | |
| continue | |
| key, extension = member.name.split(".", 1) | |
| if current is not None and key != current: | |
| yield current, parts | |
| parts = {} | |
| current = key | |
| data = archive.extractfile(member).read() | |
| parts[extension] = json.loads(data) if extension == "json" else data | |
| if current is not None: | |
| yield current, parts | |
| # for key, parts in iter_records("data/charts/charts-charts-00000.tar"): | |
| # annotation = parts["json"] | |
| # image_bytes = next(parts[x] for x in ("jpg", "png", "jpeg") if x in parts) | |