SeJinPark commited on
Commit
2535762
1 Parent(s): 46346f4

add metadata

Browse files
Multidialog.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import csv
16
+ import os
17
+ import json
18
+ import datasets
19
+
20
+ _CITATION = """\
21
+ """
22
+
23
+ _DESCRIPTION = """\
24
+ Multidialog is the first large-sccale multimodal (i.e. audio, visual, and text) dialogue corpus, consisting of approximately 400 hours of audio-visual conversation strems between 6 pairs of conversation partners.
25
+
26
+ It contina
27
+ """
28
+
29
+ _HOMEPAGE = "https://multidialog.github.io/"
30
+
31
+ _LICENSE = "Apache License 2.0"
32
+
33
+ _BASE_DATA_URL = "https://huggingface.co/datasets/IVLLab/MultiDialog/resolve/main/"
34
+
35
+ _AUDIO_ARCHIVE_URL = _BASE_DATA_URL + "{subset}/{subset}_chunks_{archive_id:04}.tar.gz"
36
+
37
+ _META_URL = _BASE_DATA_URL + "metadata/{subset}_metadata_{archive_id:04}.jsonl"
38
+
39
+
40
+ logger = datasets.utils.logging.get_logger(__name__)
41
+
42
+
43
+ class MultidialogConfig(datasets.BuilderConfig):
44
+ """BuilderConfig for Multidialog."""
45
+
46
+ def __init__(self, name, *args, **kwargs):
47
+ """BuilderConfig for Multidialog
48
+ """
49
+ super().__init__(name=name, *args, **kwargs)
50
+ self.subsets_to_download = (name,)
51
+
52
+
53
+ class Multidialog(datasets.GeneratorBasedBuilder):
54
+ """
55
+ """
56
+
57
+ VERSION = datasets.Version("1.0.0")
58
+
59
+ BUILDER_CONFIGS = [MultidialogConfig(name=subset) for subset in ["train", "test_freq", "test_rare", "valid_freq", "valid_rare"]]
60
+
61
+ DEFAULT_WRITER_BATCH_SIZE = 128
62
+
63
+ def _info(self):
64
+ features = datasets.Features(
65
+ {
66
+ "file_name": datasets.Value("string"),
67
+ "conv_id": datasets.Value("string"),
68
+ "utterance_id": datasets.Value("float32"),
69
+ "audio": datasets.Audio(sampling_rate=16_000),
70
+ "from": datasets.Value("string"),
71
+ "value": datasets.Value("string"),
72
+ "emotion": datasets.Value("string"),
73
+ "original_full_path": datasets.Value("string"), # relative path to full audio in original data dirs
74
+ }
75
+ )
76
+ return datasets.DatasetInfo(
77
+ description=_DESCRIPTION,
78
+ features=features,
79
+ homepage=_HOMEPAGE,
80
+ license=_LICENSE,
81
+ citation=_CITATION,
82
+ )
83
+
84
+ def _read_n_archives(self, n_archives_path):
85
+ with open(n_archives_path, encoding="utf-8") as f:
86
+ return int(f.read().strip())
87
+
88
+ def _split_generators(self, dl_manager):
89
+ splits = ("train", "test_freq", "test_rare", "valid_freq", "valid_rare")
90
+
91
+ n_archives = {
92
+ "train" : [15, 4],
93
+ "test_freq": [1, 1],
94
+ "test_rare": [1, 1],
95
+ "valid_freq": [1, 1],
96
+ "valid_rare": [1, 1],
97
+ }
98
+
99
+ # 2. prepare sharded archives with audio files
100
+ audio_archives_urls = {
101
+ split: [
102
+ _AUDIO_ARCHIVE_URL.format(subset=split, archive_id=i)
103
+ for i in range(n_archives[split][0])
104
+ ]
105
+ for split in splits
106
+ }
107
+ audio_archives_paths = dl_manager.download(audio_archives_urls)
108
+ # flatten archives paths from
109
+ # {"train": {"xs": [path1, path2,], "s": [path3], "m": [path5, path5]}, "dev": {"dev": [path6,...]}, "test": {"test": [...]}}
110
+ # to {"train": [path1, path2, path3, path4, path5], "dev": [path6, ...], "test": [...]}
111
+ audio_archives_paths = _flatten_nested_dict(audio_archives_paths)
112
+ local_audio_archives_paths = dl_manager.extract(audio_archives_paths) if not dl_manager.is_streaming \
113
+ else None
114
+
115
+ # 3. prepare sharded metadata csv files
116
+ meta_urls = {
117
+ split: [
118
+ _META_URL.format(subset=split, archiv_id=i)
119
+ for i in range(n_archives[split][1])
120
+ ]
121
+ for split in splits
122
+ }
123
+ meta_paths = dl_manager.download_and_extract(meta_urls)
124
+ meta_paths = _flatten_nested_dict(meta_paths)
125
+
126
+ if self.config.name == "test_freq":
127
+ return [
128
+ datasets.SplitGenerator(
129
+ name=datasets.Split.TEST,
130
+ gen_kwargs={
131
+ "audio_archives_iterators": [
132
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths["test_freq"]
133
+ ],
134
+ "local_audio_archives_paths": local_audio_archives_paths[
135
+ "test_freq"] if local_audio_archives_paths else None,
136
+ "meta_paths": meta_paths["test_freq"]
137
+ },
138
+ ),
139
+ ]
140
+
141
+ if self.config.name == "test_rare":
142
+ return [
143
+ datasets.SplitGenerator(
144
+ name=datasets.Split.TEST,
145
+ gen_kwargs={
146
+ "audio_archives_iterators": [
147
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths["test_rare"]
148
+ ],
149
+ "local_audio_archives_paths": local_audio_archives_paths[
150
+ "test_rare"] if local_audio_archives_paths else None,
151
+ "meta_paths": meta_paths["test_rare"]
152
+ },
153
+ ),
154
+ ]
155
+
156
+ if self.config.name == "valid_freq":
157
+ return [
158
+ datasets.SplitGenerator(
159
+ name=datasets.Split.VALIDATION,
160
+ gen_kwargs={
161
+ "audio_archives_iterators": [
162
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths["valid_freq"]
163
+ ],
164
+ "local_audio_archives_paths": local_audio_archives_paths[
165
+ "valid_freq"] if local_audio_archives_paths else None,
166
+ "meta_paths": meta_paths["valid_freq"]
167
+ },
168
+ ),
169
+ ]
170
+
171
+ if self.config.name == "valid_rare":
172
+ return [
173
+ datasets.SplitGenerator(
174
+ name=datasets.Split.VALIDATION,
175
+ gen_kwargs={
176
+ "audio_archives_iterators": [
177
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths["valid_rare"]
178
+ ],
179
+ "local_audio_archives_paths": local_audio_archives_paths[
180
+ "valid_rare"] if local_audio_archives_paths else None,
181
+ "meta_paths": meta_paths["valid_rare"]
182
+ },
183
+ ),
184
+ ]
185
+
186
+ if self.config.name == "train":
187
+ return [
188
+ datasets.SplitGenerator(
189
+ name=datasets.Split.TRAIN,
190
+ gen_kwargs={
191
+ "audio_archives_iterators": [
192
+ dl_manager.iter_archive(archive_path) for archive_path in audio_archives_paths["train"]
193
+ ],
194
+ "local_audio_archives_paths": local_audio_archives_paths[
195
+ "train"] if local_audio_archives_paths else None,
196
+ "meta_paths": meta_paths["train"]
197
+ },
198
+ ),
199
+ ]
200
+
201
+ def _generate_examples(self, audio_archives_iterators, local_audio_archives_paths, meta_paths):
202
+ assert len(audio_archives_iterators) == len(meta_paths)
203
+ if local_audio_archives_paths:
204
+ assert len(audio_archives_iterators) == len(local_audio_archives_paths)
205
+
206
+ for i, (meta_path, audio_archive_iterator) in enumerate(zip(meta_paths, audio_archives_iterators)):
207
+ meta_dict = dict()
208
+ with open(meta_path) as jsonl_file:
209
+ for line in jsonl_file:
210
+ meta_dict[os.path.filename(line["audpath"])[:-4]] = line
211
+ # data = json.loads(line.strip())
212
+ # meta_csv = csv.DictReader(csvfile)
213
+ # for line in meta_csv:
214
+
215
+
216
+ for audio_path_in_archive, audio_file in audio_archive_iterator:
217
+ # `audio_path_in_archive` is like "dev_chunks_0000/YOU1000000029_S0000095.wav"
218
+ audio_filename = os.path.split(audio_path_in_archive)[1]
219
+ audio_id = audio_filename.split(".wav")[0]
220
+ audio_meta = meta_dict[audio_id]
221
+ audio_meta["conv_id"] = audio_meta.pop("conv_id")
222
+ audio_meta["utterance_id"] = audio_meta.pop("utterance_id")
223
+ audio_meta["from"] = audio_meta.pop("from")
224
+ audio_meta["value"] = audio_meta.pop("value")
225
+ audio_meta["emotion"] = audio_meta.pop("emotion")
226
+ audio_meta["original_full_path"] = audio_meta.pop("audpath")
227
+ audio_meta["audio_id"] = audio_id
228
+
229
+ path = os.path.join(local_audio_archives_paths[i], audio_path_in_archive) if local_audio_archives_paths \
230
+ else audio_path_in_archive
231
+
232
+ yield audio_id, {
233
+ "audio": {"path": path , "bytes": audio_file.read()},
234
+ **{feature: value for feature, value in audio_meta.items() if feature in self.info.features}
235
+ }
236
+
237
+
238
+ def _flatten_nested_dict(nested_dict):
239
+ return {
240
+ key: [inner_list_element for inner_list in value_to_lists.values() for inner_list_element in inner_list]
241
+ for key, value_to_lists in nested_dict.items()
242
+ }
metadata/test_freq_metadata_0000.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/test_rare_metadata_0000.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/train_metadata_0000.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/train_metadata_0001.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/train_metadata_0002.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/train_metadata_0003.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/valid_freq_metadata_0000.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
metadata/valid_rare_metadata_0000.jsonl ADDED
The diff for this file is too large to render. See raw diff