|
| 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 | + self.use_html = False |
| 27 | + |
| 28 | + def set_benchmark(self, benchmark: bgym.Benchmark, demo_mode): |
| 29 | + if benchmark.name.startswith("miniwob"): |
| 30 | + self.use_html = True |
| 31 | + self.action_set_args = benchmark.high_level_action_set_args |
| 32 | + |
| 33 | + def make_agent(self): |
| 34 | + return DebugAgent(self.action_set_args, use_html=self.use_html) |
| 35 | + |
| 36 | + |
| 37 | +class DebugAgent(Agent): |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + action_set_args, |
| 41 | + use_html=False, |
| 42 | + ): |
| 43 | + self.action_set = action_set_args.make_action_set() |
| 44 | + self.use_html = use_html |
| 45 | + |
| 46 | + def obs_preprocessor(self, obs): |
| 47 | + obs = deepcopy(obs) |
| 48 | + obs["dom_txt"] = flatten_dom_to_str( |
| 49 | + obs["dom_object"], |
| 50 | + extra_properties=obs["extra_element_properties"], |
| 51 | + with_visible=True, |
| 52 | + with_clickable=True, |
| 53 | + with_center_coords=True, |
| 54 | + with_bounding_box_coords=True, |
| 55 | + filter_visible_only=False, |
| 56 | + filter_with_bid_only=False, |
| 57 | + filter_som_only=False, |
| 58 | + ) |
| 59 | + obs["axtree_txt"] = flatten_axtree_to_str( |
| 60 | + obs["axtree_object"], |
| 61 | + extra_properties=obs["extra_element_properties"], |
| 62 | + with_visible=True, |
| 63 | + with_clickable=True, |
| 64 | + with_center_coords=True, |
| 65 | + with_bounding_box_coords=True, |
| 66 | + filter_visible_only=False, |
| 67 | + filter_with_bid_only=False, |
| 68 | + filter_som_only=False, |
| 69 | + ) |
| 70 | + obs["pruned_html"] = prune_html(obs["dom_txt"]) |
| 71 | + obs["screenshot_som"] = overlay_som( |
| 72 | + obs["screenshot"], extra_properties=obs["extra_element_properties"] |
| 73 | + ) |
| 74 | + return obs |
| 75 | + |
| 76 | + def get_action(self, obs): |
| 77 | + |
| 78 | + # print(obs["pruned_html"]) |
| 79 | + print("\n") |
| 80 | + observation = obs["pruned_html"] if self.use_html else obs["axtree_txt"] |
| 81 | + action = input(observation + "\n") |
| 82 | + agent_info = AgentInfo( |
| 83 | + think="nope", |
| 84 | + chat_messages=[], |
| 85 | + stats={}, |
| 86 | + ) |
| 87 | + return action, agent_info |
| 88 | + |
| 89 | + |
| 90 | +DEBUG_AGENT = DebugAgentArgs() |
0 commit comments