Commit
•
9b337f2
1
Parent(s):
0140257
Create dataset.py
Browse files- dataset.py +44 -0
dataset.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
|
3 |
+
class AudioDataset(datasets.GeneratorBasedBuilder):
|
4 |
+
def _info(self):
|
5 |
+
return datasets.DatasetInfo(
|
6 |
+
features=datasets.Features(
|
7 |
+
{
|
8 |
+
"file_name": datasets.Value("string"),
|
9 |
+
"transcription": datasets.Value("string"),
|
10 |
+
"speaker_id": datasets.Value("int64"),
|
11 |
+
"audio": datasets.Audio(sampling_rate=16_000), # adjust the sampling rate if needed
|
12 |
+
}
|
13 |
+
),
|
14 |
+
)
|
15 |
+
|
16 |
+
def _split_generators(self, dl_manager):
|
17 |
+
data_dir = "data" # Base directory containing both metadata and audio subdirectory
|
18 |
+
return [
|
19 |
+
datasets.SplitGenerator(
|
20 |
+
name=datasets.Split.TRAIN,
|
21 |
+
gen_kwargs={"filepath": f"{data_dir}/train_metadata.csv", "audio_dir": f"{data_dir}/audio"},
|
22 |
+
),
|
23 |
+
datasets.SplitGenerator(
|
24 |
+
name=datasets.Split.TEST,
|
25 |
+
gen_kwargs={"filepath": f"{data_dir}/test_metadata.csv", "audio_dir": f"{data_dir}/audio"},
|
26 |
+
),
|
27 |
+
]
|
28 |
+
|
29 |
+
def _generate_examples(self, filepath, audio_dir):
|
30 |
+
with open(filepath, "r", encoding="utf-8") as f:
|
31 |
+
for id_, line in enumerate(f):
|
32 |
+
if id_ == 0:
|
33 |
+
continue # skip header
|
34 |
+
parts = line.strip().split(",")
|
35 |
+
file_name = parts[0]
|
36 |
+
transcription = parts[1]
|
37 |
+
speaker_id = int(parts[2])
|
38 |
+
yield id_, {
|
39 |
+
"file_name": file_name,
|
40 |
+
"transcription": transcription,
|
41 |
+
"speaker_id": speaker_id,
|
42 |
+
"audio": f"{audio_dir}/{file_name.split('/')[-1]}", # Extract the filename from the path and prepend the audio directory
|
43 |
+
}
|
44 |
+
|