Upload my_stt_dataset.py
Browse files- my_stt_dataset.py +88 -79
my_stt_dataset.py
CHANGED
@@ -1,120 +1,129 @@
|
|
1 |
import os
|
2 |
-
import tarfile
|
3 |
import csv
|
4 |
-
|
5 |
import datasets
|
6 |
from datasets import Audio
|
7 |
|
8 |
-
|
9 |
class MySTTDatasetConfig(datasets.BuilderConfig):
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
class MySTTDataset(datasets.GeneratorBasedBuilder):
|
14 |
BUILDER_CONFIGS = [
|
15 |
-
MySTTDatasetConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
]
|
17 |
-
|
|
|
|
|
18 |
def _info(self):
|
19 |
return datasets.DatasetInfo(
|
20 |
-
description="
|
21 |
-
features=datasets.Features(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
}
|
32 |
-
),
|
33 |
supervised_keys=None,
|
34 |
)
|
35 |
-
|
36 |
def _split_generators(self, dl_manager):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
val_tsv = dl_manager.download("URL_val_tsv")
|
49 |
-
|
50 |
-
test_tar = dl_manager.download_and_extract("URL_test_tar")
|
51 |
-
test_tsv = dl_manager.download("URL_test_tsv")
|
52 |
-
|
53 |
return [
|
54 |
datasets.SplitGenerator(
|
55 |
name=datasets.Split.TRAIN,
|
56 |
gen_kwargs={
|
57 |
-
"
|
58 |
"tsv_path": train_tsv,
|
59 |
},
|
60 |
),
|
61 |
datasets.SplitGenerator(
|
62 |
name=datasets.Split.VALIDATION,
|
63 |
gen_kwargs={
|
64 |
-
"
|
65 |
"tsv_path": val_tsv,
|
66 |
},
|
67 |
),
|
68 |
datasets.SplitGenerator(
|
69 |
name=datasets.Split.TEST,
|
70 |
gen_kwargs={
|
71 |
-
"
|
72 |
"tsv_path": test_tsv,
|
73 |
},
|
74 |
),
|
75 |
]
|
76 |
-
|
77 |
-
def _generate_examples(self,
|
78 |
"""
|
79 |
-
|
|
|
80 |
"""
|
81 |
-
|
82 |
-
|
83 |
-
#
|
84 |
-
#
|
85 |
-
|
86 |
-
#
|
87 |
-
|
88 |
-
#
|
89 |
-
#
|
90 |
-
|
91 |
-
#
|
92 |
-
|
93 |
-
#
|
94 |
-
|
95 |
-
|
|
|
|
|
96 |
with open(tsv_path, "r", encoding="utf-8") as f:
|
97 |
reader = csv.DictReader(f, delimiter="\t")
|
98 |
-
for
|
99 |
-
|
100 |
-
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
}
|
117 |
-
else:
|
118 |
-
# Agar mp3 topilmasa, o'ziz xato signal qilishingiz mumkin
|
119 |
-
# yoki continue qilishingiz mumkin
|
120 |
-
pass
|
|
|
1 |
import os
|
|
|
2 |
import csv
|
|
|
3 |
import datasets
|
4 |
from datasets import Audio
|
5 |
|
6 |
+
# Har xil config - 'sample' va 'full'
|
7 |
class MySTTDatasetConfig(datasets.BuilderConfig):
|
8 |
+
def __init__(self, limit=None, **kwargs):
|
9 |
+
"""
|
10 |
+
limit : int yoki None
|
11 |
+
Har bir splitdan qancha qatorni o'qish.
|
12 |
+
None bo'lsa, cheklanmagan.
|
13 |
+
"""
|
14 |
+
super().__init__(**kwargs)
|
15 |
+
self.limit = limit
|
16 |
|
17 |
|
18 |
class MySTTDataset(datasets.GeneratorBasedBuilder):
|
19 |
BUILDER_CONFIGS = [
|
20 |
+
MySTTDatasetConfig(
|
21 |
+
name="sample",
|
22 |
+
version=datasets.Version("1.0.0"),
|
23 |
+
description="Faqat har bir splitdan 10k qator ko'rsatish uchun",
|
24 |
+
limit=10_000, # masalan 10 ming
|
25 |
+
),
|
26 |
+
MySTTDatasetConfig(
|
27 |
+
name="full",
|
28 |
+
version=datasets.Version("1.0.0"),
|
29 |
+
description="Hech qanday cheklovsiz to'liq dataset",
|
30 |
+
limit=None,
|
31 |
+
),
|
32 |
]
|
33 |
+
|
34 |
+
DEFAULT_CONFIG_NAME = "sample"
|
35 |
+
|
36 |
def _info(self):
|
37 |
return datasets.DatasetInfo(
|
38 |
+
description="Speech-to-text dataset (tar ichida audio, tsvda transkript).",
|
39 |
+
features=datasets.Features({
|
40 |
+
"id": datasets.Value("string"),
|
41 |
+
"audio": Audio(sampling_rate=None),
|
42 |
+
"sentence": datasets.Value("string"),
|
43 |
+
"duration": datasets.Value("float"),
|
44 |
+
"age": datasets.Value("string"),
|
45 |
+
"gender": datasets.Value("string"),
|
46 |
+
"accents": datasets.Value("string"),
|
47 |
+
"locale": datasets.Value("string"),
|
48 |
+
}),
|
|
|
|
|
49 |
supervised_keys=None,
|
50 |
)
|
51 |
+
|
52 |
def _split_generators(self, dl_manager):
|
53 |
+
# TODO: train.tar, test.tar, validation.tar + tegishli TSV link yoki local path
|
54 |
+
# Hozircha misol tariqasida local path'lar ko'rsatamiz
|
55 |
+
train_tar = "Dataset_STT/audio/uz/train/train.tar"
|
56 |
+
train_tsv = "Dataset_STT/transcript/uz/train/train.tsv"
|
57 |
+
|
58 |
+
val_tar = "Dataset_STT/audio/uz/validation/validation.tar"
|
59 |
+
val_tsv = "Dataset_STT/transcript/uz/validation/validation.tsv"
|
60 |
+
|
61 |
+
test_tar = "Dataset_STT/audio/uz/test/test.tar"
|
62 |
+
test_tsv = "Dataset_STT/transcript/uz/test/test.tsv"
|
63 |
+
|
|
|
|
|
|
|
|
|
|
|
64 |
return [
|
65 |
datasets.SplitGenerator(
|
66 |
name=datasets.Split.TRAIN,
|
67 |
gen_kwargs={
|
68 |
+
"tar_path": train_tar,
|
69 |
"tsv_path": train_tsv,
|
70 |
},
|
71 |
),
|
72 |
datasets.SplitGenerator(
|
73 |
name=datasets.Split.VALIDATION,
|
74 |
gen_kwargs={
|
75 |
+
"tar_path": val_tar,
|
76 |
"tsv_path": val_tsv,
|
77 |
},
|
78 |
),
|
79 |
datasets.SplitGenerator(
|
80 |
name=datasets.Split.TEST,
|
81 |
gen_kwargs={
|
82 |
+
"tar_path": test_tar,
|
83 |
"tsv_path": test_tsv,
|
84 |
},
|
85 |
),
|
86 |
]
|
87 |
+
|
88 |
+
def _generate_examples(self, tar_path, tsv_path):
|
89 |
"""
|
90 |
+
limit=10_000 bo'lsa, har bir splitdan 10 mingtagina qator qaytaradi.
|
91 |
+
Agar limit=None bo'lsa, hamma qatorni o'qiydi.
|
92 |
"""
|
93 |
+
limit = self.config.limit
|
94 |
+
|
95 |
+
# Tar ichidagi mp3 fayllarni avval extract qilasiz yoki on-the-fly o'qiysiz
|
96 |
+
# Eslatma: HF Viewer uchun eng osoni audio papkaga ochib qo'yish yoki
|
97 |
+
# `dl_manager.download_and_extract(...)` ishlatishdir.
|
98 |
+
# Bu yerda misol tariqasida tar ni ochib, audio fayllarni papkaga yoyildi deb faraz qilamiz:
|
99 |
+
|
100 |
+
# Masalan audio papka: "Dataset_STT/audio/uz/train/unpacked"
|
101 |
+
# Yoki to'liq yo'li: tar_path = "Dataset_STT/audio/uz/train/train.tar"
|
102 |
+
# "unpacked" papkani o'zingiz oldindan tar -xvf bilan yaratishingiz kerak.
|
103 |
+
# Yoki tarfile moduli bilan python ichida extraction qilishingiz mumkin.
|
104 |
+
|
105 |
+
# Soddalik uchun, tar ni allaqachon manual ravishda unpack qildik deb olamiz:
|
106 |
+
audio_folder = tar_path.replace(".tar", "_unpacked")
|
107 |
+
# misol: "Dataset_STT/audio/uz/train/train_unpacked"
|
108 |
+
|
109 |
+
# Keyin TSV'ni o'qiymiz:
|
110 |
with open(tsv_path, "r", encoding="utf-8") as f:
|
111 |
reader = csv.DictReader(f, delimiter="\t")
|
112 |
+
for idx, row in enumerate(reader):
|
113 |
+
if limit is not None and idx >= limit:
|
114 |
+
break # 10k dan oshsa, to'xtaymiz
|
115 |
|
116 |
+
audio_id = row["id"]
|
117 |
+
mp3_file = audio_id + ".mp3"
|
118 |
+
mp3_path = os.path.join(audio_folder, mp3_file)
|
119 |
+
|
120 |
+
yield idx, {
|
121 |
+
"id": audio_id,
|
122 |
+
"audio": mp3_path,
|
123 |
+
"sentence": row["sentence"],
|
124 |
+
"duration": float(row["duration"]),
|
125 |
+
"age": row["age"],
|
126 |
+
"gender": row["gender"],
|
127 |
+
"accents": row["accents"],
|
128 |
+
"locale": row["locale"],
|
129 |
+
}
|
|
|
|
|
|
|
|
|
|