import datasets _CITATION = """\ @InProceedings{huggingface:dataset, title = {Small image-text set}, author={James Briggs}, year={2022} } """ _DESCRIPTION = """\ Demo dataset for testing or showing image-text capabilities. """ _HOMEPAGE = "https://huggingface.co/datasets/jamescalam/image-text-demo" _LICENSE = "" _REPO = "https://huggingface.co/datasets/jamescalam/image-text-demo" class ImageSet(datasets.GeneratorBasedBuilder): """Small sample of image-text pairs""" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { 'id': datasets.Value("int32"), 'text': datasets.Value("string"), 'image': datasets.Image(), } ), supervised_keys=None, homepage=_HOMEPAGE, citation=_CITATION, ) def _split_generators(self, dl_manager): images_path = dl_manager.download_and_extract(f"{_REPO}/resolve/main/images.tgz") image_iters = dl_manager.iter_archive(f"{images_path}/images") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "images": image_iters } ), ] def _generate_examples(self, images): """This function returns the examples in the raw (text) form.""" id_ = 0 for filepath, image in images: description = filepath.split('/')[-1][:-4] description = description.replace('_', ' ') yield id_, { "image": {"path": filepath, "bytes": image.read()}, "text": description, } idx += 1