forked from google-research/android_world
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminimal_task_runner.py
More file actions
163 lines (138 loc) · 4.99 KB
/
minimal_task_runner.py
File metadata and controls
163 lines (138 loc) · 4.99 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2025 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
#
# http://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 autodev_agent
from android_world.env import env_launcher
from android_world.task_evals import task_eval
from android_world.task_evals.information_retrieval import information_retrieval
from android_world.task_evals.information_retrieval import proto_utils
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.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)
# Print expected answer if this is an InformationRetrieval task
if isinstance(task, information_retrieval.InformationRetrieval):
try:
expected_answer = proto_utils.get_expected_answer(task.task)
print(f'Expected answer: {expected_answer}')
except Exception as e:
print(f'Could not get expected answer: {e}')
# Use AutoDev agent with logging enabled
task_name = task_type.__name__
agent = autodev_agent.AutoDev(
env,
name="autodev",
enable_logging=True,
task_name=task_name,
)
print('Goal: ' + str(task.goal))
is_done = False
for _ in range(int(task.complexity * 10)):
response = agent.step(task.goal)
if response.done:
is_done = True
break
# Check validation and log debugging info (handled by AutoDev agent)
success_score = agent.log_task_validation(task, env)
agent_successful = is_done and success_score == 1.0
# Update logger with success status
if agent.enable_logging and agent.logger:
agent.logger.set_success(
success=agent_successful,
reason=f"Task {'completed successfully' if agent_successful else 'failed'}"
)
agent.logger.end_run(status="completed")
print(
f'{"Task Successful ✅" if agent_successful else "Task Failed ❌"};'
f' {task.goal}'
)
if agent.enable_logging and agent.logger:
print(f"📝 Logs saved to: {agent.logger.current_run_dir}")
env.close()
def main(argv: Sequence[str]) -> None:
del argv
_main()
if __name__ == '__main__':
app.run(main)