-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_builder.py
281 lines (206 loc) · 13.8 KB
/
dataset_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import soundfile as sf
from datasets import load_dataset
import csv
import datasets
import os
import re
from huggingface_hub import login
login(token="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")
#n_max_train = 34605
#n_max_train = 3921
n_max_train = 3921
n_max_valid = 3171
#fsicoli/common_voice_17_0
#dataset_train_de = load_dataset('mozilla-foundation/common_voice_17_0','de', split='train', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_train_de = dataset_train_de.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_train_nl = load_dataset('mozilla-foundation/common_voice_17_0','nl', split='train', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_train_nl = dataset_train_nl.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_train_sv = load_dataset('mozilla-foundation/common_voice_17_0', 'sv-SE', split='train', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_train_sv = dataset_train_sv.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
dataset_train_fy = load_dataset('mozilla-foundation/common_voice_17_0', 'fy-NL', split='train', streaming=True, trust_remote_code=True, use_auth_token=True)
dataset_train_fy = dataset_train_fy.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_train_en = load_dataset('mozilla-foundation/common_voice_17_0', 'en', split='train', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_train_en = dataset_train_en.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_test_en = load_dataset('mozilla-foundation/common_voice_17_0', 'en', split='test', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_test_en = dataset_test_en.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_test_de = load_dataset('mozilla-foundation/common_voice_17_0', 'de', split='test', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_test_de = dataset_test_de.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_test_nl= load_dataset('mozilla-foundation/common_voice_17_0', 'nl', split='test', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_test_nl = dataset_test_nl.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_test_sv = load_dataset('mozilla-foundation/common_voice_17_0', 'sv-SE', split='test', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_test_sv = dataset_test_sv.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
dataset_test_fy= load_dataset('mozilla-foundation/common_voice_17_0', 'fy-NL', split='test', streaming=True, trust_remote_code=True, use_auth_token=True)
dataset_test_fy = dataset_test_fy.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_validation_en =load_dataset('mozilla-foundation/common_voice_17_0', 'en', split='validation', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_validation_en = dataset_validation_en.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_validation_de =load_dataset('mozilla-foundation/common_voice_17_0', 'de', split='validation', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_validation_de = dataset_validation_de.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_validation_nl =load_dataset('mozilla-foundation/common_voice_17_0', 'nl', split='validation', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_validation_nl = dataset_validation_nl.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_validation_sv =load_dataset('mozilla-foundation/common_voice_17_0', 'sv-SE', split='validation', streaming=True, trust_remote_code=True, use_auth_token=True)
#dataset_validation_sv = dataset_validation_sv.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
dataset_validation_fy =load_dataset('mozilla-foundation/common_voice_17_0', 'fy-NL', split='validation', streaming=True, trust_remote_code=True, use_auth_token=True)
dataset_validation_fy = dataset_validation_fy.cast_column("audio", datasets.features.Audio(sampling_rate=16_000))
#dataset_invalidated_en = load_dataset('mozilla-foundation/common_voice_16_1', 'en', split='invalidated', streaming=True)
#dataset_invalidated_de = load_dataset('mozilla-foundation/common_voice_16_1', 'de', split='invalidated', streaming=True)
#dataset_invalidated_nl =load_dataset('mozilla-foundation/common_voice_16_1', 'nl', split='invalidated', streaming=True)
#dataset_invalidated_sv_SE =load_dataset('mozilla-foundation/common_voice_16_1', 'sv-SE', split='invalidated', streaming=True)
#dataset_invalidated_fy =load_dataset('mozilla-foundation/common_voice_16_1', 'fy-NL', split='invalidated', streaming=True)
#dataset_other_en = load_dataset('mozilla-foundation/common_voice_16_1', 'en', split='other', streaming=True)
#dataset_other_de = load_dataset('mozilla-foundation/common_voice_16_1', 'de', split='other', streaming=True)
#dataset_other_nl= load_dataset('mozilla-foundation/common_voice_16_1', 'nl', split='other', streaming=True)
#datase_other_sv_SE =load_dataset('mozilla-foundation/common_voice_16_1', 'sv-SE', split='other', streaming=True)
#dataset_other_fy=load_dataset('mozilla-foundation/common_voice_16_1', 'fy-NL', split='other', streaming=True)
# list_of_rows_train_nl = list(dataset_train_nl) # 34605 # all: 36.4k
# list_of_rows_test_nl = list(dataset_test_nl) # 11235 # all:11.2k
# list_of_rows_validation_nl = list(dataset_validation_nl) # 11223 # all:11.2k
# #list_of_rows_invalidated_nl = list(dataset_invalidated_nl.take(5540)) # all: 5.54k
# #list_of_rows_other_nl=list(dataset_other_nl.take(2290)) # all: 2.29k
# print("nl", len(list_of_rows_train_nl), len(list_of_rows_test_nl), len(list_of_rows_validation_nl))
# list_of_rows_train_en = list(dataset_train_en.take(len(list_of_rows_train_nl))) #34605 # all: 114k
# list_of_rows_test_en = list(dataset_test_en) #11235 # all: 16.4k
# list_of_rows_validation_en = list(dataset_validation_en) # 11223 # all: 16.4k
# #list_of_rows_invalidated_en = list(dataset_invalidated_en.take(5800)) # all: 111k
# #list_of_rows_other_en = list(dataset_other_en.take(5800)) # all: 154k
# print("en", len(list_of_rows_train_en), len(list_of_rows_test_en), len(list_of_rows_validation_en))
# list_of_rows_train_de = list(dataset_train_de.take(len(list_of_rows_train_nl))) #34605 # all : 121k
# list_of_rows_test_de = list(dataset_test_de) #11235 # all: 16.2k
# list_of_rows_validation_de = list(dataset_validation_de) # 11223 # all: 16.2k
# #list_of_rows_invalidated_de = list(dataset_invalidated_de.take(5800)) # all: 53.8k
# #list_of_rows_other_de=list(dataset_other_de.take(5800)) # all: 7k
# print("de", len(list_of_rows_train_de), len(list_of_rows_test_de), len(list_of_rows_validation_de))
# list_of_rows_train_sv_SE = list(dataset_train_sv_SE) # 7657 # all: 7.66k
# list_of_rows_test_sv_SE = list(dataset_test_sv_SE) #5206 # all: 5.21k
# list_of_rows_validation_sv_SE = list(dataset_validation_sv_SE) # 5222 # all: 5.22k
# #list_of_rows_invalidated_sv_SE = list(dataset_invalidated_sv_SE.take(1420)) # all: 1.42k
# #list_of_rows_other_sv_SE=list(dataset_other_sv_SE.take(6610)) # all: 6.61k
# print("sv-SE", len(list_of_rows_train_sv_SE), len(list_of_rows_test_sv_SE), len(list_of_rows_validation_sv_SE))
# list_of_rows_train_fy = list(dataset_train_fy) #3920 # all: 3.92k
# list_of_rows_test_fy=list(dataset_test_fy) # 3171 # all: 3.17k
# list_of_rows_validation_fy = list(dataset_validation_fy) # 3171 # all:3.17k
# #list_of_rows_invalidated_fy = list(dataset_invalidated_fy.take(3950)) # all: 3.95k
# #list_of_rows_other_fy=list(dataset_other_fy.take(102000)) # all: 102k
# print("fy-NL", len(list_of_rows_train_fy), len(list_of_rows_test_fy), len(list_of_rows_validation_fy))
#print(list_of_rows_train_de)
whole_rows_train = []
multilingual_dataset_list_train = []
#multilingual_dataset_list_train.append([dataset_train_en,"en"])
#multilingual_dataset_list_train.append([dataset_train_de,"de"])
#multilingual_dataset_list_train.append([dataset_train_nl,"nl"])
#multilingual_dataset_list_train.append([dataset_train_sv,"sv-SE"])
multilingual_dataset_list_train.append([dataset_train_fy,"fy-NL"])
whole_rows_test = []
multilingual_dataset_list_test = []
#multilingual_dataset_list_test.append([dataset_test_en,"en"])
#multilingual_dataset_list_test.append([dataset_test_de,"de"])
#multilingual_dataset_list_test.append([dataset_test_nl,"nl"])
#multilingual_dataset_list_test.append([dataset_test_sv,"sv-SE"])
multilingual_dataset_list_test.append([dataset_test_fy,"fy-NL"])
whole_rows_validation = []
multilingual_dataset_list_validation = []
#multilingual_dataset_list_validation.append([dataset_validation_en,"en"])
#multilingual_dataset_list_validation.append([dataset_validation_de,"de"])
#multilingual_dataset_list_validation.append([dataset_validation_nl,"nl"])
#multilingual_dataset_list_validation.append([dataset_validation_sv,"sv-SE"])
multilingual_dataset_list_validation.append([dataset_validation_fy,"fy-NL"])
whole_rows_test_fy_NL=[]
#whole_rows_test_sv_SE=[]
#whole_rows_test_en=[]
#whole_rows_test_de=[]
#whole_rows_test_nl=[]
#clips_directory = "clips/"
os.makedirs("/data/p312702/s3_router/train/", exist_ok=True)
os.makedirs("/data/p312702/s3_router/test/", exist_ok=True)
os.makedirs("/data/p312702/s3_router/validation/",exist_ok=True)
print("train")
for pair in multilingual_dataset_list_train:
LID=pair[1]
print(LID)
n = 0
for row_dict in iter(pair[0]):
audio_content=row_dict['audio']['array']
filename = row_dict['audio']['path'].split("/")[1].replace(".mp3",".wav")
sentence_without_comma = re.sub(r',', ' ', row_dict['sentence'])
whole_rows_train.append([filename, sentence_without_comma, LID]) #up_votes, down_votes, age, gender, accent, locale, segment, variant
sf.write("train/"+filename, audio_content, 16000)
n += 1
if n == n_max_train:
break
print("test")
for pair in multilingual_dataset_list_test:
LID=pair[1]
print(LID)
for row_dict in iter(pair[0]):
audio_content=row_dict['audio']['array']
filename = row_dict['audio']['path'].split("/")[1].replace(".mp3",".wav")
sentence_without_comma = re.sub(r',', ' ', row_dict['sentence'])
whole_rows_test.append([filename, sentence_without_comma, LID]) #up_votes, down_votes, age, gender, accent, locale, segment, variant
sf.write("test/"+filename, audio_content, 16000)
if LID=="fy-NL":
whole_rows_test_fy_NL.append([filename, sentence_without_comma, LID])
elif LID=="sv-SE":
whole_rows_test_sv_SE.append([filename, sentence_without_comma, LID])
elif LID=="nl":
whole_rows_test_nl.append([filename, sentence_without_comma, LID])
elif LID=="de":
whole_rows_test_de.append([filename, sentence_without_comma, LID])
elif LID=="en":
whole_rows_test_en.append([filename, sentence_without_comma, LID])
print("validation")
for pair in multilingual_dataset_list_validation:
LID=pair[1]
print(LID)
n = 0
for row_dict in iter(pair[0]):
audio_content=row_dict['audio']['array']
sentence_without_comma = re.sub(r',', ' ', row_dict['sentence'])
filename = row_dict['audio']['path'].split("/")[1].replace(".mp3",".wav")
whole_rows_validation.append([filename, sentence_without_comma, LID]) #up_votes, down_votes, age, gender, accent, locale, segment, variant
sf.write("validation/"+filename, audio_content, 16000)
n += 1
if n == n_max_valid:
break
# export splits
with open("train/metadata.csv","w",newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_train:
writer.writerow(row)
with open("test/metadata.csv","w",newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test:
writer.writerow(row)
with open("validation/metadata.csv","w",newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_validation:
writer.writerow(row)
'''
# export test files for each language
with open("test_fy-NL.csv","w", newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test_fy_NL:
writer.writerow(row)
with open("test_nl.csv","w", newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test_nl:
writer.writerow(row)
with open("test_en.csv","w", newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test_en:
writer.writerow(row)
with open("test_de.csv","w", newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test_de:
writer.writerow(row)
with open("test_sv-SE.csv","w", newline='') as tsvfile:
writer = csv.writer(tsvfile, delimiter=',', lineterminator='\n')
writer.writerow(['file_name','transcription','lid'])
for row in whole_rows_test_sv_SE:
writer.writerow(row)
'''