File size: 4,647 Bytes
a1133d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import tarfile
import csv
import datasets
from datasets.utils.py_utils import size_str
from tqdm import tqdm


class STTUzbekConfig(datasets.BuilderConfig):
    """BuilderConfig for the STT Uzbek Dataset."""

    def __init__(self, **kwargs):
        description = (
            "Speech-to-Text dataset for the Uzbek language. "
            "The dataset contains audio files stored in different folders: "
            "`wavs`, `uz_other_dataset`, `uz_validated_dataset`, `uz_train_dataset`. "
            "The corresponding transcriptions are provided in the `metadata.csv` file."
        )
        super(STTUzbekConfig, self).__init__(description=description, **kwargs)


class STTUzbek(datasets.GeneratorBasedBuilder):
    DEFAULT_WRITER_BATCH_SIZE = 1000

    BUILDER_CONFIGS = [
        STTUzbekConfig(
            name="stt_uzbek",
            version=datasets.Version("1.0.0"),
        ),
    ]

    def _info(self):
        features = datasets.Features(
            {
                "file_name": datasets.Value("string"),
                "audio": datasets.features.Audio(sampling_rate=48_000),
                "transcription": datasets.Value("string"),
            }
        )

        return datasets.DatasetInfo(
            description=self.config.description,
            features=features,
            supervised_keys=None,
            homepage="https://huggingface.co/datasets/Beehzod/STT_uz",
            license="Apache License 2.0",
            citation="""@misc{uzbek_stt_dataset,
            author = {Beehzod},
            title = {Uzbek Speech-to-Text Dataset},
            year = {2024},
            howpublished = {https://huggingface.co/datasets/Beehzod/STT_uz},
            note = {Dataset for Uzbek language speech-to-text tasks.}
                }""",
            version=self.config.version,
        )

    def _split_generators(self, dl_manager):
        # Adjust the paths according to your setup
        wavs_dir = "audio/wavs"
        uz_other_dir = "audio/uz_other_dataset"
        uz_validated_dir = "audio/uz_validated_dataset"
        uz_train_dir = "audio/uz_train_dataset"
        metadata_file = "metadata.csv"

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "wavs_dir": wavs_dir,
                    "uz_other_dir": uz_other_dir,
                    "uz_validated_dir": uz_validated_dir,
                    "uz_train_dir": uz_train_dir,
                    "metadata_file": metadata_file,
                },
            ),
        ]

    def _generate_examples(self, wavs_dir, uz_other_dir, uz_validated_dir, uz_train_dir, metadata_file):
        with open(metadata_file, encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in tqdm(reader, desc="Processing metadata..."):
                file_name = row["file_name"]
                transcription = row["transcription"]

                # Determine the file's location based on the path prefix
                if file_name.startswith("audio/wavs"):
                    audio_path = os.path.join(wavs_dir, os.path.basename(file_name))
                elif file_name.startswith("audio/uz_other_dataset"):
                    audio_path = self._extract_from_tar(uz_other_dir, file_name)
                elif file_name.startswith("audio/uz_validated_dataset"):
                    audio_path = self._extract_from_tar(uz_validated_dir, file_name)
                elif file_name.startswith("audio/uz_train_dataset"):
                    audio_path = self._extract_from_tar(uz_train_dir, file_name)
                else:
                    raise ValueError(f"Unknown path prefix in file_name: {file_name}")

                # Yield the example
                yield file_name, {
                    "file_name": file_name,
                    "audio": audio_path,
                    "transcription": transcription,
                }

    def _extract_from_tar(self, tar_dir, file_name):
        # Extract the specific file from the tar archives
        for tar_file in os.listdir(tar_dir):
            tar_path = os.path.join(tar_dir, tar_file)
            with tarfile.open(tar_path, "r") as tar:
                try:
                    file_path = file_name.split("/")[-1]
                    extracted_file = tar.extractfile(file_path)
                    if extracted_file:
                        return {"path": file_path, "bytes": extracted_file.read()}
                except KeyError:
                    continue
        raise FileNotFoundError(f"File {file_name} not found in any tar archives in {tar_dir}.")