File size: 2,588 Bytes
fa1c809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Model-independent loader for GUI-LongDep. Standard library only.

    from gui_longdep import GUILongDep
    bench = GUILongDep(".")                  # folder of the downloaded dataset
    for sample in bench.samples:
        task = bench.task(sample)
        task["instruction"]                  # task instruction
        task["history"]                      # steps 0..t-1: {t, image, description, actions, code}
        task["screenshot"]                   # path of the screenshot at step t (to act on)
        # ... build your model's prompt, run it, convert its reply to pixel actions ...

Every step of a trajectory stores the upstream action in three model-independent forms:

    description  natural-language description of the action
    actions      structured actions with pixel coordinates (schema: README.md)
    code         the upstream action code as recorded (pyautogui; AgentNet uses 0..1 coordinates)

Turn ``history`` into your model's own prompt format and score its predictions, converted
to pixel actions, with ``score.py``. ``adapters/evocua`` is a complete example.
"""
from __future__ import annotations

import json
from pathlib import Path


def read_jsonl(path: str | Path) -> list[dict]:
    with open(path, encoding="utf-8") as fh:
        return [json.loads(line) for line in fh if line.strip()]


class GUILongDep:
    def __init__(self, root: str | Path = "."):
        self.root = Path(root)
        self.samples = read_jsonl(self.root / "data/test.jsonl")
        self.trajectories = {t["traj_id"]: t for t in read_jsonl(self.root / "data/trajectories.jsonl")}

    def steps(self, sample: dict) -> list[dict]:
        """Steps 0..t of the sample's trajectory (step t is the one to predict)."""
        traj = self.trajectories[sample["traj_id"]]
        t = int(sample["step"])
        steps = traj["steps"][: t + 1]
        if [s["t"] for s in steps] != list(range(t + 1)):
            raise ValueError(f"{sample['sample_id']}: trajectory steps 0..{t} are incomplete")
        return steps

    def image_path(self, step: dict) -> Path:
        return self.root / step["image"]

    def task(self, sample: dict) -> dict:
        """What a model may see at the sample's step: instruction, steps 0..t-1, screenshot t."""
        traj = self.trajectories[sample["traj_id"]]
        steps = self.steps(sample)
        return {
            "instruction": traj["instruction"],
            "width": traj["width"],
            "height": traj["height"],
            "history": steps[:-1],
            "screenshot": self.image_path(steps[-1]),
        }