mnansary commited on
Commit ·
3557026
1
Parent(s): 1fa7dbe
data
Browse files
cvbn.py
CHANGED
|
@@ -43,6 +43,7 @@ _LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/"
|
|
| 43 |
_API_URL = "https://commonvoice.mozilla.org/api/v1"
|
| 44 |
|
| 45 |
_TRAIN_XZ="https://drive.google.com/uc?id=1KYTCAi9J25ngzxq22mCrKTEVg0i3ZVil"
|
|
|
|
| 46 |
|
| 47 |
class CommonVoiceConfig(datasets.BuilderConfig):
|
| 48 |
"""BuilderConfig for CommonVoice."""
|
|
@@ -162,6 +163,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
|
|
| 162 |
|
| 163 |
|
| 164 |
train_tsv=dl_manager.download(_TRAIN_XZ)
|
|
|
|
| 165 |
|
| 166 |
if self.config.version < datasets.Version("5.0.0"):
|
| 167 |
path_to_data = ""
|
|
@@ -178,6 +180,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
|
|
| 178 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 179 |
"metadata_filepath": os.path.join(train_tsv,"train.tsv"),
|
| 180 |
"path_to_clips": path_to_clips,
|
|
|
|
| 181 |
},
|
| 182 |
),
|
| 183 |
datasets.SplitGenerator(
|
|
@@ -187,6 +190,7 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
|
|
| 187 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 188 |
"metadata_filepath": "/".join([path_to_data, "test.tsv"]) if path_to_data else "test.tsv",
|
| 189 |
"path_to_clips": path_to_clips,
|
|
|
|
| 190 |
},
|
| 191 |
),
|
| 192 |
datasets.SplitGenerator(
|
|
@@ -194,8 +198,9 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
|
|
| 194 |
gen_kwargs={
|
| 195 |
"local_extracted_archive": local_extracted_archive,
|
| 196 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 197 |
-
"metadata_filepath":
|
| 198 |
"path_to_clips": path_to_clips,
|
|
|
|
| 199 |
},
|
| 200 |
),
|
| 201 |
]
|
|
@@ -205,41 +210,76 @@ class CommonVoice(datasets.GeneratorBasedBuilder):
|
|
| 205 |
local_extracted_archive,
|
| 206 |
archive_iterator,
|
| 207 |
metadata_filepath,
|
| 208 |
-
path_to_clips
|
|
|
|
| 209 |
):
|
| 210 |
"""Yields examples."""
|
| 211 |
data_fields = list(self._info().features.keys())
|
| 212 |
metadata = {}
|
| 213 |
metadata_found = False
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
_API_URL = "https://commonvoice.mozilla.org/api/v1"
|
| 44 |
|
| 45 |
_TRAIN_XZ="https://drive.google.com/uc?id=1KYTCAi9J25ngzxq22mCrKTEVg0i3ZVil"
|
| 46 |
+
_DEV_XZ="https://drive.google.com/uc?id=1Q_h5HbF9BDT0_RE14gTvlcfzR2HIQyMW"
|
| 47 |
|
| 48 |
class CommonVoiceConfig(datasets.BuilderConfig):
|
| 49 |
"""BuilderConfig for CommonVoice."""
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
train_tsv=dl_manager.download(_TRAIN_XZ)
|
| 166 |
+
dev_tsv=dl_manager.download(_DEV_XZ)
|
| 167 |
|
| 168 |
if self.config.version < datasets.Version("5.0.0"):
|
| 169 |
path_to_data = ""
|
|
|
|
| 180 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 181 |
"metadata_filepath": os.path.join(train_tsv,"train.tsv"),
|
| 182 |
"path_to_clips": path_to_clips,
|
| 183 |
+
"mode":"train",
|
| 184 |
},
|
| 185 |
),
|
| 186 |
datasets.SplitGenerator(
|
|
|
|
| 190 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 191 |
"metadata_filepath": "/".join([path_to_data, "test.tsv"]) if path_to_data else "test.tsv",
|
| 192 |
"path_to_clips": path_to_clips,
|
| 193 |
+
"mode":"test",
|
| 194 |
},
|
| 195 |
),
|
| 196 |
datasets.SplitGenerator(
|
|
|
|
| 198 |
gen_kwargs={
|
| 199 |
"local_extracted_archive": local_extracted_archive,
|
| 200 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
| 201 |
+
"metadata_filepath": os.path.join(dev_tsv,"dev.tsv"),
|
| 202 |
"path_to_clips": path_to_clips,
|
| 203 |
+
"mode":"dev",
|
| 204 |
},
|
| 205 |
),
|
| 206 |
]
|
|
|
|
| 210 |
local_extracted_archive,
|
| 211 |
archive_iterator,
|
| 212 |
metadata_filepath,
|
| 213 |
+
path_to_clips,
|
| 214 |
+
mode
|
| 215 |
):
|
| 216 |
"""Yields examples."""
|
| 217 |
data_fields = list(self._info().features.keys())
|
| 218 |
metadata = {}
|
| 219 |
metadata_found = False
|
| 220 |
+
if mode in ["test"]:
|
| 221 |
+
for path, f in archive_iterator:
|
| 222 |
+
if path == metadata_filepath:
|
| 223 |
+
metadata_found = True
|
| 224 |
+
lines = (line.decode("utf-8") for line in f)
|
| 225 |
+
reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
|
| 226 |
+
for row in reader:
|
| 227 |
+
# set absolute path for mp3 audio file
|
| 228 |
+
if not row["path"].endswith(".mp3"):
|
| 229 |
+
row["path"] += ".mp3"
|
| 230 |
+
row["path"] = os.path.join(path_to_clips, row["path"])
|
| 231 |
+
# accent -> accents in CV 8.0
|
| 232 |
+
if "accents" in row:
|
| 233 |
+
row["accent"] = row["accents"]
|
| 234 |
+
del row["accents"]
|
| 235 |
+
# if data is incomplete, fill with empty values
|
| 236 |
+
for field in data_fields:
|
| 237 |
+
if field not in row:
|
| 238 |
+
row[field] = ""
|
| 239 |
+
metadata[row["path"]] = row
|
| 240 |
+
elif path.startswith(path_to_clips):
|
| 241 |
+
assert metadata_found, "Found audio clips before the metadata TSV file."
|
| 242 |
+
if not metadata:
|
| 243 |
+
break
|
| 244 |
+
if path in metadata:
|
| 245 |
+
result = metadata[path]
|
| 246 |
+
# set the audio feature and the path to the extracted file
|
| 247 |
+
path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
|
| 248 |
+
result["audio"] = {"path": path, "bytes": f.read()}
|
| 249 |
+
# set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
|
| 250 |
+
result["path"] = path if local_extracted_archive else None
|
| 251 |
+
|
| 252 |
+
yield path, result
|
| 253 |
+
else:
|
| 254 |
+
metadata_found = True
|
| 255 |
+
with open(metadata_filepath, "rb") as file_obj:
|
| 256 |
+
lines = (line.decode("utf-8") for line in file_obj)
|
| 257 |
+
reader = csv.DictReader(lines, delimiter="\t", quoting=csv.QUOTE_NONE)
|
| 258 |
+
for row in reader:
|
| 259 |
+
# set absolute path for mp3 audio file
|
| 260 |
+
if not row["path"].endswith(".mp3"):
|
| 261 |
+
row["path"] += ".mp3"
|
| 262 |
+
row["path"] = os.path.join(path_to_clips, row["path"])
|
| 263 |
+
# accent -> accents in CV 8.0
|
| 264 |
+
if "accents" in row:
|
| 265 |
+
row["accent"] = row["accents"]
|
| 266 |
+
del row["accents"]
|
| 267 |
+
# if data is incomplete, fill with empty values
|
| 268 |
+
for field in data_fields:
|
| 269 |
+
if field not in row:
|
| 270 |
+
row[field] = ""
|
| 271 |
+
metadata[row["path"]] = row
|
| 272 |
+
for path, f in archive_iterator:
|
| 273 |
+
if path.startswith(path_to_clips):
|
| 274 |
+
assert metadata_found, "Found audio clips before the metadata TSV file."
|
| 275 |
+
if not metadata:
|
| 276 |
+
break
|
| 277 |
+
if path in metadata:
|
| 278 |
+
result = metadata[path]
|
| 279 |
+
# set the audio feature and the path to the extracted file
|
| 280 |
+
path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
|
| 281 |
+
result["audio"] = {"path": path, "bytes": f.read()}
|
| 282 |
+
# set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
|
| 283 |
+
result["path"] = path if local_extracted_archive else None
|
| 284 |
+
|
| 285 |
+
yield path, result
|