nakkhatra commited on
Commit
5dd05dc
1 Parent(s): 112ec03

updated _generate_examples

Browse files
Files changed (1) hide show
  1. common_voice_bn.py +38 -69
common_voice_bn.py CHANGED
@@ -174,7 +174,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
174
  # if path_to_data
175
  # else "train.tsv",
176
  #custom train.tsv
177
- "metadata_filepath": "/".join([path_to_tsvs, "train.tsv"]),
178
  "path_to_clips": path_to_clips,
179
  },
180
  ),
@@ -232,78 +232,47 @@ def _generate_examples(
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,
 
174
  # if path_to_data
175
  # else "train.tsv",
176
  #custom train.tsv
177
+ "metadata_filepath": "/".join([path_to_data, "train.tsv"]) if path_to_data else "train.tsv",
178
  "path_to_clips": path_to_clips,
179
  },
180
  ),
 
232
  archive_iterator,
233
  metadata_filepath,
234
  path_to_clips,
 
235
  ):
236
  """Yields examples."""
237
  data_fields = list(self._info().features.keys())
238
  metadata = {}
239
+ metadata_found = True
240
+ with open(metadata_filepath, "rb") as file_obj:
241
+ lines = (line.decode("utf-8") for line in file_obj)
242
+ #lines = (line.decode("utf-8") for line in f)
243
+ reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
244
+ for row in reader:
245
+ # set absolute path for mp3 audio file
246
+ if not row["path"].endswith(".mp3"):
247
+ row["path"] += ".mp3"
248
+ row["path"] = os.path.join(path_to_clips, row["path"])
249
+ # accent -> accents in CV 8.0
250
+ if "accents" in row:
251
+ row["accent"] = row["accents"]
252
+ del row["accents"]
253
+ # if data is incomplete, fill with empty values
254
+ for field in data_fields:
255
+ if field not in row:
256
+ row[field] = ""
257
+ metadata[row["path"]] = row
258
+ for path, f in archive_iterator:
259
+ if path.startswith(path_to_clips):
260
+ assert metadata_found, "Found audio clips before the metadata TSV file."
261
+ if not metadata:
262
+ break
263
+ if path in metadata:
264
+ result = metadata[path]
265
+ # set the audio feature and the path to the extracted file
266
+ path = (
267
+ os.path.join(local_extracted_archive, path)
268
+ if local_extracted_archive
269
+ else path
270
+ )
271
+ result["audio"] = {"path": path, "bytes": f.read()}
272
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
273
+ result["path"] = path if local_extracted_archive else None
274
+
275
+ yield path, result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
  # def _generate_examples(
278
  # self,