File size: 6,707 Bytes
6b604bf
573b8b7
 
 
c226be5
573b8b7
 
 
 
 
 
 
 
 
 
 
 
4384c4a
573b8b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35806e
 
 
573b8b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbc3430
573b8b7
 
 
 
 
 
 
d628e9d
c226be5
573b8b7
51d46ae
573b8b7
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import datasets

logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """\\nWikipedia-based Image Text (WIT) Dataset is a large multimodal multilingual dataset. 
WIT is composed of a curated set of 37.6 million entity rich image-text examples with 11.5 million unique images across 108 Wikipedia languages. 
Its size enables WIT to be used as a pretraining dataset for multimodal machine learning models.
"""
_CITATION = """
@article{srinivasan2021wit,
  title={WIT: Wikipedia-based Image Text Dataset for Multimodal Multilingual Machine Learning},
  author={Srinivasan, Krishna and Raman, Karthik and Chen, Jiecao and Bendersky, Michael and Najork, Marc},
  journal={arXiv preprint arXiv:2103.01913},
  year={2021}
}
"""
_URL = "https://github.com/google-research-datasets/wit"
_DATA_URL = "https://huggingface.co/datasets/keshan/wit-dataset/resolve/628260b88f51c831a60120d2ebc17c3475f282af/data/{language}.tar.gz"
_LANGUAGES = [
              'ms',
              'eu',
              'si',
              'ko',
              'nv',
              'id',
              'tg',
              'mn',
              'fa',
              'bg',
              'ia',
              'ca',
              'jv',
              'vi',
              'ja',
              'bs',
              'te',
              'war',
              'hy',
              'sv',
              'az',
              'lah',
              'ht',
              'sl',
              'pt',
              'an',
              'br',
              'nn',
              'ceb',
              'ce',
              'qu',
              'gl',
              'fy',
              'vec',
              'zh',
              'iw',
              'vo',
              'xmf',
              'nds',
              'bar',
              'ba',
              'sr-Latn',
              'hsb',
              'yue',
              'arz',
              'es',
              'bn',
              'de',
              'mk',
              'pa',
              'zh-TW',
              'io',
              'lb',
              'azb',
              'ga',
              'cs',
              'fi',
              'cv',
              'sr',
              'lv',
              'my',
              'mg',
              'hu',
              'it',
              'kk',
              'be',
              'sq',
              'ru',
              'ar',
              'cy',
              'hr',
              'be-tarask',
              'is',
              'tt',
              'mr',
              'ro',
              'en',
              'fil',
              'uz',
              'af',
              'et',
              'fr',
              'no',
              'ckb',
              'nan',
              'sw',
              'la',
              'lmo',
              'th',
              'ta',
              'ast',
              'eo',
              'tr',
              'uk',
              'ur',
              'ne',
              'kn',
              'da',
              'nl',
              'ka',
              'pl',
              'el',
              'sco',
              'hi',
              'sk',
              'oc',
              'lt',
              'ml'
              ]

class WITConfig(datasets.BuilderConfig):
    """BuilderConfig for WIT."""
    def __init__(self, *args, languages, **kwargs):
        """BuilderConfig for WIT.
        Args:
            languages (:obj:`List[str]`): list of languages to load
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(
            *args,
            name="+".join(languages),
            **kwargs,
        )
        self.languages = languages

class WIT(datasets.GeneratorBasedBuilder):
    """WIT, WIT to be used as a pretraining dataset for multimodal machine learning models."""
    BUILDER_CONFIGS = [WITConfig(languages=[lang]) for lang in _LANGUAGES]
    BUILDER_CONFIG_CLASS = WITConfig
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "language": datasets.Value("string"),
                    "page_url": datasets.Value("string"),
                    "image_url": datasets.Value("string"),
                    "page_title": datasets.Value("string"),
                    "section_title": datasets.Value("string"),
                    "hierarchical_section_title": datasets.Value("string"),
                    "caption_reference_description": datasets.Value("string"),
                    "caption_attribution_description": datasets.Value("string"),
                    "caption_alt_text_description": datasets.Value("string"),
                    "mime_type": datasets.Value("string"),
                    "original_height": datasets.Value("string"),#datasets.Value("int8"),
                    "original_width": datasets.Value("string"),#datasets.Value("int8"),
                    "is_main_image": datasets.Value("string"),
                    "attribution_passes_lang_id": datasets.Value("string"),
                    "page_changed_recently": datasets.Value("string"),
                    "context_page_description": datasets.Value("string"),
                    "context_section_description": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_URL,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        abs_path_to_data = dl_manager.download_and_extract(
            _DATA_URL.format(language=self.config.name)
            )
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": os.path.join(abs_path_to_data, f'{self.config.name}/wit_v1.train.all.{self.config.name}.tsv'),
                },
            ),
        ]
    
    def _generate_examples(self, filepath):
      data_fields = list(self._info().features.keys())
      path_idx = data_fields.index("image_url")
      
      with open(filepath, encoding="utf-8") as f:
          lines = f.readlines()
          headline = lines[0]

          column_names = headline.strip().split('\t')
          assert (
                column_names == data_fields
            ), f"The file should have {data_fields} as column names, but has {column_names}"

          for id_, line in enumerate(lines[1:]):
            field_values = line.strip().split("\t")
            # if data is incomplete, fill with empty values
            if len(field_values) < len(data_fields):
                field_values += (len(data_fields) - len(field_values)) * ["''"]

            yield id_, {key: value for key, value in zip(data_fields, field_values)}