Datasets:
Create load_script.py
Browse files- load_script.py +62 -0
load_script.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
|
3 |
+
_CITATION = """\
|
4 |
+
@InProceedings{huggingface:dataset,
|
5 |
+
title = {Small htr examples images},
|
6 |
+
author={Gabriel Borg},
|
7 |
+
year={2023}
|
8 |
+
}
|
9 |
+
"""
|
10 |
+
|
11 |
+
_DESCRIPTION = """\
|
12 |
+
Demo dataset for the htr demo.
|
13 |
+
"""
|
14 |
+
_HOMEPAGE = "https://github.com/Borg93/htr_gradio_file_placeholder"
|
15 |
+
|
16 |
+
_LICENSE = ""
|
17 |
+
|
18 |
+
_REPO = "https://github.com/Borg93/htr_gradio_file_placeholder/raw/main/images.tar.gz"
|
19 |
+
_METADATA_URL = "https://raw.githubusercontent.com/Borg93/htr_gradio_file_placeholder/main/images.txt"
|
20 |
+
|
21 |
+
class ExampleImages(datasets.GeneratorBasedBuilder):
|
22 |
+
"""Small sample of image-text pairs"""
|
23 |
+
|
24 |
+
def _info(self):
|
25 |
+
return datasets.DatasetInfo(
|
26 |
+
description=_DESCRIPTION,
|
27 |
+
features=datasets.Features(
|
28 |
+
{
|
29 |
+
'text': datasets.Value("string"),
|
30 |
+
'image': datasets.Image(),
|
31 |
+
}
|
32 |
+
),
|
33 |
+
supervised_keys=None,
|
34 |
+
homepage=_HOMEPAGE,
|
35 |
+
citation=_CITATION,
|
36 |
+
)
|
37 |
+
|
38 |
+
def _split_generators(self, dl_manager):
|
39 |
+
images_archive = dl_manager.download(_REPO)
|
40 |
+
metadata_paths = dl_manager.download(_METADATA_URL)
|
41 |
+
image_iters = dl_manager.iter_archive(images_archive)
|
42 |
+
return [
|
43 |
+
datasets.SplitGenerator(
|
44 |
+
name=datasets.Split.TRAIN,
|
45 |
+
gen_kwargs={
|
46 |
+
"images": image_iters,
|
47 |
+
"metadata_path": metadata_paths
|
48 |
+
}
|
49 |
+
),
|
50 |
+
]
|
51 |
+
|
52 |
+
def _generate_examples(self, images, metadata_path):
|
53 |
+
"""Generate images and text."""
|
54 |
+
with open(metadata_path, encoding="utf-8") as f:
|
55 |
+
metadata_list = f.read().split("\n")
|
56 |
+
dataset_rows = zip(images, metadata_list)
|
57 |
+
for img_obj, meta_txt in dataset_rows:
|
58 |
+
file_path, file_obj = img_obj
|
59 |
+
yield file_path, {
|
60 |
+
"image": {"path": file_path, "bytes": file_obj.read()},
|
61 |
+
"text": meta_txt,
|
62 |
+
}
|