|
|
|
|
|
from android_env.wrappers import base_wrapper |
|
from android_env.environment import AndroidEnv |
|
import numpy as np |
|
from .target_handlers import TransformerSet |
|
from typing import Union, Optional |
|
from typing import List |
|
import functools |
|
import dm_env |
|
|
|
class InstructionRewritingWrapper(base_wrapper.BaseWrapper): |
|
|
|
""" |
|
Transforms the instructions and commands of AndroidEnv. |
|
""" |
|
|
|
def __init__( self |
|
, env: AndroidEnv |
|
, search_pattern_file: str |
|
, article_pattern_file: str |
|
, article_command_pattern_file: str |
|
, categ_pattern_file: str |
|
, author_pattern_file: str |
|
, question_pattern_file: str |
|
, doccano_file: str |
|
): |
|
|
|
super(InstructionRewritingWrapper, self).__init__(env) |
|
|
|
rng = np.random.default_rng() |
|
|
|
self._transformer: TransformerSet =\ |
|
TransformerSet( search_pattern_file |
|
, article_pattern_file |
|
, article_command_pattern_file |
|
, categ_pattern_file |
|
, author_pattern_file |
|
, question_pattern_file |
|
, doccano_file |
|
, rng |
|
) |
|
|
|
self._command: Optional[List[str]] = None |
|
self._instructions: List[str] = [] |
|
|
|
|
|
def _reset_state(self): |
|
self._command = None |
|
self._instructions = [] |
|
|
|
def command(self) -> List[str]: |
|
|
|
if self._command is None: |
|
self._command = list( map( functools.partial( self._transformer.transform |
|
, environment="command" |
|
) |
|
, self._env.command() |
|
) |
|
) |
|
return self._command |
|
|
|
def task_instructions(self, latest_only: bool=False) -> Union[str, List[str]]: |
|
|
|
if latest_only: |
|
return self._instructions[-1] if len(self._instructions)>0 else "" |
|
else: |
|
return self._instructions.copy() |
|
|
|
|
|
def _process_timestep(self, timestep: dm_env.TimeStep) -> dm_env.TimeStep: |
|
|
|
self._instructions = list( map( functools.partial( self._transformer.transform |
|
, environment="instruction" |
|
) |
|
, self._env.task_instructions() |
|
) |
|
) |
|
return timestep |
|
|
|
|
|
|