patrickvonplaten commited on
Commit
d30bdd1
1 Parent(s): bd52d66
Files changed (1) hide show
  1. librispeech_asr_demo.py +137 -0
librispeech_asr_demo.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from __future__ import absolute_import, division, print_function
20
+
21
+ import glob
22
+ import os
23
+
24
+ import datasets
25
+
26
+
27
+ _CITATION = """\
28
+ @inproceedings{panayotov2015librispeech,
29
+ title={Librispeech: an ASR corpus based on public domain audio books},
30
+ author={Panayotov, Vassil and Chen, Guoguo and Povey, Daniel and Khudanpur, Sanjeev},
31
+ booktitle={Acoustics, Speech and Signal Processing (ICASSP), 2015 IEEE International Conference on},
32
+ pages={5206--5210},
33
+ year={2015},
34
+ organization={IEEE}
35
+ }
36
+ """
37
+
38
+ _DESCRIPTION = """\
39
+ LibriSpeech is a corpus of approximately 1000 hours of read English speech with sampling rate of 16 kHz,
40
+ prepared by Vassil Panayotov with the assistance of Daniel Povey. The data is derived from read
41
+ audiobooks from the LibriVox project, and has been carefully segmented and aligned.
42
+
43
+ Note that in order to limit the required storage for preparing this dataset, the audio
44
+ is stored in the .flac format and is not converted to a float32 array. To convert, the audio
45
+ file to a float32 array, please make use of the `.map()` function as follows:
46
+
47
+
48
+ ```python
49
+ import soundfile as sf
50
+
51
+ def map_to_array(batch):
52
+ speech_array, _ = sf.read(batch["file"])
53
+ batch["speech"] = speech_array
54
+ return batch
55
+
56
+ dataset = dataset.map(map_to_array, remove_columns=["file"])
57
+ ```
58
+ """
59
+
60
+ _URL = "http://www.openslr.org/12"
61
+ _DL_URL = "https://s3.amazonaws.com/datasets.huggingface.co/librispeech_asr/2.1.0/"
62
+ _DL_URL = "https://s3.amazonaws.com/datasets.huggingface.co/librispeech_asr/2.1.0/"
63
+
64
+ _DL_URLS = {
65
+ "clean": {
66
+ "dev": _DL_URL + "dev_clean.tar.gz",
67
+ }
68
+ }
69
+
70
+
71
+ class LibrispeechASRConfig(datasets.BuilderConfig):
72
+ """BuilderConfig for LibriSpeechASR."""
73
+
74
+ def __init__(self, **kwargs):
75
+ """
76
+ Args:
77
+ data_dir: `string`, the path to the folder containing the files in the
78
+ downloaded .tar
79
+ citation: `string`, citation for the data set
80
+ url: `string`, url for information about the data set
81
+ **kwargs: keyword arguments forwarded to super.
82
+ """
83
+ super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
84
+
85
+
86
+ class LibrispeechASR(datasets.GeneratorBasedBuilder):
87
+ """Librispeech dataset."""
88
+
89
+ BUILDER_CONFIGS = [
90
+ LibrispeechASRConfig(name="clean", description="'Clean' speech."),
91
+ LibrispeechASRConfig(name="other", description="'Other', more challenging, speech."),
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.features.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=("speech", "text"),
108
+ homepage=_URL,
109
+ citation=_CITATION,
110
+ )
111
+
112
+ def _split_generators(self, dl_manager):
113
+ archive_path = dl_manager.download_and_extract(_DL_URLS[self.config.name])
114
+ return [
115
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"archive_path": archive_path["dev"], "split_name": f"dev_{self.config.name}"}),
116
+ ]
117
+
118
+ def _generate_examples(self, archive_path, split_name):
119
+ """Generate examples from a Librispeech archive_path."""
120
+ transcripts_glob = os.path.join(archive_path, split_name, "*/*/*.txt")
121
+ for transcript_file in glob.glob(transcripts_glob):
122
+ path = os.path.dirname(transcript_file)
123
+ with open(os.path.join(path, transcript_file)) as f:
124
+ for line in f:
125
+ line = line.strip()
126
+ key, transcript = line.split(" ", 1)
127
+ audio_file = f"{key}.flac"
128
+ speaker_id, chapter_id = [int(el) for el in key.split("-")[:2]]
129
+ example = {
130
+ "id": key,
131
+ "speaker_id": speaker_id,
132
+ "chapter_id": chapter_id,
133
+ "file": os.path.join(path, audio_file),
134
+ "audio": os.path.join(path, audio_file),
135
+ "text": transcript,
136
+ }
137
+ yield key, example