polinaeterna
commited on
Commit
·
8db4a65
1
Parent(s):
f3bf4e9
get local paths to audio files
Browse files- multilingual_librispeech.py +63 -27
multilingual_librispeech.py
CHANGED
|
@@ -45,7 +45,10 @@ English, German, Dutch, Spanish, French, Italian, Portuguese, Polish.
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
_URL = "http://www.openslr.org/94"
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
class MultilingualLibrispeechConfig(datasets.BuilderConfig):
|
|
@@ -97,20 +100,28 @@ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
|
|
| 97 |
)
|
| 98 |
|
| 99 |
def _split_generators(self, dl_manager):
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
download_transcript = partial(
|
| 102 |
-
download_extract_transcript,
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
download_limited_ids = partial(
|
| 108 |
-
download_extract_limited_ids,
|
| 109 |
)
|
| 110 |
|
| 111 |
train_kwargs = {
|
| 112 |
"transcript_path": download_transcript(split="train"),
|
| 113 |
-
"audio_archives":
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
|
| 116 |
train_splits = [
|
|
@@ -137,18 +148,22 @@ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
|
|
| 137 |
datasets.SplitGenerator(
|
| 138 |
name=datasets.Split.VALIDATION, gen_kwargs={
|
| 139 |
"transcript_path": download_transcript(split="dev"),
|
| 140 |
-
"audio_archives":
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
),
|
| 143 |
datasets.SplitGenerator(
|
| 144 |
name=datasets.Split.TEST, gen_kwargs={
|
| 145 |
"transcript_path": download_transcript(split="test"),
|
| 146 |
-
"audio_archives":
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
),
|
| 149 |
]
|
| 150 |
|
| 151 |
-
def _generate_examples(self, transcript_path, audio_archives, limited_ids_paths=None):
|
| 152 |
"""Generate examples from a Multilingual LibriSpeech data dir."""
|
| 153 |
transcripts = dict()
|
| 154 |
with open(transcript_path, "r", encoding="utf-8") as file:
|
|
@@ -164,7 +179,7 @@ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
|
|
| 164 |
|
| 165 |
limited_ids = set(limited_ids)
|
| 166 |
|
| 167 |
-
for audio_archive in audio_archives:
|
| 168 |
# TODO: check that archive doesn't contain needed ids
|
| 169 |
# if limited_ids and audio_archive not in limited_ids_archives_names:
|
| 170 |
# continue
|
|
@@ -179,9 +194,11 @@ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
|
|
| 179 |
# this only can be true in limited supervision sets ("train.9h" and "train.1h")
|
| 180 |
continue
|
| 181 |
|
|
|
|
|
|
|
| 182 |
yield audio_filename, {
|
| 183 |
-
"file":
|
| 184 |
-
"audio": {"path":
|
| 185 |
"text": audio_transcript,
|
| 186 |
"speaker_id": speaker_id,
|
| 187 |
"chapter_id": chapter_id,
|
|
@@ -190,7 +207,7 @@ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
|
|
| 190 |
|
| 191 |
|
| 192 |
def download_extract_limited_ids(dl_manager, root_dir, sub_folder):
|
| 193 |
-
"""Download
|
| 194 |
|
| 195 |
sub_path = os.path.join(root_dir, "train", sub_folder)
|
| 196 |
|
|
@@ -201,33 +218,52 @@ def download_extract_limited_ids(dl_manager, root_dir, sub_folder):
|
|
| 201 |
# "limited_supervision/1h/0/handles.txt", "limited_supervision/1h/1/handles.txt", ...
|
| 202 |
limited_ids_paths = [os.path.join(sub_path, str(i), "handles.txt") for i in range(6)]
|
| 203 |
|
| 204 |
-
limited_ids_paths = dl_manager.
|
| 205 |
|
| 206 |
return limited_ids_paths
|
| 207 |
|
| 208 |
|
| 209 |
def download_extract_transcript(dl_manager, root_dir, split):
|
| 210 |
-
"""
|
| 211 |
-
|
| 212 |
-
return dl_manager.download_and_extract(transcript_path)
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
def download_audio_archives(dl_manager, root_dir, split):
|
| 216 |
-
"""Prepare archives with audio files for iterating over them.
|
| 217 |
|
| 218 |
Return:
|
| 219 |
-
|
| 220 |
"""
|
|
|
|
|
|
|
|
|
|
| 221 |
|
|
|
|
| 222 |
# each split contains many .tar.gz archives with its audio files
|
| 223 |
# audio_filenames.txt contains the names of these archives
|
| 224 |
split_dir = os.path.join(root_dir, split)
|
| 225 |
audio_filenames_path = dl_manager.download(os.path.join(split_dir, "audio_filenames.txt"))
|
| 226 |
|
| 227 |
-
with
|
| 228 |
audio_filenames = [line.strip() for line in file.readlines()]
|
| 229 |
|
| 230 |
-
|
| 231 |
-
audio_archives = [dl_manager.iter_archive(archive_path) for archive_path in archive_paths]
|
| 232 |
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
_URL = "http://www.openslr.org/94"
|
| 48 |
+
|
| 49 |
+
_BASE_URL = "https://huggingface.co/datasets/facebook/multilingual_librispeech/resolve/main/"
|
| 50 |
+
|
| 51 |
+
_DL_URL_FORMAT = _BASE_URL + "data/mls_{name}"
|
| 52 |
|
| 53 |
|
| 54 |
class MultilingualLibrispeechConfig(datasets.BuilderConfig):
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
def _split_generators(self, dl_manager):
|
| 103 |
+
download_kwargs = {
|
| 104 |
+
"dl_manager": dl_manager,
|
| 105 |
+
"root_dir": self.config.data_root_dir
|
| 106 |
+
}
|
| 107 |
download_transcript = partial(
|
| 108 |
+
download_extract_transcript, **download_kwargs
|
| 109 |
)
|
| 110 |
+
download_audio_non_streaming = partial(
|
| 111 |
+
download_extract_audio_archives, **download_kwargs
|
| 112 |
+
)
|
| 113 |
+
download_audio_streaming = partial(
|
| 114 |
+
download_audio_archives, **download_kwargs
|
| 115 |
)
|
| 116 |
download_limited_ids = partial(
|
| 117 |
+
download_extract_limited_ids, **download_kwargs
|
| 118 |
)
|
| 119 |
|
| 120 |
train_kwargs = {
|
| 121 |
"transcript_path": download_transcript(split="train"),
|
| 122 |
+
"audio_archives": download_audio_streaming(split="train"),
|
| 123 |
+
"local_audio_archives_paths": download_audio_non_streaming(split="train")
|
| 124 |
+
if not dl_manager.is_streaming else None
|
| 125 |
}
|
| 126 |
|
| 127 |
train_splits = [
|
|
|
|
| 148 |
datasets.SplitGenerator(
|
| 149 |
name=datasets.Split.VALIDATION, gen_kwargs={
|
| 150 |
"transcript_path": download_transcript(split="dev"),
|
| 151 |
+
"audio_archives": download_audio_streaming(split="dev"),
|
| 152 |
+
"local_audio_archives_paths": download_audio_non_streaming(split="dev")
|
| 153 |
+
if not dl_manager.is_streaming else None
|
| 154 |
}
|
| 155 |
),
|
| 156 |
datasets.SplitGenerator(
|
| 157 |
name=datasets.Split.TEST, gen_kwargs={
|
| 158 |
"transcript_path": download_transcript(split="test"),
|
| 159 |
+
"audio_archives": download_audio_streaming(split="test"),
|
| 160 |
+
"local_audio_archives_paths": download_audio_non_streaming(split="test")
|
| 161 |
+
if not dl_manager.is_streaming else None
|
| 162 |
}
|
| 163 |
),
|
| 164 |
]
|
| 165 |
|
| 166 |
+
def _generate_examples(self, transcript_path, audio_archives, local_audio_archives_paths, limited_ids_paths=None):
|
| 167 |
"""Generate examples from a Multilingual LibriSpeech data dir."""
|
| 168 |
transcripts = dict()
|
| 169 |
with open(transcript_path, "r", encoding="utf-8") as file:
|
|
|
|
| 179 |
|
| 180 |
limited_ids = set(limited_ids)
|
| 181 |
|
| 182 |
+
for archive_idx, audio_archive in enumerate(audio_archives):
|
| 183 |
# TODO: check that archive doesn't contain needed ids
|
| 184 |
# if limited_ids and audio_archive not in limited_ids_archives_names:
|
| 185 |
# continue
|
|
|
|
| 194 |
# this only can be true in limited supervision sets ("train.9h" and "train.1h")
|
| 195 |
continue
|
| 196 |
|
| 197 |
+
path = os.path.join(local_audio_archives_paths[archive_idx], audio_filename)\
|
| 198 |
+
if local_audio_archives_paths else audio_filename
|
| 199 |
yield audio_filename, {
|
| 200 |
+
"file": path if local_audio_archives_paths else None,
|
| 201 |
+
"audio": {"path": path, "bytes": file.read()},
|
| 202 |
"text": audio_transcript,
|
| 203 |
"speaker_id": speaker_id,
|
| 204 |
"chapter_id": chapter_id,
|
|
|
|
| 207 |
|
| 208 |
|
| 209 |
def download_extract_limited_ids(dl_manager, root_dir, sub_folder):
|
| 210 |
+
"""Download handles.txt files containing ids for limited supervision train sets. """
|
| 211 |
|
| 212 |
sub_path = os.path.join(root_dir, "train", sub_folder)
|
| 213 |
|
|
|
|
| 218 |
# "limited_supervision/1h/0/handles.txt", "limited_supervision/1h/1/handles.txt", ...
|
| 219 |
limited_ids_paths = [os.path.join(sub_path, str(i), "handles.txt") for i in range(6)]
|
| 220 |
|
| 221 |
+
limited_ids_paths = dl_manager.download(limited_ids_paths)
|
| 222 |
|
| 223 |
return limited_ids_paths
|
| 224 |
|
| 225 |
|
| 226 |
def download_extract_transcript(dl_manager, root_dir, split):
|
| 227 |
+
"""
|
| 228 |
+
Download file with audio transcriptions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
Return:
|
| 231 |
+
path (str): path to locally extracted `transcripts.txt` file
|
| 232 |
"""
|
| 233 |
+
transcript_path = os.path.join(root_dir, split, "transcripts.txt")
|
| 234 |
+
return dl_manager.download(transcript_path)
|
| 235 |
+
|
| 236 |
|
| 237 |
+
def download_audio_archive_paths(dl_manager, root_dir, split):
|
| 238 |
# each split contains many .tar.gz archives with its audio files
|
| 239 |
# audio_filenames.txt contains the names of these archives
|
| 240 |
split_dir = os.path.join(root_dir, split)
|
| 241 |
audio_filenames_path = dl_manager.download(os.path.join(split_dir, "audio_filenames.txt"))
|
| 242 |
|
| 243 |
+
with open(audio_filenames_path, "r", encoding="utf-8") as file:
|
| 244 |
audio_filenames = [line.strip() for line in file.readlines()]
|
| 245 |
|
| 246 |
+
return dl_manager.download([os.path.join(split_dir, "audio", filename) for filename in audio_filenames])
|
|
|
|
| 247 |
|
| 248 |
+
|
| 249 |
+
# for non-streaming case
|
| 250 |
+
def download_extract_audio_archives(dl_manager, root_dir, split):
|
| 251 |
+
"""
|
| 252 |
+
Download and extract audio archives locally.
|
| 253 |
+
|
| 254 |
+
Return:
|
| 255 |
+
archive_paths (List `str`): paths to locally extracted archives
|
| 256 |
+
"""
|
| 257 |
+
archive_paths = download_audio_archive_paths(dl_manager, root_dir, split)
|
| 258 |
+
return [dl_manager.extract(archive_path) for archive_path in archive_paths]
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
# for streaming case
|
| 262 |
+
def download_audio_archives(dl_manager, root_dir, split):
|
| 263 |
+
"""Prepare archives with audio files for iterating over them.
|
| 264 |
+
|
| 265 |
+
Return:
|
| 266 |
+
audio_archives (List `Generator`): list of generators to iterate over files in each audio archive.
|
| 267 |
+
"""
|
| 268 |
+
archive_paths = download_audio_archive_paths(dl_manager, root_dir, split)
|
| 269 |
+
return [dl_manager.iter_archive(archive_path) for archive_path in archive_paths]
|