sajjadrauf commited on
Commit
ed7393d
1 Parent(s): d5eb921

Create image-text-demo.py

Browse files
Files changed (1) hide show
  1. image-text-demo.py +59 -0
image-text-demo.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+
3
+ _CITATION = """\
4
+ @InProceedings{huggingface:dataset,
5
+ title = {Small image-text set},
6
+ author={James Briggs},
7
+ year={2022}
8
+ }
9
+ """
10
+
11
+ _DESCRIPTION = """\
12
+ Demo dataset for testing or showing image-text capabilities.
13
+ """
14
+ _HOMEPAGE = "https://huggingface.co/datasets/jamescalam/image-text-demo"
15
+
16
+ _LICENSE = ""
17
+
18
+ _REPO = "https://huggingface.co/datasets/jamescalam/image-text-demo"
19
+
20
+ class ImageSet(datasets.GeneratorBasedBuilder):
21
+ """Small sample of image-text pairs"""
22
+
23
+ def _info(self):
24
+ return datasets.DatasetInfo(
25
+ description=_DESCRIPTION,
26
+ features=datasets.Features(
27
+ {
28
+ 'text': datasets.Value("string"),
29
+ 'image': datasets.Image(),
30
+ }
31
+ ),
32
+ supervised_keys=None,
33
+ homepage=_HOMEPAGE,
34
+ citation=_CITATION,
35
+ )
36
+
37
+ def _split_generators(self, dl_manager):
38
+ images_archive = dl_manager.download(f"{_REPO}/resolve/main/images.tgz")
39
+ image_iters = dl_manager.iter_archive(images_archive)
40
+ return [
41
+ datasets.SplitGenerator(
42
+ name=datasets.Split.TRAIN,
43
+ gen_kwargs={
44
+ "images": image_iters
45
+ }
46
+ ),
47
+ ]
48
+
49
+ def _generate_examples(self, images):
50
+ """This function returns the examples in the raw (text) form."""
51
+ idx = 0
52
+ for filepath, image in images:
53
+ description = filepath.split('/')[-1][:-4]
54
+ description = description.replace('_', ' ')
55
+ yield idx, {
56
+ "image": {"path": filepath, "bytes": image.read()},
57
+ "text": description,
58
+ }
59
+ idx += 1