forked from mnotgod96/AppAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_explorer.py
279 lines (264 loc) · 10.4 KB
/
self_explorer.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import argparse
import ast
import datetime
import json
import os
import re
import sys
import time
import prompts
from config import load_config
from and_controller import list_all_devices, AndroidController, traverse_tree
from model import ask_gpt4v, parse_explore_rsp, parse_reflect_rsp
from utils import print_with_color, draw_bbox_multi, encode_image
arg_desc = "AppAgent - Autonomous Exploration"
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=arg_desc)
parser.add_argument("--app")
parser.add_argument("--root_dir", default="./")
args = vars(parser.parse_args())
configs = load_config()
app = args["app"]
root_dir = args["root_dir"]
if not app:
print_with_color("What is the name of the target app?", "blue")
app = input()
app = app.replace(" ", "")
work_dir = os.path.join(root_dir, "apps")
if not os.path.exists(work_dir):
os.mkdir(work_dir)
work_dir = os.path.join(work_dir, app)
if not os.path.exists(work_dir):
os.mkdir(work_dir)
demo_dir = os.path.join(work_dir, "demos")
if not os.path.exists(demo_dir):
os.mkdir(demo_dir)
demo_timestamp = int(time.time())
task_name = datetime.datetime.fromtimestamp(demo_timestamp).strftime("self_explore_%Y-%m-%d_%H-%M-%S")
task_dir = os.path.join(demo_dir, task_name)
os.mkdir(task_dir)
docs_dir = os.path.join(work_dir, "auto_docs")
if not os.path.exists(docs_dir):
os.mkdir(docs_dir)
explore_log_path = os.path.join(task_dir, f"log_explore_{task_name}.txt")
reflect_log_path = os.path.join(task_dir, f"log_reflect_{task_name}.txt")
device_list = list_all_devices()
if not device_list:
print_with_color("ERROR: No device found!", "red")
sys.exit()
print_with_color(f"List of devices attached:\n{str(device_list)}", "yellow")
if len(device_list) == 1:
device = device_list[0]
print_with_color(f"Device selected: {device}", "yellow")
else:
print_with_color("Please choose the Android device to start demo by entering its ID:", "blue")
device = input()
controller = AndroidController(device)
width, height = controller.get_device_size()
if not width and not height:
print_with_color("ERROR: Invalid device size!", "red")
sys.exit()
print_with_color(f"Screen resolution of {device}: {width}x{height}", "yellow")
print_with_color("Please enter the description of the task you want me to complete in a few sentences:", "blue")
task_desc = input()
round_count = 0
doc_count = 0
useless_list = set()
last_act = "None"
task_complete = False
while round_count < configs["MAX_ROUNDS"]:
round_count += 1
print_with_color(f"Round {round_count}", "yellow")
screenshot_before = controller.get_screenshot(f"{round_count}_before", task_dir)
xml_path = controller.get_xml(f"{round_count}", task_dir)
if screenshot_before == "ERROR" or xml_path == "ERROR":
break
clickable_list = []
focusable_list = []
traverse_tree(xml_path, clickable_list, "clickable", True)
traverse_tree(xml_path, focusable_list, "focusable", True)
elem_list = []
for elem in clickable_list:
if elem.uid in useless_list:
continue
elem_list.append(elem)
for elem in focusable_list:
if elem.uid in useless_list:
continue
bbox = elem.bbox
center = (bbox[0][0] + bbox[1][0]) // 2, (bbox[0][1] + bbox[1][1]) // 2
close = False
for e in clickable_list:
bbox = e.bbox
center_ = (bbox[0][0] + bbox[1][0]) // 2, (bbox[0][1] + bbox[1][1]) // 2
dist = (abs(center[0] - center_[0]) ** 2 + abs(center[1] - center_[1]) ** 2) ** 0.5
if dist <= configs["MIN_DIST"]:
close = True
break
if not close:
elem_list.append(elem)
draw_bbox_multi(screenshot_before, os.path.join(task_dir, f"{round_count}_before_labeled.png"), elem_list,
dark_mode=configs["DARK_MODE"])
prompt = re.sub(r"<task_description>", task_desc, prompts.self_explore_task_template)
prompt = re.sub(r"<last_act>", last_act, prompt)
base64_img_before = encode_image(os.path.join(task_dir, f"{round_count}_before_labeled.png"))
content = [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_img_before}"
}
}
]
print_with_color("Thinking about what to do in the next step...", "yellow")
rsp = ask_gpt4v(content)
if "error" not in rsp:
with open(explore_log_path, "a") as logfile:
log_item = {"step": round_count, "prompt": prompt, "image": f"{round_count}_before_labeled.png",
"response": rsp}
logfile.write(json.dumps(log_item) + "\n")
res = parse_explore_rsp(rsp)
act_name = res[0]
last_act = res[-1]
res = res[:-1]
if act_name == "FINISH":
task_complete = True
break
if act_name == "tap":
_, area = res
tl, br = elem_list[area - 1].bbox
ret = controller.tap(tl, br)
if ret == "ERROR":
print_with_color("ERROR: tap execution failed", "red")
break
elif act_name == "text":
_, input_str = res
ret = controller.text(input_str)
if ret == "ERROR":
print_with_color("ERROR: text execution failed", "red")
break
elif act_name == "long_press":
_, area = res
tl, br = elem_list[area - 1].bbox
ret = controller.long_press(tl, br)
if ret == "ERROR":
print_with_color("ERROR: long press execution failed", "red")
break
elif act_name == "swipe":
_, area, swipe_dir, dist = res
tl, br = elem_list[area - 1].bbox
ret = controller.swipe(tl, br, swipe_dir, dist)
if ret == "ERROR":
print_with_color("ERROR: long press execution failed", "red")
break
else:
break
time.sleep(configs["REQUEST_INTERVAL"])
else:
print_with_color(rsp["error"]["message"], "red")
break
screenshot_after = controller.get_screenshot(f"{round_count}_after", task_dir)
if screenshot_after == "ERROR":
break
draw_bbox_multi(screenshot_after, os.path.join(task_dir, f"{round_count}_after_labeled.png"), elem_list,
dark_mode=configs["DARK_MODE"])
base64_img_after = encode_image(os.path.join(task_dir, f"{round_count}_after_labeled.png"))
if act_name == "tap":
prompt = re.sub(r"<action>", "tapping", prompts.self_explore_reflect_template)
elif act_name == "text":
continue
elif act_name == "long_press":
prompt = re.sub(r"<action>", "long pressing", prompts.self_explore_reflect_template)
elif act_name == "swipe":
swipe_dir = res[2]
if swipe_dir == "up" or swipe_dir == "down":
act_name = "v_swipe"
elif swipe_dir == "left" or swipe_dir == "right":
act_name = "h_swipe"
prompt = re.sub(r"<action>", "swiping", prompts.self_explore_reflect_template)
else:
print_with_color("ERROR: Undefined act!", "red")
break
prompt = re.sub(r"<ui_element>", str(area), prompt)
prompt = re.sub(r"<task_desc>", task_desc, prompt)
prompt = re.sub(r"<last_act>", last_act, prompt)
content = [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_img_before}"
}
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_img_after}"
}
}
]
print_with_color("Reflecting on my previous action...", "yellow")
rsp = ask_gpt4v(content)
if "error" not in rsp:
resource_id = elem_list[int(area) - 1].uid
with open(reflect_log_path, "a") as logfile:
log_item = {"step": round_count, "prompt": prompt, "image_before": f"{round_count}_before_labeled.png",
"image_after": f"{round_count}_after.png", "response": rsp}
logfile.write(json.dumps(log_item) + "\n")
res = parse_reflect_rsp(rsp)
decision = res[0]
if decision == "ERROR":
break
if decision == "INEFFECTIVE":
useless_list.add(resource_id)
last_act = "None"
elif decision == "BACK" or decision == "CONTINUE" or decision == "SUCCESS":
if decision == "BACK" or decision == "CONTINUE":
useless_list.add(resource_id)
last_act = "None"
if decision == "BACK":
ret = controller.back()
if ret == "ERROR":
print_with_color("ERROR: back execution failed", "red")
break
doc = res[-1]
doc_name = resource_id + ".txt"
doc_path = os.path.join(docs_dir, doc_name)
if os.path.exists(doc_path):
doc_content = ast.literal_eval(open(doc_path).read())
if doc_content[act_name]:
print_with_color(f"Documentation for the element {resource_id} already exists.", "yellow")
continue
else:
doc_content = {
"tap": "",
"text": "",
"v_swipe": "",
"h_swipe": "",
"long_press": ""
}
doc_content[act_name] = doc
with open(doc_path, "w") as outfile:
outfile.write(str(doc_content))
doc_count += 1
print_with_color(f"Documentation generated and saved to {doc_path}", "yellow")
else:
print_with_color(f"ERROR: Undefined decision! {decision}", "red")
break
else:
print_with_color(rsp["error"]["message"], "red")
break
time.sleep(configs["REQUEST_INTERVAL"])
if task_complete:
print_with_color(f"Autonomous exploration completed successfully. {doc_count} docs generated.", "yellow")
elif round_count == configs["MAX_ROUNDS"]:
print_with_color(f"Autonomous exploration finished due to reaching max rounds. {doc_count} docs generated.",
"yellow")
else:
print_with_color(f"Autonomous exploration finished unexpectedly. {doc_count} docs generated.", "red")