VoTuongQuan commited on
Commit
2c96496
·
verified ·
1 Parent(s): ee0b000

Update test1.py

Browse files
Files changed (1) hide show
  1. test1.py +91 -93
test1.py CHANGED
@@ -1,94 +1,92 @@
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
 
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
+ features=datasets.Features(
20
+ {
21
+ "speaker_id": datasets.Value("string"),
22
+ "path": datasets.Value("string"),
23
+ "audio": datasets.Audio(sampling_rate=16_000),
24
+ "sentence": datasets.Value("string"),
25
+ }
26
+ ),
27
+ supervised_keys=None,
28
+ )
29
+
30
+ def _split_generators(self, dl_manager):
31
+ prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS)
32
+ archive = dl_manager.download(_DATA_URL)
33
+ train_dir = "vivos_noisy/train"
34
+ test_dir = "vivos_noisy/test"
35
+
36
+ return [
37
+ datasets.SplitGenerator(
38
+ name=datasets.Split.TRAIN,
39
+ # These kwargs will be passed to _generate_examples
40
+ gen_kwargs={
41
+ "prompts_path": prompts_paths["train"],
42
+ "path_to_clips": train_dir + "/waves",
43
+ "audio_files": dl_manager.iter_archive(archive),
44
+ },
45
+ ),
46
+ datasets.SplitGenerator(
47
+ name=datasets.Split.TEST,
48
+ # These kwargs will be passed to _generate_examples
49
+ gen_kwargs={
50
+ "prompts_path": prompts_paths["test"],
51
+ "path_to_clips": test_dir + "/waves",
52
+ "audio_files": dl_manager.iter_archive(archive),
53
+ },
54
+ ),
55
+ ]
56
+
57
+ def _generate_examples(self, prompts_path, path_to_clips, audio_files):
58
+ """Yields examples as (key, example) tuples."""
59
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
60
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
61
+ examples = {}
62
+ with open(prompts_path, encoding="utf-8") as f:
63
+ for row in f:
64
+ data = row.strip().split(" ", 1)
65
+ # Extract speaker_id from the full filename
66
+ # For example: VIVOS_NOISY_VIVOSDEV01_R002_001 -> VIVOS_NOISY_VIVOSDEV01
67
+ filename_parts = data[0].split("_")
68
+ if len(filename_parts) >= 3:
69
+ # Join the first 3 parts to get the speaker_id (VIVOS_NOISY_VIVOSDEV01)
70
+ speaker_id = "_".join(filename_parts[:3])
71
+ else:
72
+ # Fallback if the naming convention is different
73
+ speaker_id = filename_parts[0]
74
+
75
+ audio_path = "/".join([path_to_clips, speaker_id, data[0] + ".wav"])
76
+ examples[audio_path] = {
77
+ "speaker_id": speaker_id,
78
+ "path": audio_path,
79
+ "sentence": data[1],
80
+ }
81
+
82
+ inside_clips_dir = False
83
+ id_ = 0
84
+ for path, f in audio_files:
85
+ if path.startswith(path_to_clips):
86
+ inside_clips_dir = True
87
+ if path in examples:
88
+ audio = {"path": path, "bytes": f.read()}
89
+ yield id_, {**examples[path], "audio": audio}
90
+ id_ += 1
91
+ elif inside_clips_dir:
 
 
92
  break