|  | import json | 
					
						
						|  | from math import sqrt | 
					
						
						|  | import re | 
					
						
						|  | from nltk.translate.bleu_score import sentence_bleu | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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')] | 
					
						
						|  |  | 
					
						
						|  | 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 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 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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] | 
					
						
						|  |  | 
					
						
						|  | 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] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if gold_action == 'click' or gold == 'rightClick': | 
					
						
						|  |  | 
					
						
						|  | 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)) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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)) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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) | 
					
						
						|  |  | 
					
						
						|  |  |