new_dataset_stt / dataset.py
Elyordev's picture
Upload dataset.py
62eb364 verified
raw
history blame
4.93 kB
import csv
import os
from typing import Iterator, Tuple
import datasets
_DESCRIPTION = """\
Bu dataset mp3 formatdagi audio fayllar va tsv metadata fayllardan iborat.
Audio fayllar .tar arxiv ichida saqlangan va tsv faylda fayl nomlari (masalan, H3H38EY38D8.mp3) keltirilgan.
"""
_HOMEPAGE = "https://huggingface.co/datasets/Elyordev/new_dataset_stt"
_LICENSE = "MIT"
# Har bir split uchun .tsv va .tar fayllarning repo ichidagi joylashuvi.
# (Sizning repo tarkibingizda train/train.tsv, train/train.tar, va hokazo bo'lsa)
_URLS = {
"train": {
"tsv": "train/train.tsv",
"tar": "train/train.tar",
},
"validation": {
"tsv": "validation/validation.tsv",
"tar": "validation/validation.tar",
},
"test": {
"tsv": "test/test.tsv",
"tar": "test/test.tar",
},
}
class MyDatasetSTTConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(MyDatasetSTTConfig, self).__init__(**kwargs)
class MyDatasetSTT(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
MyDatasetSTTConfig(
name="default",
version=VERSION,
description="My new STT dataset with mp3 audios in tar archives",
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features({
"id": datasets.Value("string"),
"path": datasets.Value("string"), # Fayl nomi, masalan: H3H38EY38D8.mp3
"sentence": datasets.Value("string"),
"duration": datasets.Value("float"),
"age": datasets.Value("string"),
"gender": datasets.Value("string"),
"accents": datasets.Value("string"),
"locale": datasets.Value("string"),
# Audio feature: datasets.Audio avtomatik tarzda tar URI orqali yuklaydi
"audio": datasets.Audio(sampling_rate=16000),
}),
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
"""
Har bir split uchun .tsv va .tar fayllarni dl_manager yordamida yuklab olamiz.
Orqa fon’da tar-faylni to‘liq extract qilish shart emas.
'tar://...' URI orqali audio oqimini bevosita o‘qish mumkin.
"""
downloaded_files = {}
for split in _URLS:
downloaded_files[split] = {
"tsv": dl_manager.download_and_extract(_URLS[split]["tsv"]),
"tar": dl_manager.download_and_extract(_URLS[split]["tar"]),
}
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"tsv_path": downloaded_files["train"]["tsv"],
"tar_path": downloaded_files["train"]["tar"],
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"tsv_path": downloaded_files["validation"]["tsv"],
"tar_path": downloaded_files["validation"]["tar"],
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"tsv_path": downloaded_files["test"]["tsv"],
"tar_path": downloaded_files["test"]["tar"],
},
),
]
def _generate_examples(self, tsv_path: str, tar_path: str) -> Iterator[Tuple[int, dict]]:
"""
Har bir .tsv fayldagi qatordan misol (example) yaratamiz.
Audio faylga murojaat qilish uchun "tar://" sintaksisidan foydalanamiz:
Bu format: "tar://<tar fayl yo'li>#<tsv fayldagi path>".
"""
with open(tsv_path, encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t")
for idx, row in enumerate(reader):
# MP3 fayl nomi, masalan "H3H38EY38D8.mp3"
mp3_file = row["path"]
# Audio fayl uchun URI: masalan, "tar://.../train.tar#H3H38EY38D8.mp3"
audio_ref = f"tar://{tar_path}#{mp3_file}"
yield idx, {
"id": row["id"],
"path": mp3_file,
"sentence": row["sentence"],
"duration": float(row.get("duration", 0.0)),
"age": row.get("age", ""),
"gender": row.get("gender", ""),
"accents": row.get("accents", ""),
"locale": row.get("locale", ""),
"audio": audio_ref, # tar ichidan oqish
}