updated _generate_examples
Browse files- 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([
|
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 =
|
241 |
-
|
242 |
-
for
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
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,
|