File size: 2,973 Bytes
672ecc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
import os
from pathlib import Path
import time
ROOT = Path(os.environ.get('RSI_GUI_ROOT', str(Path(__file__).resolve().parents[1])))
CACHE = Path(os.environ.get('RSI_GUI_CACHE', str(Path.home() / '.cache/rsi_anything/gui')))
CACHE.mkdir(parents=True, exist_ok=True)
PORT = int(os.environ.get('RSI_GUI_POLICY_PORT', '8767'))
BASE_MODELS = Path(os.environ.get('RSI_BASE_MODELS_DIR', str(Path.home() / '.cache/rsi_anything/base_models'))).expanduser()
BASE_WORLD = BASE_MODELS / 'Qwen2.5-Coder-3B-Instruct'
BASE_POLICY = BASE_MODELS / 'ReasonFlux-Coder-7B'

def digest(value):
    return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False).encode()).hexdigest()

def atomic(path, value):
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    temp = path.with_name(path.name + f'.{os.getpid()}.tmp')
    temp.write_text(json.dumps(value, ensure_ascii=False, indent=2))
    temp.replace(path)

def context(obs, action):
    return json.dumps({'instruction': obs['instruction'], 'visible_elements': obs['elements'], 'remaining_actions': obs['remaining_actions'], 'candidate_action': action}, sort_keys=True, ensure_ascii=False)

def file_hash(path):
    h = hashlib.sha256()
    with Path(path).open('rb') as f:
        for chunk in iter(lambda : f.read(8 * 1024 * 1024), b''):
            h.update(chunk)
    return h.hexdigest()

def atomic_jsonl(path, rows):
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    tmp = p.with_name(p.name + f'.{os.getpid()}.tmp')
    with tmp.open('w') as f:
        for row in rows:
            f.write(json.dumps(row, ensure_ascii=False) + '\n')
    tmp.replace(p)

def checkpoint_identity(path):
    if path is None:
        return 'base'
    p = Path(path)
    meta = p / 'inference.json'
    weights = p / 'gui_state.pt' if (p / 'gui_state.pt').exists() else p / 'adapter_model.safetensors'
    return digest([str(p.resolve()), file_hash(meta), file_hash(weights)])

def effective_cost(rows):
    return sum((r['policy_nominal_seconds'] + r['world_seconds'] + r['browser_seconds'] for r in rows)) / len(rows)

def goal_bits(obs):
    sentence = obs['instruction']
    (prefix, suffix) = ('Select ', ' and click Submit.')
    if not sentence.startswith(prefix) or not sentence.endswith(suffix):
        raise ValueError('Unexpected official instruction: ' + sentence)
    wanted = sentence[len(prefix):-len(suffix)]
    names = set() if wanted == 'nothing' else set(wanted.split(', '))
    labels = [x['label'] for x in obs['elements']]
    if not names.issubset(set(labels)):
        raise ValueError('Target names absent from visible labels')
    return [x in names for x in labels]

def correctness(obs):
    desired = goal_bits(obs)
    return sum((e['checked'] == g for (e, g) in zip(obs['elements'], desired))) / len(desired)

def legal_actions(obs):
    return [{'kind': 'click', 'slot': e['slot']} for e in obs['elements']] + [{'kind': 'submit'}]