andreagasparini commited on
Commit
ed2f49e
1 Parent(s): 0f0707e

Create new file

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