jamescalam commited on
Commit
61bf1ad
1 Parent(s): 2d7603c

Create new file

Browse files
Files changed (1) hide show
  1. image-text-demo.py +60 -0
image-text-demo.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-set"
15
+
16
+ _LICENSE = ""
17
+
18
+ _REPO = "https://huggingface.co/datasets/jamescalam/image-set"
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
+ 'id': datasets.Value("int32"),
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_path = dl_manager.download_and_extract(f"{_REPO}/resolve/main/unsplash.tgz")
40
+ image_iters = dl_manager.iter_archive(f"{images_path}/images")
41
+ return [
42
+ datasets.SplitGenerator(
43
+ name=datasets.Split.TRAIN,
44
+ gen_kwargs={
45
+ "images": image_iters
46
+ }
47
+ ),
48
+ ]
49
+
50
+ def _generate_examples(self, images):
51
+ """This function returns the examples in the raw (text) form."""
52
+ id_ = 0
53
+ for filepath, image in images:
54
+ description = filepath.split('/')[-1][:-4]
55
+ description = description.replace('_', ' ')
56
+ yield id_, {
57
+ "image": {"path": filepath, "bytes": image.read()},
58
+ "text": description,
59
+ }
60
+ idx += 1