sanchit-gandhi HF staff commited on
Commit
ec52119
1 Parent(s): 6f18842

from peoples speech

Browse files
Files changed (1) hide show
  1. peoples_speech-clean.py +227 -0
peoples_speech-clean.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import json
16
+ import os
17
+
18
+ import datasets
19
+ from datasets.tasks import AutomaticSpeechRecognition
20
+ from tqdm.auto import tqdm
21
+
22
+
23
+ # Find for instance the citation on arxiv or on the dataset repo/website
24
+ _CITATION = """\
25
+ @article{DBLP:journals/corr/abs-2111-09344,
26
+ author = {Daniel Galvez and
27
+ Greg Diamos and
28
+ Juan Ciro and
29
+ Juan Felipe Ceron and
30
+ Keith Achorn and
31
+ Anjali Gopi and
32
+ David Kanter and
33
+ Maximilian Lam and
34
+ Mark Mazumder and
35
+ Vijay Janapa Reddi},
36
+ title = {The People's Speech: A Large-Scale Diverse English Speech Recognition
37
+ Dataset for Commercial Usage},
38
+ journal = {CoRR},
39
+ volume = {abs/2111.09344},
40
+ year = {2021},
41
+ url = {https://arxiv.org/abs/2111.09344},
42
+ eprinttype = {arXiv},
43
+ eprint = {2111.09344},
44
+ timestamp = {Mon, 22 Nov 2021 16:44:07 +0100},
45
+ biburl = {https://dblp.org/rec/journals/corr/abs-2111-09344.bib},
46
+ bibsource = {dblp computer science bibliography, https://dblp.org}
47
+ }
48
+ """
49
+
50
+ # You can copy an official description
51
+ _DESCRIPTION = """\
52
+ The People's Speech is a free-to-download 30,000-hour and growing supervised
53
+ conversational English speech recognition dataset licensed for academic and
54
+ commercial usage under CC-BY-SA (with a CC-BY subset).
55
+ """
56
+
57
+ _HOMEPAGE = "https://mlcommons.org/en/peoples-speech/"
58
+
59
+ _LICENSE = [
60
+ "cc-by-2.0", "cc-by-2.5", "cc-by-3.0", "cc-by-4.0", "cc-by-sa-2.5",
61
+ "cc-by-sa-3.0", "cc-by-sa-4.0"
62
+ ]
63
+
64
+ _BASE_URL = "https://huggingface.co/datasets/MLCommons/peoples_speech/resolve/main/"
65
+
66
+ # relative path to data inside dataset's repo
67
+ _DATA_URL = _BASE_URL + "{split}/{config}/{config}_{archive_id:06d}.tar"
68
+
69
+ # relative path to file containing number of audio archives inside dataset's repo
70
+ _N_SHARDS_URL = _BASE_URL + "n_shards.json"
71
+
72
+ # relative path to metadata inside dataset's repo
73
+ _MANIFEST_URL = _BASE_URL + "{split}/{config}.json"
74
+
75
+
76
+ class PeoplesSpeechConfig(datasets.BuilderConfig):
77
+
78
+ def __init__(self, *args, **kwargs):
79
+ super().__init__(*args, **kwargs)
80
+
81
+
82
+ class PeoplesSpeech(datasets.GeneratorBasedBuilder):
83
+ """The People's Speech dataset."""
84
+
85
+ VERSION = datasets.Version("1.1.0")
86
+ BUILDER_CONFIGS = [
87
+ PeoplesSpeechConfig(name="microset", version=VERSION, description="Small subset of clean data for example pusposes."),
88
+ PeoplesSpeechConfig(name="clean", version=VERSION, description="Clean, CC-BY licensed subset."),
89
+ PeoplesSpeechConfig(name="dirty", version=VERSION, description="Dirty, CC-BY licensed subset."),
90
+ PeoplesSpeechConfig(name="clean_sa", version=VERSION, description="Clean, CC-BY-SA licensed subset."),
91
+ PeoplesSpeechConfig(name="dirty_sa", version=VERSION, description="Dirty, CC-BY-SA licensed subset."),
92
+ PeoplesSpeechConfig(name="test", version=VERSION, description="Only test data."),
93
+ PeoplesSpeechConfig(name="validation", version=VERSION, description="Only validation data."),
94
+ ]
95
+ DEFAULT_CONFIG_NAME = "clean"
96
+ DEFAULT_WRITER_BATCH_SIZE = 512
97
+
98
+ def _info(self):
99
+ return datasets.DatasetInfo(
100
+ description=_DESCRIPTION,
101
+ features=datasets.Features(
102
+ {
103
+ "id": datasets.Value("string"),
104
+ "audio": datasets.Audio(sampling_rate=16_000),
105
+ "duration_ms": datasets.Value("int32"),
106
+ "text": datasets.Value("string"),
107
+ }
108
+ ),
109
+ task_templates=[AutomaticSpeechRecognition()],
110
+ homepage=_HOMEPAGE,
111
+ license="/".join(_LICENSE), # license must be a string
112
+ citation=_CITATION,
113
+ )
114
+
115
+ def _split_generators(self, dl_manager):
116
+
117
+ if self.config.name == "microset":
118
+ # take only first data archive for demo purposes
119
+ url = [_DATA_URL.format(split="train", config="clean", archive_id=0)]
120
+ archive_path = dl_manager.download(url)
121
+ local_extracted_archive_path = dl_manager.extract(archive_path) if not dl_manager.is_streaming else [None]
122
+ manifest_url = _MANIFEST_URL.format(split="train", config="clean_000000") # train/clean_000000.json
123
+ manifest_path = dl_manager.download_and_extract(manifest_url)
124
+
125
+ return [
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.TRAIN,
128
+ gen_kwargs={
129
+ "local_extracted_archive_paths": local_extracted_archive_path,
130
+ # use iter_archive here to access the files in the TAR archives:
131
+ "archives": [dl_manager.iter_archive(path) for path in archive_path],
132
+ "manifest_path": manifest_path,
133
+ },
134
+ ),
135
+ ]
136
+
137
+ n_shards_path = dl_manager.download_and_extract(_N_SHARDS_URL)
138
+ with open(n_shards_path, encoding="utf-8") as f:
139
+ n_shards = json.load(f)
140
+
141
+ if self.config.name in ["validation", "test"]:
142
+ splits_to_configs = {self.config.name: self.config.name}
143
+ else:
144
+ splits_to_configs = {
145
+ "train": self.config.name,
146
+ "validation": "validation",
147
+ "test": "test"
148
+ }
149
+
150
+ audio_urls = {
151
+ split: [
152
+ _DATA_URL.format(split=split, config=config, archive_id=i) for i in range(n_shards[split][config])
153
+ ] for split, config in splits_to_configs.items()
154
+ }
155
+ audio_archive_paths = dl_manager.download(audio_urls)
156
+
157
+ # In non-streaming mode, we extract the archives to have the data locally:
158
+ local_extracted_archive_paths = dl_manager.extract(audio_archive_paths) \
159
+ if not dl_manager.is_streaming else \
160
+ {split: [None] * len(audio_archive_paths) for split in splits_to_configs}
161
+
162
+ manifest_urls = {
163
+ split: _MANIFEST_URL.format(split=split, config=config) for split, config in splits_to_configs.items()
164
+ }
165
+ manifest_paths = dl_manager.download_and_extract(manifest_urls)
166
+
167
+ # To access the audio data from the TAR archives using the download manager,
168
+ # we have to use the dl_manager.iter_archive method
169
+ #
170
+ # This is because dl_manager.download_and_extract
171
+ # doesn't work to stream TAR archives in streaming mode.
172
+ # (we have to stream the files of a TAR archive one by one)
173
+ #
174
+ # The iter_archive method returns an iterable of (path_within_archive, file_obj) for every
175
+ # file in a TAR archive.
176
+ splits_to_names = {
177
+ "train": datasets.Split.TRAIN,
178
+ "validation": datasets.Split.VALIDATION,
179
+ "test": datasets.Split.TEST,
180
+ }
181
+ split_generators = []
182
+ for split in splits_to_configs:
183
+ split_generators.append(
184
+ datasets.SplitGenerator(
185
+ name=splits_to_names[split],
186
+ gen_kwargs={
187
+ "local_extracted_archive_paths": local_extracted_archive_paths[split],
188
+ # use iter_archive here to access the files in the TAR archives:
189
+ "archives": [dl_manager.iter_archive(path) for path in audio_archive_paths[split]],
190
+ "manifest_path": manifest_paths[split],
191
+ }
192
+ )
193
+ )
194
+
195
+ return split_generators
196
+
197
+ def _generate_examples(self, local_extracted_archive_paths, archives, manifest_path):
198
+ meta = dict()
199
+ with open(manifest_path, "r", encoding="utf-8") as f:
200
+ for line in tqdm(f, desc="reading metadata file"):
201
+ sample_meta = json.loads(line)
202
+ _id = sample_meta["audio_document_id"]
203
+ texts = sample_meta["training_data"]["label"]
204
+ audio_filenames = sample_meta["training_data"]["name"]
205
+ durations = sample_meta["training_data"]["duration_ms"]
206
+ for audio_filename, text, duration in zip(audio_filenames, texts, durations):
207
+ audio_filename = audio_filename.lstrip("./")
208
+ meta[audio_filename] = {
209
+ "audio_document_id": _id,
210
+ "text": text,
211
+ "duration_ms": duration
212
+ }
213
+
214
+ for local_extracted_archive_path, archive in zip(local_extracted_archive_paths, archives):
215
+ # Here we iterate over all the files within the TAR archive:
216
+ for audio_filename, audio_file in archive:
217
+ audio_filename = audio_filename.lstrip("./")
218
+ # if an audio file exists locally (i.e. in default, non-streaming mode) set the full path to it
219
+ # joining path to directory that the archive was extracted to and audio filename.
220
+ path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path \
221
+ else audio_filename
222
+ yield audio_filename, {
223
+ "id": audio_filename,
224
+ "audio": {"path": path, "bytes": audio_file.read()},
225
+ "text": meta[audio_filename]["text"],
226
+ "duration_ms": meta[audio_filename]["duration_ms"]
227
+ }