Santiago commited on
Commit
315a6f9
1 Parent(s): 7864c2c

feat: add loading script

Browse files
Files changed (1) hide show
  1. waxal-wolof.py +224 -0
waxal-wolof.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """TODO: Add a description here."""
15
+
16
+
17
+ import csv
18
+ import itertools
19
+ import logging
20
+ import os
21
+
22
+ import datasets
23
+
24
+ logger = logging.getLogger()
25
+
26
+
27
+ # TODO: Add BibTeX citation
28
+ # Find for instance the citation on arxiv or on the dataset repo/website
29
+ _CITATION = """\
30
+ @InProceedings{huggingface:dataset,
31
+ title = {A great new dataset},
32
+ author={huggingface, Inc.
33
+ },
34
+ year={2020}
35
+ }
36
+ """
37
+
38
+ # TODO: Add description of the dataset here
39
+ # You can copy an official description
40
+ _DESCRIPTION = """\
41
+ """
42
+
43
+ # TODO: Add a link to an official homepage for the dataset here
44
+ _HOMEPAGE = ""
45
+
46
+ # TODO: Add the licence for the dataset here if you can find it
47
+ _LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0)"
48
+
49
+ _MODALITIES_COMBINATION = [
50
+ ["audio", "image", "text"],
51
+ ["audio", "text"],
52
+ ["audio", "image"],
53
+ ["image", "text"],
54
+ ["audio"],
55
+ ["image"],
56
+ ["text"],
57
+ ]
58
+
59
+ _URLs = {
60
+ "train-transcriptions": "train_transcriptions.csv",
61
+ "test-transcriptions": "test_transcriptions.csv",
62
+ "image-files": "images.tar.gz",
63
+ "captioned-images": "captioned_images.tar.gz",
64
+ "audio-files": "audios.tar.gz",
65
+ "transcribed-audio": "transcribed_audio.tar.gz"
66
+ }
67
+
68
+
69
+ class WaxalConfig(datasets.BuilderConfig):
70
+ """BuilderConfig for Waxal dataset."""
71
+
72
+ def __init__(self, name, version, modalities, **kwargs):
73
+ self.modalities = modalities
74
+ self.language = kwargs.pop("language", None)
75
+
76
+ modalities_str = " to ".join(self.modalities)
77
+ description = f"Waxal {modalities_str} in {self.language}"
78
+
79
+ super(WaxalConfig, self).__init__(
80
+ name=name,
81
+ version=version,
82
+ description=description,
83
+ **kwargs,
84
+ )
85
+
86
+
87
+ class WaxalWolof(datasets.GeneratorBasedBuilder):
88
+ """TODO: Short description of my dataset."""
89
+
90
+ BUILDER_CONFIGS = [
91
+ WaxalConfig(
92
+ name="-".join(modalities),
93
+ version=datasets.Version("1.1.0"),
94
+ modalities=modalities,
95
+ language="wolof",
96
+ )
97
+ for modalities in _MODALITIES_COMBINATION
98
+ ]
99
+
100
+ DEFAULT_CONFIG_NAME = "audio-text"
101
+
102
+ def _info(self):
103
+ features = {}
104
+
105
+ if "audio" in self.config.modalities:
106
+ features["audio"] = datasets.features.Audio()
107
+ features["audio_duration"] = datasets.Value("float")
108
+ features["participant"] = datasets.Value("int32")
109
+
110
+ if "image" in self.config.modalities:
111
+ features["image"] = datasets.features.Image()
112
+
113
+ if "text" in self.config.modalities:
114
+ features["text_annotation"] = datasets.Value("string")
115
+
116
+ return datasets.DatasetInfo(
117
+ description=_DESCRIPTION,
118
+ features=datasets.Features(features),
119
+ homepage=_HOMEPAGE,
120
+ license=_LICENSE,
121
+ citation=_CITATION,
122
+ )
123
+
124
+ @property
125
+ def with_audio(self):
126
+ return "audio" in self.config.modalities
127
+
128
+ @property
129
+ def with_image(self):
130
+ return "image" in self.config.modalities
131
+
132
+ @property
133
+ def with_text(self):
134
+ return "text" in self.config.modalities
135
+
136
+ def _split_generators(self, dl_manager):
137
+ logger.debug("splitting")
138
+ audio_url_key = "transcribed-audio" if self.with_text else "audio-files"
139
+ image_url_key = "captioned-images" if self.with_text else "image-files"
140
+
141
+ audio_files = (
142
+ dl_manager.download_and_extract(_URLs[audio_url_key])
143
+ if self.with_audio
144
+ else None
145
+ )
146
+
147
+ image_files = (
148
+ dl_manager.download_and_extract(_URLs[image_url_key])
149
+ if self.with_image
150
+ else None
151
+ )
152
+
153
+ return [
154
+ datasets.SplitGenerator(
155
+ name=datasets.Split.TRAIN,
156
+ gen_kwargs={
157
+ "metadata_path": dl_manager.download(
158
+ _URLs["train-transcriptions"]
159
+ ),
160
+ "audio_files": audio_files,
161
+ "image_files": image_files,
162
+ },
163
+ ),
164
+ datasets.SplitGenerator(
165
+ name=datasets.Split.TEST,
166
+ gen_kwargs={
167
+ "metadata_path": dl_manager.download(
168
+ _URLs["test-transcriptions"]
169
+ ),
170
+ "audio_files": audio_files,
171
+ "image_files": image_files,
172
+ },
173
+ ),
174
+ ]
175
+
176
+ def _generate_examples(
177
+ self,
178
+ metadata_path,
179
+ audio_files=None,
180
+ path_to_audio="transcribed_audio",
181
+ image_files=None,
182
+ path_to_images="captioned_images",
183
+ ):
184
+ metadata = {}
185
+
186
+ with open(metadata_path) as buf:
187
+ reader = csv.DictReader(buf)
188
+ for row in reader:
189
+ del row["prompt"] # TODO(shpotes): remove it in future versions!
190
+
191
+ if self.with_text:
192
+ if not row["transcription"]:
193
+ continue
194
+
195
+ if self.with_image:
196
+ row["image_file_path"] = os.path.join(
197
+ path_to_images, self.config.language, row["image_file_name"]
198
+ )
199
+ if self.with_audio:
200
+ row["audio_file_path"] = os.path.join(
201
+ path_to_audio, row["audio_file_name"]
202
+ ) # TODO(shpotes): add lang name to the csv path.
203
+
204
+ metadata[row["idx"]] = row
205
+
206
+ for idx, sample in metadata.items():
207
+ result = {}
208
+
209
+ if self.with_audio:
210
+ result["participant"] = sample["participant"]
211
+ result["audio_duration"] = sample["duration"]
212
+ audio_path = os.path.join(audio_files, sample["audio_file_path"])
213
+ with open(audio_path, "rb") as f:
214
+ result["audio"] = {"path": audio_path, "bytes": f.read()}
215
+
216
+ if self.with_image:
217
+ image_path = os.path.join(image_files, sample["image_file_path"])
218
+ with open(image_path, "rb") as f:
219
+ result["image"] = {"path": image_path, "bytes": f.read()}
220
+
221
+ if self.with_text:
222
+ result["text_annotation"] = sample["transcription"]
223
+
224
+ yield idx, result