import os import posixpath import datasets from datasets import DatasetInfo, DownloadManager from fsspec.core import url_to_fs _EXTENSION = [".png", ".jpg", ".jpeg", ".webp", ".bmp"] class DanbooruDataset(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ # add number before name for sorting datasets.BuilderConfig(name="full"), ] def _info(self) -> DatasetInfo: features = { "image": datasets.Image(), "post_id": datasets.Value("int64") } info = datasets.DatasetInfo( features=datasets.Features(features), supervised_keys=None, citation="", ) return info def _split_generators(self, dl_manager: DownloadManager): fs, path = url_to_fs(dl_manager._base_path) urls = fs.glob(posixpath.join(path, "**/*.tar"), detail=False) archives = dl_manager.download(["hf://" + url for url in urls]) archives = [dl_manager.iter_archive(archives) for archives in archives] return [datasets.SplitGenerator(name="train", gen_kwargs={"archives": archives})] def _generate_examples(self, archives): for archive in archives: for path, f in archive: path_root, path_ext = os.path.splitext(path) if path_ext.lower() not in _EXTENSION: continue post_id = int(os.path.basename(path_root)) yield path, {"image": {"bytes": f.read()}, "post_id": post_id}