Patrick von Platen
commited on
Commit
•
80c3cd3
1
Parent(s):
5a3f751
up
Browse files- librispeech_local.py +124 -0
- librispeech_local.py.lock +0 -0
librispeech_local.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 glob
|
21 |
+
import os
|
22 |
+
|
23 |
+
import datasets
|
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 |
+
Note that in order to limit the required storage for preparing this dataset, the audio
|
43 |
+
is stored in the .flac format and is not converted to a float32 array. To convert, the audio
|
44 |
+
file to a float32 array, please make use of the `.map()` function as follows:
|
45 |
+
|
46 |
+
|
47 |
+
```python
|
48 |
+
import soundfile as sf
|
49 |
+
|
50 |
+
def map_to_array(batch):
|
51 |
+
speech_array, _ = sf.read(batch["file"])
|
52 |
+
batch["speech"] = speech_array
|
53 |
+
return batch
|
54 |
+
|
55 |
+
dataset = dataset.map(map_to_array, remove_columns=["file"])
|
56 |
+
```
|
57 |
+
"""
|
58 |
+
|
59 |
+
_URL = "http://www.openslr.org/12"
|
60 |
+
|
61 |
+
|
62 |
+
class LibrispeechASRConfig(datasets.BuilderConfig):
|
63 |
+
"""BuilderConfig for LibriSpeechASR."""
|
64 |
+
|
65 |
+
def __init__(self, **kwargs):
|
66 |
+
"""
|
67 |
+
Args:
|
68 |
+
data_dir: `string`, the path to the folder containing the files in the
|
69 |
+
downloaded .tar
|
70 |
+
citation: `string`, citation for the data set
|
71 |
+
url: `string`, url for information about the data set
|
72 |
+
**kwargs: keyword arguments forwarded to super.
|
73 |
+
"""
|
74 |
+
super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
|
75 |
+
|
76 |
+
|
77 |
+
class LibrispeechASR(datasets.GeneratorBasedBuilder):
|
78 |
+
"""Librispeech dataset."""
|
79 |
+
|
80 |
+
BUILDER_CONFIGS = [
|
81 |
+
LibrispeechASRConfig(name="clean", description="'Clean' speech."),
|
82 |
+
]
|
83 |
+
|
84 |
+
def _info(self):
|
85 |
+
return datasets.DatasetInfo(
|
86 |
+
description=_DESCRIPTION,
|
87 |
+
features=datasets.Features(
|
88 |
+
{
|
89 |
+
"file": datasets.Value("string"),
|
90 |
+
"text": datasets.Value("string"),
|
91 |
+
"speaker_id": datasets.Value("int64"),
|
92 |
+
"chapter_id": datasets.Value("int64"),
|
93 |
+
"id": datasets.Value("string"),
|
94 |
+
}
|
95 |
+
),
|
96 |
+
supervised_keys=("file", "text"),
|
97 |
+
homepage=_URL,
|
98 |
+
citation=_CITATION,
|
99 |
+
)
|
100 |
+
|
101 |
+
def _split_generators(self, dl_manager):
|
102 |
+
manual_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
103 |
+
|
104 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"archive_path": manual_dir})]
|
105 |
+
|
106 |
+
def _generate_examples(self, archive_path):
|
107 |
+
"""Generate examples from a Librispeech archive_path."""
|
108 |
+
transcripts_glob = os.path.join(archive_path, "LibriSpeech", "*/*/*/*.txt")
|
109 |
+
for transcript_file in sorted(glob.glob(transcripts_glob)):
|
110 |
+
path = os.path.dirname(transcript_file)
|
111 |
+
with open(os.path.join(path, transcript_file), "r", encoding="utf-8") as f:
|
112 |
+
for line in f:
|
113 |
+
line = line.strip()
|
114 |
+
key, transcript = line.split(" ", 1)
|
115 |
+
audio_file = f"{key}.flac"
|
116 |
+
speaker_id, chapter_id = [int(el) for el in key.split("-")[:2]]
|
117 |
+
example = {
|
118 |
+
"id": key,
|
119 |
+
"speaker_id": speaker_id,
|
120 |
+
"chapter_id": chapter_id,
|
121 |
+
"file": os.path.join(path, audio_file),
|
122 |
+
"text": transcript,
|
123 |
+
}
|
124 |
+
yield key, example
|
librispeech_local.py.lock
ADDED
File without changes
|