oreva commited on
Commit
ee563a6
·
1 Parent(s): d3a13e5

Replaced data loading script

Browse files
Files changed (1) hide show
  1. blab_long_audio.py +352 -0
blab_long_audio.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import datasets
4
+ from datasets import Features, Value, DatasetInfo, SplitGenerator, BuilderConfig, LargeList, Sequence
5
+
6
+
7
+
8
+ TASKS = [
9
+ "word_localization",
10
+ "advertisement_localization",
11
+ "named_entity_localization",
12
+ "speaker_number_estimation",
13
+ "entire_duration",
14
+ "event_duration",
15
+ "emotion_ranking",
16
+ "emotion_reasoning",
17
+ ]
18
+
19
+ _DOCUMENT_DATASET_VERSION = "1.0.0"
20
+
21
+
22
+
23
+
24
+ # --- Main Dataset Builder Class ---
25
+ class BLAB(datasets.GeneratorBasedBuilder):
26
+ """class BLAB(object): A dataset builder supporting various audio QA tasks,
27
+ each with its own specific data schema.
28
+ """
29
+ BUILDER_CONFIGS = [
30
+ BuilderConfig(
31
+ name=task,
32
+ version=datasets.Version(_DOCUMENT_DATASET_VERSION),
33
+ description=f"BLAB dataset for task: {task}",
34
+ ) for task in TASKS
35
+ ]
36
+
37
+ def _info(self):
38
+ """Defines the dataset schema (features) based on the selected task configuration."""
39
+ # --- Schema Definitions for each individual task ---
40
+
41
+ if self.config.name == "word_localization":
42
+ return DatasetInfo(
43
+ features=Features({
44
+ "video_url": Value("string"),
45
+ "audio": Value("string"),
46
+ "question": Value("string"),
47
+ "groundtruth": LargeList(
48
+ feature=Features({
49
+ "word": Value("string"),
50
+ "start": Value("float32"),
51
+ "end": Value("float32"),
52
+ })
53
+ )
54
+ }),
55
+ description="Schema for the Word Localization task: segmenting and labeling words.",
56
+ license="MIT",
57
+ )
58
+
59
+ elif self.config.name == "advertisement_localization":
60
+ return DatasetInfo(
61
+ features=Features({
62
+ "video_url": Value("string"),
63
+ "audio": Value("string"),
64
+ "question": Value("string"),
65
+ "groundtruth": Features({
66
+ "ads_segment": LargeList(
67
+ feature=Features({
68
+ "text": Value("string"),
69
+ "start": Value("float32"),
70
+ "end": Value("float32"),
71
+ }),
72
+ ),
73
+ "word_timestamp": LargeList(
74
+ feature=Features({
75
+ "word": Value("string"),
76
+ "start": Value("float32"),
77
+ "end": Value("float32"),
78
+ }),
79
+ ),
80
+ })
81
+ }),
82
+ description="Schema for Advertisement Localization task: identifying ad segments and their transcripts.",
83
+ # ... (other metadata)
84
+ )
85
+
86
+ elif self.config.name == "named_entity_localization":
87
+ return DatasetInfo(
88
+ features=Features({
89
+ "video_url": Value("string"),
90
+ "audio": Value("string"),
91
+ "question": Value("string"),
92
+ "groundtruth": Features({
93
+ "entities": LargeList(
94
+ feature=Features({
95
+ "entity_type": Value("string"),
96
+ "entity": Value("string"),
97
+ "start": Value("float32"),
98
+ "end": Value("float32"),
99
+ }),
100
+ ),
101
+ "word_timestamp": LargeList(
102
+ feature=Features({
103
+ "word": Value("string"),
104
+ "start": Value("float32"),
105
+ "end": Value("float32"),
106
+ }),
107
+ ),
108
+ })
109
+ }),
110
+ description="Schema for Named Entity Localization task: identifying specific entities and their timestamps.",
111
+ # ... (other metadata)
112
+ )
113
+
114
+ elif self.config.name == "speaker_number_estimation":
115
+ return DatasetInfo(
116
+ features=Features({
117
+ "video_url": Value("string"),
118
+ "audio": Value("string"),
119
+ "question": Value("string"),
120
+ "groundtruth": Sequence(Value("int32"))
121
+ }),
122
+ description="Schema for Speaker Number Estimation task: counting speakers in a segment.",
123
+ # ... (other metadata)
124
+ )
125
+
126
+ elif self.config.name == "entire_duration":
127
+ return DatasetInfo(
128
+ features=Features({
129
+ "video_url": Value("string"),
130
+ "audio": Value("string"),
131
+ "question": Value("string"),
132
+ "groundtruth": Value("float32")
133
+ }),
134
+ description="Schema for Entire Duration task: determining the total duration of an audio.",
135
+
136
+ )
137
+
138
+ elif self.config.name == "event_duration":
139
+ return DatasetInfo(
140
+ features=Features({
141
+ "video_url": Value("string"),
142
+ "audio": Value("string"),
143
+ "question": Value("string"),
144
+ "groundtruth": Value("float32"),
145
+ "answer_type": Value("string"),
146
+ }),
147
+ description="Schema for Event Duration task: identifying and timing specific events.",
148
+ # ... (other metadata)
149
+ )
150
+
151
+ elif self.config.name == "emotion_ranking":
152
+ return DatasetInfo(
153
+ features=Features({
154
+ "video_url": Value("string"),
155
+ "audio": Value("string"),
156
+ "question": Value("string"),
157
+ "type": Value("string"),
158
+ "correct_option": Value("string"),
159
+ "option_A": Value("string"),
160
+ "option_B": Value("string"),
161
+ "option_C": Value("string"),
162
+ "option_D": Value("string"),
163
+ "option_E": Value("string"),
164
+ "correct_answer": Value("string"), # Stores the correct_answer string
165
+ }),
166
+ description="Schema for Emotion Ranking task: selecting the best emotion option.",
167
+ # ... (other metadata)
168
+ )
169
+
170
+ elif self.config.name == "emotion_reasoning":
171
+ return DatasetInfo(
172
+ features=Features({
173
+ "video_url": Value("string"),
174
+ "audio": Value("string"),
175
+ "question": Value("string"),
176
+ "type": Value("string"),
177
+ "correct_option": Value("string"),
178
+ "option_A": Value("string"),
179
+ "option_B": Value("string"),
180
+ "option_C": Value("string"),
181
+ "option_D": Value("string"),
182
+ "correct_answer": Value("string"), # Stores the correct_answer string
183
+ }),
184
+ description="Schema for Emotion Reasoning task: explaining emotional context.",
185
+ # ... (other metadata)
186
+ )
187
+ else:
188
+ raise ValueError(f"Unknown config name: {self.config.name}")
189
+
190
+ def _split_generators(self, dl_manager):
191
+ """Returns SplitGenerators based on the selected task configuration."""
192
+ data_files = {}
193
+
194
+ if self.config.name == "word_localization":
195
+ data_files = {"word_localization": "blab_long_audio/word_localization.json"}
196
+ elif self.config.name == "advertisement_localization":
197
+ data_files = {"advertisement_localization": "blab_long_audio/advertisement_localization.json"}
198
+ elif self.config.name == "named_entity_localization":
199
+ data_files = {"named_entity_localization": "blab_long_audio/named_entity_localization.json"}
200
+ elif self.config.name == "speaker_number_estimation":
201
+ data_files = {"speaker_number_estimation": "blab_long_audio/speaker_number_estimation.json"}
202
+ elif self.config.name == "entire_duration":
203
+ data_files = {"entire_duration": "blab_long_audio/entire_duration.json"}
204
+ elif self.config.name == "event_duration":
205
+ data_files = {"event_duration": "blab_long_audio/event_duration.json"}
206
+ elif self.config.name == "emotion_ranking":
207
+ data_files = {"emotion_ranking": "blab_long_audio/emotion_ranking.json"}
208
+ elif self.config.name == "emotion_reasoning":
209
+ data_files = {"emotion_reasoning": "blab_long_audio/emotion_reasoning.json"}
210
+ else:
211
+ raise ValueError(f"Unknown config name: {self.config.name}")
212
+
213
+ resolved_data_files = dl_manager.download_and_extract(data_files)
214
+
215
+ generators = []
216
+ for split_name, filepath in resolved_data_files.items():
217
+ generators.append(
218
+ SplitGenerator(
219
+ name=split_name,
220
+ gen_kwargs={"filepath": filepath}
221
+ )
222
+ )
223
+ return generators
224
+
225
+ def _generate_examples(self, filepath):
226
+ """Yields examples from the dataset files, parsing data based on the active config."""
227
+ with open(filepath, 'r', encoding='utf-8') as f:
228
+ all_data = json.load(f) # For .json files, load the entire array
229
+
230
+ for id_, data in enumerate(all_data):
231
+ try:
232
+ # Common fields for all tasks (handle missing with .get)
233
+ video_url = data.get("video_url", None)
234
+ audio = data.get("audio", None)
235
+ question = data.get("question", None)
236
+ #answer_type = data.get("answer_type", None)
237
+
238
+ example = {
239
+ "video_url": video_url,
240
+ "audio": audio,
241
+ "question": question,
242
+ #"answer_type": answer_type # Include as it's a common field in your schemas
243
+ }
244
+
245
+ # --- Task-specific groundtruth and other fields ---
246
+ if self.config.name == "word_localization":
247
+ raw_groundtruth = data.get("groundtruth", [])
248
+ processed_groundtruth = []
249
+ for item in raw_groundtruth:
250
+ if isinstance(item, dict):
251
+ processed_groundtruth.append({
252
+ "word": item.get("word", None),
253
+ "start": item.get("start", None),
254
+ "end": item.get("end", None),
255
+ })
256
+ example["groundtruth"] = processed_groundtruth
257
+
258
+ elif self.config.name == "advertisement_localization":
259
+ raw_groundtruth = data.get("groundtruth", {})
260
+ raw_ads_segments = raw_groundtruth.get("ads_segment", [])
261
+ processed_ads_segments = []
262
+ for ad_item in raw_ads_segments:
263
+ if isinstance(ad_item, dict):
264
+ processed_ads_segments.append({
265
+ "text": ad_item.get("text", None),
266
+ "start": ad_item.get("start", None),
267
+ "end": ad_item.get("end", None),
268
+ })
269
+ raw_word_timestamps = raw_groundtruth.get("word_timestamp", [])
270
+ processed_word_timestamps = []
271
+ for word_item in raw_word_timestamps:
272
+ if isinstance(word_item, dict):
273
+ processed_word_timestamps.append({
274
+ "word": word_item.get("word", None),
275
+ "start": word_item.get("start", None),
276
+ "end": word_item.get("end", None),
277
+ })
278
+ example["groundtruth"] = {
279
+ "ads_segment": processed_ads_segments,
280
+ "word_timestamp": processed_word_timestamps,
281
+ }
282
+
283
+ elif self.config.name == "named_entity_localization":
284
+ raw_groundtruth = data.get("groundtruth", {})
285
+ raw_entities = raw_groundtruth.get("entities", [])
286
+ processed_entities = []
287
+ for entity_item in raw_entities:
288
+ if isinstance(entity_item, dict):
289
+ processed_entities.append({
290
+ "entity_type": entity_item.get("entity_type", None),
291
+ "entity": entity_item.get("entity", None),
292
+ "start": entity_item.get("start", None),
293
+ "end": entity_item.get("end", None),
294
+ })
295
+ raw_word_timestamps = raw_groundtruth.get("word_timestamp", [])
296
+ processed_word_timestamps = []
297
+ for word_item in raw_word_timestamps:
298
+ if isinstance(word_item, dict):
299
+ processed_word_timestamps.append({
300
+ "word": word_item.get("word", None),
301
+ "start": word_item.get("start", None),
302
+ "end": word_item.get("end", None),
303
+ })
304
+ example["groundtruth"] = {
305
+ "entities": processed_entities,
306
+ "word_timestamp": processed_word_timestamps,
307
+ }
308
+
309
+ elif self.config.name == "speaker_number_estimation":
310
+ raw_groundtruth = data.get("groundtruth", None)
311
+ processed_groundtruth = []
312
+ if raw_groundtruth is not None:
313
+ if isinstance(raw_groundtruth, list):
314
+ processed_groundtruth = [int(x) for x in raw_groundtruth if isinstance(x, (int, float))]
315
+ elif isinstance(raw_groundtruth, (int, float)):
316
+ processed_groundtruth = [int(raw_groundtruth)]
317
+
318
+ example["groundtruth"] = processed_groundtruth
319
+
320
+ elif self.config.name == "entire_duration":
321
+ example["groundtruth"] = data.get("groundtruth", None) # Assuming float
322
+
323
+ elif self.config.name == "event_duration":
324
+ example["groundtruth"] = data.get("groundtruth", None)
325
+ example["answer_type"] = data.get("answer_type", None)
326
+
327
+ elif self.config.name == "emotion_ranking":
328
+ example["type"] = data.get("type", None)
329
+ example["correct_option"] = data.get("correct_option", None)
330
+ example["option_A"] = data.get("option_A", None)
331
+ example["option_B"] = data.get("option_B", None)
332
+ example["option_C"] = data.get("option_C", None)
333
+ example["option_D"] = data.get("option_D", None)
334
+ example["option_E"] = data.get("option_E", None)
335
+ example["correct_answer"] = data.get("correct_answer", None)
336
+
337
+ elif self.config.name == "emotion_reasoning":
338
+ example["type"] = data.get("type", None)
339
+ example["correct_option"] = data.get("correct_option", None)
340
+ example["option_A"] = data.get("option_A", None)
341
+ example["option_B"] = data.get("option_B", None)
342
+ example["option_C"] = data.get("option_C", None)
343
+ example["option_D"] = data.get("option_D", None)
344
+ example["correct_answer"] = data.get("correct_answer", None)
345
+
346
+ else:
347
+ raise ValueError(f"Unknown config name: {self.config.name}. This should not happen if BUILDER_CONFIGS and _info are consistent.")
348
+
349
+ yield id_, example
350
+
351
+ except Exception as e:
352
+ print(f"Error processing example {id_} in {filepath} for config {self.config.name}: {e}")