taqwa92 commited on
Commit
efb2aa0
1 Parent(s): 9e48ab4

Upload mg.trial4 (1).py

Browse files
Files changed (1) hide show
  1. mg.trial4 (1).py +187 -0
mg.trial4 (1).py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import datasets
3
+ import os
4
+
5
+
6
+ _DESCRIPTION = "MGB2 speech recognition dataset AR"
7
+ _HOMEPAGE = "https://arabicspeech.org/mgb2/"
8
+ _LICENSE = "MGB-2 License agreement"
9
+
10
+
11
+
12
+
13
+
14
+ _DATA_ARCHIVE_ROOT = "https://huggingface.co/datasets/taqwa92/mg.trial4/tree/main/audio/ar/test"
15
+ _DATA_ARCHIVE_ROOT1 ="https://huggingface.co/datasets/taqwa92/mg.trial4/tree/main/transcript/ar/test"
16
+
17
+ _DATA_URL = {
18
+ "test": _DATA_ARCHIVE_ROOT + "test.zip",
19
+
20
+ }
21
+
22
+ _TEXT_URL = {
23
+ "test": _DATA_ARCHIVE_ROOT1 + "test.tsv",
24
+
25
+
26
+ class MGDB2Dataset(datasets.GeneratorBasedBuilder):
27
+ def _info(self):
28
+ return datasets.DatasetInfo(
29
+ description=_DESCRIPTION,
30
+ features=datasets.Features(
31
+ {
32
+ "path": datasets.Value("string"),
33
+ "audio": datasets.Audio(sampling_rate=16_000),
34
+ "text": datasets.Value("string"),
35
+ }
36
+ ),
37
+ supervised_keys=None,
38
+ homepage=_HOMEPAGE,
39
+ license=_LICENSE,
40
+
41
+ )
42
+
43
+ def _split_generators(self, dl_manager):
44
+ wav_archive = dl_manager.download(_DATA_URL)
45
+ txt_archive = dl_manager.download(_TEXT_URL)
46
+ test_dir = "dataset/test"
47
+ dev_dir = "dataset/dev"
48
+ train_dir = "dataset/train"
49
+
50
+
51
+ print("Starting write datasets.........................................................")
52
+
53
+
54
+ if dl_manager.is_streaming:
55
+ print("from streaming.........................................................")
56
+
57
+
58
+
59
+
60
+ return [
61
+ datasets.SplitGenerator(
62
+ name=datasets.Split.TEST,
63
+ gen_kwargs={
64
+ "path_to_txt": test_dir + "/txt",
65
+ "path_to_wav": test_dir + "/wav",
66
+ "wav_files": dl_manager.iter_archive(wav_archive['test']),
67
+ "txt_files": dl_manager.iter_archive(txt_archive['test']),
68
+ },
69
+ ),
70
+ datasets.SplitGenerator(
71
+ name=datasets.Split.VALIDATION,
72
+ gen_kwargs={
73
+ "path_to_txt": dev_dir + "/txt",
74
+ "path_to_wav": dev_dir + "/wav",
75
+ "wav_files": dl_manager.iter_archive(wav_archive['dev']),
76
+ "txt_files": dl_manager.iter_archive(txt_archive['dev']),
77
+ },
78
+ ),
79
+ datasets.SplitGenerator(
80
+ name=datasets.Split.TRAIN,
81
+ gen_kwargs={
82
+ "path_to_txt": train_dir + "/txt",
83
+ "path_to_wav": train_dir + "/wav",
84
+ "wav_files": dl_manager.iter_archive(wav_archive['train']),
85
+ "txt_files": dl_manager.iter_archive(txt_archive['train']),
86
+ },
87
+ ),
88
+ ]
89
+ else:
90
+ print("from non streaming.........................................................")
91
+
92
+
93
+ test_txt_files=dl_manager.extract(txt_archive['test']);
94
+ print("txt file list .....................................",txt_archive['test'])
95
+
96
+
97
+ print("txt file names .....................................",test_txt_files)
98
+
99
+
100
+ return [
101
+ datasets.SplitGenerator(
102
+ name=datasets.Split.TEST,
103
+ gen_kwargs={
104
+ "path_to_txt": test_dir + "/txt",
105
+ "path_to_wav": test_dir + "/wav",
106
+ "wav_files": dl_manager.extract(wav_archive['test']),
107
+ "txt_files": test_txt_files,
108
+ },
109
+ ),
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.VALIDATION,
112
+ gen_kwargs={
113
+ "path_to_txt": dev_dir + "/txt",
114
+ "path_to_wav": dev_dir + "/wav",
115
+ "wav_files": dl_manager.extract(wav_archive['dev']),
116
+ "txt_files": dl_manager.extract(txt_archive['dev']),
117
+ },
118
+ ),
119
+ datasets.SplitGenerator(
120
+ name=datasets.Split.TRAIN,
121
+ gen_kwargs={
122
+ "path_to_txt": train_dir + "/txt",
123
+ "path_to_wav": train_dir + "/wav",
124
+ "wav_files": dl_manager.extract(wav_archive['train']),
125
+ "txt_files": dl_manager.extract(txt_archive['train']),
126
+ },
127
+ ),
128
+ ]
129
+ print("end of generation.........................................................")
130
+
131
+
132
+
133
+
134
+ def _generate_examples(self, path_to_txt, path_to_wav, wav_files, txt_files):
135
+ """
136
+ This assumes that the text directory alphabetically precedes the wav dir
137
+ The file names for wav and text seem to match and are unique
138
+ We can use them for the dictionary matching them
139
+ """
140
+
141
+ print("start of generate examples.........................................................")
142
+
143
+ print("txt file names............................",txt_files)
144
+ print("wav_files names....................................",wav_files)
145
+
146
+ examples = {}
147
+ id_ = 0
148
+ # need to prepare the transcript - wave map
149
+ for item in txt_files:
150
+
151
+
152
+ print("copying txt file...............",item)
153
+
154
+ if type(item) is tuple:
155
+ # iter_archive will return path and file
156
+ path, f = item
157
+ txt = f.read().decode(encoding="utf-8").strip()
158
+ else:
159
+ # extract will return path only
160
+ path = item
161
+ with open(path, encoding="utf-8") as f:
162
+ txt = f.read().strip()
163
+
164
+ if path.find(path_to_txt) > -1:
165
+ # construct the wav path
166
+ # which is used as an identifier
167
+ wav_path = os.path.split(path)[1].replace("_utf8", "").replace(".txt", ".wav").strip()
168
+
169
+ examples[wav_path] = {
170
+ "text": txt,
171
+ "path": wav_path,
172
+ }
173
+
174
+ for wf in wav_files:
175
+ for item in wf:
176
+ if type(item) is tuple:
177
+ path, f = item
178
+ wav_data = f.read()
179
+ else:
180
+ path = item
181
+ with open(path, "rb") as f:
182
+ wav_data = f.read()
183
+ if path.find(path_to_wav) > -1:
184
+ wav_path = os.path.split(path)[1].strip()
185
+ audio = {"path": path, "bytes": wav_data}
186
+ yield id_, {**examples[wav_path], "audio": audio}
187
+ id_ += 1