pain commited on
Commit
cff6e0f
1 Parent(s): 8bf7391

Create MASC.py

Browse files
Files changed (1) hide show
  1. MASC.py +148 -0
MASC.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """ MASC Dataset"""
16
+
17
+ # This script has been adopted from this dataset: "mozilla-foundation/common_voice_11_0"
18
+
19
+ import csv
20
+ import os
21
+ import json
22
+
23
+ import datasets
24
+ from datasets.utils.py_utils import size_str
25
+ from tqdm import tqdm
26
+
27
+ _CITATION = """\
28
+ @INPROCEEDINGS{10022652,
29
+ author={Al-Fetyani, Mohammad and Al-Barham, Muhammad and Abandah, Gheith and Alsharkawi, Adham and Dawas, Maha},
30
+ booktitle={2022 IEEE Spoken Language Technology Workshop (SLT)},
31
+ title={MASC: Massive Arabic Speech Corpus},
32
+ year={2023},
33
+ volume={},
34
+ number={},
35
+ pages={1006-1013},
36
+ doi={10.1109/SLT54892.2023.10022652}}
37
+ }
38
+ """
39
+
40
+ # TODO: Add description of the dataset here
41
+ # You can copy an official description
42
+ _DESCRIPTION = """\
43
+ MASC is a dataset that contains 1,000 hours of speech sampled at 16 kHz and crawled from over 700 YouTube channels. The dataset is multi-regional, multi-genre, and multi-dialect intended to advance the research and development of Arabic speech technology with a special emphasis on Arabic speech recognition.
44
+ """
45
+
46
+ _HOMEPAGE = "https://ieee-dataport.org/open-access/masc-massive-arabic-speech-corpus"
47
+ _LICENSE = "https://creativecommons.org/licenses/by/4.0/"
48
+ _BASE_URL = "https://huggingface.co/datasets/pain/MASC/resolve/main/"
49
+ _AUDIO_URL1 = _BASE_URL + "audio/{split}/{split}_{shard_idx}.tar.gz"
50
+ _AUDIO_URL2 = _BASE_URL + "audio/{split}/{split}_{shard_idx}.tar.xz"
51
+ _TRANSCRIPT_URL = _BASE_URL + "transcript/{split}/{split}.csv"
52
+
53
+ class MASC(datasets.GeneratorBasedBuilder):
54
+
55
+ VERSION = datasets.Version("1.0.0")
56
+
57
+ def _info(self):
58
+
59
+ features = datasets.Features(
60
+ {
61
+ "video_id": datasets.Value("string"),
62
+ "start": datasets.Value("float64"),
63
+ "end": datasets.Value("float64"),
64
+ "duration": datasets.Value("float64"),
65
+ "text": datasets.Value("string"),
66
+ "type": datasets.Value("string"),
67
+ "file_path": datasets.Value("string"),
68
+ "audio": datasets.features.Audio(sampling_rate=16_000),
69
+ }
70
+ )
71
+
72
+ return datasets.DatasetInfo(
73
+ description=_DESCRIPTION,
74
+ features=features,
75
+ supervised_keys=None,
76
+ homepage=_HOMEPAGE,
77
+ license=_LICENSE,
78
+ citation=_CITATION,
79
+ version=self.config.version,
80
+ )
81
+
82
+ def _split_generators(self, dl_manager):
83
+
84
+ n_shards = {"train": 8,"dev": 1, "test": 1}
85
+ audio_urls = {}
86
+ splits = ("train", "dev", "test")
87
+
88
+ for split in splits:
89
+ audio_urls[split] = [
90
+ _AUDIO_URL2.format(split=split, shard_idx="{:02d}".format(i+1)) if split=="train" else _AUDIO_URL1.format(split=split, shard_idx="{:02d}".format(i+1)) for i in range(n_shards[split])
91
+ ]
92
+ archive_paths = dl_manager.download(audio_urls)
93
+ local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
94
+
95
+ meta_urls = {split: _TRANSCRIPT_URL.format(split=split) for split in splits}
96
+
97
+ meta_paths = dl_manager.download(meta_urls)
98
+
99
+ split_generators = []
100
+ split_names = {
101
+ "train": datasets.Split.TRAIN,
102
+ "dev": datasets.Split.VALIDATION,
103
+ "test": datasets.Split.TEST,
104
+ }
105
+ for split in splits:
106
+ split_generators.append(
107
+ datasets.SplitGenerator(
108
+ name=split_names.get(split, split),
109
+ gen_kwargs={
110
+ "local_extracted_archive_paths": local_extracted_archive_paths.get(split),
111
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(split)],
112
+ "meta_path": meta_paths[split],
113
+ },
114
+ ),
115
+ )
116
+
117
+ return split_generators
118
+
119
+ def _generate_examples(self, local_extracted_archive_paths, archives, meta_path):
120
+ data_fields = list(self._info().features.keys())
121
+ metadata = {}
122
+ with open(meta_path, encoding="utf-8") as f:
123
+ reader = csv.DictReader(f, delimiter=",", quoting=csv.QUOTE_NONE)
124
+ for row in reader:
125
+ if not row["file_path"].endswith(".wav"):
126
+ row["file_path"] += ".wav"
127
+ for field in data_fields:
128
+ if field not in row:
129
+ row[field] = ""
130
+ metadata[row["file_path"]] = row
131
+
132
+ for i, audio_archive in enumerate(archives):
133
+ for filename, file in audio_archive:
134
+ _, filename = os.path.split(filename)
135
+ if filename in metadata:
136
+ result = dict(metadata[filename])
137
+ # set the audio feature and the path to the extracted file
138
+ path = os.path.join(local_extracted_archive_paths[i], filename) if local_extracted_archive_paths else filename
139
+
140
+ try:
141
+ result["audio"] = {"path": path, "bytes": file.read()}
142
+ except ReadError as e:
143
+ # Handle the ReadError
144
+ print("An error occurred while reading the data:", str(e))
145
+ continiue
146
+ # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
147
+ result["file_path"] = path if local_extracted_archive_paths else filename
148
+ yield path, result