-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.py
More file actions
227 lines (195 loc) · 7.31 KB
/
run.py
File metadata and controls
227 lines (195 loc) · 7.31 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from collections.abc import Sequence
import os
from absl import app
from absl import flags
from absl import logging
from android_world import checkpointer as checkpointer_lib
from android_world import registry
from android_world import suite_utils
from android_world.agents import base_agent
from android_world.agents import infer
from android_world.agents import m3a, m3a_wm
from android_world.env import env_launcher
from android_world.env import interface
logging.set_verbosity(logging.WARNING)
os.environ['GRPC_VERBOSITY'] = 'ERROR' # Only show errors
os.environ['GRPC_TRACE'] = 'none' # Disable tracing
# 返回adb的路径
def _find_adb_directory() -> str:
"""Returns the directory where adb is located."""
potential_paths = [
os.path.expanduser('/home/zyh/zyh_documents/zla/bin/Android/Sdk/platform-tools/adb'), # 自定义的adb路径
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路径
_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.',
)
# AVD的端口,默认为5554,在启动AVD时设置
_DEVICE_CONSOLE_PORT = flags.DEFINE_integer(
'console_port',
5556,
'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.',
)
_GRPC_PORT = flags.DEFINE_integer(
'grpc_port',
8554,
'the prot of grpc',
)
_SUITE_FAMILY = flags.DEFINE_enum(
'suite_family',
registry.TaskRegistry.ANDROID_WORLD_FAMILY,
[
# Families from the paper.
registry.TaskRegistry.ANDROID_WORLD_FAMILY,
registry.TaskRegistry.MINIWOB_FAMILY_SUBSET,
# Other families for more testing.
registry.TaskRegistry.MINIWOB_FAMILY,
registry.TaskRegistry.ANDROID_FAMILY,
registry.TaskRegistry.INFORMATION_RETRIEVAL_FAMILY,
],
'Suite family to run. See registry.py for more information.',
)
_TASK_RANDOM_SEED = flags.DEFINE_integer(
'task_random_seed', 30, 'Random seed for task randomness.'
)
_TASKS = flags.DEFINE_list(
'tasks',
None,
'List of specific tasks to run in the given suite family. If None, run all'
' tasks in the suite family.',
)
_N_TASK_COMBINATIONS = flags.DEFINE_integer(
'n_task_combinations',
1,
'Number of task instances to run for each task template.',
)
_CHECKPOINT_DIR = flags.DEFINE_string(
'checkpoint_dir',
'/home/zyh/zyh_documents/zla/ViMo/ViMo-Empowered_Agent/ckpt/m3a_qw3_wm_qw3sft600',
'The directory to save checkpoints and resume evaluation from. If the'
' directory contains existing checkpoint files, evaluation will resume from'
' the latest checkpoint. If the directory is empty or does not exist, a new'
' directory will be created.',
)
# 保存结果的路径,默认在用户主目录下
_OUTPUT_PATH = flags.DEFINE_string(
'output_path',
os.path.expanduser('/home/zyh/zyh_documents/zla/ViMo/ViMo-Empowered_Agent/result'),
'The path to save results to if not resuming from a checkpoint is not provided.',
)
# 选择agent,默认为m3a_gpt4v
_AGENT_NAME = flags.DEFINE_string('agent_name', 'm3a_tars', help='Agent name.')
# 是否固定任务的随机种子
_FIXED_TASK_SEED = flags.DEFINE_boolean(
'fixed_task_seed',
False,
'Whether to use the same task seed when running multiple task combinations'
' (n_task_combinations > 1).',
)
# MiniWoB任务的界面切换暂停时间(0.2秒),因为MiniWoB是轻量级的,界面加载很快
_MINIWOB_TRANSITION_PAUSE = 0.2
# MiniWoB任务的额外指导原则,提醒智能体这是模拟应用,不要使用 navigate_home 动作
_MINIWOB_ADDITIONAL_GUIDELINES = [
(
'This task is running in a mock app, you must stay in this app and'
' DO NOT use the `navigate_home` action.'
),
]
def _get_agent(
env: interface.AsyncEnv,
family: str | None = None,
) -> base_agent.EnvironmentInteractingAgent:
"""Gets agent."""
print('Initializing agent...')
agent = None
if _AGENT_NAME.value == 'm3a_gemini25f':
agent = m3a.M3A(env, infer.Gpt4Wrapper(model_name='gemini-2.5-flash'),)
elif _AGENT_NAME.value in ['m3a_gemini25f_Code2World']:
from android_world.agents import m3a_qwen
agent = m3a_qwen.M3A_VM(env, infer.Gpt4Wrapper('gemini-2.5-flash'))
if not agent:
raise ValueError(f'Unknown agent: {_AGENT_NAME.value}')
if (
agent.name in ['M3A', 'T3A', 'SeeAct']
and family
and family.startswith('miniwob')
and hasattr(agent, 'set_task_guidelines')
):
agent.set_task_guidelines(_MINIWOB_ADDITIONAL_GUIDELINES)
agent.name = _AGENT_NAME.value
print(f"初始化成功,使用的是{_AGENT_NAME.value}")
return agent
def _main() -> None:
"""Runs eval suite and gets rewards back."""
#################################### 加载并设置环境 ####################################
env = env_launcher.load_and_setup_env(
console_port=_DEVICE_CONSOLE_PORT.value,
emulator_setup=_EMULATOR_SETUP.value,
adb_path=_ADB_PATH.value,
grpc_port=_GRPC_PORT.value
)
#################################### 初始化任务套件以及agent ####################################
n_task_combinations = _N_TASK_COMBINATIONS.value
task_registry = registry.TaskRegistry()
suite = suite_utils.create_suite(
task_registry.get_registry(family=_SUITE_FAMILY.value),
n_task_combinations=n_task_combinations,
seed=_TASK_RANDOM_SEED.value,
tasks=_TASKS.value,
use_identical_params=_FIXED_TASK_SEED.value,
)
suite.suite_family = _SUITE_FAMILY.value
agent = _get_agent(env, _SUITE_FAMILY.value)
if _SUITE_FAMILY.value.startswith('miniwob'):
# MiniWoB pages change quickly, don't need to wait for screen to stabilize.
agent.transition_pause = _MINIWOB_TRANSITION_PAUSE
else:
agent.transition_pause = None
if _CHECKPOINT_DIR.value:
checkpoint_dir = _CHECKPOINT_DIR.value
else:
checkpoint_dir = checkpointer_lib.create_run_directory(_OUTPUT_PATH.value)
#################################### 开始测试 ####################################
print(
f'Starting eval with agent {_AGENT_NAME.value} and writing to'
f' {checkpoint_dir}'
)
suite_utils.run(
suite,
agent,
checkpointer=checkpointer_lib.IncrementalCheckpointer(checkpoint_dir),
demo_mode=False,
)
print(
f'Finished running agent {_AGENT_NAME.value} on {_SUITE_FAMILY.value}'
f' family. Wrote to {checkpoint_dir}.'
)
env.close()
def main(argv: Sequence[str]) -> None:
del argv
_main()
if __name__ == '__main__':
app.run(main)