phdkhanh2507 commited on
Commit
14b9144
·
verified ·
1 Parent(s): 5770750

Upload vietnamese-speaker-clip.py

Browse files
Files changed (1) hide show
  1. vietnamese-speaker-clip.py +147 -0
vietnamese-speaker-clip.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 short clips of Vietnamese speakers.
17
+ """
18
+ _HOMEPAGE = "https://github.com/tanthinhdt/vietnamese-av-asr"
19
+ _REPO_PATH = "datasets/phdkhanh2507/vietnamese-speaker-clip"
20
+ _REPO_URL = f"https://huggingface.co/{_REPO_PATH}/resolve/main"
21
+ _URLS = {
22
+ "meta": f"{_REPO_URL}/metadata/" + "{channel}.parquet",
23
+ "visual": f"{_REPO_URL}/visual/" + "{channel}.zip",
24
+ "audio": f"{_REPO_URL}/audio/" + "{channel}.zip",
25
+ }
26
+ _CONFIGS = ["all"]
27
+ if fs.exists(_REPO_PATH + "/metadata"):
28
+ _CONFIGS.extend([
29
+ os.path.basename(file_name)[:-8]
30
+ for file_name in fs.listdir(_REPO_PATH + "/metadata", detail=False)
31
+ if file_name.endswith(".parquet")
32
+ ])
33
+
34
+
35
+ class VietnameseSpeakerClipConfig(datasets.BuilderConfig):
36
+ """Vietnamese Speaker Clip configuration."""
37
+
38
+ def __init__(self, name, **kwargs):
39
+ """
40
+ :param name: Name of subset.
41
+ :param kwargs: Arguments.
42
+ """
43
+ super(VietnameseSpeakerClipConfig, self).__init__(
44
+ name=name,
45
+ version=datasets.Version("1.0.0"),
46
+ description=_DESCRIPTION,
47
+ **kwargs,
48
+ )
49
+
50
+
51
+ class VietnameseSpeakerClip(datasets.GeneratorBasedBuilder):
52
+ """Vietnamese Speaker Clip dataset."""
53
+
54
+ BUILDER_CONFIGS = [VietnameseSpeakerClipConfig(name) for name in _CONFIGS]
55
+ DEFAULT_CONFIG_NAME = "all"
56
+
57
+ def _info(self) -> datasets.DatasetInfo:
58
+ features = datasets.Features({
59
+ "id": datasets.Value("string"),
60
+ "channel": datasets.Value("string"),
61
+ "visual": datasets.Value("string"),
62
+ "duration": datasets.Value("float64"),
63
+ "fps": datasets.Value("int8"),
64
+ "audio": datasets.Value("string"),
65
+ "sampling_rate": datasets.Value("int64"),
66
+ })
67
+
68
+ return datasets.DatasetInfo(
69
+ description=_DESCRIPTION,
70
+ features=features,
71
+ homepage=_HOMEPAGE,
72
+ citation=_CITATION,
73
+ )
74
+
75
+ def _split_generators(
76
+ self, dl_manager: datasets.DownloadManager
77
+ ) -> List[datasets.SplitGenerator]:
78
+ """
79
+ Get splits.
80
+ :param dl_manager: Download manager.
81
+ :return: Splits.
82
+ """
83
+ config_names = _CONFIGS[1:] if self.config.name == "all" else [self.config.name]
84
+
85
+ metadata_paths = dl_manager.download(
86
+ [_URLS["meta"].format(channel=channel) for channel in config_names]
87
+ )
88
+ visual_dirs = dl_manager.download_and_extract(
89
+ [_URLS["visual"].format(channel=channel) for channel in config_names]
90
+ )
91
+ audio_dirs = dl_manager.download_and_extract(
92
+ [_URLS["audio"].format(channel=channel) for channel in config_names]
93
+ )
94
+
95
+ visual_dict = {
96
+ channel: visual_dir for channel, visual_dir in zip(config_names, visual_dirs)
97
+ }
98
+ audio_dict = {
99
+ channel: audio_dir for channel, audio_dir in zip(config_names, audio_dirs)
100
+ }
101
+
102
+ return [
103
+ datasets.SplitGenerator(
104
+ name=datasets.Split.TRAIN,
105
+ gen_kwargs={
106
+ "metadata_paths": metadata_paths,
107
+ "visual_dict": visual_dict,
108
+ "audio_dict": audio_dict,
109
+ },
110
+ ),
111
+ ]
112
+
113
+ def _generate_examples(
114
+ self, metadata_paths: List[str],
115
+ visual_dict: dict,
116
+ audio_dict: dict,
117
+ ) -> Tuple[int, dict]:
118
+ """
119
+ Generate examples from metadata.
120
+ :param metadata_paths: Paths to metadata.
121
+ :param visual_dict: Paths to directory containing visual data.
122
+ :param audio_dict: Paths to directory containing audio data.
123
+ :yield: Example.
124
+ """
125
+ dataset = datasets.load_dataset(
126
+ "parquet",
127
+ data_files=metadata_paths,
128
+ split="train",
129
+ )
130
+ for i, sample in enumerate(dataset):
131
+ channel = sample["channel"]
132
+ visual_path = os.path.join(
133
+ visual_dict[channel], channel, sample["id"] + ".mp4"
134
+ )
135
+ audio_path = os.path.join(
136
+ audio_dict[channel], channel, sample["id"] + ".wav"
137
+ )
138
+
139
+ yield i, {
140
+ "id": sample["id"],
141
+ "channel": channel,
142
+ "visual": visual_path,
143
+ "duration": sample["duration"],
144
+ "fps": sample["fps"],
145
+ "audio": audio_path,
146
+ "sampling_rate": sample["sampling_rate"],
147
+ }