test_images_demo / load_script.py
Gabriel's picture
Create load_script.py
859ef6f
raw
history blame
1.95 kB
import datasets
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Small htr examples images},
author={Gabriel Borg},
year={2023}
}
"""
_DESCRIPTION = """\
Demo dataset for the htr demo.
"""
_HOMEPAGE = "https://github.com/Borg93/htr_gradio_file_placeholder"
_LICENSE = ""
_REPO = "https://github.com/Borg93/htr_gradio_file_placeholder/raw/main/images.tar.gz"
_METADATA_URL = "https://raw.githubusercontent.com/Borg93/htr_gradio_file_placeholder/main/images.txt"
class ExampleImages(datasets.GeneratorBasedBuilder):
"""Small sample of image-text pairs"""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
'text': datasets.Value("string"),
'image': datasets.Image(),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
images_archive = dl_manager.download(_REPO)
metadata_paths = dl_manager.download(_METADATA_URL)
image_iters = dl_manager.iter_archive(images_archive)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"images": image_iters,
"metadata_path": metadata_paths
}
),
]
def _generate_examples(self, images, metadata_path):
"""Generate images and text."""
with open(metadata_path, encoding="utf-8") as f:
metadata_list = f.read().split("\n")
dataset_rows = zip(images, metadata_list)
for img_obj, meta_txt in dataset_rows:
file_path, file_obj = img_obj
yield file_path, {
"image": {"path": file_path, "bytes": file_obj.read()},
"text": meta_txt,
}