ivelin commited on
Commit
5d5ab1d
1 Parent(s): 25f2910

feat: initial draft of dataset script generator

Browse files
Files changed (1) hide show
  1. ui_refexp.py +150 -0
ui_refexp.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ @misc{bai2021uibert,
29
+ title={UIBert: Learning Generic Multimodal Representations for UI Understanding},
30
+ author={Chongyang Bai and Xiaoxue Zang and Ying Xu and Srinivas Sunkara and Abhinav Rastogi and Jindong Chen and Blaise Aguera y Arcas},
31
+ year={2021},
32
+ eprint={2107.13731},
33
+ archivePrefix={arXiv},
34
+ primaryClass={cs.CV}
35
+ }
36
+ """
37
+
38
+ # TODO: Add description of the dataset here
39
+ # You can copy an official description
40
+ _DESCRIPTION = """\
41
+ This dataset is intended for UI understanding, referring expression and action automation model training. It's based on the UIBert RefExp dataset from Google Research, which is based on the RICO dataset.
42
+ """
43
+
44
+ # TODO: Add a link to an official homepage for the dataset here
45
+ _HOMEPAGE = "https://github.com/google-research-datasets/uibert"
46
+
47
+ # TODO: Add the licence for the dataset here if you can find it
48
+ _LICENSE = "CC BY 4.0"
49
+
50
+ # TODO: Add link to the official dataset URLs here
51
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
52
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
53
+ _DATA_URLs = {
54
+ "ui_refexp": "https://huggingface.co/datasets/ncoop57/rico_captions/resolve/main/captions_hierarchies_images.zip",
55
+ }
56
+
57
+
58
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
59
+ class UIRefExp(datasets.GeneratorBasedBuilder):
60
+ """TODO: Short description of my dataset."""
61
+
62
+ VERSION = datasets.Version("1.1.0")
63
+
64
+ # This is an example of a dataset with multiple configurations.
65
+ # If you don't want/need to define several sub-sets in your dataset,
66
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
67
+
68
+ # If you need to make complex sub-parts in the datasets with configurable options
69
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
70
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
71
+
72
+ # You will be able to load one or the other configurations in the following list with
73
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
74
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
75
+ # BUILDER_CONFIGS = [
76
+ # datasets.BuilderConfig(
77
+ # name="ui_refexp",
78
+ # version=VERSION,
79
+ # 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.",
80
+ # ),
81
+ # # datasets.BuilderConfig(
82
+ # # name="screenshots_captions_filtered",
83
+ # # version=VERSION,
84
+ # # description="Contains 25k 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. Filtering was done as discussed in this paper: https://aclanthology.org/2020.acl-main.729.pdf",
85
+ # # ),
86
+ # ]
87
+
88
+ # DEFAULT_CONFIG_NAME = "screenshots_captions_filtered"
89
+
90
+ def _info(self):
91
+ features = datasets.Features(
92
+ {
93
+ "screenshot_path": datasets.Value("string"),
94
+ "caption": datasets.Value("string"),
95
+ # This is a JSON obj, but will be coded as a string
96
+ "hierarchy": datasets.Value("string"),
97
+ }
98
+ )
99
+
100
+ return datasets.DatasetInfo(
101
+ description=_DESCRIPTION,
102
+ features=features,
103
+ supervised_keys=None,
104
+ homepage=_HOMEPAGE,
105
+ license=_LICENSE,
106
+ citation=_CITATION,
107
+ )
108
+
109
+ def _split_generators(self, dl_manager):
110
+ """Returns SplitGenerators."""
111
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
112
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
113
+
114
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
115
+ # 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.
116
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
117
+ my_urls = _DATA_URLs[self.config.name]
118
+ data_dir = dl_manager.download_and_extract(my_urls)
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ # These kwargs will be passed to _generate_examples
123
+ gen_kwargs={
124
+ "root_dir": data_dir,
125
+ "split": "train",
126
+ },
127
+ )
128
+ ]
129
+
130
+ def _generate_examples(
131
+ self,
132
+ root_dir,
133
+ split, # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
134
+ ):
135
+ """Yields examples as (key, example) tuples."""
136
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
137
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
138
+
139
+ screen_glob = sorted(glob.glob(os.path.join(root_dir, "**/*.jpg")))
140
+ hierarchy_glob = sorted(glob.glob(os.path.join(root_dir, "**/*.json")))
141
+ caption_glob = sorted(glob.glob(os.path.join(root_dir, "**/*.txt")))
142
+ for idx, (screen_filepath, hierarchy_filepath, caption_filepath) in enumerate(
143
+ zip(screen_glob, hierarchy_glob, caption_glob)
144
+ ):
145
+ with open(hierarchy_filepath, "r", encoding="utf-8") as f:
146
+ hierarchy = f.read()
147
+ with open(caption_filepath, "r", encoding="utf-8") as f:
148
+ caption = f.read()
149
+
150
+ yield idx, {"screenshot_path": screen_filepath, "hierarchy": hierarchy, "caption": caption}