ncoop57 commited on
Commit
99f197c
1 Parent(s): 5b1585b

Add dataset loading file

Browse files
Files changed (1) hide show
  1. rico_captions.py +141 -0
rico_captions.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import glob
20
+ import os
21
+
22
+ import datasets
23
+
24
+ import numpy as np
25
+
26
+ # TODO: Add BibTeX citation
27
+ # Find for instance the citation on arxiv or on the dataset repo/website
28
+ _CITATION = """\
29
+ @InProceedings{huggingface:dataset,
30
+ title = {A great new dataset},
31
+ author={huggingface, Inc.
32
+ },
33
+ year={2020}
34
+ }
35
+ """
36
+
37
+ # TODO: Add description of the dataset here
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
41
+ """
42
+
43
+ # TODO: Add a link to an official homepage for the dataset here
44
+ _HOMEPAGE = "http://interactionmining.org/rico"
45
+
46
+ # TODO: Add the licence for the dataset here if you can find it
47
+ _LICENSE = ""
48
+
49
+ # TODO: Add link to the official dataset URLs here
50
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
51
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
52
+ _DATA_URLs = {
53
+ "screenshots_captions": "https://huggingface.co/datasets/ncoop57/rico_captions/resolve/main/captions_images.zip",
54
+ }
55
+
56
+
57
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
58
+ class RicoDataset(datasets.GeneratorBasedBuilder):
59
+ """TODO: Short description of my dataset."""
60
+
61
+ VERSION = datasets.Version("1.1.0")
62
+
63
+ # This is an example of a dataset with multiple configurations.
64
+ # If you don't want/need to define several sub-sets in your dataset,
65
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
66
+
67
+ # If you need to make complex sub-parts in the datasets with configurable options
68
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
69
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
70
+
71
+ # You will be able to load one or the other configurations in the following list with
72
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
73
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
74
+ BUILDER_CONFIGS = [
75
+ datasets.BuilderConfig(
76
+ name="screenshots_captions",
77
+ version=VERSION,
78
+ description="Contains 66k+ unique UI screens. For each UI, we present a screenshot (JPG file) and the text shown on the screen that was extracted using an OCR model.",
79
+ ),
80
+ ]
81
+
82
+ DEFAULT_CONFIG_NAME = "screenshots_captions"
83
+
84
+ def _info(self):
85
+ if self.config.name == "screenshots_captions":
86
+ features = datasets.Features(
87
+ {
88
+ "screenshot_path": datasets.Value("string"),
89
+ # This is a JSON obj, but will be coded as a string
90
+ "caption": datasets.Value("string"),
91
+ }
92
+ )
93
+
94
+ return datasets.DatasetInfo(
95
+ description=_DESCRIPTION,
96
+ features=features,
97
+ supervised_keys=None,
98
+ homepage=_HOMEPAGE,
99
+ license=_LICENSE,
100
+ citation=_CITATION,
101
+ )
102
+
103
+ def _split_generators(self, dl_manager):
104
+ """Returns SplitGenerators."""
105
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
106
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
107
+
108
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
109
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
110
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
111
+ my_urls = _DATA_URLs[self.config.name]
112
+ data_dir = dl_manager.download_and_extract(my_urls)
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=datasets.Split.TRAIN,
116
+ # These kwargs will be passed to _generate_examples
117
+ gen_kwargs={
118
+ "root_dir": data_dir,
119
+ "split": "train",
120
+ },
121
+ )
122
+ ]
123
+
124
+ def _generate_examples(
125
+ self,
126
+ root_dir,
127
+ split, # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
128
+ ):
129
+ """Yields examples as (key, example) tuples."""
130
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
131
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
132
+
133
+ screen_glob = sorted(glob.glob(os.path.join(root_dir, "**/*.jpg")))
134
+ caption_glob = sorted(glob.glob(os.path.join(root_dir, "**/*.txt")))
135
+ for idx, (screen_filepath, caption_filepath) in enumerate(
136
+ zip(screen_glob, caption_glob)
137
+ ):
138
+ with open(caption_filepath, "r", encoding="utf-8") as f:
139
+ caption = f.read()
140
+
141
+ yield idx, {"screenshot_path": screen_filepath, "caption": caption}