VoTuongQuan commited on
Commit
2ac7b11
·
verified ·
1 Parent(s): d03d437

Upload vivos_noisy.py

Browse files
Files changed (1) hide show
  1. vivos_noisy.py +94 -0
vivos_noisy.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+
3
+ _DATA_URL = "data/vivos_noisy.tar.gz"
4
+
5
+ _PROMPTS_URLS = {
6
+ "train": "data/train_prompts.txt.gz",
7
+ "test": "data/test_prompts.txt.gz",
8
+ }
9
+
10
+
11
+ class VivosNoisyDataset(datasets.GeneratorBasedBuilder):
12
+ """VIVOS NOISY is a Vietnamese speech corpus with added noise, based on the original VIVOS dataset.
13
+ This corpus is prepared for Vietnamese Automatic Speech Recognition task under noisy environments."""
14
+
15
+ VERSION = datasets.Version("1.1.0")
16
+
17
+ def _info(self):
18
+ return datasets.DatasetInfo(
19
+ # This is the description that will appear on the datasets page.
20
+ description=_DESCRIPTION,
21
+ features=datasets.Features(
22
+ {
23
+ "speaker_id": datasets.Value("string"),
24
+ "path": datasets.Value("string"),
25
+ "audio": datasets.Audio(sampling_rate=16_000),
26
+ "sentence": datasets.Value("string"),
27
+ }
28
+ ),
29
+ supervised_keys=None,
30
+ )
31
+
32
+ def _split_generators(self, dl_manager):
33
+ prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS)
34
+ archive = dl_manager.download(_DATA_URL)
35
+ train_dir = "vivos_noisy/train"
36
+ test_dir = "vivos_noisy/test"
37
+
38
+ return [
39
+ datasets.SplitGenerator(
40
+ name=datasets.Split.TRAIN,
41
+ # These kwargs will be passed to _generate_examples
42
+ gen_kwargs={
43
+ "prompts_path": prompts_paths["train"],
44
+ "path_to_clips": train_dir + "/waves",
45
+ "audio_files": dl_manager.iter_archive(archive),
46
+ },
47
+ ),
48
+ datasets.SplitGenerator(
49
+ name=datasets.Split.TEST,
50
+ # These kwargs will be passed to _generate_examples
51
+ gen_kwargs={
52
+ "prompts_path": prompts_paths["test"],
53
+ "path_to_clips": test_dir + "/waves",
54
+ "audio_files": dl_manager.iter_archive(archive),
55
+ },
56
+ ),
57
+ ]
58
+
59
+ def _generate_examples(self, prompts_path, path_to_clips, audio_files):
60
+ """Yields examples as (key, example) tuples."""
61
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
62
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
63
+ examples = {}
64
+ with open(prompts_path, encoding="utf-8") as f:
65
+ for row in f:
66
+ data = row.strip().split(" ", 1)
67
+ # Extract speaker_id from the full filename
68
+ # For example: VIVOS_NOISY_VIVOSDEV01_R002_001 -> VIVOS_NOISY_VIVOSDEV01
69
+ filename_parts = data[0].split("_")
70
+ if len(filename_parts) >= 3:
71
+ # Join the first 3 parts to get the speaker_id (VIVOS_NOISY_VIVOSDEV01)
72
+ speaker_id = "_".join(filename_parts[:3])
73
+ else:
74
+ # Fallback if the naming convention is different
75
+ speaker_id = filename_parts[0]
76
+
77
+ audio_path = "/".join([path_to_clips, speaker_id, data[0] + ".wav"])
78
+ examples[audio_path] = {
79
+ "speaker_id": speaker_id,
80
+ "path": audio_path,
81
+ "sentence": data[1],
82
+ }
83
+
84
+ inside_clips_dir = False
85
+ id_ = 0
86
+ for path, f in audio_files:
87
+ if path.startswith(path_to_clips):
88
+ inside_clips_dir = True
89
+ if path in examples:
90
+ audio = {"path": path, "bytes": f.read()}
91
+ yield id_, {**examples[path], "audio": audio}
92
+ id_ += 1
93
+ elif inside_clips_dir:
94
+ break