Seosnaps commited on
Commit
dc1a82e
1 Parent(s): b7bff19

Update hausa_2_eng_2.py

Browse files
Files changed (1) hide show
  1. hausa_2_eng_2.py +185 -0
hausa_2_eng_2.py CHANGED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ import csv
5
+ import os
6
+ import json
7
+
8
+ import datasets
9
+ from datasets.utils.py_utils import size_str
10
+ from tqdm import tqdm
11
+
12
+ from .languages import LANGUAGES
13
+ from .release_stats import STATS
14
+
15
+
16
+ _CITATION = """\
17
+ @inproceedings{commonvoice:2020,
18
+ 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.},
19
+ title = {Common Voice: A Massively-Multilingual Speech Corpus},
20
+ booktitle = {Proceedings of the 12th Conference on Language Resources and Evaluation (LREC 2020)},
21
+ pages = {4211--4215},
22
+ year = 2020
23
+ }
24
+ """
25
+
26
+ _LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/"
27
+
28
+
29
+ _HOMEPAGE = "https://huggingface.co/datasets/Seon25/hausa_2_eng_2"
30
+
31
+ _BASE_URL = "https://huggingface.co/datasets/Seon25/hausa_2_eng_2/main"
32
+
33
+ _AUDIO_URL = _BASE_URL + "audio/{lang}/{split}/{lang}_{split}_{shard_idx}.tar"
34
+
35
+ _TRANSCRIPT_URL = _BASE_URL + "transcript/{lang}/{split}.tsv"
36
+
37
+ _N_SHARDS_URL = _BASE_URL + "n_shards.json"
38
+
39
+
40
+ class Hausa2EngConfig(datasets.BuilderConfig):
41
+ """BuilderConfig for CommonVoice."""
42
+
43
+ def __init__(self, name, version, **kwargs):
44
+ self.language = kwargs.pop("language", None)
45
+ self.release_date = kwargs.pop("release_date", None)
46
+ self.num_clips = kwargs.pop("num_clips", None)
47
+ self.num_speakers = kwargs.pop("num_speakers", None)
48
+ self.validated_hr = kwargs.pop("validated_hr", None)
49
+ self.total_hr = kwargs.pop("total_hr", None)
50
+ self.size_bytes = kwargs.pop("size_bytes", None)
51
+ self.size_human = size_str(self.size_bytes)
52
+ description = (
53
+ f"Common Voice speech to text dataset in {self.language} released on {self.release_date}. "
54
+ f"The dataset comprises {self.validated_hr} hours of validated transcribed speech data "
55
+ f"out of {self.total_hr} hours in total from {self.num_speakers} speakers. "
56
+ f"The dataset contains {self.num_clips} audio clips and has a size of {self.size_human}."
57
+ )
58
+ super(Hausa2EngConfig, self).__init__(
59
+ name=name,
60
+ version=datasets.Version(version),
61
+ description=description,
62
+ **kwargs,
63
+ )
64
+
65
+
66
+
67
+ class Hausa2Eng(datasets.GeneratorBasedBuilder):
68
+ DEFAULT_WRITER_BATCH_SIZE = 1000
69
+
70
+ BUILDER_CONFIGS = [
71
+ Hausa2EngConfig(
72
+ name=lang,
73
+ version=STATS["version"],
74
+ language=LANGUAGES[lang],
75
+ release_date=STATS["date"],
76
+ num_clips=lang_stats["clips"],
77
+ num_speakers=lang_stats["users"],
78
+ validated_hr=float(lang_stats["validHrs"]) if lang_stats["validHrs"] else None,
79
+ total_hr=float(lang_stats["totalHrs"]) if lang_stats["totalHrs"] else None,
80
+ size_bytes=int(lang_stats["size"]) if lang_stats["size"] else None,
81
+ )
82
+ for lang, lang_stats in STATS["locales"].items()
83
+ ]
84
+
85
+ def _info(self):
86
+ total_languages = len(STATS["locales"])
87
+ total_valid_hours = STATS["totalValidHrs"]
88
+ description = (
89
+ "Common Voice is Mozilla's initiative to help teach machines how real people speak. "
90
+ f"The dataset currently consists of {total_valid_hours} validated hours of speech "
91
+ f" in {total_languages} languages, but more voices and languages are always added."
92
+ )
93
+ features = datasets.Features(
94
+ {
95
+ "client_id": datasets.Value("string"),
96
+ "path": datasets.Value("string"),
97
+ "audio": datasets.features.Audio(sampling_rate=48_000),
98
+ "sentence": datasets.Value("string"),
99
+ "up_votes": datasets.Value("int64"),
100
+ "down_votes": datasets.Value("int64"),
101
+ "age": datasets.Value("string"),
102
+ "gender": datasets.Value("string"),
103
+ "accent": datasets.Value("string"),
104
+ "locale": datasets.Value("string"),
105
+ "segment": datasets.Value("string"),
106
+ "variant": datasets.Value("string"),
107
+ }
108
+ )
109
+
110
+ return datasets.DatasetInfo(
111
+ description=description,
112
+ features=features,
113
+ supervised_keys=None,
114
+ homepage=_HOMEPAGE,
115
+ license=_LICENSE,
116
+ citation=_CITATION,
117
+ version=self.config.version,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+ lang = self.config.name
122
+ n_shards_path = dl_manager.download_and_extract(_N_SHARDS_URL)
123
+ with open(n_shards_path, encoding="utf-8") as f:
124
+ n_shards = json.load(f)
125
+
126
+ audio_urls = {}
127
+ splits = ("train", "dev", "test", "other", "invalidated")
128
+ for split in splits:
129
+ audio_urls[split] = [
130
+ _AUDIO_URL.format(lang=lang, split=split, shard_idx=i) for i in range(n_shards[lang][split])
131
+ ]
132
+ archive_paths = dl_manager.download(audio_urls)
133
+ local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
134
+
135
+ meta_urls = {split: _TRANSCRIPT_URL.format(lang=lang, split=split) for split in splits}
136
+ meta_paths = dl_manager.download_and_extract(meta_urls)
137
+
138
+ split_generators = []
139
+ split_names = {
140
+ "train": datasets.Split.TRAIN,
141
+ "dev": datasets.Split.VALIDATION,
142
+ "test": datasets.Split.TEST,
143
+ }
144
+ for split in splits:
145
+ split_generators.append(
146
+ datasets.SplitGenerator(
147
+ name=split_names.get(split, split),
148
+ gen_kwargs={
149
+ "local_extracted_archive_paths": local_extracted_archive_paths.get(split),
150
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(split)],
151
+ "meta_path": meta_paths[split],
152
+ },
153
+ ),
154
+ )
155
+
156
+ return split_generators
157
+
158
+ def _generate_examples(self, local_extracted_archive_paths, archives, meta_path):
159
+ data_fields = list(self._info().features.keys())
160
+ metadata = {}
161
+ with open(meta_path, encoding="utf-8") as f:
162
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
163
+ for row in tqdm(reader, desc="Reading metadata..."):
164
+ if not row["path"].endswith(".mp3"):
165
+ row["path"] += ".mp3"
166
+ # accent -> accents in CV 8.0
167
+ if "accents" in row:
168
+ row["accent"] = row["accents"]
169
+ del row["accents"]
170
+ # if data is incomplete, fill with empty values
171
+ for field in data_fields:
172
+ if field not in row:
173
+ row[field] = ""
174
+ metadata[row["path"]] = row
175
+
176
+ for i, audio_archive in enumerate(archives):
177
+ for path, file in audio_archive:
178
+ _, filename = os.path.split(path)
179
+ if filename in metadata:
180
+ result = dict(metadata[filename])
181
+ # set the audio feature and the path to the extracted file
182
+ path = os.path.join(local_extracted_archive_paths[i], path) if local_extracted_archive_paths else path
183
+ result["audio"] = {"path": path, "bytes": file.read()}
184
+ result["path"] = path
185
+ yield path, result