phdkhanh2507 commited on
Commit
d3012d1
·
verified ·
1 Parent(s): f6ef36f

Upload denoised-vietnamese-audio.py

Browse files
Files changed (1) hide show
  1. denoised-vietnamese-audio.py +132 -0
denoised-vietnamese-audio.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2023 Thinh T. Duong
2
+ import os
3
+ import datasets
4
+ from huggingface_hub import HfFileSystem
5
+ from typing import List, Tuple
6
+
7
+
8
+ logger = datasets.logging.get_logger(__name__)
9
+ fs = HfFileSystem()
10
+
11
+
12
+ _CITATION = """
13
+
14
+ """
15
+ _DESCRIPTION = """
16
+ This dataset contain denoised audio of Vietnamese speakers.
17
+ """
18
+ _HOMEPAGE = "https://github.com/tanthinhdt/vietnamese-av-asr"
19
+ _REPO_PATH = "datasets/phdkhanh2507/denoised-vietnamese-audio"
20
+ _REPO_URL = f"https://huggingface.co/{_REPO_PATH}/resolve/main"
21
+ _URLS = {
22
+ "meta": f"{_REPO_URL}/metadata/" + "{channel}.parquet",
23
+ "audio": f"{_REPO_URL}/audio/" + "{channel}.zip",
24
+ }
25
+ _CONFIGS = ["all"]
26
+ if fs.exists(_REPO_PATH + "/metadata"):
27
+ _CONFIGS.extend([
28
+ os.path.basename(file_name)[:-8]
29
+ for file_name in fs.listdir(_REPO_PATH + "/metadata", detail=False)
30
+ if file_name.endswith(".parquet")
31
+ ])
32
+
33
+
34
+ class DenoisedVietnameseAudioConfig(datasets.BuilderConfig):
35
+ """Denoised Vietnamese Audio configuration."""
36
+
37
+ def __init__(self, name, **kwargs):
38
+ """
39
+ :param name: Name of subset.
40
+ :param kwargs: Arguments.
41
+ """
42
+ super().__init__(
43
+ name=name,
44
+ version=datasets.Version("1.0.0"),
45
+ description=_DESCRIPTION,
46
+ **kwargs,
47
+ )
48
+
49
+
50
+ class DenoisedVietnameseAudio(datasets.GeneratorBasedBuilder):
51
+ """Denoised Vietnamese Audio dataset."""
52
+
53
+ BUILDER_CONFIGS = [DenoisedVietnameseAudioConfig(name) for name in _CONFIGS]
54
+ DEFAULT_CONFIG_NAME = "all"
55
+
56
+ def _info(self) -> datasets.DatasetInfo:
57
+ features = datasets.Features({
58
+ "id": datasets.Value("string"),
59
+ "channel": datasets.Value("string"),
60
+ "duration": datasets.Value("float64"),
61
+ "fps": datasets.Value("int8"),
62
+ "audio": datasets.Value("string"),
63
+ "sampling_rate": datasets.Value("int64"),
64
+ })
65
+
66
+ return datasets.DatasetInfo(
67
+ description=_DESCRIPTION,
68
+ features=features,
69
+ homepage=_HOMEPAGE,
70
+ citation=_CITATION,
71
+ )
72
+
73
+ def _split_generators(
74
+ self, dl_manager: datasets.DownloadManager
75
+ ) -> List[datasets.SplitGenerator]:
76
+ """
77
+ Get splits.
78
+ :param dl_manager: Download manager.
79
+ :return: Splits.
80
+ """
81
+ config_names = _CONFIGS[1:] if self.config.name == "all" else [self.config.name]
82
+
83
+ metadata_paths = dl_manager.download(
84
+ [_URLS["meta"].format(channel=channel) for channel in config_names]
85
+ )
86
+ audio_dirs = dl_manager.download_and_extract(
87
+ [_URLS["audio"].format(channel=channel) for channel in config_names]
88
+ )
89
+
90
+ audio_dict = {
91
+ channel: audio_dir for channel, audio_dir in zip(config_names, audio_dirs)
92
+ }
93
+
94
+ return [
95
+ datasets.SplitGenerator(
96
+ name=datasets.Split.TRAIN,
97
+ gen_kwargs={
98
+ "metadata_paths": metadata_paths,
99
+ "audio_dict": audio_dict,
100
+ },
101
+ ),
102
+ ]
103
+
104
+ def _generate_examples(
105
+ self, metadata_paths: List[str],
106
+ audio_dict: dict,
107
+ ) -> Tuple[int, dict]:
108
+ """
109
+ Generate examples from metadata.
110
+ :param metadata_paths: Paths to metadata.
111
+ :param audio_dict: Paths to directory containing audio.
112
+ :yield: Example.
113
+ """
114
+ dataset = datasets.load_dataset(
115
+ "parquet",
116
+ data_files=metadata_paths,
117
+ split="train",
118
+ )
119
+ for i, sample in enumerate(dataset):
120
+ channel = sample["channel"]
121
+ audio_path = os.path.join(
122
+ audio_dict[channel], channel, sample["id"] + ".wav"
123
+ )
124
+
125
+ yield i, {
126
+ "id": sample["id"],
127
+ "channel": channel,
128
+ "duration": sample["duration"],
129
+ "fps": sample["fps"],
130
+ "audio": audio_path,
131
+ "sampling_rate": sample["sampling_rate"],
132
+ }