File size: 3,155 Bytes
d58c721 12d83cf d58c721 12d83cf d58c721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# Created by Danyang Zhang @X-Lance.
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):
# class InstructionRewritingWrapper {{{ #
"""
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
):
# method __init__ {{{ #
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] = []
# }}} method __init__ #
def _reset_state(self):
self._command = None
self._instructions = []
def command(self) -> List[str]:
# method command {{{ #
if self._command is None:
self._command = list( map( functools.partial( self._transformer.transform
, environment="command"
)
, self._env.command()
)
)
return self._command
# }}} method command #
def task_instructions(self, latest_only: bool=False) -> Union[str, List[str]]:
# method task_instructions {{{ #
if latest_only:
return self._instructions[-1] if len(self._instructions)>0 else ""
else:
return self._instructions.copy()
# }}} method task_instructions #
def _process_timestep(self, timestep: dm_env.TimeStep) -> dm_env.TimeStep:
# method _process_timestep {{{ #
self._instructions = list( map( functools.partial( self._transformer.transform
, environment="instruction"
)
, self._env.task_instructions()
)
)
return timestep
# }}} method _process_timestep #
# }}} class InstructionRewritingWrapper #
|