Datasets:
Download tools/read_webdataset.py from laion/Vivarium-Greenscreen-Characters-FLUX-9B: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/datasets/laion/Vivarium-Greenscreen-Characters-FLUX-9B/resolve/61743ad28b6d1bf91f61884d9680c80ed4af11d3/tools/read_webdataset.py
- Command line
-
hf download hf://datasets/laion/Vivarium-Greenscreen-Characters-FLUX-9B@61743ad28b6d1bf91f61884d9680c80ed4af11d3/tools/read_webdataset.py
-
curl -L -o read_webdataset.py https://huggingface.co/datasets/laion/Vivarium-Greenscreen-Characters-FLUX-9B/resolve/61743ad28b6d1bf91f61884d9680c80ed4af11d3/tools/read_webdataset.py
2.61 kB
| """Read local or public-Hub WebDataset shards without extracting image files.""" | |
| import argparse | |
| import io | |
| import json | |
| import tarfile | |
| from pathlib import Path | |
| from urllib.request import urlopen | |
| from PIL import Image | |
| def samples(fileobj): | |
| """Yield (PIL image, metadata, text) while retaining only one sample in RAM.""" | |
| with tarfile.open(fileobj=fileobj, mode='r|*') as archive: | |
| pending = {} | |
| current = None | |
| for member in archive: | |
| key, extension = member.name.rsplit('.', 1) | |
| if current is not None and key != current: | |
| if set(pending) != {'png', 'json', 'txt'}: | |
| raise ValueError('Incomplete WebDataset sample: ' + current) | |
| metadata = json.loads(pending['json']) | |
| with Image.open(io.BytesIO(pending['png'])) as im: | |
| yield im.convert('RGB'), metadata, pending['txt'].decode() | |
| pending = {} | |
| current = key | |
| if extension in pending: | |
| raise ValueError('Repeated member: ' + member.name) | |
| pending[extension] = archive.extractfile(member).read() | |
| if pending: | |
| if set(pending) != {'png', 'json', 'txt'}: | |
| raise ValueError('Incomplete WebDataset sample: ' + current) | |
| with Image.open(io.BytesIO(pending['png'])) as im: | |
| yield im.convert('RGB'), json.loads(pending['json']), pending['txt'].decode() | |
| if __name__ == '__main__': | |
| p=argparse.ArgumentParser() | |
| p.add_argument('shard',help='Local TAR path, https URL, or shard filename in --repo') | |
| p.add_argument('--repo',default='laion/Anim-E-Ghibli-Backgrounds-FLUX-9B') | |
| p.add_argument('--revision',default='main') | |
| p.add_argument('--prefix',default='data',help='Repository shard directory, e.g. city-data or extension-data') | |
| p.add_argument('--limit',type=int,default=5) | |
| a=p.parse_args() | |
| if Path(a.shard).is_file(): | |
| source=open(a.shard,'rb') | |
| else: | |
| url=a.shard if a.shard.startswith('https://') else f'https://huggingface.co/datasets/{a.repo}/resolve/{a.revision}/{a.prefix}/{a.shard}' | |
| source=urlopen(url,timeout=90) | |
| with source: | |
| for i,(image,metadata,text) in enumerate(samples(source)): | |
| print(json.dumps(dict(id=metadata['id'],size=image.size,primary_class=metadata.get('primary_class'), | |
| subcategory=metadata.get('subcategory'),personality_id=metadata.get('personality_id'), | |
| emotion=metadata.get('emotion'),seed=metadata['seed'],prompt=text),ensure_ascii=False)) | |
| if i+1>=a.limit: break | |