nakkhatra commited on
Commit
112ec03
1 Parent(s): 55fb778

updated _generate_examples

Browse files
Files changed (1) hide show
  1. common_voice_bn.py +116 -39
common_voice_bn.py CHANGED
@@ -226,55 +226,132 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
226
  # ),
227
  ]
228
 
229
-
230
-
231
- def _generate_examples(
232
  self,
233
  local_extracted_archive,
234
  archive_iterator,
235
  metadata_filepath,
236
  path_to_clips,
 
237
  ):
238
  """Yields examples."""
239
  data_fields = list(self._info().features.keys())
240
  metadata = {}
241
  metadata_found = False
242
- for path, f in archive_iterator:
243
- if path == metadata_filepath:
244
- metadata_found = True
245
- lines = (line.decode("utf-8") for line in f)
246
- reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
247
- for row in reader:
248
- # set absolute path for mp3 audio file
249
- if not row["path"].endswith(".mp3"):
250
- row["path"] += ".mp3"
251
- row["path"] = os.path.join(path_to_clips, row["path"])
252
- # accent -> accents in CV 8.0
253
- if "accents" in row:
254
- row["accent"] = row["accents"]
255
- del row["accents"]
256
- # if data is incomplete, fill with empty values
257
- for field in data_fields:
258
- if field not in row:
259
- row[field] = ""
260
- metadata[row["path"]] = row
261
- elif path.startswith(path_to_clips):
262
- assert metadata_found, "Found audio clips before the metadata TSV file."
263
- if not metadata:
264
- break
265
- if path in metadata:
266
- result = metadata[path]
267
- # set the audio feature and the path to the extracted file
268
- path = (
269
- os.path.join(local_extracted_archive, path)
270
- if local_extracted_archive
271
- else path
272
- )
273
- result["audio"] = {"path": path, "bytes": f.read()}
274
- # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
275
- result["path"] = path if local_extracted_archive else None
276
-
277
- yield path, result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
 
280
 
 
226
  # ),
227
  ]
228
 
229
+ def _generate_examples(
 
 
230
  self,
231
  local_extracted_archive,
232
  archive_iterator,
233
  metadata_filepath,
234
  path_to_clips,
235
+ mode
236
  ):
237
  """Yields examples."""
238
  data_fields = list(self._info().features.keys())
239
  metadata = {}
240
  metadata_found = False
241
+ if mode in ["dev","test"]:
242
+ for path, f in archive_iterator:
243
+ if path == metadata_filepath:
244
+ metadata_found = True
245
+ lines = (line.decode("utf-8") for line in f)
246
+ reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
247
+ for row in reader:
248
+ # set absolute path for mp3 audio file
249
+ if not row["path"].endswith(".mp3"):
250
+ row["path"] += ".mp3"
251
+ row["path"] = os.path.join(path_to_clips, row["path"])
252
+ # accent -> accents in CV 8.0
253
+ if "accents" in row:
254
+ row["accent"] = row["accents"]
255
+ del row["accents"]
256
+ # if data is incomplete, fill with empty values
257
+ for field in data_fields:
258
+ if field not in row:
259
+ row[field] = ""
260
+ metadata[row["path"]] = row
261
+ elif path.startswith(path_to_clips):
262
+ assert metadata_found, "Found audio clips before the metadata TSV file."
263
+ if not metadata:
264
+ break
265
+ if path in metadata:
266
+ result = metadata[path]
267
+ # set the audio feature and the path to the extracted file
268
+ path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
269
+ result["audio"] = {"path": path, "bytes": f.read()}
270
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
271
+ result["path"] = path if local_extracted_archive else None
272
+
273
+ yield path, result
274
+ else:
275
+ metadata_found = True
276
+ with open(metadata_filepath, "rb") as file_obj:
277
+ lines = (line.decode("utf-8") for line in file_obj)
278
+ reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
279
+ for row in reader:
280
+ # set absolute path for mp3 audio file
281
+ if not row["path"].endswith(".mp3"):
282
+ row["path"] += ".mp3"
283
+ row["path"] = os.path.join(path_to_clips, row["path"])
284
+ # accent -> accents in CV 8.0
285
+ if "accents" in row:
286
+ row["accent"] = row["accents"]
287
+ del row["accents"]
288
+ # if data is incomplete, fill with empty values
289
+ for field in data_fields:
290
+ if field not in row:
291
+ row[field] = ""
292
+ metadata[row["path"]] = row
293
+ for path, f in archive_iterator:
294
+ if path.startswith(path_to_clips):
295
+ assert metadata_found, "Found audio clips before the metadata TSV file."
296
+ if not metadata:
297
+ break
298
+ if path in metadata:
299
+ result = metadata[path]
300
+ # set the audio feature and the path to the extracted file
301
+ path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
302
+ result["audio"] = {"path": path, "bytes": f.read()}
303
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
304
+ result["path"] = path if local_extracted_archive else None
305
+
306
+ yield path, result
307
+
308
+ # def _generate_examples(
309
+ # self,
310
+ # local_extracted_archive,
311
+ # archive_iterator,
312
+ # metadata_filepath,
313
+ # path_to_clips,
314
+ # ):
315
+ # """Yields examples."""
316
+ # data_fields = list(self._info().features.keys())
317
+ # metadata = {}
318
+ # metadata_found = False
319
+ # for path, f in archive_iterator:
320
+ # if path == metadata_filepath:
321
+ # metadata_found = True
322
+ # lines = (line.decode("utf-8") for line in f)
323
+ # reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
324
+ # for row in reader:
325
+ # # set absolute path for mp3 audio file
326
+ # if not row["path"].endswith(".mp3"):
327
+ # row["path"] += ".mp3"
328
+ # row["path"] = os.path.join(path_to_clips, row["path"])
329
+ # # accent -> accents in CV 8.0
330
+ # if "accents" in row:
331
+ # row["accent"] = row["accents"]
332
+ # del row["accents"]
333
+ # # if data is incomplete, fill with empty values
334
+ # for field in data_fields:
335
+ # if field not in row:
336
+ # row[field] = ""
337
+ # metadata[row["path"]] = row
338
+ # elif path.startswith(path_to_clips):
339
+ # assert metadata_found, "Found audio clips before the metadata TSV file."
340
+ # if not metadata:
341
+ # break
342
+ # if path in metadata:
343
+ # result = metadata[path]
344
+ # # set the audio feature and the path to the extracted file
345
+ # path = (
346
+ # os.path.join(local_extracted_archive, path)
347
+ # if local_extracted_archive
348
+ # else path
349
+ # )
350
+ # result["audio"] = {"path": path, "bytes": f.read()}
351
+ # # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
352
+ # result["path"] = path if local_extracted_archive else None
353
+
354
+ # yield path, result
355
 
356
 
357