Beehzod commited on
Commit
a1133d7
1 Parent(s): 767c5cb

Create stt_uz_structure.py

Browse files
Files changed (1) hide show
  1. stt_uz_structure.py +116 -0
stt_uz_structure.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tarfile
3
+ import csv
4
+ import datasets
5
+ from datasets.utils.py_utils import size_str
6
+ from tqdm import tqdm
7
+
8
+
9
+ class STTUzbekConfig(datasets.BuilderConfig):
10
+ """BuilderConfig for the STT Uzbek Dataset."""
11
+
12
+ def __init__(self, **kwargs):
13
+ description = (
14
+ "Speech-to-Text dataset for the Uzbek language. "
15
+ "The dataset contains audio files stored in different folders: "
16
+ "`wavs`, `uz_other_dataset`, `uz_validated_dataset`, `uz_train_dataset`. "
17
+ "The corresponding transcriptions are provided in the `metadata.csv` file."
18
+ )
19
+ super(STTUzbekConfig, self).__init__(description=description, **kwargs)
20
+
21
+
22
+ class STTUzbek(datasets.GeneratorBasedBuilder):
23
+ DEFAULT_WRITER_BATCH_SIZE = 1000
24
+
25
+ BUILDER_CONFIGS = [
26
+ STTUzbekConfig(
27
+ name="stt_uzbek",
28
+ version=datasets.Version("1.0.0"),
29
+ ),
30
+ ]
31
+
32
+ def _info(self):
33
+ features = datasets.Features(
34
+ {
35
+ "file_name": datasets.Value("string"),
36
+ "audio": datasets.features.Audio(sampling_rate=48_000),
37
+ "transcription": datasets.Value("string"),
38
+ }
39
+ )
40
+
41
+ return datasets.DatasetInfo(
42
+ description=self.config.description,
43
+ features=features,
44
+ supervised_keys=None,
45
+ homepage="https://huggingface.co/datasets/Beehzod/STT_uz",
46
+ license="Apache License 2.0",
47
+ citation="""@misc{uzbek_stt_dataset,
48
+ author = {Beehzod},
49
+ title = {Uzbek Speech-to-Text Dataset},
50
+ year = {2024},
51
+ howpublished = {https://huggingface.co/datasets/Beehzod/STT_uz},
52
+ note = {Dataset for Uzbek language speech-to-text tasks.}
53
+ }""",
54
+ version=self.config.version,
55
+ )
56
+
57
+ def _split_generators(self, dl_manager):
58
+ # Adjust the paths according to your setup
59
+ wavs_dir = "audio/wavs"
60
+ uz_other_dir = "audio/uz_other_dataset"
61
+ uz_validated_dir = "audio/uz_validated_dataset"
62
+ uz_train_dir = "audio/uz_train_dataset"
63
+ metadata_file = "metadata.csv"
64
+
65
+ return [
66
+ datasets.SplitGenerator(
67
+ name=datasets.Split.TRAIN,
68
+ gen_kwargs={
69
+ "wavs_dir": wavs_dir,
70
+ "uz_other_dir": uz_other_dir,
71
+ "uz_validated_dir": uz_validated_dir,
72
+ "uz_train_dir": uz_train_dir,
73
+ "metadata_file": metadata_file,
74
+ },
75
+ ),
76
+ ]
77
+
78
+ def _generate_examples(self, wavs_dir, uz_other_dir, uz_validated_dir, uz_train_dir, metadata_file):
79
+ with open(metadata_file, encoding="utf-8") as f:
80
+ reader = csv.DictReader(f)
81
+ for row in tqdm(reader, desc="Processing metadata..."):
82
+ file_name = row["file_name"]
83
+ transcription = row["transcription"]
84
+
85
+ # Determine the file's location based on the path prefix
86
+ if file_name.startswith("audio/wavs"):
87
+ audio_path = os.path.join(wavs_dir, os.path.basename(file_name))
88
+ elif file_name.startswith("audio/uz_other_dataset"):
89
+ audio_path = self._extract_from_tar(uz_other_dir, file_name)
90
+ elif file_name.startswith("audio/uz_validated_dataset"):
91
+ audio_path = self._extract_from_tar(uz_validated_dir, file_name)
92
+ elif file_name.startswith("audio/uz_train_dataset"):
93
+ audio_path = self._extract_from_tar(uz_train_dir, file_name)
94
+ else:
95
+ raise ValueError(f"Unknown path prefix in file_name: {file_name}")
96
+
97
+ # Yield the example
98
+ yield file_name, {
99
+ "file_name": file_name,
100
+ "audio": audio_path,
101
+ "transcription": transcription,
102
+ }
103
+
104
+ def _extract_from_tar(self, tar_dir, file_name):
105
+ # Extract the specific file from the tar archives
106
+ for tar_file in os.listdir(tar_dir):
107
+ tar_path = os.path.join(tar_dir, tar_file)
108
+ with tarfile.open(tar_path, "r") as tar:
109
+ try:
110
+ file_path = file_name.split("/")[-1]
111
+ extracted_file = tar.extractfile(file_path)
112
+ if extracted_file:
113
+ return {"path": file_path, "bytes": extracted_file.read()}
114
+ except KeyError:
115
+ continue
116
+ raise FileNotFoundError(f"File {file_name} not found in any tar archives in {tar_dir}.")