yuekai commited on
Commit
b6d87a1
·
verified ·
1 Parent(s): f51a9cc

Create speechio.py

Browse files
Files changed (1) hide show
  1. speechio.py +184 -0
speechio.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ Aishell dataset.
16
+ """
17
+
18
+ import os
19
+ import datasets
20
+ from huggingface_hub import list_repo_files
21
+ import gzip
22
+ import json
23
+
24
+ repo_id = "yuekai/speechio"
25
+
26
+ _DESCRIPTION = """\
27
+ speechio test set
28
+ """
29
+ _HOMEPAGE = "https://github.com/SpeechColab"
30
+
31
+ SPEECHIO_TESTSET_INDEX=26
32
+ _SUBSETS = []
33
+ for i in range(SPEECHIO_TESTSET_INDEX + 1):
34
+ idx = f"{i}".zfill(2)
35
+ _SUBSETS.append(f"SPEECHIO_ASR_ZH000{idx}")
36
+
37
+ _BASE_DATA_URL = f"https://huggingface.co/datasets/{repo_id}/resolve/main/"
38
+
39
+ _AUDIO_ARCHIVE_URL = _BASE_DATA_URL + "data/speechio_cuts_{subset}.{archive_id:08}.tar.gz"
40
+
41
+ _META_URL = _BASE_DATA_URL + "data/speechio_cuts_{subset}.{archive_id:08}.jsonl.gz"
42
+
43
+ FILES = list_repo_files(repo_id, repo_type="dataset")
44
+
45
+ logger = datasets.utils.logging.get_logger(__name__)
46
+
47
+
48
+ class CustomAudioConfig(datasets.BuilderConfig):
49
+ """BuilderConfig for the dataset."""
50
+
51
+ def __init__(self, name, *args, **kwargs):
52
+ """BuilderConfig for the dataset.
53
+ """
54
+ super().__init__(name=name, *args, **kwargs)
55
+ assert name in _SUBSETS, f"Unknown subset {name}"
56
+ self.subsets_to_download = (name,)
57
+
58
+
59
+ class SPEECHIO(datasets.GeneratorBasedBuilder):
60
+ """
61
+ Aishell is an evolving, multi-domain English speech recognition corpus with 10,000 hours of high quality
62
+ labeled audio suitable for supervised training, and 40,000 hours of total audio suitable for semi-supervised
63
+ and unsupervised training (this implementation contains only labelled data for now).
64
+ Around 40,000 hours of transcribed audio is first collected from audiobooks, podcasts
65
+ and YouTube, covering both read and spontaneous speaking styles, and a variety of topics, such as arts, science,
66
+ sports, etc. A new forced alignment and segmentation pipeline is proposed to create sentence segments suitable
67
+ for speech recognition training, and to filter out segments with low-quality transcription. For system training,
68
+ Aishell provides five subsets of different sizes, 10h, 250h, 1000h, 2500h, and 10000h.
69
+ For our 10,000-hour XL training subset, we cap the word error rate at 4% during the filtering/validation stage,
70
+ and for all our other smaller training subsets, we cap it at 0%. The DEV and TEST evaluation sets, on the other hand,
71
+ are re-processed by professional human transcribers to ensure high transcription quality.
72
+ """
73
+
74
+ VERSION = datasets.Version("1.0.0")
75
+
76
+ BUILDER_CONFIGS = [CustomAudioConfig(name=subset) for subset in _SUBSETS]
77
+
78
+ DEFAULT_WRITER_BATCH_SIZE = 128
79
+
80
+ def _info(self):
81
+ features = datasets.Features(
82
+ {
83
+ "segment_id": datasets.Value("string"),
84
+ "speaker": datasets.Value("string"),
85
+ "text": datasets.Value("string"),
86
+ "audio": datasets.Audio(sampling_rate=16_000),
87
+ "original_full_path": datasets.Value("string"), # relative path to full audio in original data dirs
88
+ }
89
+ )
90
+ return datasets.DatasetInfo(
91
+ description=_DESCRIPTION,
92
+ features=features,
93
+ homepage=_HOMEPAGE,
94
+ )
95
+
96
+ def _split_generators(self, dl_manager):
97
+
98
+ splits = (self.config.name,)
99
+
100
+
101
+ split_to_n_archives = {
102
+ split: int(len([file for file in FILES if f"cuts_{split}" in file]) / 2)
103
+ for split in splits
104
+ }
105
+
106
+ # 2. prepare sharded archives with audio files
107
+ audio_archives_urls = {
108
+ split:
109
+ [
110
+ _AUDIO_ARCHIVE_URL.format(subset=split,
111
+ archive_id=i)
112
+ for i in range(split_to_n_archives[split])
113
+ ]
114
+ for split in splits
115
+ }
116
+
117
+ audio_archives_paths = dl_manager.download(audio_archives_urls)
118
+
119
+ local_audio_archives_paths = dl_manager.extract(audio_archives_paths) if not dl_manager.is_streaming \
120
+ else None
121
+
122
+ # 3. prepare sharded metadata csv files
123
+ meta_urls = {
124
+ split: [
125
+ _META_URL.format(subset=split, archive_id=i)
126
+ for i in range(split_to_n_archives[split])
127
+ ]
128
+ for split in splits
129
+ }
130
+
131
+ meta_paths = dl_manager.download(meta_urls)
132
+
133
+
134
+ return [
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.TEST,
137
+ gen_kwargs={
138
+ "audio_archives_iterators": [
139
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths[self.config.name]
140
+ ],
141
+ "local_audio_archives_paths": local_audio_archives_paths[
142
+ self.config.name] if local_audio_archives_paths else None,
143
+ "meta_paths": meta_paths[self.config.name]
144
+ },
145
+ ),
146
+ ]
147
+
148
+ def _generate_examples(self, audio_archives_iterators, local_audio_archives_paths, meta_paths):
149
+
150
+ def load_meta(file_path):
151
+ data = {}
152
+
153
+ with gzip.open(file_path, 'rt', encoding='utf-8') as f:
154
+ for line in f:
155
+ item = json.loads(line)
156
+ data[item["id"]] = item
157
+ return data
158
+
159
+ assert len(audio_archives_iterators) == len(meta_paths)
160
+ if local_audio_archives_paths:
161
+ assert len(audio_archives_iterators) == len(local_audio_archives_paths)
162
+
163
+ for i, (meta_path, audio_archive_iterator) in enumerate(zip(meta_paths, audio_archives_iterators)):
164
+ meta_dict = load_meta(meta_path)
165
+
166
+ for audio_path_in_archive, audio_file in audio_archive_iterator:
167
+ # `audio_path_in_archive` is like "data/aishell_cuts_test.00000000/BAC/BAC009S0764W0393-359.wav"
168
+ audio_filename = os.path.split(audio_path_in_archive)[-1]
169
+
170
+ audio_id = audio_filename.split(".wav")[0]
171
+ audio_meta = meta_dict[audio_id]
172
+
173
+ audio_meta["segment_id"] = audio_id
174
+ audio_meta["original_full_path"] = audio_meta["recording"]["sources"][0]["source"]
175
+ audio_meta["text"] = audio_meta['supervisions'][0]['text']
176
+ audio_meta["speaker"] = audio_meta['supervisions'][0]['speaker']
177
+
178
+ path = os.path.join(local_audio_archives_paths[i], audio_path_in_archive) if local_audio_archives_paths \
179
+ else audio_path_in_archive
180
+
181
+ yield audio_id, {
182
+ "audio": {"path": path , "bytes": audio_file.read()},
183
+ **{feature: value for feature, value in audio_meta.items() if feature in self.info.features}
184
+ }