Update mypin-voice-dataset.py
Browse files- mypin-voice-dataset.py +34 -7
mypin-voice-dataset.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import datasets
|
2 |
import os
|
|
|
3 |
|
4 |
class MyDataset(datasets.GeneratorBasedBuilder):
|
5 |
def _info(self):
|
@@ -21,17 +22,43 @@ class MyDataset(datasets.GeneratorBasedBuilder):
|
|
21 |
return [
|
22 |
datasets.SplitGenerator(
|
23 |
name=datasets.Split.TRAIN,
|
24 |
-
gen_kwargs={
|
|
|
|
|
|
|
|
|
|
|
25 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
]
|
27 |
|
28 |
def _generate_examples(self, filepath, audio_dir):
|
29 |
import json
|
30 |
|
31 |
with open(filepath, "r") as f:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import datasets
|
2 |
import os
|
3 |
+
import random
|
4 |
|
5 |
class MyDataset(datasets.GeneratorBasedBuilder):
|
6 |
def _info(self):
|
|
|
22 |
return [
|
23 |
datasets.SplitGenerator(
|
24 |
name=datasets.Split.TRAIN,
|
25 |
+
gen_kwargs={
|
26 |
+
"filepath": metadata,
|
27 |
+
"audio_dir": data_dir
|
28 |
+
"split": "train",
|
29 |
+
"split_ratio": 0.8,
|
30 |
+
},
|
31 |
),
|
32 |
+
datasets.SplitGenerator(
|
33 |
+
name=datasets.Split.VALIDATION,
|
34 |
+
gen_kwargs={
|
35 |
+
"filepath": metadata,
|
36 |
+
"audio_dir": data_dir
|
37 |
+
"split": "eval",
|
38 |
+
"split_ratio": 0.2,
|
39 |
+
},
|
40 |
+
)
|
41 |
]
|
42 |
|
43 |
def _generate_examples(self, filepath, audio_dir):
|
44 |
import json
|
45 |
|
46 |
with open(filepath, "r") as f:
|
47 |
+
data = [json.loads(line) for line in f]
|
48 |
+
|
49 |
+
# Shuffle data for randomness
|
50 |
+
random.shuffle(data)
|
51 |
+
|
52 |
+
# Split data into train and eval
|
53 |
+
split_index = int(len(data) * split_ratio)
|
54 |
+
if split == "train":
|
55 |
+
examples = data[:split_index]
|
56 |
+
else:
|
57 |
+
examples = data[split_index:]
|
58 |
+
|
59 |
+
for idx, line in enumerate(f):
|
60 |
+
record = json.loads(line)
|
61 |
+
yield idx, {
|
62 |
+
"sentence": record["sentence"],
|
63 |
+
"audio": os.path.join(audio_dir, record["file_name"]), # Path to the audio file
|
64 |
+
}
|