javigonzalezcorbelle commited on
Commit
3b21504
1 Parent(s): 1287860

test common voice script

Browse files
Files changed (2) hide show
  1. common_voice_11_0.py +184 -0
  2. parlament_parla.py +0 -116
common_voice_11_0.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """ Common Voice Dataset"""
16
+
17
+
18
+ import csv
19
+ import os
20
+ import json
21
+
22
+ import datasets
23
+ from datasets.utils.py_utils import size_str
24
+ from tqdm import tqdm
25
+
26
+ from .languages import LANGUAGES
27
+
28
+
29
+
30
+ _CITATION = """\
31
+ @inproceedings{commonvoice:2020,
32
+ author = {Ardila, R. and Branson, M. and Davis, K. and Henretty, M. and Kohler, M. and Meyer, J. and Morais, R. and Saunders, L. and Tyers, F. M. and Weber, G.},
33
+ title = {Common Voice: A Massively-Multilingual Speech Corpus},
34
+ booktitle = {Proceedings of the 12th Conference on Language Resources and Evaluation (LREC 2020)},
35
+ pages = {4211--4215},
36
+ year = 2020
37
+ }
38
+ """
39
+
40
+ _HOMEPAGE = ""
41
+
42
+ _LICENSE = ""
43
+
44
+ # TODO: change "streaming" to "main" after merge!
45
+ _BASE_URL = "https://huggingface.co/datasets/gcjavi/parlaspeech-tests/tree/main/"
46
+
47
+ _AUDIO_URL = _BASE_URL + "audio/{lang}/{split}/{lang}_{split}.tar"
48
+
49
+ _TRANSCRIPT_URL = _BASE_URL + "data/{lang}/{split}/{split}/{split}_{lang}.tsv"
50
+
51
+ #_N_SHARDS_URL = _BASE_URL + "n_shards.json"
52
+
53
+
54
+ class CommonVoiceConfig(datasets.BuilderConfig):
55
+ """BuilderConfig for CommonVoice."""
56
+
57
+ def __init__(self, name, version, **kwargs):
58
+ self.language = kwargs.pop("language", None)
59
+ self.release_date = kwargs.pop("release_date", None)
60
+ self.num_clips = kwargs.pop("num_clips", None)
61
+ self.num_speakers = kwargs.pop("num_speakers", None)
62
+ self.validated_hr = kwargs.pop("validated_hr", None)
63
+ self.total_hr = kwargs.pop("total_hr", None)
64
+ self.size_bytes = kwargs.pop("size_bytes", None)
65
+ self.size_human = size_str(self.size_bytes)
66
+ description = (
67
+ f"Common Voice speech to text dataset in {self.language} released on {self.release_date}. "
68
+ f"The dataset comprises {self.validated_hr} hours of validated transcribed speech data "
69
+ f"out of {self.total_hr} hours in total from {self.num_speakers} speakers. "
70
+ f"The dataset contains {self.num_clips} audio clips and has a size of {self.size_human}."
71
+ )
72
+ super(CommonVoiceConfig, self).__init__(
73
+ name=name,
74
+ version=datasets.Version(version),
75
+ description=description,
76
+ **kwargs,
77
+ )
78
+
79
+
80
+ class CommonVoice(datasets.GeneratorBasedBuilder):
81
+ DEFAULT_WRITER_BATCH_SIZE = 1000
82
+
83
+ BUILDER_CONFIGS = [
84
+ CommonVoiceConfig(
85
+ name=lang,
86
+ version="1.0",
87
+ language=lang,
88
+ )
89
+ for lang in ["clean", "other"]
90
+ ]
91
+
92
+ def _info(self):
93
+ total_languages = 2
94
+ description = (
95
+ "Common Voice is Mozilla's initiative to help teach machines how real people speak. "
96
+ f"The dataset currently consists of {total_valid_hours} validated hours of speech "
97
+ f" in {total_languages} languages, but more voices and languages are always added."
98
+ )
99
+ features = datasets.Features(
100
+ {
101
+ "speaker_id": datasets.Value("string"),
102
+ "path": datasets.Value("string"),
103
+ #"audio": datasets.features.Audio(sampling_rate=48_000),
104
+ "sentence": datasets.Value("string"),
105
+ "gender": datasets.Value("string"),
106
+ }
107
+ )
108
+
109
+ return datasets.DatasetInfo(
110
+ description=description,
111
+ features=features,
112
+ supervised_keys=None,
113
+ homepage=_HOMEPAGE,
114
+ license=_LICENSE,
115
+ citation=_CITATION,
116
+ version=self.config.version,
117
+ )
118
+
119
+ def _split_generators(self, dl_manager):
120
+ lang = self.config.name
121
+ #n_shards_path = dl_manager.download_and_extract(_N_SHARDS_URL)
122
+ #with open(n_shards_path, encoding="utf-8") as f:
123
+ #n_shards = json.load(f)
124
+
125
+ audio_urls = {}
126
+ splits = ("train", "dev", "test")
127
+ #for split in splits:
128
+ #audio_urls[split] = [
129
+ #_AUDIO_URL.format(lang=lang, split=split, shard_idx=i) for i in range(n_shards[lang][split])
130
+ #]
131
+ #archive_paths = dl_manager.download(audio_urls)
132
+ #local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
133
+ local_extracted_archive_paths = []
134
+ meta_urls = {split: _TRANSCRIPT_URL.format(lang=lang, split=split) for split in splits}
135
+ meta_paths = dl_manager.download_and_extract(meta_urls)
136
+
137
+ split_generators = []
138
+ split_names = {
139
+ "train": datasets.Split.TRAIN,
140
+ "dev": datasets.Split.VALIDATION,
141
+ "test": datasets.Split.TEST,
142
+ }
143
+ for split in splits:
144
+ split_generators.append(
145
+ datasets.SplitGenerator(
146
+ name=split_names.get(split, split),
147
+ gen_kwargs={
148
+ "local_extracted_archive_paths": local_extracted_archive_paths.get(split),
149
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(split)],
150
+ "meta_path": meta_paths[split],
151
+ },
152
+ ),
153
+ )
154
+
155
+ return split_generators
156
+
157
+ def _generate_examples(self, local_extracted_archive_paths, archives, meta_path):
158
+ data_fields = list(self._info().features.keys())
159
+ metadata = {}
160
+ with open(meta_path, encoding="utf-8") as f:
161
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
162
+ for row in tqdm(reader, desc="Reading metadata..."):
163
+ if not row["path"].endswith(".mp3"):
164
+ row["path"] += ".mp3"
165
+ # accent -> accents in CV 8.0
166
+ if "accents" in row:
167
+ row["accent"] = row["accents"]
168
+ del row["accents"]
169
+ # if data is incomplete, fill with empty values
170
+ for field in data_fields:
171
+ if field not in row:
172
+ row[field] = ""
173
+ metadata[row["path"]] = row
174
+
175
+ for i, audio_archive in enumerate(archives):
176
+ for path, file in audio_archive:
177
+ _, filename = os.path.split(path)
178
+ if filename in metadata:
179
+ result = dict(metadata[filename])
180
+ # set the audio feature and the path to the extracted file
181
+ path = os.path.join(local_extracted_archive_paths[i], path) if local_extracted_archive_paths else path
182
+ result["audio"] = {"path": path, "bytes": file.read()}
183
+ result["path"] = path
184
+ yield path, result
parlament_parla.py DELETED
@@ -1,116 +0,0 @@
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
- """ParlamentParla - Speech corpus of Catalan Parliamentary sessions."""
16
-
17
- import pandas as pd
18
-
19
- import datasets
20
- from datasets.tasks import AutomaticSpeechRecognition
21
-
22
-
23
- _CITATION = """\
24
- @dataset{kulebi_baybars_2021_5541827,
25
- author = {Külebi, Baybars},
26
- title = {{ParlamentParla - Speech corpus of Catalan
27
- Parliamentary sessions}},
28
- month = oct,
29
- year = 2021,
30
- publisher = {Zenodo},
31
- version = {v2.0},
32
- doi = {10.5281/zenodo.5541827},
33
- url = {https://doi.org/10.5281/zenodo.5541827}
34
- }
35
- """
36
-
37
- _DESCRIPTION = """\
38
- This is the ParlamentParla speech corpus for Catalan prepared by Col·lectivaT. The audio segments were extracted from recordings the Catalan Parliament (Parlament de Catalunya) plenary sessions, which took place between 2007/07/11 - 2018/07/17. We aligned the transcriptions with the recordings and extracted the corpus. The content belongs to the Catalan Parliament and the data is released conforming their terms of use.
39
-
40
- Preparation of this corpus was partly supported by the Department of Culture of the Catalan autonomous government, and the v2.0 was supported by the Barcelona Supercomputing Center, within the framework of the project AINA of the Departament de Polítiques Digitals.
41
-
42
- As of v2.0 the corpus is separated into 211 hours of clean and 400 hours of other quality segments. Furthermore, each speech segment is tagged with its speaker and each speaker with their gender. The statistics are detailed in the readme file.
43
-
44
- For more information, go to https://github.com/CollectivaT-dev/ParlamentParla or mail info@collectivat.cat.
45
- """
46
-
47
- _HOMEPAGE = "https://huggingface.co/datasets/gcjavi/parlaspeech-tests/tree/main"
48
-
49
- _LICENSE = "Creative Commons Attribution 4.0 International"
50
-
51
- _INDEX_REPO = "https://huggingface.co/datasets/gcjavi/parlaspeech-tests/tree/main/"
52
- _URLS = {
53
- "index": _INDEX_REPO + "data/{config}/{split}/{split}_{config}.tsv",
54
- "audio": "",
55
- }
56
-
57
- _SPLITS = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "dev", datasets.Split.TEST: "test"}
58
-
59
-
60
- class ParlamentParla(datasets.GeneratorBasedBuilder):
61
- """ParlamentParla."""
62
-
63
- VERSION = datasets.Version("2.7.1")
64
-
65
- BUILDER_CONFIGS = [
66
- datasets.BuilderConfig(name="clean", version=VERSION, description="211 hours of clean quality segments."),
67
- datasets.BuilderConfig(name="other", version=VERSION, description="400 hours of other quality segments."),
68
- ]
69
-
70
- def _info(self):
71
- return datasets.DatasetInfo(
72
- description=_DESCRIPTION,
73
- features=datasets.Features(
74
- {
75
- "path": datasets.Value("string"),
76
- "audio": datasets.features.Audio(),
77
- "speaker_id": datasets.Value("string"),
78
- "sentence": datasets.Value("string"),
79
- "gender": datasets.Value("string"),
80
- }
81
- ),
82
- supervised_keys=None,
83
- homepage=_HOMEPAGE,
84
- license=_LICENSE,
85
- citation=_CITATION,
86
- task_templates=[
87
- AutomaticSpeechRecognition(transcription_column="sentence")
88
- ],
89
- )
90
-
91
- def _split_generators(self, dl_manager):
92
- urls = {
93
- split: {key: url.format(config=self.config.name, split=_SPLITS[split]) for key, url in _URLS.items()}
94
- for split in _SPLITS
95
- }
96
- dl_dir = dl_manager.download(urls)
97
- return [
98
- datasets.SplitGenerator(
99
- name=split,
100
- gen_kwargs={
101
- "index_path": dl_dir[split]["index"],
102
- "audio_files": dl_manager.iter_archive(dl_dir[split]["audio"]),
103
- },
104
- )
105
- for split in _SPLITS
106
- ]
107
-
108
- def _generate_examples(self, index_path, audio_files):
109
- with open(index_path, encoding="utf-8") as index_file:
110
- index = pd.read_csv(index_file, delimiter="\t", index_col="path").to_dict(orient="index")
111
- # clean: 83568 = 79269 + 2155 + 2144 ; other: 146669 = 142813 + 1957 + 1899
112
- for key, (path, file) in enumerate(audio_files):
113
- if path.endswith(".wav"):
114
- data = index.pop(path)
115
- audio = {"path": path, "bytes": file.read()}
116
- yield key, {"path": path, "audio": audio, **data}