|
| 1 | +""" |
| 2 | +A demonstration of how observation/action traces can be extracted |
| 3 | +for WorkArena tasks without modifying the task code. |
| 4 | +
|
| 5 | +Author: Alexandre Drouin (alexandre.drouin@servicenow.com) |
| 6 | +
|
| 7 | +Notes: |
| 8 | +- This approach relies on monkey patching the playwright actions to log the actions and observations. |
| 9 | + It has not been tested for parallel execution. It might work with multiprocessing, but it will for |
| 10 | + sure not work with multithreading. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +import importlib |
| 15 | +import logging |
| 16 | +import os |
| 17 | +import pickle |
| 18 | +import playwright.sync_api as playwright_sync |
| 19 | + |
| 20 | +from browsergym.core.env import BrowserEnv |
| 21 | +from browsergym.workarena import ALL_WORKARENA_TASKS |
| 22 | +from collections import defaultdict |
| 23 | +from tenacity import retry, stop_after_attempt, wait_fixed |
| 24 | +from time import time |
| 25 | + |
| 26 | + |
| 27 | +N_PER_TASK = 10 |
| 28 | + |
| 29 | + |
| 30 | +def monkey_patch_playwright(observation_callback, trace_storage): |
| 31 | + """ |
| 32 | + A function that overrides the default playwright actions to log the actions and observations. |
| 33 | +
|
| 34 | + Parameters: |
| 35 | + ------------ |
| 36 | + observation_callback: callable |
| 37 | + A function that returns the observation of the environment. |
| 38 | + trace_storage: list |
| 39 | + A list to store the trace of the actions and observations. |
| 40 | + These will be appended in-place. |
| 41 | +
|
| 42 | + """ |
| 43 | + |
| 44 | + def wrapper(func, interface): |
| 45 | + def wrapped(*args, **kwargs): |
| 46 | + # Get the observation |
| 47 | + obs = observation_callback() |
| 48 | + |
| 49 | + # Get the BID of the element on which we are acting. |
| 50 | + if interface.__name__ == "Locator": |
| 51 | + # Get the locator |
| 52 | + locator = args[0] |
| 53 | + # Get the BID |
| 54 | + bid = locator.element_handle().evaluate('(el) => el.getAttribute("bid")') |
| 55 | + elif interface.__name__ == "Keyboard": |
| 56 | + # Get the BID of the element |
| 57 | + bid = "keyboard" |
| 58 | + else: |
| 59 | + # Get the BID of the element |
| 60 | + bid = args[0].evaluate('(el) => el.getAttribute("bid")') |
| 61 | + |
| 62 | + logging.info(f"Action: {func.__name__} BID: {bid} -- Args: {args[1:]} {kwargs}") |
| 63 | + trace_storage.append( |
| 64 | + { |
| 65 | + "obs": obs, |
| 66 | + "action": func.__name__, |
| 67 | + "args": args[1:], |
| 68 | + "kwargs": kwargs, |
| 69 | + "bid": bid, |
| 70 | + "time": time(), |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + # Resume action |
| 75 | + return func(*args, **kwargs) |
| 76 | + |
| 77 | + return wrapped |
| 78 | + |
| 79 | + # Interfaces and actions we want to monkey patch |
| 80 | + importlib.reload(playwright_sync) |
| 81 | + from playwright.sync_api import Page, Frame, Locator, Keyboard, ElementHandle |
| 82 | + |
| 83 | + # TODO: Make sure the list of interfaces and actions is exhaustive |
| 84 | + # It covers all that is used in WorkArena cheats as of April 11, 2024 |
| 85 | + interfaces = [Page, Frame, Locator, Keyboard, ElementHandle] |
| 86 | + actions = ["click", "select_option", "set_checked", "fill", "press", "type", "down", "up"] |
| 87 | + |
| 88 | + for interface in interfaces: |
| 89 | + for action in actions: |
| 90 | + if hasattr(interface, action): |
| 91 | + setattr(interface, action, wrapper(getattr(interface, action), interface)) |
| 92 | + print(f"Monkey patched {interface.__name__}.{action}") |
| 93 | + |
| 94 | + |
| 95 | +@retry(stop=stop_after_attempt(3), wait=wait_fixed(2)) |
| 96 | +def extract_trace(task_cls, headless=True): |
| 97 | + """ |
| 98 | + Extracts the trace of actions and observations for a given task. |
| 99 | +
|
| 100 | + Parameters: |
| 101 | + ------------ |
| 102 | + task_cls: class |
| 103 | + The class of the task to extract the trace from. |
| 104 | +
|
| 105 | + """ |
| 106 | + # Instantiate a new environment |
| 107 | + env = BrowserEnv(task_entrypoint=task_cls, headless=headless, slow_mo=1000) |
| 108 | + |
| 109 | + # Setup customized tracing |
| 110 | + trace = [] |
| 111 | + monkey_patch_playwright(observation_callback=env._get_obs, trace_storage=trace) |
| 112 | + |
| 113 | + env.reset() |
| 114 | + env.task.cheat(env.page, env.chat.messages) |
| 115 | + env.close() |
| 116 | + |
| 117 | + return trace |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + os.makedirs("trace_profiling", exist_ok=True) |
| 122 | + |
| 123 | + task_traces = defaultdict(list) |
| 124 | + for task in ALL_WORKARENA_TASKS: |
| 125 | + print("Task:", task) |
| 126 | + for i in range(N_PER_TASK): |
| 127 | + print(f"Extracting trace {i+1}/{N_PER_TASK}") |
| 128 | + trace = extract_trace(task, headless=True) |
| 129 | + task_traces[task].append(trace) |
| 130 | + |
| 131 | + pickle.dump(task_traces, open("trace_profiling/task_traces.pkl", "wb")) |
0 commit comments