Upload my_stt_dataset.py
Browse files- my_stt_dataset.py +94 -92
my_stt_dataset.py
CHANGED
@@ -3,127 +3,129 @@ 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 |
-
|
21 |
-
name="
|
22 |
-
version=
|
23 |
-
description="
|
24 |
-
|
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 = "
|
35 |
-
|
36 |
def _info(self):
|
|
|
|
|
|
|
|
|
37 |
return datasets.DatasetInfo(
|
38 |
-
description="
|
39 |
-
features=datasets.Features(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
supervised_keys=None,
|
|
|
50 |
)
|
51 |
-
|
52 |
def _split_generators(self, dl_manager):
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
return [
|
65 |
datasets.SplitGenerator(
|
66 |
name=datasets.Split.TRAIN,
|
67 |
gen_kwargs={
|
68 |
-
"
|
69 |
"tsv_path": train_tsv,
|
70 |
},
|
71 |
),
|
72 |
datasets.SplitGenerator(
|
73 |
-
name=datasets.Split.
|
74 |
gen_kwargs={
|
75 |
-
"
|
76 |
-
"tsv_path":
|
77 |
},
|
78 |
),
|
79 |
datasets.SplitGenerator(
|
80 |
-
name=datasets.Split.
|
81 |
gen_kwargs={
|
82 |
-
"
|
83 |
-
"tsv_path":
|
84 |
},
|
85 |
),
|
86 |
]
|
87 |
-
|
88 |
-
def _generate_examples(self,
|
89 |
"""
|
90 |
-
|
91 |
-
|
|
|
92 |
"""
|
93 |
-
|
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 |
-
|
114 |
-
|
115 |
-
|
116 |
audio_id = row["id"]
|
117 |
mp3_file = audio_id + ".mp3"
|
118 |
-
mp3_path = os.path.join(
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import datasets
|
4 |
from datasets import Audio
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
class MySTTDataset(datasets.GeneratorBasedBuilder):
|
8 |
+
"""
|
9 |
+
Common Voice uslubidagi minimal dataset skript:
|
10 |
+
- 3 ta tar fayl (train/test/validation)
|
11 |
+
- Har bir tar fayl ichida .mp3 audio
|
12 |
+
- Har bir split'ga mos TSV fayl (train.tsv, test.tsv, validation.tsv)
|
13 |
+
- Audio ustuni -> HF Viewer da "play" tugmasi
|
14 |
+
"""
|
15 |
+
VERSION = datasets.Version("1.0.0")
|
16 |
+
|
17 |
+
# Agar ko'p config bo'lmasa, bu qismni soddalashtiramiz.
|
18 |
BUILDER_CONFIGS = [
|
19 |
+
datasets.BuilderConfig(
|
20 |
+
name="uz",
|
21 |
+
version=VERSION,
|
22 |
+
description="STT dataset for Uzbek language (example).",
|
23 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
]
|
25 |
+
|
26 |
+
DEFAULT_CONFIG_NAME = "uz"
|
27 |
+
|
28 |
def _info(self):
|
29 |
+
"""
|
30 |
+
Bu yerda datasetning xususiyatlari (features) e'lon qilinadi.
|
31 |
+
'audio' ustuni Audio() turida bo'lsa, viewer pleyer ko'rsatadi.
|
32 |
+
"""
|
33 |
return datasets.DatasetInfo(
|
34 |
+
description="Uzbek STT dataset: audio in .tar, transcriptions in .tsv.",
|
35 |
+
features=datasets.Features(
|
36 |
+
{
|
37 |
+
"id": datasets.Value("string"),
|
38 |
+
"audio": Audio(sampling_rate=None),
|
39 |
+
"sentence": datasets.Value("string"),
|
40 |
+
"duration": datasets.Value("float"),
|
41 |
+
"age": datasets.Value("string"),
|
42 |
+
"gender": datasets.Value("string"),
|
43 |
+
"accents": datasets.Value("string"),
|
44 |
+
"locale": datasets.Value("string"),
|
45 |
+
}
|
46 |
+
),
|
47 |
supervised_keys=None,
|
48 |
+
version=self.VERSION,
|
49 |
)
|
50 |
+
|
51 |
def _split_generators(self, dl_manager):
|
52 |
+
"""
|
53 |
+
Har bir split uchun: tar va tsv fayllar yo'lini belgilab,
|
54 |
+
dl_manager orqali yuklab/extract qildirib, so'ng _generate_examples() ga beramiz.
|
55 |
+
"""
|
56 |
+
# local path misoli (reposingizda bo'lsa).
|
57 |
+
# Agar huggingface.co'dan yuklamoqchi bo'lsangiz, URL qilishingiz mumkin
|
58 |
+
train_tar = "Dataset_STT/audio/uz/train.tar"
|
59 |
+
train_tsv = "Dataset_STT/transcript/uz/train.tsv"
|
60 |
+
|
61 |
+
test_tar = "Dataset_STT/audio/uz/test.tar"
|
62 |
+
test_tsv = "Dataset_STT/transcript/uz/test.tsv"
|
63 |
+
|
64 |
+
val_tar = "Dataset_STT/audio/uz/validation.tar"
|
65 |
+
val_tsv = "Dataset_STT/transcript/uz/validation.tsv"
|
66 |
+
|
67 |
+
# Bu fayllarni download+extract (yoki local bo'lsa, faqat extract) qilamiz:
|
68 |
+
# Eslatma: agar localda bo'lsayu, dl_manager `is_local=True` deb topishi mumkin,
|
69 |
+
# ammo baribir .extract ishlaydi.
|
70 |
+
|
71 |
+
train_tar_extracted = dl_manager.extract(train_tar)
|
72 |
+
test_tar_extracted = dl_manager.extract(test_tar)
|
73 |
+
val_tar_extracted = dl_manager.extract(val_tar)
|
74 |
+
|
75 |
+
# Har bir splitted datasetga mos "SplitGenerator" qaytaramiz
|
76 |
+
# "gen_kwargs" -> _generate_examples() ga paramlar
|
77 |
return [
|
78 |
datasets.SplitGenerator(
|
79 |
name=datasets.Split.TRAIN,
|
80 |
gen_kwargs={
|
81 |
+
"archive_dir": train_tar_extracted, # tar fayl ochilib yoyilgan papka
|
82 |
"tsv_path": train_tsv,
|
83 |
},
|
84 |
),
|
85 |
datasets.SplitGenerator(
|
86 |
+
name=datasets.Split.TEST,
|
87 |
gen_kwargs={
|
88 |
+
"archive_dir": test_tar_extracted,
|
89 |
+
"tsv_path": test_tsv,
|
90 |
},
|
91 |
),
|
92 |
datasets.SplitGenerator(
|
93 |
+
name=datasets.Split.VALIDATION,
|
94 |
gen_kwargs={
|
95 |
+
"archive_dir": val_tar_extracted,
|
96 |
+
"tsv_path": val_tsv,
|
97 |
},
|
98 |
),
|
99 |
]
|
100 |
+
|
101 |
+
def _generate_examples(self, archive_dir, tsv_path):
|
102 |
"""
|
103 |
+
Ushbu metod har bir split uchun audio+transkript juftliklarini geneate qiladi.
|
104 |
+
- 'archive_dir' papkada .tar dan ochilgan .mp3 fayllar mavjud.
|
105 |
+
- 'tsv_path' faylini qatorma-qator o'qib, 'id' -> "id.mp3" yo'lini izlaymiz.
|
106 |
"""
|
107 |
+
# TSV ni o'qiymiz:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
with open(tsv_path, "r", encoding="utf-8") as f:
|
109 |
reader = csv.DictReader(f, delimiter="\t")
|
110 |
for idx, row in enumerate(reader):
|
111 |
+
# tsv da shunaqa ustunlar bo'lishi kutiladi:
|
112 |
+
# id, sentence, duration, age, gender, accents, locale
|
|
|
113 |
audio_id = row["id"]
|
114 |
mp3_file = audio_id + ".mp3"
|
115 |
+
mp3_path = os.path.join(archive_dir, mp3_file)
|
116 |
+
|
117 |
+
# Agar audio fayl exist bo'lsa:
|
118 |
+
if os.path.isfile(mp3_path):
|
119 |
+
yield idx, {
|
120 |
+
"id": audio_id,
|
121 |
+
"audio": mp3_path, # Audio() -> pleyer
|
122 |
+
"sentence": row.get("sentence", ""),
|
123 |
+
"duration": float(row.get("duration", 0.0)),
|
124 |
+
"age": row.get("age", ""),
|
125 |
+
"gender": row.get("gender", ""),
|
126 |
+
"accents": row.get("accents", ""),
|
127 |
+
"locale": row.get("locale", ""),
|
128 |
+
}
|
129 |
+
else:
|
130 |
+
# Audio topilmasa, skip (yoki exception)
|
131 |
+
continue
|