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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 |
+
"dev-clean": _DL_URL + "dev-clean.tar.gz",
|
| 49 |
+
"test-clean": _DL_URL + "test-clean.tar.gz",
|
| 50 |
+
"train-clean-100": _DL_URL + "train-clean-100.tar.gz",
|
| 51 |
+
"train-clean-360": _DL_URL + "train-clean-360.tar.gz",
|
| 52 |
+
"test-other": _DL_URL + "test-other.tar.gz",
|
| 53 |
+
"dev-other": _DL_URL + "dev-other.tar.gz",
|
| 54 |
+
"train-other-500": _DL_URL + "train-other-500.tar.gz",
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class LibrispeechASRConfig(datasets.BuilderConfig):
|
| 59 |
+
"""BuilderConfig for LibriSpeechASR."""
|
| 60 |
+
|
| 61 |
+
def __init__(self, **kwargs):
|
| 62 |
+
"""
|
| 63 |
+
Args:
|
| 64 |
+
data_dir: `string`, the path to the folder containing the files in the
|
| 65 |
+
downloaded .tar
|
| 66 |
+
citation: `string`, citation for the data set
|
| 67 |
+
url: `string`, url for information about the data set
|
| 68 |
+
**kwargs: keyword arguments forwarded to super.
|
| 69 |
+
"""
|
| 70 |
+
super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class LibrispeechASR(datasets.GeneratorBasedBuilder):
|
| 74 |
+
"""Librispeech dataset."""
|
| 75 |
+
|
| 76 |
+
DEFAULT_WRITER_BATCH_SIZE = 256
|
| 77 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 78 |
+
BUILDER_CONFIGS = [
|
| 79 |
+
LibrispeechASRConfig(name="dev-clean", description="'Clean' speech."),
|
| 80 |
+
LibrispeechASRConfig(name="test-clean", description="'Clean' speech."),
|
| 81 |
+
LibrispeechASRConfig(name="train-clean-100", description="'Clean' speech."),
|
| 82 |
+
LibrispeechASRConfig(name="train-clean-360", description="'Clean' speech."),
|
| 83 |
+
LibrispeechASRConfig(name="dev-other", description="'Other', more challenging, speech."),
|
| 84 |
+
LibrispeechASRConfig(name="test-other", description="'Other', more challenging, speech."),
|
| 85 |
+
LibrispeechASRConfig(name="train-other-500", description="'Other', more challenging, speech."),
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
def _info(self):
|
| 89 |
+
return datasets.DatasetInfo(
|
| 90 |
+
description=_DESCRIPTION,
|
| 91 |
+
features=datasets.Features(
|
| 92 |
+
{
|
| 93 |
+
"file": datasets.Value("string"),
|
| 94 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
| 95 |
+
"text": datasets.Value("string"),
|
| 96 |
+
"speaker_id": datasets.Value("int64"),
|
| 97 |
+
"chapter_id": datasets.Value("int64"),
|
| 98 |
+
"id": datasets.Value("string"),
|
| 99 |
+
}
|
| 100 |
+
),
|
| 101 |
+
supervised_keys=("file", "text"),
|
| 102 |
+
homepage=_URL,
|
| 103 |
+
citation=_CITATION,
|
| 104 |
+
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
def _split_generators(self, dl_manager):
|
| 108 |
+
archive_path = dl_manager.download(_DL_URLS[self.config.name])
|
| 109 |
+
# (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
|
| 110 |
+
local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
|
| 111 |
+
|
| 112 |
+
splits = [
|
| 113 |
+
datasets.SplitGenerator(
|
| 114 |
+
name=self.config.name,
|
| 115 |
+
gen_kwargs={
|
| 116 |
+
"local_extracted_archive": local_extracted_archive.get(self.config.name),
|
| 117 |
+
"files": dl_manager.iter_archive(self.config.name),
|
| 118 |
+
},
|
| 119 |
+
),
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
return splits
|
| 123 |
+
|
| 124 |
+
def _generate_examples(self, files, local_extracted_archive):
|
| 125 |
+
"""Generate examples from a LibriSpeech archive_path."""
|
| 126 |
+
key = 0
|
| 127 |
+
audio_data = {}
|
| 128 |
+
transcripts = []
|
| 129 |
+
for path, f in files:
|
| 130 |
+
if path.endswith(".flac"):
|
| 131 |
+
id_ = path.split("/")[-1][: -len(".flac")]
|
| 132 |
+
audio_data[id_] = f.read()
|
| 133 |
+
elif path.endswith(".trans.txt"):
|
| 134 |
+
for line in f:
|
| 135 |
+
if line:
|
| 136 |
+
line = line.decode("utf-8").strip()
|
| 137 |
+
id_, transcript = line.split(" ", 1)
|
| 138 |
+
audio_file = f"{id_}.flac"
|
| 139 |
+
speaker_id, chapter_id = [int(el) for el in id_.split("-")[:2]]
|
| 140 |
+
audio_file = (
|
| 141 |
+
os.path.join(local_extracted_archive, audio_file)
|
| 142 |
+
if local_extracted_archive
|
| 143 |
+
else audio_file
|
| 144 |
+
)
|
| 145 |
+
transcripts.append(
|
| 146 |
+
{
|
| 147 |
+
"id": id_,
|
| 148 |
+
"speaker_id": speaker_id,
|
| 149 |
+
"chapter_id": chapter_id,
|
| 150 |
+
"file": audio_file,
|
| 151 |
+
"text": transcript,
|
| 152 |
+
}
|
| 153 |
+
)
|
| 154 |
+
if audio_data and len(audio_data) == len(transcripts):
|
| 155 |
+
for transcript in transcripts:
|
| 156 |
+
audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
|
| 157 |
+
yield key, {"audio": audio, **transcript}
|
| 158 |
+
key += 1
|
| 159 |
+
audio_data = {}
|
| 160 |
+
transcripts = []
|