def fields_factory(task, utterance): if isinstance(utterance, dict): fields = Fields(utterance["fields"]) else: fields = get_field_extractor(task)(utterance) fields._d.pop("dummy", None) if not len(fields): # assert False, str(task) fields._d["objective"] = utterance return fields
def __init__(self, index, subdomain, seed, headless=False, base_url=None, cache_state=False, threading=True, reward_processor=None, wait_ms=0., block_on_reset=True, refresh_freq=0, initial_mode='train'): """Starts a new Selenium WebDriver session. Args: index (int): Instance index subdomain (str): MiniWoB task name (e.g., "click-test") seed (object): Random seed headless (bool): Whether to render GUI base_url (str): Base URL (default to localhost at port 8000) cache_state (bool): Whether to cache and return the initial state; only make sense if the task interface never changes threading (bool): Whether to run this instance as a Thread reward_processor (callable; optional): A function that takes the metadata and return a reward (see miniwob.reward) wait_ms (float): Pause the instance after each action for this amount of time (in milliseconds). block_on_reset (bool): On reset, block until the page loads. refresh_freq (int): Every this number of episodes, refresh the page at the beginning of the next episode. Takes time but cleans up any lingering states and memory leaks. *** Must specify `seeds` at each reset call. initial_mode (str): Initial data mode (e.g., "train", "test") """ super(MiniWoBInstance, self).__init__() # Overrides Thread.daemon: Kill this thread when the parent is killed self.daemon = True self.died = False self.index = index self.init_seed = repr(seed) self.headless = headless base_url = base_url or self.DEFAULT_BASE_URL if subdomain.startswith('flight.'): assert not base_url.startswith('file://'),\ ('For {} domain, MINIWOB_BASE_URL cannot be file://. ' ' See "Run a simple server" in README').format(subdomain) self.url = urllib.parse.urljoin( base_url, subdomain.replace('.', '/') + '/wrapper.html') self.window_width = self.FLIGHT_WINDOW_WIDTH self.window_height = self.FLIGHT_WINDOW_HEIGHT self.task_width = self.FLIGHT_TASK_WIDTH self.task_height = self.FLIGHT_TASK_HEIGHT else: self.url = urllib.parse.urljoin( base_url, 'miniwob/{}.html'.format(subdomain)) self.window_width = self.WINDOW_WIDTH self.window_height = self.WINDOW_HEIGHT self.task_width = self.TASK_WIDTH self.task_height = self.TASK_HEIGHT self.field_extractor = get_field_extractor(subdomain) self.cache_state = cache_state self.threading = threading self.reward_processor = reward_processor self.wait_ms = wait_ms self.block_on_reset = block_on_reset self.refresh_freq = refresh_freq self.num_episodes = 0 self.mode = initial_mode self.record_screenshots = False if reward_processor is None: # Use the original reward self.reward_processor = get_original_reward self.start_time = float('inf') self.task_queue = Queue() if not threading: # Hack: override the start method of Thread self.start = self.create_driver