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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""Librispeech automatic speech recognition dataset."""
|
18 |
+
|
19 |
+
|
20 |
+
import os
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
from datasets.tasks import AutomaticSpeechRecognition
|
24 |
+
|
25 |
+
|
26 |
+
_CITATION = """\
|
27 |
+
@inproceedings{panayotov2015librispeech,
|
28 |
+
title={Librispeech: an ASR corpus based on public domain audio books},
|
29 |
+
author={Panayotov, Vassil and Chen, Guoguo and Povey, Daniel and Khudanpur, Sanjeev},
|
30 |
+
booktitle={Acoustics, Speech and Signal Processing (ICASSP), 2015 IEEE International Conference on},
|
31 |
+
pages={5206--5210},
|
32 |
+
year={2015},
|
33 |
+
organization={IEEE}
|
34 |
+
}
|
35 |
+
"""
|
36 |
+
|
37 |
+
_DESCRIPTION = """\
|
38 |
+
LibriSpeech is a corpus of approximately 1000 hours of read English speech with sampling rate of 16 kHz,
|
39 |
+
prepared by Vassil Panayotov with the assistance of Daniel Povey. The data is derived from read
|
40 |
+
audiobooks from the LibriVox project, and has been carefully segmented and aligned.87
|
41 |
+
"""
|
42 |
+
|
43 |
+
_URL = "http://www.openslr.org/12"
|
44 |
+
_DL_URL = "http://www.openslr.org/resources/12/"
|
45 |
+
|
46 |
+
|
47 |
+
_DL_URLS = {
|
48 |
+
"clean": {
|
49 |
+
"dev": _DL_URL + "dev-clean.tar.gz",
|
50 |
+
},
|
51 |
+
"other": {
|
52 |
+
"test": _DL_URL + "test-other.tar.gz",
|
53 |
+
"dev": _DL_URL + "dev-other.tar.gz",
|
54 |
+
"train.500": _DL_URL + "train-other-500.tar.gz",
|
55 |
+
},
|
56 |
+
"all": {
|
57 |
+
"dev.clean": _DL_URL + "dev-clean.tar.gz",
|
58 |
+
"dev.other": _DL_URL + "dev-other.tar.gz",
|
59 |
+
"test.clean": _DL_URL + "test-clean.tar.gz",
|
60 |
+
"test.other": _DL_URL + "test-other.tar.gz",
|
61 |
+
"train.clean.100": _DL_URL + "train-clean-100.tar.gz",
|
62 |
+
"train.clean.360": _DL_URL + "train-clean-360.tar.gz",
|
63 |
+
"train.other.500": _DL_URL + "train-other-500.tar.gz",
|
64 |
+
},
|
65 |
+
}
|
66 |
+
|
67 |
+
|
68 |
+
class LibrispeechASRConfig(datasets.BuilderConfig):
|
69 |
+
"""BuilderConfig for LibriSpeechASR."""
|
70 |
+
|
71 |
+
def __init__(self, **kwargs):
|
72 |
+
"""
|
73 |
+
Args:
|
74 |
+
data_dir: `string`, the path to the folder containing the files in the
|
75 |
+
downloaded .tar
|
76 |
+
citation: `string`, citation for the data set
|
77 |
+
url: `string`, url for information about the data set
|
78 |
+
**kwargs: keyword arguments forwarded to super.
|
79 |
+
"""
|
80 |
+
super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
|
81 |
+
|
82 |
+
|
83 |
+
class LibrispeechASR(datasets.GeneratorBasedBuilder):
|
84 |
+
"""Librispeech dataset."""
|
85 |
+
|
86 |
+
DEFAULT_WRITER_BATCH_SIZE = 256
|
87 |
+
DEFAULT_CONFIG_NAME = "all"
|
88 |
+
BUILDER_CONFIGS = [
|
89 |
+
LibrispeechASRConfig(name="clean", description="'Clean' speech."),
|
90 |
+
LibrispeechASRConfig(name="other", description="'Other', more challenging, speech."),
|
91 |
+
LibrispeechASRConfig(name="all", description="Combined clean and other dataset."),
|
92 |
+
]
|
93 |
+
|
94 |
+
def _info(self):
|
95 |
+
return datasets.DatasetInfo(
|
96 |
+
description=_DESCRIPTION,
|
97 |
+
features=datasets.Features(
|
98 |
+
{
|
99 |
+
"file": datasets.Value("string"),
|
100 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
101 |
+
"text": datasets.Value("string"),
|
102 |
+
"speaker_id": datasets.Value("int64"),
|
103 |
+
"chapter_id": datasets.Value("int64"),
|
104 |
+
"id": datasets.Value("string"),
|
105 |
+
}
|
106 |
+
),
|
107 |
+
supervised_keys=("file", "text"),
|
108 |
+
homepage=_URL,
|
109 |
+
citation=_CITATION,
|
110 |
+
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
|
111 |
+
)
|
112 |
+
|
113 |
+
def _split_generators(self, dl_manager):
|
114 |
+
archive_path = dl_manager.download(_DL_URLS[self.config.name])
|
115 |
+
# (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
|
116 |
+
local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
|
117 |
+
|
118 |
+
if self.config.name == "clean":
|
119 |
+
dev_splits = [
|
120 |
+
datasets.SplitGenerator(
|
121 |
+
name=datasets.Split.VALIDATION,
|
122 |
+
gen_kwargs={
|
123 |
+
"local_extracted_archive": local_extracted_archive.get("dev"),
|
124 |
+
"files": dl_manager.iter_archive(archive_path["dev"]),
|
125 |
+
},
|
126 |
+
)
|
127 |
+
]
|
128 |
+
|
129 |
+
elif self.config.name == "other":
|
130 |
+
train_splits = [
|
131 |
+
datasets.SplitGenerator(
|
132 |
+
name="train.500",
|
133 |
+
gen_kwargs={
|
134 |
+
"local_extracted_archive": local_extracted_archive.get("train.500"),
|
135 |
+
"files": dl_manager.iter_archive(archive_path["train.500"]),
|
136 |
+
},
|
137 |
+
)
|
138 |
+
]
|
139 |
+
dev_splits = [
|
140 |
+
datasets.SplitGenerator(
|
141 |
+
name=datasets.Split.VALIDATION,
|
142 |
+
gen_kwargs={
|
143 |
+
"local_extracted_archive": local_extracted_archive.get("dev"),
|
144 |
+
"files": dl_manager.iter_archive(archive_path["dev"]),
|
145 |
+
},
|
146 |
+
)
|
147 |
+
]
|
148 |
+
test_splits = [
|
149 |
+
datasets.SplitGenerator(
|
150 |
+
name=datasets.Split.TEST,
|
151 |
+
gen_kwargs={
|
152 |
+
"local_extracted_archive": local_extracted_archive.get("test"),
|
153 |
+
"files": dl_manager.iter_archive(archive_path["test"]),
|
154 |
+
},
|
155 |
+
)
|
156 |
+
]
|
157 |
+
elif self.config.name == "all":
|
158 |
+
train_splits = [
|
159 |
+
datasets.SplitGenerator(
|
160 |
+
name="train.clean.100",
|
161 |
+
gen_kwargs={
|
162 |
+
"local_extracted_archive": local_extracted_archive.get("train.clean.100"),
|
163 |
+
"files": dl_manager.iter_archive(archive_path["train.clean.100"]),
|
164 |
+
},
|
165 |
+
),
|
166 |
+
datasets.SplitGenerator(
|
167 |
+
name="train.clean.360",
|
168 |
+
gen_kwargs={
|
169 |
+
"local_extracted_archive": local_extracted_archive.get("train.clean.360"),
|
170 |
+
"files": dl_manager.iter_archive(archive_path["train.clean.360"]),
|
171 |
+
},
|
172 |
+
),
|
173 |
+
datasets.SplitGenerator(
|
174 |
+
name="train.other.500",
|
175 |
+
gen_kwargs={
|
176 |
+
"local_extracted_archive": local_extracted_archive.get("train.other.500"),
|
177 |
+
"files": dl_manager.iter_archive(archive_path["train.other.500"]),
|
178 |
+
},
|
179 |
+
),
|
180 |
+
]
|
181 |
+
dev_splits = [
|
182 |
+
datasets.SplitGenerator(
|
183 |
+
name="validation.clean",
|
184 |
+
gen_kwargs={
|
185 |
+
"local_extracted_archive": local_extracted_archive.get("validation.clean"),
|
186 |
+
"files": dl_manager.iter_archive(archive_path["dev.clean"]),
|
187 |
+
},
|
188 |
+
),
|
189 |
+
datasets.SplitGenerator(
|
190 |
+
name="validation.other",
|
191 |
+
gen_kwargs={
|
192 |
+
"local_extracted_archive": local_extracted_archive.get("validation.other"),
|
193 |
+
"files": dl_manager.iter_archive(archive_path["dev.other"]),
|
194 |
+
},
|
195 |
+
),
|
196 |
+
]
|
197 |
+
test_splits = [
|
198 |
+
datasets.SplitGenerator(
|
199 |
+
name="test.clean",
|
200 |
+
gen_kwargs={
|
201 |
+
"local_extracted_archive": local_extracted_archive.get("test.clean"),
|
202 |
+
"files": dl_manager.iter_archive(archive_path["test.clean"]),
|
203 |
+
},
|
204 |
+
),
|
205 |
+
datasets.SplitGenerator(
|
206 |
+
name="test.other",
|
207 |
+
gen_kwargs={
|
208 |
+
"local_extracted_archive": local_extracted_archive.get("test.other"),
|
209 |
+
"files": dl_manager.iter_archive(archive_path["test.other"]),
|
210 |
+
},
|
211 |
+
),
|
212 |
+
]
|
213 |
+
|
214 |
+
return train_splits + dev_splits + test_splits
|
215 |
+
|
216 |
+
def _generate_examples(self, files, local_extracted_archive):
|
217 |
+
"""Generate examples from a LibriSpeech archive_path."""
|
218 |
+
key = 0
|
219 |
+
audio_data = {}
|
220 |
+
transcripts = []
|
221 |
+
for path, f in files:
|
222 |
+
if path.endswith(".flac"):
|
223 |
+
id_ = path.split("/")[-1][: -len(".flac")]
|
224 |
+
audio_data[id_] = f.read()
|
225 |
+
elif path.endswith(".trans.txt"):
|
226 |
+
for line in f:
|
227 |
+
if line:
|
228 |
+
line = line.decode("utf-8").strip()
|
229 |
+
id_, transcript = line.split(" ", 1)
|
230 |
+
audio_file = f"{id_}.flac"
|
231 |
+
speaker_id, chapter_id = [int(el) for el in id_.split("-")[:2]]
|
232 |
+
audio_file = (
|
233 |
+
os.path.join(local_extracted_archive, audio_file)
|
234 |
+
if local_extracted_archive
|
235 |
+
else audio_file
|
236 |
+
)
|
237 |
+
transcripts.append(
|
238 |
+
{
|
239 |
+
"id": id_,
|
240 |
+
"speaker_id": speaker_id,
|
241 |
+
"chapter_id": chapter_id,
|
242 |
+
"file": audio_file,
|
243 |
+
"text": transcript,
|
244 |
+
}
|
245 |
+
)
|
246 |
+
if audio_data and len(audio_data) == len(transcripts):
|
247 |
+
for transcript in transcripts:
|
248 |
+
audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
|
249 |
+
yield key, {"audio": audio, **transcript}
|
250 |
+
key += 1
|
251 |
+
audio_data = {}
|
252 |
+
transcripts = []
|