|
| 1 | +from copy import deepcopy |
| 2 | +from dataclasses import asdict, dataclass |
| 3 | +from functools import partial |
| 4 | + |
| 5 | +import bgym |
| 6 | +from browsergym.experiments.agent import Agent, AgentInfo |
| 7 | +from browsergym.utils.obs import flatten_axtree_to_str, flatten_dom_to_str, overlay_som, prune_html |
| 8 | + |
| 9 | +from agentlab.agents.agent_args import AgentArgs |
| 10 | +from agentlab.llm.chat_api import BaseModelArgs |
| 11 | +from agentlab.llm.llm_utils import ParseError, image_to_png_base64_url, parse_html_tags_raise, retry |
| 12 | +from agentlab.llm.tracking import cost_tracker_decorator |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class DebugAgentArgs(AgentArgs): |
| 17 | + |
| 18 | + def __post_init__(self): |
| 19 | + try: # some attributes might be temporarily args.CrossProd for hyperparameter generation |
| 20 | + self.agent_name = f"debug".replace("/", "_") |
| 21 | + except AttributeError: |
| 22 | + pass |
| 23 | + self.action_set_args = bgym.DEFAULT_BENCHMARKS[ |
| 24 | + "miniwob_tiny_test" |
| 25 | + ]().high_level_action_set_args |
| 26 | + |
| 27 | + def set_benchmark(self, benchmark: bgym.Benchmark, demo_mode): |
| 28 | + self.action_set_args = benchmark.high_level_action_set_args |
| 29 | + |
| 30 | + def make_agent(self): |
| 31 | + return DebugAgent(self.action_set_args) |
| 32 | + |
| 33 | + |
| 34 | +class DebugAgent(Agent): |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + action_set_args, |
| 38 | + ): |
| 39 | + self.action_set = action_set_args.make_action_set() |
| 40 | + |
| 41 | + def obs_preprocessor(self, obs): |
| 42 | + obs = deepcopy(obs) |
| 43 | + obs["dom_txt"] = flatten_dom_to_str( |
| 44 | + obs["dom_object"], |
| 45 | + extra_properties=obs["extra_element_properties"], |
| 46 | + with_visible=True, |
| 47 | + with_clickable=True, |
| 48 | + with_center_coords=True, |
| 49 | + with_bounding_box_coords=True, |
| 50 | + filter_visible_only=False, |
| 51 | + filter_with_bid_only=False, |
| 52 | + filter_som_only=False, |
| 53 | + ) |
| 54 | + obs["axtree_txt"] = flatten_axtree_to_str( |
| 55 | + obs["axtree_object"], |
| 56 | + extra_properties=obs["extra_element_properties"], |
| 57 | + with_visible=True, |
| 58 | + with_clickable=True, |
| 59 | + with_center_coords=True, |
| 60 | + with_bounding_box_coords=True, |
| 61 | + filter_visible_only=False, |
| 62 | + filter_with_bid_only=False, |
| 63 | + filter_som_only=False, |
| 64 | + ) |
| 65 | + obs["pruned_html"] = prune_html(obs["dom_txt"]) |
| 66 | + obs["screenshot_som"] = overlay_som( |
| 67 | + obs["screenshot"], extra_properties=obs["extra_element_properties"] |
| 68 | + ) |
| 69 | + return obs |
| 70 | + |
| 71 | + def get_action(self, obs): |
| 72 | + |
| 73 | + # print(obs["pruned_html"]) |
| 74 | + print("\n") |
| 75 | + action = input(obs["axtree_txt"] + "\n") |
| 76 | + agent_info = AgentInfo( |
| 77 | + think="nope", |
| 78 | + chat_messages=[], |
| 79 | + stats={}, |
| 80 | + ) |
| 81 | + return action, agent_info |
| 82 | + |
| 83 | + |
| 84 | +DEBUG_AGENT = DebugAgentArgs() |
0 commit comments