-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
348 lines (293 loc) · 12.3 KB
/
utils.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
import io
import sys
import json
import base64
import requests
from openai import OpenAI
from langdetect import detect
from configs import out_root, prompts_root, cache_root, imgbed_root, OPENAI_KEY
client = OpenAI()
def perror(str):
print("\033[91m"+str+"\033[0m")
def pwarn(str):
print("\033[33m"+str+"\033[0m")
def process_multilines_output(x):
lines=x.split("\n")
label=lines[-1].strip().lower()
explanation="\n".join(lines[:-1]) if len(lines)>1 else ""
return {"label":label,"explanation":explanation}
def onlineImg_process(prompt, url, model="gpt-4-vision-preview", max_tokens=1000, temperature=0.1):
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"{url}",
},
},
],
}
],
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].message.content
def offlineImg_process(prompt, image_path, model="gpt-4-vision-preview", max_tokens=1000, temperature=0.1):
# Encode function
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Getting the base64 string
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_KEY}"
}
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": max_tokens,
"temperature": temperature
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return eval(response.text)["choices"][0]["message"]["content"]
def gpt_no_image(prompt, model="gpt-3.5-turbo", max_tokens=1000, temperature=0.1):
response = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": prompt}
],
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].message.content
def image_caption(source, is_url=True):
with open(prompts_root + "img_caption.md", "r") as f:
image_caption_prompt = f.read()
with open(cache_root + "img_caption.json","r") as f:
image_caption_cache = json.loads(f.read())
if source=="" or source is None:
return ""
elif source in image_caption_cache:
return image_caption_cache[source]
else:
# Get the image caption
try:
if is_url:
image_path=source
if "http" not in source:
image_path = imgbed_root + source
caption= onlineImg_process(image_caption_prompt, image_path, max_tokens=1000)
else:
caption= offlineImg_process(image_caption_prompt, image_path, max_tokens=1000)
image_caption_cache[source]=caption
except:
return ""
with open(cache_root+"img_caption.json","w") as f:
f.write(json.dumps(image_caption_cache))
return caption
# prompt=prompt.format(CAPTION)
def metric(labels, pred_labels):
def confusion_matrix(truth, pred):
tp = sum((l == 1 and p == 1) for l, p in zip(truth, pred))
fp = sum((l == 0 and p == 1) for l, p in zip(truth, pred))
fn = sum((l == 1 and p == 0) for l, p in zip(truth, pred))
tn = sum((l == 0 and p == 0) for l, p in zip(truth, pred))
precision = tp / (tp + fp) if tp + fp > 0 else 0
recall = tp / (tp + fn) if tp + fn > 0 else 0
f1 = 2 * precision * recall / (precision + recall) if precision + recall > 0 else 0
return tp, fp, fn, tn, precision, recall, f1
accuracy = sum((l == p) for l, p in zip(labels, pred_labels)) / len(labels)
rumor_labels = labels
rumor_pred_labels = pred_labels
non_rumor_labels = [1 - l for l in labels]
non_rumor_pred_labels = [1 - p for p in pred_labels]
rumor_metrics = confusion_matrix(rumor_labels, rumor_pred_labels)
non_rumor_metrics = confusion_matrix(non_rumor_labels, non_rumor_pred_labels)
return {
'labels': labels,
'predictions': pred_labels,
'accuracy': accuracy,
'rumor': {
'true_positives': rumor_metrics[0],
'false_positives': rumor_metrics[1],
'false_negatives': rumor_metrics[2],
'true_negatives': rumor_metrics[3],
'precision': rumor_metrics[4],
'recall': rumor_metrics[5],
'f1': rumor_metrics[6]
},
'non_rumor': {
'true_positives': non_rumor_metrics[0],
'false_positives': non_rumor_metrics[1],
'false_negatives': non_rumor_metrics[2],
'true_negatives': non_rumor_metrics[3],
'precision': non_rumor_metrics[4],
'recall': non_rumor_metrics[5],
'f1': non_rumor_metrics[6]
}
}
def write_metric_result(file_name, data, mode='w', prefix=''):
with open(file_name, mode, encoding='utf-8') as f:
if prefix:
f.write('{}\n'.format(prefix))
f.write('Labels:\n{}\nPredictions:\n{}\n\n'.format(data['labels'], data['predictions']))
f.write('Accuracy: {}\n\n'.format(data['accuracy']))
f.write('Rumor Section:\n')
f.write('True positives: {}\n'.format(data['rumor']['true_positives']))
f.write('False positives: {}\n'.format(data['rumor']['false_positives']))
f.write('False negatives: {}\n'.format(data['rumor']['false_negatives']))
f.write('True negatives: {}\n'.format(data['rumor']['true_negatives']))
f.write('Precision: {}\n'.format(data['rumor']['precision']))
f.write('Recall: {}\n'.format(data['rumor']['recall']))
f.write('F1 Score: {}\n\n'.format(data['rumor']['f1']))
f.write('Non-rumor Section:\n')
f.write('True positives: {}\n'.format(data['non_rumor']['true_positives']))
f.write('False positives: {}\n'.format(data['non_rumor']['false_positives']))
f.write('False negatives: {}\n'.format(data['non_rumor']['false_negatives']))
f.write('True negatives: {}\n'.format(data['non_rumor']['true_negatives']))
f.write('Precision: {}\n'.format(data['non_rumor']['precision']))
f.write('Recall: {}\n'.format(data['non_rumor']['recall']))
f.write('F1 Score: {}\n\n'.format(data['non_rumor']['f1']))
def stats(data_path):
with open(data_path, 'r', encoding='utf-8') as f:
data = json.load(f)
num_items = len(data)
labels = []
predictions = []
zero_shot_predictions = []
total_correct = 0
total_incorrect = 0
zero_shot_correct = 0
zero_shot_incorrect = 0
total_modified = 0
total_modified_0_to_1 = 0
total_modified_0_to_1_correct = 0
total_modified_0_to_1_incorrect = 0
total_modified_1_to_0 = 0
total_modified_1_to_0_correct = 0
total_modified_1_to_0_incorrect = 0
total_unmodified = 0
total_modified_correct = 0
total_modified_incorrect = 0
for item in data:
labels.append(item['label'])
predictions.append(item['prediction'])
zero_shot_predictions.append(item['direct'])
if item['label'] == item['direct']:
if item['prediction'] != item['label']:
total_incorrect += 1
zero_shot_correct += 1
total_modified += 1
if item['direct'] == 0:
total_modified_0_to_1 += 1
total_modified_0_to_1_incorrect += 1
else:
total_modified_1_to_0 += 1
total_modified_1_to_0_incorrect += 1
total_modified_incorrect += 1
else:
total_correct += 1
zero_shot_correct += 1
total_unmodified += 1
else:
if item['prediction'] == item['label']:
total_correct += 1
zero_shot_incorrect += 1
total_modified += 1
if item['direct'] == 0:
total_modified_0_to_1 += 1
total_modified_0_to_1_correct += 1
else:
total_modified_1_to_0 += 1
total_modified_1_to_0_correct += 1
total_modified_correct += 1
else:
total_incorrect += 1
zero_shot_incorrect += 1
total_unmodified += 1
print('Total items: {}'.format(num_items))
print('Total correct: {}'.format(total_correct))
print('Total incorrect: {}'.format(total_incorrect))
print('Total Accuracy: {}'.format(total_correct / num_items))
print('Zero-shot correct: {}'.format(zero_shot_correct))
print('Zero-shot incorrect: {}'.format(zero_shot_incorrect))
print('Zero-shot Accuracy: {}'.format(zero_shot_correct / num_items))
print(
'Total modified: {}\n\t| 0 -> 1: {}\n\t\t| Correct: {}\n\t\t| Incorrect : {}\n\t| 1-> 0: {}\n\t\t| Correct: {}\n\t\t| Incorrect : {}'.format(
total_modified, total_modified_0_to_1, total_modified_0_to_1_correct, total_modified_0_to_1_incorrect,
total_modified_1_to_0, total_modified_1_to_0_correct, total_modified_1_to_0_incorrect))
print('Total unmodified: {}'.format(total_unmodified))
print('Total modified correct: {}'.format(total_modified_correct))
print('Total modified incorrect: {}'.format(total_modified_incorrect))
def stats_str(path):
sio = io.StringIO()
sys.stdout = sio
stats(path)
sys.stdout = sys.__stdout__
sio.seek(0)
return sio.read()
def predict_region(s):
lang = detect(s)
region_map = {
'en': 'us-en',
# 'ca': 'ct-ca',
'zh-cn': 'tw-tzh',
'zh-tw': 'tw-tzh',
# 'fr': 'fr-fr',
# 'tr': 'tr-tr',
# 'nl': 'nl-nl',
}
if lang in region_map:
return region_map[lang]
else:
return 'us-en'
def save(labels, pred_labels, zero_shot_labels, current_index, all_results, output_result, output_score):
with open(output_result, 'w', encoding='utf-8') as f:
json.dump(all_results, f, ensure_ascii=False, indent=4)
with open(output_score, 'w', encoding='utf-8') as f:
f.write('Labels:\n{}\nZero-shot:\n{}\nPredictions:\n{}\nCurrent Index:{}\n'.format(labels, zero_shot_labels,
pred_labels, current_index))
f.write(stats_str(output_result))
evaluation_result = metric(labels, pred_labels)
write_metric_result(output_score, evaluation_result, 'a', prefix='lemma section')
evaluation_result = metric(labels, zero_shot_labels)
write_metric_result(output_score, evaluation_result, 'a', prefix='zero shot section')
def save_baseline(labels, pred_labels, current_index, all_results, output_result, output_score):
with open(output_result, 'w', encoding='utf-8') as f:
json.dump(all_results, f, ensure_ascii=False, indent=4)
with open(output_score, 'w', encoding='utf-8') as f:
f.write('Labels:\n{}\nPredictions:\n{}\nCurrent Index:{}\n'.format(labels, pred_labels, current_index))
evaluation_result = metric(labels, pred_labels)
write_metric_result(output_score, evaluation_result, 'a', prefix='lemma section')
evaluation_result = metric(labels, pred_labels)
write_metric_result(output_score, evaluation_result, 'a', prefix='zero shot section')
if __name__ == '__main__':
path = 'out/lemma_twitter_output.json'
with open(path, 'r', encoding = "utf-8") as f:
data = json.load(f)
labels = [items['label'] for items in data]
preds = [items['prediction'] for items in data]
print(metric(labels, preds))
print(stats(path))