File size: 6,124 Bytes
5e4bcb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from math import sqrt
import re
from nltk.translate.bleu_score import sentence_bleu

# gold label file
gold_fn = 'test.json'

pred_fn = 'llava-v1.5-13b.json'
gold = json.load(open(gold_fn))
pred = json.load(open(pred_fn))

sequence_match = 0
action_score = 0
total_click_penalty = 0
total_press_penalty = 0
total_write_penalty = 0
ideal_score = 0
max_click_penalty = 0
max_press_penalty = 0
max_write_penalty = 0



def get_bounds(box: dict(), cx, cy):
    for i in box:
        tl = box[i]["top_left"]
        br = box[i]["bottom_right"]
        if (tl[0]+br[0])/2 == cx and (tl[1]+br[1])/2 == cy:
            return (tl,br)
    
    assert False

    
def dynamic_dirichlet_l2_penalty(tl, br, px, py):
    
    len_x = br[0] - tl[0]
    len_y = br[1] - tl[1]
    
    cx = ( br[0] - tl[0] ) / 2
    cy = ( br[1] - tl[1] ) / 2
    
    dx = abs(cx - px) - (len_x * 0.5)
    dy = abs(cy - py) - (len_y * 0.5)
    dist = sqrt((dx * (dx > 0)) ** 2 + (dy * (dy > 0)) ** 2)
    
    mu = sqrt( len_x ** 2 + len_y ** 2)
    
    score = mu / (dist+mu)
    penalty = 1 - score
    return penalty

for idx in gold:
    
    gold_script = open(gold[idx]['task']).read().strip().split('\n')[2:]
    llm_script = pred[idx].strip().split()
    llm_script = [x for x in llm_script if x.strip().startswith('pyautogui')]
    #find extreme case values
    sample_weight = (len(gold_script)-0.9)

    ideal_score += sample_weight
    for gold_line in gold_script:
        action_type = gold_line.split("pyautogui.")[1].split("(")[0]
        if action_type == 'click' or action_type == 'rightClick' or action_type == 'moveTo' or action_type == 'dragTo':
            max_click_penalty += sample_weight/len(gold_script)
        if action_type == 'press' or action_type == 'hotkey':
            max_press_penalty += sample_weight/len(gold_script)
        if action_type == 'write':
            max_write_penalty += sample_weight/len(gold_script)
       
    seq_match_flag = 1
    click_penalty = 0
    press_penalty = 0
    write_penalty = 0
    
    # if length doesn't seq match is 0
    # llm_script = llm_script[:len(gold_script)]
    if len(llm_script) != len(gold_script):
        seq_match_flag = 0
    if seq_match_flag == 1:
        for i in range(len(gold_script)):
            gold_line = gold_script[i].strip()
            gold_action = gold_line.split('pyautogui.')[1].split('(')[0]
            pred_line = llm_script[i]
            if pred_line.startswith('pyautogui.') == False:
                seq_match_flag = 0
                break
            pred_action = pred_line.split('pyautogui.')[1].split('(')[0]
            if pred_action != gold_action:
                seq_match_flag = 0
                break
        
    # find penalties for correct and wrong sequences 
    box_path = gold[idx]['box']
    box_num = box_path.split("_")[-1].split(".json")[0]
    box_path = "_".join(box_path.split("_")[:-1])+box_num+"_boxes.json"
    box = json.load(open(box_path))

    for i in range(len(gold_script)):
        gold_line = gold_script[i].strip()
        gold_action = gold_line.split('pyautogui.')[1].split('(')[0]
        # just add the penalties
        if seq_match_flag == 0:
            if gold_action == 'click' or gold_action == 'rightClick' or gold_action == 'moveTo' or gold_action == 'dragTo':
                click_penalty += 1/len(gold_script)
            if gold_action == 'press' or gold_action == 'hotkey':
                press_penalty += 1/len(gold_script)
            if gold_action == 'write':
                write_penalty += 1/len(gold_script)
            continue   
        pred_line = llm_script[i]
        pred_action = pred_line.split('pyautogui.')[1].split('(')[0]

        # l2 penalty for click
        
        if gold_action == 'click' or gold == 'rightClick':
            # get original box bounds
            gold_cx = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[0]
            gold_cy = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[1].split(')')[0]
            tl, br = get_bounds(box, float(gold_cx), float(gold_cy))
            
            # get predicted point
            pred_cx = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[0]
            pred_cy = gold_line.split("pyautogui.")[1].split('(')[1].split(',')[1].split(')')[0]
            
            click_penalty += (1.0/len(gold_script)) * dynamic_dirichlet_l2_penalty(tl, br, float(pred_cx), float(pred_cy))
            
        # penalty for press
        if gold_action == 'press':
            gold_key = gold_line.split("\"")[1]
            pred_key = (re.split("\"|'", pred_line))[1]
            if gold_key.strip() != pred_key.strip():
                press_penalty += 1/len(gold_script)
            
        # penalty for hotkey
        if gold_action == 'hotkey':
            gold_keys = gold_line.split("(")[1].split(")")[0].split(",")
            pred_keys = pred_line.split("(")[1].split(")")[0].split(",")
            
            gold_key_set = set([x[1:-1] for x in gold_keys if len(x)>2])
            pred_key_set = set([x[1:-1] for x in pred_keys if len(x)>2])
            if gold_key_set != pred_key_set:
                press_penalty += 1/len(gold_script)
    
    
        if gold_action == 'write':
            reference = [gold_line.split("\"")[1]]
            candidate = re.split("\"|'", pred_line)[1]
            write_penalty += (1-sentence_bleu(reference, candidate, weights=(0.5, 0.5))) / len(gold_script)
            
    sequence_match += (seq_match_flag) * sample_weight
    action_score += (max(seq_match_flag - click_penalty - press_penalty - write_penalty, 0)) * sample_weight
    if seq_match_flag:
        total_click_penalty += click_penalty  * sample_weight
        total_press_penalty += press_penalty * sample_weight
        total_write_penalty += write_penalty * sample_weight
    

print(ideal_score)   
print(f"Sequence match: {sequence_match/ideal_score}")
print(f"Action match: {action_score/ideal_score}")
    
    
print(total_click_penalty/ideal_score)
print(total_press_penalty/ideal_score)
print(total_write_penalty/ideal_score)