htdung167 commited on
Commit
f78d7e0
1 Parent(s): 9d6e57d

Upload vivos-preprocessed.py

Browse files
Files changed (1) hide show
  1. vivos-preprocessed.py +158 -0
vivos-preprocessed.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+ import datasets
17
+
18
+
19
+ _CITATION = """\
20
+ @inproceedings{luong-vu-2016-non,
21
+ title = "A non-expert {K}aldi recipe for {V}ietnamese Speech Recognition System",
22
+ author = "Luong, Hieu-Thi and
23
+ Vu, Hai-Quan",
24
+ booktitle = "Proceedings of the Third International Workshop on Worldwide Language Service Infrastructure and Second Workshop on Open Infrastructures and Analysis Frameworks for Human Language Technologies ({WLSI}/{OIAF}4{HLT}2016)",
25
+ month = dec,
26
+ year = "2016",
27
+ address = "Osaka, Japan",
28
+ publisher = "The COLING 2016 Organizing Committee",
29
+ url = "https://aclanthology.org/W16-5207",
30
+ pages = "51--55",
31
+ }
32
+ """
33
+
34
+ _DESCRIPTION = """\
35
+ VIVOS is a free Vietnamese speech corpus consisting of 15 hours of recording speech prepared for
36
+ Vietnamese Automatic Speech Recognition task.
37
+ The corpus was prepared by AILAB, a computer science lab of VNUHCM - University of Science, with Prof. Vu Hai Quan is the head of.
38
+ We publish this corpus in hope to attract more scientists to solve Vietnamese speech recognition problems.
39
+ """
40
+
41
+ _HOMEPAGE = "https://doi.org/10.5281/zenodo.7068130"
42
+
43
+ _LICENSE = "CC BY-NC-SA 4.0"
44
+
45
+ # Source data: "https://zenodo.org/record/7068130/files/vivos.tar.gz"
46
+ _DATA_URL = "data/vivos.tar.gz"
47
+
48
+ _PROMPTS_URLS = {
49
+ "train": "data/prompts-train.txt.gz",
50
+ "preprocessed_train": "data/preprocessed-prompts-train.txt.gz",
51
+ "test": "data/prompts-test.txt.gz",
52
+ "preprocessed_test": "data/preprocessed-prompts-test.txt.gz",
53
+
54
+ }
55
+
56
+
57
+ class VivosPreprocessedDataset(datasets.GeneratorBasedBuilder):
58
+ """VIVOS is a free Vietnamese speech corpus consisting of 15 hours of recording speech prepared for
59
+ Vietnamese Automatic Speech Recognition task."""
60
+
61
+ VERSION = datasets.Version("1.0.0")
62
+
63
+ # This is an example of a dataset with multiple configurations.
64
+ # If you don't want/need to define several sub-sets in your dataset,
65
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
66
+
67
+ # If you need to make complex sub-parts in the datasets with configurable options
68
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
69
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
70
+
71
+ def _info(self):
72
+ return datasets.DatasetInfo(
73
+ # This is the description that will appear on the datasets page.
74
+ description=_DESCRIPTION,
75
+ features=datasets.Features(
76
+ {
77
+ "path": datasets.Value("string"),
78
+ "audio": datasets.Audio(sampling_rate=16_000),
79
+ "original_sentence": datasets.Value("string"),
80
+ "preprocessed_sentence": datasets.Value("string"),
81
+ }
82
+ ),
83
+ supervised_keys=None,
84
+ homepage=_HOMEPAGE,
85
+ license=_LICENSE,
86
+ citation=_CITATION,
87
+ )
88
+
89
+ def _split_generators(self, dl_manager):
90
+ """Returns SplitGenerators."""
91
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
92
+
93
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
94
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
95
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
96
+ prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS)
97
+ archive = dl_manager.download(_DATA_URL)
98
+ train_dir = "vivos/train"
99
+ test_dir = "vivos/test"
100
+
101
+ return [
102
+ datasets.SplitGenerator(
103
+ name=datasets.Split.TRAIN,
104
+ # These kwargs will be passed to _generate_examples
105
+ gen_kwargs={
106
+ "prompts_path": prompts_paths["train"],
107
+ "preprocessed_prompts_path": prompts_paths["preprocessed_train"],
108
+ "path_to_clips": train_dir + "/waves",
109
+ "audio_files": dl_manager.iter_archive(archive),
110
+ },
111
+ ),
112
+ datasets.SplitGenerator(
113
+ name=datasets.Split.TEST,
114
+ # These kwargs will be passed to _generate_examples
115
+ gen_kwargs={
116
+ "prompts_path": prompts_paths["test"],
117
+ "preprocessed_prompts_path": prompts_paths["preprocessed_test"],
118
+ "path_to_clips": test_dir + "/waves",
119
+ "audio_files": dl_manager.iter_archive(archive),
120
+ },
121
+ ),
122
+ ]
123
+
124
+ def _generate_examples(self, prompts_path, preprocessed_prompts_path, path_to_clips, audio_files):
125
+ """Yields examples as (key, example) tuples."""
126
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
127
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
128
+ examples = {}
129
+
130
+ prep_prompts = {}
131
+ with open(preprocessed_prompts_path, encoding="utf-8") as f:
132
+ for row in f:
133
+ if row.strip() == "":
134
+ continue
135
+ data = row.strip().split(" ", 1)
136
+ prep_prompts[data[0]] = data[1].strip()
137
+
138
+ with open(prompts_path, encoding="utf-8") as f:
139
+ for row in f:
140
+ data = row.strip().split(" ", 1)
141
+ speaker_id = data[0].split("_")[0]
142
+ audio_path = "/".join([path_to_clips, speaker_id, data[0] + ".wav"])
143
+ examples[audio_path] = {
144
+ "path": audio_path,
145
+ "original_sentence": data[1],
146
+ "preprocessed_sentence": prep_prompts[data[0]]
147
+ }
148
+ inside_clips_dir = False
149
+ id_ = 0
150
+ for path, f in audio_files:
151
+ if path.startswith(path_to_clips):
152
+ inside_clips_dir = True
153
+ if path in examples:
154
+ audio = {"path": path, "bytes": f.read()}
155
+ yield id_, {**examples[path], "audio": audio}
156
+ id_ += 1
157
+ elif inside_clips_dir:
158
+ break