holylovenia commited on
Commit
aa63843
1 Parent(s): 28f1068

Upload cvss.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. cvss.py +249 -0
cvss.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 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
+ """
17
+ This template serves as a starting point for contributing a dataset to the Nusantara Dataset repo.
18
+ When modifying it for your dataset, look for TODO items that offer specific instructions.
19
+ Full documentation on writing dataset loading scripts can be found here:
20
+ https://huggingface.co/docs/datasets/add_dataset.html
21
+ To create a dataset loading script you will create a class and implement 3 methods:
22
+ * `_info`: Establishes the schema for the dataset, and returns a datasets.DatasetInfo object.
23
+ * `_split_generators`: Downloads and extracts data for each split (e.g. train/val/test) or associate local data with each split.
24
+ * `_generate_examples`: Creates examples from data on disk that conform to each schema defined in `_info`.
25
+ TODO: Before submitting your script, delete this doc string and replace it with a description of your dataset.
26
+ [nusantara_schema_name] = (kb, pairs, qa, text, t2t, entailment)
27
+ """
28
+ import csv
29
+ import os
30
+ from pathlib import Path
31
+ from typing import Dict, List, Tuple
32
+
33
+ import datasets
34
+ import pandas as pd
35
+
36
+ from nusacrowd.utils import schemas
37
+ from nusacrowd.utils.configs import NusantaraConfig
38
+ from nusacrowd.utils.constants import Tasks
39
+
40
+ _CITATION = """\
41
+ @inproceedings{jia2022cvss,
42
+ title={{CVSS} Corpus and Massively Multilingual Speech-to-Speech Translation},
43
+ author={Jia, Ye and Tadmor Ramanovich, Michelle and Wang, Quan and Zen, Heiga},
44
+ booktitle={Proceedings of Language Resources and Evaluation Conference (LREC)},
45
+ pages={6691--6703},
46
+ year={2022}
47
+ }
48
+ """
49
+ _DATASETNAME = "cvss"
50
+
51
+ _DESCRIPTION = """\
52
+ CVSS is a massively multilingual-to-English speech-to-speech translation corpus,
53
+ covering sentence-level parallel speech-to-speech translation pairs from 21
54
+ languages into English.
55
+ """
56
+
57
+ _HOMEPAGE = "https://github.com/google-research-datasets/cvss"
58
+ _LOCAL = False
59
+ _LANGUAGES = ["ind", "eng"]
60
+ _LANG_CODE = {"ind": "id", "eng": "en"}
61
+ _LICENSE = "CC-BY 4.0"
62
+
63
+ _URLS = {_DATASETNAME: "https://storage.googleapis.com/cvss"}
64
+ _SUPPORTED_TASKS = [
65
+ Tasks.SPEECH_TO_SPEECH_TRANSLATION] # example: [Tasks.TRANSLATION, Tasks.NAMED_ENTITY_RECOGNITION, Tasks.RELATION_EXTRACTION]
66
+
67
+ _SOURCE_VERSION = "1.0.0"
68
+ _NUSANTARA_VERSION = "1.0.0"
69
+
70
+
71
+ class CVSS(datasets.GeneratorBasedBuilder):
72
+ """
73
+ CVSS is a dataset on speech-to-speech translation. The data available are Indonesian audio files
74
+ and their English transcriptions. There are two versions of the datasets, both derived from CoVoST 2,
75
+ with each version providing unique values:
76
+ CVSS-C: All the translation speeches are in a single canonical speaker's voice. Despite being synthetic, these
77
+ speeches are of very high naturalness and cleanness, as well as having a consistent speaking style. These properties
78
+ ease the modeling of the target speech and enable models to produce high quality translation speech suitable for
79
+ user-facing applications.
80
+ CVSS-T: The translation speeches are in voices transferred from the corresponding source speeches. Each translation
81
+ pair has similar voices on the two sides despite being in different languages, making this dataset suitable for building models that preserve speakers' voices when translating speech into different languages.
82
+ Together with the source speeches originated from Common Voice, they make two multilingual speech-to-speech
83
+ translation datasets each with about 1,900 hours of speech.
84
+ In addition to translation speech, CVSS also provides normalized translation text matching the pronunciation in the translation speech (e.g. on numbers, currencies, acronyms, etc.), which can be used for both model training as well as standardizing evaluation.
85
+ """
86
+
87
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
88
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
89
+
90
+ cv_URL_TEMPLATE = "https://voice-prod-bundler-ee1969a6ce8178826482b88e843c335139bd3fb4.s3.amazonaws.com/cv-corpus-6.1-2020-12-11/{lang}.tar.gz"
91
+
92
+ BUILDER_CONFIGS = [
93
+ NusantaraConfig(
94
+ name="cvss_c_source",
95
+ version=SOURCE_VERSION,
96
+ description="CVSS source schema, all translation speeches are in a single canonical speaker's voice",
97
+ schema="source",
98
+ subset_id="cvss_c",
99
+ ),
100
+ NusantaraConfig(
101
+ name="cvss_t_source",
102
+ version=SOURCE_VERSION,
103
+ description="CVSS source schema, translation speeches are in voices transferred " "from the corresponding source speeches",
104
+ schema="source",
105
+ subset_id="cvss_t",
106
+ ),
107
+ NusantaraConfig(
108
+ name="cvss_c_nusantara_s2s",
109
+ version=NUSANTARA_VERSION,
110
+ description="CVSS Nusantara schema, all translation speeches are in a single canonical speaker's voice.",
111
+ schema="nusantara_s2s",
112
+ subset_id="cvss_c",
113
+ ),
114
+ NusantaraConfig(
115
+ name="cvss_t_nusantara_s2s",
116
+ version=NUSANTARA_VERSION,
117
+ description="CVSS Nusantara schema, translation speeches are in voices transferred " "from the corresponding source speeches",
118
+ schema="nusantara_s2s",
119
+ subset_id="cvss_t",
120
+ ),
121
+ ]
122
+
123
+ DEFAULT_CONFIG_NAME = "cvss_c_source"
124
+
125
+ def _get_download_urls(self, name="cvss_c", languages="id", version="1.0"):
126
+ return f"{_URLS[_DATASETNAME]}/{name}_v{version}/{name}_{languages}_en_v{version}.tar.gz"
127
+
128
+ def _info(self) -> datasets.DatasetInfo:
129
+ if self.config.schema == "source":
130
+ features = datasets.Features(
131
+ {
132
+ "id": datasets.Value("string"),
133
+ "file": datasets.Value("string"),
134
+ "audio": datasets.Audio(sampling_rate=24_000),
135
+ "text": datasets.Value("string"),
136
+ "original_file": datasets.Value("string"),
137
+ "original_audio": datasets.Audio(sampling_rate=48_000),
138
+ "original_text": datasets.Value("string"),
139
+ }
140
+ )
141
+ elif self.config.schema == "nusantara_s2s":
142
+ features = schemas.speech2speech_features
143
+
144
+ return datasets.DatasetInfo(
145
+ description=_DESCRIPTION,
146
+ features=features,
147
+ homepage=_HOMEPAGE,
148
+ license=_LICENSE,
149
+ citation=_CITATION,
150
+ )
151
+
152
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
153
+ urls = self._get_download_urls(self.config.name[:6])
154
+ data_dir = dl_manager.download_and_extract(urls)
155
+
156
+ cv_url = self.cv_URL_TEMPLATE.format(lang=_LANG_CODE[_LANGUAGES[0]])
157
+ cv_dir = dl_manager.download_and_extract(cv_url)
158
+ cv_dir = cv_dir + "/" + "/".join(["cv-corpus-6.1-2020-12-11", _LANG_CODE[_LANGUAGES[0]]])
159
+ cv_tsv_dir = os.path.join(cv_dir, "validated.tsv")
160
+
161
+ return [
162
+ datasets.SplitGenerator(
163
+ name=datasets.Split.TRAIN,
164
+ gen_kwargs={
165
+ "cvss_path": data_dir,
166
+ "cv_path": cv_dir,
167
+ "cv_tsv_path": cv_tsv_dir,
168
+ "split": "train",
169
+ },
170
+ ),
171
+ datasets.SplitGenerator(
172
+ name=datasets.Split.TEST,
173
+ gen_kwargs={
174
+ "cvss_path": data_dir,
175
+ "cv_path": cv_dir,
176
+ "cv_tsv_path": cv_tsv_dir,
177
+ "split": "test",
178
+ },
179
+ ),
180
+ datasets.SplitGenerator(
181
+ name=datasets.Split.VALIDATION,
182
+ gen_kwargs={
183
+ "cvss_path": data_dir,
184
+ "cv_path": cv_dir,
185
+ "cv_tsv_path": cv_tsv_dir,
186
+ "split": "dev",
187
+ },
188
+ ),
189
+ ]
190
+
191
+ def _generate_examples(self, cvss_path: Path, cv_path: Path, cv_tsv_path: Path, split: str) -> Tuple[int, Dict]:
192
+ # open cv tsv
193
+ cvss_tsv = self._load_df_from_tsv(os.path.join(cvss_path, f"{split}.tsv"))
194
+ cv_tsv = self._load_df_from_tsv(cv_tsv_path)
195
+ cvss_tsv.columns = ["path", "translation"]
196
+
197
+ df = pd.merge(
198
+ left=cv_tsv[["path", "sentence", "client_id"]],
199
+ right=cvss_tsv[["path", "translation"]],
200
+ how="inner",
201
+ on="path",
202
+ )
203
+ for id, row in df.iterrows():
204
+ translated_audio_path = os.path.join(cvss_path, split, f"{row['path']}.wav")
205
+ translated_text = row["translation"]
206
+ original_audio_path = os.path.join(cv_path, "clips", row["path"])
207
+ original_text = row["sentence"]
208
+ if self.config.schema == "source":
209
+ yield id, {
210
+ "id": id,
211
+ "audio": translated_audio_path,
212
+ "file": translated_audio_path,
213
+ "text": translated_text,
214
+ "original_audio": original_audio_path,
215
+ "original_file": original_audio_path,
216
+ "original_text": original_text
217
+ }
218
+ elif self.config.schema == "nusantara_s2s":
219
+ yield id, {
220
+ "id": id,
221
+ "path_1": original_audio_path,
222
+ "audio_1": original_audio_path,
223
+ "text_1": original_text,
224
+ "metadata_1": {
225
+ "name": "original_" + row["client_id"],
226
+ "speaker_age": None,
227
+ "speaker_gender": None,
228
+ },
229
+ "path_2": translated_audio_path,
230
+ "audio_2": translated_audio_path,
231
+ "text_2": translated_text,
232
+ "metadata_2": {
233
+ "name": 'translation',
234
+ "speaker_age": None,
235
+ "speaker_gender": None,
236
+ },
237
+ }
238
+
239
+ @staticmethod
240
+ def _load_df_from_tsv(path):
241
+ return pd.read_csv(
242
+ path,
243
+ sep="\t",
244
+ header=0,
245
+ encoding="utf-8",
246
+ escapechar="\\",
247
+ quoting=csv.QUOTE_NONE,
248
+ na_filter=False,
249
+ )