Siddhant commited on
Commit
a22e847
·
1 Parent(s): 70e9071

First version of MySpeechAsr

Browse files
Files changed (2) hide show
  1. dummy_data.zip +3 -0
  2. myspeechasr.py +280 -0
dummy_data.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a42672e6d1e054ebfed873d36a9b9cee3a919a159104b59649b887ea257e3582
3
+ size 381925
myspeechasr.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """LibrispeechASR.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1FrAIWodQp3jV9v_u_NSYIfzB-q1LRTi8
8
+ """
9
+
10
+ #!pip install datasets
11
+
12
+ #!pip install transformers
13
+
14
+ # imports
15
+
16
+ from transformers import TrainingArguments, Trainer, pipeline, AutoTokenizer, AutoConfig, AutoModelForSequenceClassification, HfArgumentParser, EvalPrediction
17
+ from datasets import list_datasets, load_dataset, Dataset
18
+ from pprint import pprint
19
+ import torch
20
+ from datasets.tasks import AutomaticSpeechRecognition
21
+
22
+ #from __future__ import absolute_import, division, print_function
23
+
24
+ import glob
25
+ import os
26
+
27
+ import datasets
28
+
29
+ _CITATION = """\
30
+ @inproceedings{panayotov2015librispeech,
31
+ title={Librispeech: an ASR corpus based on public domain audio books},
32
+ author={Panayotov, Vassil and Chen, Guoguo and Povey, Daniel and Khudanpur, Sanjeev},
33
+ booktitle={Acoustics, Speech and Signal Processing (ICASSP), 2015 IEEE International Conference on},
34
+ pages={5206--5210},
35
+ year={2015},
36
+ organization={IEEE}
37
+ }
38
+ """
39
+
40
+ _DESCRIPTION = """\
41
+ LibriSpeech is a corpus of approximately 1000 hours of read English speech with sampling rate of 16 kHz,
42
+ prepared by Vassil Panayotov with the assistance of Daniel Povey. The data is derived from read
43
+ audiobooks from the LibriVox project, and has been carefully segmented and aligned.87
44
+ """
45
+
46
+
47
+ _URL = "http://www.openslr.org/12"
48
+ _DL_URL = "http://www.openslr.org/resources/12/"
49
+
50
+ _DL_URLS = {
51
+ "clean": {
52
+ "dev": _DL_URL + "dev-clean.tar.gz",
53
+ "test": _DL_URL + "test-clean.tar.gz",
54
+ "train.100": _DL_URL + "train-clean-100.tar.gz",
55
+ "train.360": _DL_URL + "train-clean-360.tar.gz",
56
+ },
57
+ "other": {
58
+ "test": _DL_URL + "test-other.tar.gz",
59
+ "dev": _DL_URL + "dev-other.tar.gz",
60
+ "train.500": _DL_URL + "train-other-500.tar.gz",
61
+ },
62
+ "all": {
63
+ "dev.clean": _DL_URL + "dev-clean.tar.gz",
64
+ "dev.other": _DL_URL + "dev-other.tar.gz",
65
+ "test.clean": _DL_URL + "test-clean.tar.gz",
66
+ "test.other": _DL_URL + "test-other.tar.gz",
67
+ "train.clean.100": _DL_URL + "train-clean-100.tar.gz",
68
+ "train.clean.360": _DL_URL + "train-clean-360.tar.gz",
69
+ "train.other.500": _DL_URL + "train-other-500.tar.gz",
70
+ },
71
+ }
72
+
73
+ class MyspeechASRConfig(datasets.BuilderConfig):
74
+
75
+ def __init__(self, **kwargs):
76
+ """
77
+ Args:
78
+ data_dir: `string`, the path to the folder containing the files in the
79
+ downloaded .tar
80
+ citation: `string`, citation for the data set
81
+ url: `string`, url for information about the data set
82
+ **kwargs: keyword arguments forwarded to super.
83
+ """
84
+ super(MyspeechASRConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
85
+
86
+ class MyspeechASR(datasets.GeneratorBasedBuilder):
87
+ """Librispeech dataset."""
88
+
89
+ DEFAULT_WRITER_BATCH_SIZE = 256
90
+ DEFAULT_CONFIG_NAME = "all"
91
+ BUILDER_CONFIGS = [
92
+ MyspeechASRConfig(name="clean", description="'Clean' speech."),
93
+ MyspeechASRConfig(name="other", description="'Other', more challenging, speech."),
94
+ MyspeechASRConfig(name="all", description="Combined clean and other dataset."),
95
+ ]
96
+
97
+ def _info(self):
98
+ return datasets.DatasetInfo(
99
+ description=_DESCRIPTION,
100
+ features=datasets.Features(
101
+ {
102
+ "file": datasets.Value("string"),
103
+ "audio": datasets.Audio(sampling_rate=16_000),
104
+ "text": datasets.Value("string"),
105
+ "speaker_id": datasets.Value("int64"),
106
+ "chapter_id": datasets.Value("int64"),
107
+ "id": datasets.Value("string"),
108
+ }
109
+ ),
110
+ supervised_keys=("file", "text"),
111
+ homepage=_URL,
112
+ citation=_CITATION,
113
+ task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
114
+ )
115
+
116
+ def _split_generators(self, dl_manager):
117
+ archive_path = dl_manager.download(_DL_URLS[self.config.name])
118
+ # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
119
+ local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
120
+
121
+ if self.config.name == "clean":
122
+ train_splits = [
123
+ datasets.SplitGenerator(
124
+ name="train.100",
125
+ gen_kwargs={
126
+ "local_extracted_archive": local_extracted_archive.get("train.100"),
127
+ "files": dl_manager.iter_archive(archive_path["train.100"]),
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name="train.360",
132
+ gen_kwargs={
133
+ "local_extracted_archive": local_extracted_archive.get("train.360"),
134
+ "files": dl_manager.iter_archive(archive_path["train.360"]),
135
+ },
136
+ ),
137
+ ]
138
+ dev_splits = [
139
+ datasets.SplitGenerator(
140
+ name=datasets.Split.VALIDATION,
141
+ gen_kwargs={
142
+ "local_extracted_archive": local_extracted_archive.get("dev"),
143
+ "files": dl_manager.iter_archive(archive_path["dev"]),
144
+ },
145
+ )
146
+ ]
147
+ test_splits = [
148
+ datasets.SplitGenerator(
149
+ name=datasets.Split.TEST,
150
+ gen_kwargs={
151
+ "local_extracted_archive": local_extracted_archive.get("test"),
152
+ "files": dl_manager.iter_archive(archive_path["test"]),
153
+ },
154
+ )
155
+ ]
156
+ elif self.config.name == "other":
157
+ train_splits = [
158
+ datasets.SplitGenerator(
159
+ name="train.500",
160
+ gen_kwargs={
161
+ "local_extracted_archive": local_extracted_archive.get("train.500"),
162
+ "files": dl_manager.iter_archive(archive_path["train.500"]),
163
+ },
164
+ )
165
+ ]
166
+ dev_splits = [
167
+ datasets.SplitGenerator(
168
+ name=datasets.Split.VALIDATION,
169
+ gen_kwargs={
170
+ "local_extracted_archive": local_extracted_archive.get("dev"),
171
+ "files": dl_manager.iter_archive(archive_path["dev"]),
172
+ },
173
+ )
174
+ ]
175
+ test_splits = [
176
+ datasets.SplitGenerator(
177
+ name=datasets.Split.TEST,
178
+ gen_kwargs={
179
+ "local_extracted_archive": local_extracted_archive.get("test"),
180
+ "files": dl_manager.iter_archive(archive_path["test"]),
181
+ },
182
+ )
183
+ ]
184
+ elif self.config.name == "all":
185
+ train_splits = [
186
+ datasets.SplitGenerator(
187
+ name="train.clean.100",
188
+ gen_kwargs={
189
+ "local_extracted_archive": local_extracted_archive.get("train.clean.100"),
190
+ "files": dl_manager.iter_archive(archive_path["train.clean.100"]),
191
+ },
192
+ ),
193
+ datasets.SplitGenerator(
194
+ name="train.clean.360",
195
+ gen_kwargs={
196
+ "local_extracted_archive": local_extracted_archive.get("train.clean.360"),
197
+ "files": dl_manager.iter_archive(archive_path["train.clean.360"]),
198
+ },
199
+ ),
200
+ datasets.SplitGenerator(
201
+ name="train.other.500",
202
+ gen_kwargs={
203
+ "local_extracted_archive": local_extracted_archive.get("train.other.500"),
204
+ "files": dl_manager.iter_archive(archive_path["train.other.500"]),
205
+ },
206
+ ),
207
+ ]
208
+ dev_splits = [
209
+ datasets.SplitGenerator(
210
+ name="validation.clean",
211
+ gen_kwargs={
212
+ "local_extracted_archive": local_extracted_archive.get("validation.clean"),
213
+ "files": dl_manager.iter_archive(archive_path["dev.clean"]),
214
+ },
215
+ ),
216
+ datasets.SplitGenerator(
217
+ name="validation.other",
218
+ gen_kwargs={
219
+ "local_extracted_archive": local_extracted_archive.get("validation.other"),
220
+ "files": dl_manager.iter_archive(archive_path["dev.other"]),
221
+ },
222
+ ),
223
+ ]
224
+ test_splits = [
225
+ datasets.SplitGenerator(
226
+ name="test.clean",
227
+ gen_kwargs={
228
+ "local_extracted_archive": local_extracted_archive.get("test.clean"),
229
+ "files": dl_manager.iter_archive(archive_path["test.clean"]),
230
+ },
231
+ ),
232
+ datasets.SplitGenerator(
233
+ name="test.other",
234
+ gen_kwargs={
235
+ "local_extracted_archive": local_extracted_archive.get("test.other"),
236
+ "files": dl_manager.iter_archive(archive_path["test.other"]),
237
+ },
238
+ ),
239
+ ]
240
+
241
+ return train_splits + dev_splits + test_splits
242
+
243
+ def _generate_examples(self, files, local_extracted_archive):
244
+ """Generate examples from a LibriSpeech archive_path."""
245
+ key = 0
246
+ audio_data = {}
247
+ transcripts = []
248
+ for path, f in files:
249
+ if path.endswith(".flac"):
250
+ id_ = path.split("/")[-1][: -len(".flac")]
251
+ audio_data[id_] = f.read()
252
+ elif path.endswith(".trans.txt"):
253
+ for line in f:
254
+ if line:
255
+ line = line.decode("utf-8").strip()
256
+ id_, transcript = line.split(" ", 1)
257
+ audio_file = f"{id_}.flac"
258
+ speaker_id, chapter_id = [int(el) for el in id_.split("-")[:2]]
259
+ audio_file = (
260
+ os.path.join(local_extracted_archive, audio_file)
261
+ if local_extracted_archive
262
+ else audio_file
263
+ )
264
+ transcripts.append(
265
+ {
266
+ "id": id_,
267
+ "speaker_id": speaker_id,
268
+ "chapter_id": chapter_id,
269
+ "file": audio_file,
270
+ "text": transcript,
271
+ }
272
+ )
273
+ if audio_data and len(audio_data) == len(transcripts):
274
+ for transcript in transcripts:
275
+ audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
276
+ yield key, {"audio": audio, **transcript}
277
+ key += 1
278
+ audio_data = {}
279
+ transcripts = []
280
+