mnansary commited on
Commit
71a8029
1 Parent(s): 3050906

mode added for example generation

Browse files
Files changed (1) hide show
  1. CommonVoiceBangla.py +74 -55
CommonVoiceBangla.py CHANGED
@@ -18,7 +18,7 @@
18
  import csv
19
  import os
20
  import urllib
21
-
22
  import datasets
23
  import requests
24
  from datasets.utils.py_utils import size_str
@@ -37,7 +37,7 @@ _CITATION = """\
37
  }
38
  """
39
 
40
- _HOMEPAGE = "https://commonvoice.mozilla.org/en/datasets"
41
 
42
  _LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/"
43
 
@@ -71,7 +71,7 @@ class CommonVoiceConfig(datasets.BuilderConfig):
71
 
72
 
73
  class CommonVoice(datasets.GeneratorBasedBuilder):
74
- DEFAULT_CONFIG_NAME = "en"
75
  DEFAULT_WRITER_BATCH_SIZE = 1000
76
 
77
  BUILDER_CONFIGS = [
@@ -165,14 +165,16 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
165
  path_to_data = "/".join([bundle_version, self.config.name])
166
  path_to_clips = "/".join([path_to_data, "clips"]) if path_to_data else "clips"
167
 
 
168
  return [
169
  datasets.SplitGenerator(
170
  name=datasets.Split.TRAIN,
171
  gen_kwargs={
172
  "local_extracted_archive": local_extracted_archive,
173
  "archive_iterator": dl_manager.iter_archive(archive_path),
174
- "metadata_filepath": "/".join([path_to_data, "train.tsv"]) if path_to_data else "train.tsv",
175
  "path_to_clips": path_to_clips,
 
176
  },
177
  ),
178
  datasets.SplitGenerator(
@@ -182,6 +184,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
182
  "archive_iterator": dl_manager.iter_archive(archive_path),
183
  "metadata_filepath": "/".join([path_to_data, "test.tsv"]) if path_to_data else "test.tsv",
184
  "path_to_clips": path_to_clips,
 
185
  },
186
  ),
187
  datasets.SplitGenerator(
@@ -191,26 +194,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
191
  "archive_iterator": dl_manager.iter_archive(archive_path),
192
  "metadata_filepath": "/".join([path_to_data, "dev.tsv"]) if path_to_data else "dev.tsv",
193
  "path_to_clips": path_to_clips,
194
- },
195
- ),
196
- datasets.SplitGenerator(
197
- name="other",
198
- gen_kwargs={
199
- "local_extracted_archive": local_extracted_archive,
200
- "archive_iterator": dl_manager.iter_archive(archive_path),
201
- "metadata_filepath": "/".join([path_to_data, "other.tsv"]) if path_to_data else "other.tsv",
202
- "path_to_clips": path_to_clips,
203
- },
204
- ),
205
- datasets.SplitGenerator(
206
- name="invalidated",
207
- gen_kwargs={
208
- "local_extracted_archive": local_extracted_archive,
209
- "archive_iterator": dl_manager.iter_archive(archive_path),
210
- "metadata_filepath": "/".join([path_to_data, "invalidated.tsv"])
211
- if path_to_data
212
- else "invalidated.tsv",
213
- "path_to_clips": path_to_clips,
214
  },
215
  ),
216
  ]
@@ -221,40 +205,75 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
221
  archive_iterator,
222
  metadata_filepath,
223
  path_to_clips,
 
224
  ):
225
  """Yields examples."""
226
  data_fields = list(self._info().features.keys())
227
  metadata = {}
228
  metadata_found = False
229
- for path, f in archive_iterator:
230
- if path == metadata_filepath:
231
- metadata_found = True
232
- lines = (line.decode("utf-8") for line in f)
233
- reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
234
- for row in reader:
235
- # set absolute path for mp3 audio file
236
- if not row["path"].endswith(".mp3"):
237
- row["path"] += ".mp3"
238
- row["path"] = os.path.join(path_to_clips, row["path"])
239
- # accent -> accents in CV 8.0
240
- if "accents" in row:
241
- row["accent"] = row["accents"]
242
- del row["accents"]
243
- # if data is incomplete, fill with empty values
244
- for field in data_fields:
245
- if field not in row:
246
- row[field] = ""
247
- metadata[row["path"]] = row
248
- elif path.startswith(path_to_clips):
249
- assert metadata_found, "Found audio clips before the metadata TSV file."
250
- if not metadata:
251
- break
252
- if path in metadata:
253
- result = metadata[path]
254
- # set the audio feature and the path to the extracted file
255
- path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
256
- result["audio"] = {"path": path, "bytes": f.read()}
257
- # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
258
- result["path"] = path if local_extracted_archive else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
 
260
- yield path, result
 
18
  import csv
19
  import os
20
  import urllib
21
+ import shutil
22
  import datasets
23
  import requests
24
  from datasets.utils.py_utils import size_str
 
37
  }
38
  """
39
 
40
+ _HOMEPAGE = "https://commonvoice.mozilla.org/bn/datasets"
41
 
42
  _LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/"
43
 
 
71
 
72
 
73
  class CommonVoice(datasets.GeneratorBasedBuilder):
74
+ DEFAULT_CONFIG_NAME = "bn"
75
  DEFAULT_WRITER_BATCH_SIZE = 1000
76
 
77
  BUILDER_CONFIGS = [
 
165
  path_to_data = "/".join([bundle_version, self.config.name])
166
  path_to_clips = "/".join([path_to_data, "clips"]) if path_to_data else "clips"
167
 
168
+
169
  return [
170
  datasets.SplitGenerator(
171
  name=datasets.Split.TRAIN,
172
  gen_kwargs={
173
  "local_extracted_archive": local_extracted_archive,
174
  "archive_iterator": dl_manager.iter_archive(archive_path),
175
+ "metadata_filepath": "train.tsv",
176
  "path_to_clips": path_to_clips,
177
+ "mode":"train",
178
  },
179
  ),
180
  datasets.SplitGenerator(
 
184
  "archive_iterator": dl_manager.iter_archive(archive_path),
185
  "metadata_filepath": "/".join([path_to_data, "test.tsv"]) if path_to_data else "test.tsv",
186
  "path_to_clips": path_to_clips,
187
+ "mode":"test",
188
  },
189
  ),
190
  datasets.SplitGenerator(
 
194
  "archive_iterator": dl_manager.iter_archive(archive_path),
195
  "metadata_filepath": "/".join([path_to_data, "dev.tsv"]) if path_to_data else "dev.tsv",
196
  "path_to_clips": path_to_clips,
197
+ "mode":"dev",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  },
199
  ),
200
  ]
 
205
  archive_iterator,
206
  metadata_filepath,
207
  path_to_clips,
208
+ mode
209
  ):
210
  """Yields examples."""
211
  data_fields = list(self._info().features.keys())
212
  metadata = {}
213
  metadata_found = False
214
+ if mode in ["dev","test"]:
215
+ for path, f in archive_iterator:
216
+ if path == metadata_filepath:
217
+ metadata_found = True
218
+ lines = (line.decode("utf-8") for line in f)
219
+ reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
220
+ for row in reader:
221
+ # set absolute path for mp3 audio file
222
+ if not row["path"].endswith(".mp3"):
223
+ row["path"] += ".mp3"
224
+ row["path"] = os.path.join(path_to_clips, row["path"])
225
+ # accent -> accents in CV 8.0
226
+ if "accents" in row:
227
+ row["accent"] = row["accents"]
228
+ del row["accents"]
229
+ # if data is incomplete, fill with empty values
230
+ for field in data_fields:
231
+ if field not in row:
232
+ row[field] = ""
233
+ metadata[row["path"]] = row
234
+ elif path.startswith(path_to_clips):
235
+ assert metadata_found, "Found audio clips before the metadata TSV file."
236
+ if not metadata:
237
+ break
238
+ if path in metadata:
239
+ result = metadata[path]
240
+ # set the audio feature and the path to the extracted file
241
+ path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
242
+ result["audio"] = {"path": path, "bytes": f.read()}
243
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
244
+ result["path"] = path if local_extracted_archive else None
245
+
246
+ yield path, result
247
+ else:
248
+ metadata_found = True
249
+ with open(metadata_filepath, "rb") as file_obj:
250
+ lines = (line.decode("utf-8") for line in file_obj)
251
+ reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
252
+ for row in reader:
253
+ # set absolute path for mp3 audio file
254
+ if not row["path"].endswith(".mp3"):
255
+ row["path"] += ".mp3"
256
+ row["path"] = os.path.join(path_to_clips, row["path"])
257
+ # accent -> accents in CV 8.0
258
+ if "accents" in row:
259
+ row["accent"] = row["accents"]
260
+ del row["accents"]
261
+ # if data is incomplete, fill with empty values
262
+ for field in data_fields:
263
+ if field not in row:
264
+ row[field] = ""
265
+ metadata[row["path"]] = row
266
+ for path, f in archive_iterator:
267
+ if path.startswith(path_to_clips):
268
+ assert metadata_found, "Found audio clips before the metadata TSV file."
269
+ if not metadata:
270
+ break
271
+ if path in metadata:
272
+ result = metadata[path]
273
+ # set the audio feature and the path to the extracted file
274
+ path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
275
+ result["audio"] = {"path": path, "bytes": f.read()}
276
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
277
+ result["path"] = path if local_extracted_archive else None
278
 
279
+ yield path, result