-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmllm_exp.py
419 lines (350 loc) · 14.7 KB
/
mllm_exp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import os
import json
import argparse
import pickle
import re
import pandas as pd
from dotenv import load_dotenv
from tqdm import tqdm
from tenacity import retry, wait_random_exponential, stop_after_attempt
from sklearn.metrics import classification_report, confusion_matrix
import torch
from transformers import AutoProcessor, LlavaForConditionalGeneration
from PIL import Image
import get_api_res
from prompt_library import *
load_dotenv()
class MLLM_EXP:
def __init__(
self,
dataset_name,
model_name,
prompt_method,
device,
raw_image_path="MMQA_Raw/final_dataset_images/",
):
self.dataset_name = dataset_name
self.model_name = model_name
self.prompt_method = prompt_method
self.raw_image_path = raw_image_path
self.torch_device = device
if self.model_name == "llava":
self.llava_model = LlavaForConditionalGeneration.from_pretrained(
"llava-hf/llava-1.5-7b-hf",
torch_dtype=torch.float16,
).to(self.torch_device)
self.llava_processor = AutoProcessor.from_pretrained(
"llava-hf/llava-1.5-7b-hf"
)
def load_data(self):
with open(f"dataset/{self.dataset_name}", "r") as f:
data = json.load(f)
return data
def save_to_jsonl(self, data, output_file):
with open(output_file, "a") as f:
json_record = json.dumps(data, ensure_ascii=False)
f.write(json_record + "\n")
def save_to_pickle(self, data, output_file):
with open(output_file, "wb") as f:
pickle.dump(data, f)
def retrieve_text_evidence(self, text_id_list):
with open("MMQA_Raw/MMQA_texts.jsonl", "r") as f:
json_list = list(f)
for json_str in json_list:
entry = json.loads(json_str)
for text_id in text_id_list:
if text_id == entry["id"]:
return "".join(entry["text"])
def retrieve_image_path(self, image_id_list):
with open("MMQA_Raw/MMQA_images.jsonl", "r") as f:
json_list = list(f)
ret_list = []
for json_str in json_list:
entry = json.loads(json_str)
for image_id in image_id_list:
if image_id == entry["id"]:
image_name = entry["path"]
ret_list.append(f"{self.raw_image_path}{image_name}")
return ret_list
def retrieve_table_evidence(self, table_id_list):
with open("MMQA_Raw/MMQA_tables.jsonl", "r") as f:
json_list = list(f)
ret_list = []
for json_str in json_list:
entry = json.loads(json_str)
for table_id in table_id_list:
if table_id == entry["id"]:
ret_list.append(entry["table"])
return ret_list
@retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(5))
def call_gemini(self, claim, text_evidence, image_list, table_evidence):
if self.prompt_method == "closed_book":
system_prompt = closed_book_system
prompt = f"Claim: {claim}"
response = get_api_res.get_gemini_text_response(
prompt,
image_list,
model="gemini-1.5-flash",
system_prompt=system_prompt,
temperature=0.0,
)
return response
if self.prompt_method == "cot":
system_prompt = chain_of_thought
prompt = f"Claim: {claim}"
response = get_api_res.get_gemini_text_response(
prompt,
image_list,
model="gemini-1.5-flash",
system_prompt=system_prompt,
temperature=0.0,
)
return response
if self.prompt_method == "open_book":
system_prompt = open_book_system
prompt = f"""Claim: {claim}
Text Evidence: {text_evidence}
Table Evidence: {table_evidence}
Image Evidence: """
response = get_api_res.get_gemini_text_response(
prompt,
image_list,
model="gemini-1.5-flash",
system_prompt=system_prompt,
temperature=0.0,
)
return response
@retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(5))
def call_openai(self, claim, text_evidence, image_list, table_evidence):
if self.prompt_method == "closed_book":
system_prompt = closed_book_system
prompt = f"Claim: {claim}"
response = get_api_res.get_openai_text_response(
prompt,
image_list,
model="gpt-4o-mini",
system_prompt=system_prompt,
temperature=0.0,
)
return response
if self.prompt_method == "cot":
system_prompt = chain_of_thought
prompt = f"""Claim: {claim}
Text Evidence: {text_evidence}
Table Evidence: {table_evidence}
Image Evidence: """
response = get_api_res.get_openai_text_response(
prompt,
image_list,
model="gpt-4o-mini",
system_prompt=system_prompt,
temperature=0.0,
)
return response
if self.prompt_method == "open_book":
system_prompt = open_book_system
prompt = f"""Claim: {claim}
Text Evidence: {text_evidence}
Table Evidence: {table_evidence}
Image Evidence: """
response = get_api_res.get_openai_text_response(
prompt,
image_list,
model="gpt-4o-mini",
system_prompt=system_prompt,
temperature=0.0,
)
return response
def call_llava(self, claim, text_evidence, image_list, table_evidence):
if self.prompt_method == "closed_book":
system_prompt = closed_book_system
prompt = f"""A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.
###Human: {system_prompt} \nClaim: {claim}\n###Assistant:"""
inputs = self.llava_processor(text=prompt, return_tensors="pt").to(
self.torch_device
)
generate_ids = self.llava_model.generate(**inputs, max_new_tokens=10000)
res = self.llava_processor.batch_decode(
generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
return res
if self.prompt_method == "cot":
system_prompt = chain_of_thought
prompt = f"""A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.
###Human: {system_prompt} \nClaim: {claim}\n###Assistant:"""
inputs = self.llava_processor(text=prompt, return_tensors="pt").to(
self.torch_device
)
generate_ids = self.llava_model.generate(**inputs, max_new_tokens=10000)
res = self.llava_processor.batch_decode(
generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
return res
if self.prompt_method == "open_book":
system_prompt = open_book_system
if len(image_list) >= 1:
prompt = f"""A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.
###Human: <image> \n{system_prompt} \nClaim: {claim} \nText Evidence: {text_evidence} \nTable Evidence: {table_evidence}\n###Assistant:"""
image = Image.open(image_list[0])
inputs = self.llava_processor(
text=prompt, images=image, return_tensors="pt"
).to(self.torch_device)
generate_ids = self.llava_model.generate(**inputs, max_new_tokens=10000)
res = self.llava_processor.batch_decode(
generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
return res
else:
prompt = f"""A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.
###Human: \n{system_prompt} \nClaim: {claim} \nText Evidence: {text_evidence} \nTable Evidence: {table_evidence}\n###Assistant:"""
inputs = self.llava_processor(text=prompt, return_tensors="pt").to(
self.torch_device
)
generate_ids = self.llava_model.generate(**inputs, max_new_tokens=10000)
res = self.llava_processor.batch_decode(
generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
return res
def get_output(self, data):
results = []
for entry in tqdm(data, total=len(data), desc="Getting Output"):
claim = entry["claim"]
wiki_context = entry["wiki_context"]
if len(entry["text_evidence"]) != 0:
text_evidence = self.retrieve_text_evidence(entry["text_evidence"])
else:
text_evidence = ""
context = wiki_context + text_evidence
if len(entry["image_evidence"]) != 0:
image_list = self.retrieve_image_path(entry["image_evidence"])
else:
image_list = ""
if len(entry["table_evidence"]) != 0:
table_evidence = self.retrieve_table_evidence(entry["table_evidence"])
table_evidence_str = ",".join(
str(element) for element in table_evidence
)
else:
table_evidence_str = ""
if self.model_name == "gemini":
res = self.call_gemini(claim, context, image_list, table_evidence_str)
# print(res)
results.append(res)
if self.model_name == "gpt-4o-mini":
res = self.call_openai(claim, context, image_list, table_evidence_str)
# print(res)
results.append(res)
if self.model_name == "llava":
res = self.call_llava(claim, context, image_list, table_evidence_str)
# print(res)
results.append(res)
self.save_to_pickle(
results,
f"MLLM_Results/{self.dataset_name[:-5]}_{self.model_name}_{self.prompt_method}.pkl",
) # TODO: Fix dataset_name
def evaluate(self, data):
with open(
f"MLLM_Results/{self.dataset_name[:-5]}_{self.model_name}_{self.prompt_method}.pkl",
"rb",
) as f:
output = pickle.load(f)
# print(output)
if args.model == "gpt-4o-mini" or args.model == "gemini":
prediction_list, confidence_list = [], []
for i in output:
try:
pattern1 = re.compile(
r"Prediction:\s*(.*)",
re.IGNORECASE,
)
match = pattern1.match(i)[1]
prediction_list.append(re.sub("[\W_]+", "", str(match)))
except:
prediction_list.append("")
print(prediction_list)
print(len(prediction_list))
# print(confidence_list)
if args.model == "llava":
prediction_list, confidence_list = [], []
for i in output:
try:
match = i.split("Prediction:")[2].split("Explanation:")[0]
prediction_list.append(re.sub("[\W_]+", "", str(match)))
except:
prediction_list.append("")
print(prediction_list)
print(len(prediction_list))
# print(confidence_list)
num_correct = 0
gold_label = [entry["label"] for entry in data]
# gold_label = [entry["label"] for entry in data][:-1] # For Gemini 2hop
# gold_label = [entry["label"] for entry in data][:-2] # For Gemini 1hop
print(len(gold_label))
assert len(prediction_list) == len(gold_label)
# Cleanup bad output
to_remove = []
for i in range(len(gold_label)):
if prediction_list[i] != "SUPPORT" and prediction_list[i] != "REFUTE":
to_remove.append(i)
for index in sorted(to_remove, reverse=True):
del prediction_list[index]
del gold_label[index]
assert len(prediction_list) == len(gold_label)
# Calculate accuracy
for i in range(len(gold_label)):
if prediction_list[i] == gold_label[i]:
num_correct = num_correct + 1
accuracy = num_correct / len(gold_label)
print(f"Accuracy: {accuracy}")
# Print result
target_names = ["REFUTE", "SUPPORT"]
label_map = {"REFUTE": 0, "SUPPORT": 1}
labels = [label_map[e] for e in gold_label]
predictions = [label_map[e] for e in prediction_list]
print("Classification Report")
print("=" * 60)
print(
classification_report(
labels, predictions, target_names=target_names, digits=4
)
)
print(confusion_matrix(labels, predictions))
def run(self):
data = self.load_data() #[:50] # TODO: Toy Example
if not os.path.exists("MLLM_Results"):
os.makedirs("MLLM_Results")
self.get_output(data) # TODO: Get response
self.evaluate(data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="4hop.json",
help="dataset [1hop, 2hop, 3hop, 4hop]",
)
parser.add_argument(
"--model",
type=str,
default="llava",
help="model [llava, gpt-4o-mini, gemini]",
)
parser.add_argument(
"--prompt_type",
type=str,
default="open_book",
help="prompt type [open_book, closed_book, cot]",
)
parser.add_argument("--device", type=str, default="cuda:0", help="cuda device")
args = parser.parse_args()
print(args)
mllm = MLLM_EXP(args.dataset, args.model, args.prompt_type, args.device)
mllm.run()