forked from google-research/android_world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimal_task_runner.py
131 lines (113 loc) · 3.92 KB
/
minimal_task_runner.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2024 The android_world Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Runs a single task.
The minimal_run.py module is used to run a single task, it is a minimal version
of the run.py module. A task can be specified, otherwise a random task is
selected.
"""
from collections.abc import Sequence
import os
import random
from typing import Type
from absl import app
from absl import flags
from absl import logging
from android_world import registry
from android_world.agents import infer
from android_world.agents import t3a
from android_world.env import env_launcher
from android_world.task_evals import task_eval
logging.set_verbosity(logging.WARNING)
os.environ['GRPC_VERBOSITY'] = 'ERROR' # Only show errors
os.environ['GRPC_TRACE'] = 'none' # Disable tracing
def _find_adb_directory() -> str:
"""Returns the directory where adb is located."""
potential_paths = [
os.path.expanduser('~/Library/Android/sdk/platform-tools/adb'),
os.path.expanduser('~/Android/Sdk/platform-tools/adb'),
]
for path in potential_paths:
if os.path.isfile(path):
return path
raise EnvironmentError(
'adb not found in the common Android SDK paths. Please install Android'
" SDK and ensure adb is in one of the expected directories. If it's"
' already installed, point to the installed location.'
)
_ADB_PATH = flags.DEFINE_string(
'adb_path',
_find_adb_directory(),
'Path to adb. Set if not installed through SDK.',
)
_EMULATOR_SETUP = flags.DEFINE_boolean(
'perform_emulator_setup',
False,
'Whether to perform emulator setup. This must be done once and only once'
' before running Android World. After an emulator is setup, this flag'
' should always be False.',
)
_DEVICE_CONSOLE_PORT = flags.DEFINE_integer(
'console_port',
5554,
'The console port of the running Android device. This can usually be'
' retrieved by looking at the output of `adb devices`. In general, the'
' first connected device is port 5554, the second is 5556, and'
' so on.',
)
_TASK = flags.DEFINE_string(
'task',
None,
'A specific task to run.',
)
def _main() -> None:
"""Runs a single task."""
env = env_launcher.load_and_setup_env(
console_port=_DEVICE_CONSOLE_PORT.value,
emulator_setup=_EMULATOR_SETUP.value,
adb_path=_ADB_PATH.value,
)
env_launcher.verify_api_level(env)
env.reset(go_home=True)
task_registry = registry.TaskRegistry()
aw_registry = task_registry.get_registry(task_registry.ANDROID_WORLD_FAMILY)
if _TASK.value:
if _TASK.value not in aw_registry:
raise ValueError('Task {} not found in registry.'.format(_TASK.value))
task_type: Type[task_eval.TaskEval] = aw_registry[_TASK.value]
else:
task_type: Type[task_eval.TaskEval] = random.choice(
list(aw_registry.values())
)
params = task_type.generate_random_params()
task = task_type(params)
task.initialize_task(env)
agent = t3a.T3A(env, infer.Gpt4Wrapper('gpt-4-turbo-2024-04-09'))
print('Goal: ' + str(task.goal))
is_done = False
for _ in range(task.complexity * 10):
response = agent.step(task.goal)
if response.done:
is_done = True
break
agent_successful = is_done and task.is_successful(env) == 1
print(
f'{"Task Successful ✅" if agent_successful else "Task Failed ❌"};'
f' {task.goal}'
)
env.close()
def main(argv: Sequence[str]) -> None:
del argv
_main()
if __name__ == '__main__':
app.run(main)