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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from math import sqrt
|
| 3 |
+
import re
|
| 4 |
+
from nltk.translate.bleu_score import sentence_bleu
|
| 5 |
+
|
| 6 |
+
# gold label file
|
| 7 |
+
gold_fn = 'test.json'
|
| 8 |
+
|
| 9 |
+
pred_fn = 'llava-v1.5-13b.json'
|
| 10 |
+
gold = json.load(open(gold_fn))
|
| 11 |
+
pred = json.load(open(pred_fn))
|
| 12 |
+
|
| 13 |
+
sequence_match = 0
|
| 14 |
+
action_score = 0
|
| 15 |
+
total_click_penalty = 0
|
| 16 |
+
total_press_penalty = 0
|
| 17 |
+
total_write_penalty = 0
|
| 18 |
+
ideal_score = 0
|
| 19 |
+
max_click_penalty = 0
|
| 20 |
+
max_press_penalty = 0
|
| 21 |
+
max_write_penalty = 0
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def get_bounds(box: dict(), cx, cy):
|
| 26 |
+
for i in box:
|
| 27 |
+
tl = box[i]["top_left"]
|
| 28 |
+
br = box[i]["bottom_right"]
|
| 29 |
+
if (tl[0]+br[0])/2 == cx and (tl[1]+br[1])/2 == cy:
|
| 30 |
+
return (tl,br)
|
| 31 |
+
|
| 32 |
+
assert False
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def dynamic_dirichlet_l2_penalty(tl, br, px, py):
|
| 36 |
+
|
| 37 |
+
len_x = br[0] - tl[0]
|
| 38 |
+
len_y = br[1] - tl[1]
|
| 39 |
+
|
| 40 |
+
cx = ( br[0] - tl[0] ) / 2
|
| 41 |
+
cy = ( br[1] - tl[1] ) / 2
|
| 42 |
+
|
| 43 |
+
dx = abs(cx - px) - (len_x * 0.5)
|
| 44 |
+
dy = abs(cy - py) - (len_y * 0.5)
|
| 45 |
+
dist = sqrt((dx * (dx > 0)) ** 2 + (dy * (dy > 0)) ** 2)
|
| 46 |
+
|
| 47 |
+
mu = sqrt( len_x ** 2 + len_y ** 2)
|
| 48 |
+
|
| 49 |
+
score = mu / (dist+mu)
|
| 50 |
+
penalty = 1 - score
|
| 51 |
+
return penalty
|
| 52 |
+
|
| 53 |
+
for idx in gold:
|
| 54 |
+
|
| 55 |
+
gold_script = open(gold[idx]['task']).read().strip().split('\n')[2:]
|
| 56 |
+
llm_script = pred[idx].strip().split()
|
| 57 |
+
llm_script = [x for x in llm_script if x.strip().startswith('pyautogui')]
|
| 58 |
+
#find extreme case values
|
| 59 |
+
sample_weight = (len(gold_script)-0.9)
|
| 60 |
+
|
| 61 |
+
ideal_score += sample_weight
|
| 62 |
+
for gold_line in gold_script:
|
| 63 |
+
action_type = gold_line.split("pyautogui.")[1].split("(")[0]
|
| 64 |
+
if action_type == 'click' or action_type == 'rightClick' or action_type == 'moveTo' or action_type == 'dragTo':
|
| 65 |
+
max_click_penalty += sample_weight/len(gold_script)
|
| 66 |
+
if action_type == 'press' or action_type == 'hotkey':
|
| 67 |
+
max_press_penalty += sample_weight/len(gold_script)
|
| 68 |
+
if action_type == 'write':
|
| 69 |
+
max_write_penalty += sample_weight/len(gold_script)
|
| 70 |
+
|
| 71 |
+
seq_match_flag = 1
|
| 72 |
+
click_penalty = 0
|
| 73 |
+
press_penalty = 0
|
| 74 |
+
write_penalty = 0
|
| 75 |
+
|
| 76 |
+
# if length doesn't seq match is 0
|
| 77 |
+
# llm_script = llm_script[:len(gold_script)]
|
| 78 |
+
if len(llm_script) != len(gold_script):
|
| 79 |
+
seq_match_flag = 0
|
| 80 |
+
if seq_match_flag == 1:
|
| 81 |
+
for i in range(len(gold_script)):
|
| 82 |
+
gold_line = gold_script[i].strip()
|
| 83 |
+
gold_action = gold_line.split('pyautogui.')[1].split('(')[0]
|
| 84 |
+
pred_line = llm_script[i]
|
| 85 |
+
if pred_line.startswith('pyautogui.') == False:
|
| 86 |
+
seq_match_flag = 0
|
| 87 |
+
break
|
| 88 |
+
pred_action = pred_line.split('pyautogui.')[1].split('(')[0]
|
| 89 |
+
if pred_action != gold_action:
|
| 90 |
+
seq_match_flag = 0
|
| 91 |
+
break
|
| 92 |
+
|
| 93 |
+
# find penalties for correct and wrong sequences
|
| 94 |
+
box_path = gold[idx]['box']
|
| 95 |
+
box_num = box_path.split("_")[-1].split(".json")[0]
|
| 96 |
+
box_path = "_".join(box_path.split("_")[:-1])+box_num+"_boxes.json"
|
| 97 |
+
box = json.load(open(box_path))
|
| 98 |
+
|
| 99 |
+
for i in range(len(gold_script)):
|
| 100 |
+
gold_line = gold_script[i].strip()
|
| 101 |
+
gold_action = gold_line.split('pyautogui.')[1].split('(')[0]
|
| 102 |
+
# just add the penalties
|
| 103 |
+
if seq_match_flag == 0:
|
| 104 |
+
if gold_action == 'click' or gold_action == 'rightClick' or gold_action == 'moveTo' or gold_action == 'dragTo':
|
| 105 |
+
click_penalty += 1/len(gold_script)
|
| 106 |
+
if gold_action == 'press' or gold_action == 'hotkey':
|
| 107 |
+
press_penalty += 1/len(gold_script)
|
| 108 |
+
if gold_action == 'write':
|
| 109 |
+
write_penalty += 1/len(gold_script)
|
| 110 |
+
continue
|
| 111 |
+
pred_line = llm_script[i]
|
| 112 |
+
pred_action = pred_line.split('pyautogui.')[1].split('(')[0]
|
| 113 |
+
|
| 114 |
+
# l2 penalty for click
|
| 115 |
+
|
| 116 |
+
if gold_action == 'click' or gold == 'rightClick':
|
| 117 |
+
# get original box bounds
|
| 118 |
+
gold_cx = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[0]
|
| 119 |
+
gold_cy = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[1].split(')')[0]
|
| 120 |
+
tl, br = get_bounds(box, float(gold_cx), float(gold_cy))
|
| 121 |
+
|
| 122 |
+
# get predicted point
|
| 123 |
+
pred_cx = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[0]
|
| 124 |
+
pred_cy = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[1].split(')')[0]
|
| 125 |
+
|
| 126 |
+
click_penalty += (1.0/len(gold_script)) * dynamic_dirichlet_l2_penalty(tl, br, float(pred_cx), float(pred_cy))
|
| 127 |
+
|
| 128 |
+
# penalty for press
|
| 129 |
+
if gold_action == 'press':
|
| 130 |
+
gold_key = gold_line.split("\"")[1]
|
| 131 |
+
pred_key = (re.split("\"|'", pred_line))[1]
|
| 132 |
+
if gold_key.strip() != pred_key.strip():
|
| 133 |
+
press_penalty += 1/len(gold_script)
|
| 134 |
+
|
| 135 |
+
# penalty for hotkey
|
| 136 |
+
if gold_action == 'hotkey':
|
| 137 |
+
gold_keys = gold_line.split("(")[1].split(")")[0].split(",")
|
| 138 |
+
pred_keys = pred_line.split("(")[1].split(")")[0].split(",")
|
| 139 |
+
|
| 140 |
+
gold_key_set = set([x[1:-1] for x in gold_keys if len(x)>2])
|
| 141 |
+
pred_key_set = set([x[1:-1] for x in pred_keys if len(x)>2])
|
| 142 |
+
if gold_key_set != pred_key_set:
|
| 143 |
+
press_penalty += 1/len(gold_script)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if gold_action == 'write':
|
| 147 |
+
reference = [gold_line.split("\"")[1]]
|
| 148 |
+
candidate = re.split("\"|'", pred_line)[1]
|
| 149 |
+
write_penalty += (1-sentence_bleu(reference, candidate, weights=(0.5, 0.5))) / len(gold_script)
|
| 150 |
+
|
| 151 |
+
sequence_match += (seq_match_flag) * sample_weight
|
| 152 |
+
action_score += (max(seq_match_flag - click_penalty - press_penalty - write_penalty, 0)) * sample_weight
|
| 153 |
+
if seq_match_flag:
|
| 154 |
+
total_click_penalty += click_penalty * sample_weight
|
| 155 |
+
total_press_penalty += press_penalty * sample_weight
|
| 156 |
+
total_write_penalty += write_penalty * sample_weight
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
print(ideal_score)
|
| 160 |
+
print(f"Sequence match: {sequence_match/ideal_score}")
|
| 161 |
+
print(f"Action match: {action_score/ideal_score}")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
print(total_click_penalty/ideal_score)
|
| 165 |
+
print(total_press_penalty/ideal_score)
|
| 166 |
+
print(total_write_penalty/ideal_score)
|
| 167 |
+
|