Views
No views yet
Qwen2.5-VL-3B-Instruct and trained using the proposed Actor2Reasoner framework, enhanced through reinforcement learning to improve its planning and reflection capabilities for GUI tasks.pip install transformers qwen-vl-utils1import cv2
2import json
3import torch
4import requests
5from PIL import Image
6from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
7from qwen_vl_utils import process_vision_info, smart_resize
8
9MAX_IMAGE_PIXELS = 5600*28*28
10
11# Load model and processor
12model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
13 "Reallm-Labs/InfiGUI-R1-3B",
14 torch_dtype=torch.bfloat16,
15 attn_implementation="flash_attention_2",
16 device_map="auto"
17)
18processor = AutoProcessor.from_pretrained("Reallm-Labs/InfiGUI-R1-3B", max_pixels=MAX_IMAGE_PIXELS, padding_side="left")
19
20# Prepare image
21img_url = "https://raw.githubusercontent.com/Reallm-Labs/InfiGUI-R1/main/images/test_img.png"
22response = requests.get(img_url)
23with open("test_img.png", "wb") as f:
24 f.write(response.content)
25image = Image.open("test_img.png")
26width, height = image.size
27new_height, new_width = smart_resize(height, width, max_pixels=MAX_IMAGE_PIXELS)
28
29# Prepare inputs
30instruction = "View detailed storage space usage"
31
32system_prompt = 'You FIRST think about the reasoning process as an internal monologue and then provide the final answer.\nThe reasoning process MUST BE enclosed within <think> </think> tags.'
33## The following prompts are primarily sourced from https://github.com/QwenLM/Qwen2.5-VL
34tool_prompt = "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{\"type\": \"function\", \"function\": {\"name\": \"mobile_use\", \"description\": \"Use a touchscreen to interact with a mobile device, and take screenshots.\\n* This is an interface to a mobile device with touchscreen. You can perform actions like clicking, typing, swiping, etc.\\n* Some applications may take time to start or process actions, so you may need to wait and take successive screenshots to see the results of your actions.\\n* The screen's resolution is " + str(new_width) + "x" + str(new_height) + ".\\n* Make sure to click any buttons, links, icons, etc with the cursor tip in the center of the element. Don't click boxes on their edges unless asked.\", \"parameters\": {\"properties\": {\"action\": {\"description\": \"The action to perform. The available actions are:\\n* `key`: Perform a key event on the mobile device.\\n - This supports adb's `keyevent` syntax.\\n - Examples: \\\"volume_up\\\", \\\"volume_down\\\", \\\"power\\\", \\\"camera\\\", \\\"clear\\\".\\n* `click`: Click the point on the screen with coordinate (x, y).\\n* `long_press`: Press the point on the screen with coordinate (x, y) for specified seconds.\\n* `swipe`: Swipe from the starting point with coordinate (x, y) to the end point with coordinates2 (x2, y2).\\n* `type`: Input the specified text into the activated input box.\\n* `system_button`: Press the system button.\\n* `open`: Open an app on the device.\\n* `wait`: Wait specified seconds for the change to happen.\\n* `terminate`: Terminate the current task and report its completion status.\", \"enum\": [\"key\", \"click\", \"long_press\", \"swipe\", \"type\", \"system_button\", \"open\", \"wait\", \"terminate\"], \"type\": \"string\"}, \"coordinate\": {\"description\": \"(x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=click`, `action=long_press`, and `action=swipe`.\", \"type\": \"array\"}, \"coordinate2\": {\"description\": \"(x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=swipe`.\", \"type\": \"array\"}, \"text\": {\"description\": \"Required only by `action=key`, `action=type`, and `action=open`.\", \"type\": \"string\"}, \"time\": {\"description\": \"The seconds to wait. Required only by `action=long_press` and `action=wait`.\", \"type\": \"number\"}, \"button\": {\"description\": \"Back means returning to the previous interface, Home means returning to the desktop, Menu means opening the application background menu, and Enter means pressing the enter. Required only by `action=system_button`\", \"enum\": [\"Back\", \"Home\", \"Menu\", \"Enter\"], \"type\": \"string\"}, \"status\": {\"description\": \"The status of the task. Required only by `action=terminate`.\", \"type\": \"string\", \"enum\": [\"success\", \"failure\"]}}, \"required\": [\"action\"], \"type\": \"object\"}}}\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>"
35grounding_prompt = f'The screen\'s resolution is {new_width}x{new_height}.\nPoint to the UI element most relevant to "{instruction}", output its coordinates using JSON format:\n```json\n[\n {{"point_2d": [x, y], "label": "object name/description"}}\n]```'
36trajectory_prompt = f'The user query: {instruction}\nTask progress (You have done the following operation on the current device): '
37
38# Build messages
39grounding_messages = [
40 {"role": "system", "content": system_prompt},
41 {
42 "role": "user",
43 "content": [
44 {"type": "image", "image": "test_img.png"},
45 {"type": "text", "text": grounding_prompt}
46 ]
47 }
48]
49trajectory_messages = [
50 {"role": "system", "content": system_prompt + "\n\n" + tool_prompt},
51 {
52 "role": "user",
53 "content": [
54 {"type": "text", "text": trajectory_prompt},
55 {"type": "image", "image": "test_img.png"}
56 ],
57 },
58]
59messages = [grounding_messages, trajectory_messages]
60
61# Process and generate
62text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
63image_inputs, video_inputs = process_vision_info(messages)
64inputs = processor(text=text, images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt").to("cuda")
65generated_ids = model.generate(**inputs, max_new_tokens=512)
66output_text = processor.batch_decode(
67 [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)],
68 skip_special_tokens=True,
69 clean_up_tokenization_spaces=False
70)
71
72# Visualize results
73output_text = [ot.split("</think>")[-1] for ot in output_text]
74
75grounding_output = output_text[0].replace("```json", "").replace("```", "").strip()
76trajectory_output = output_text[1].replace("<tool_call>", "").replace("</tool_call>", "").strip()
77
78try:
79 grounding_output = json.loads(grounding_output)
80 trajectory_output = json.loads(trajectory_output)
81
82 grounding_coords = grounding_output[0]['point_2d']
83 trajectory_coords = trajectory_output["arguments"]['coordinate'] if "coordinate" in trajectory_output["arguments"] else None
84
85 grounding_label = grounding_output[0]['label']
86 trajectory_label = json.dumps(trajectory_output["arguments"])
87
88 # Load the original image
89 img = cv2.imread("test_img.png")
90 if img is None:
91 raise ValueError("Could not load the image")
92
93 height, width = img.shape[:2]
94
95 # Create copies for each visualization
96 grounding_img = img.copy()
97 trajectory_img = img.copy()
98
99 # Visualize grounding coordinates
100 if grounding_coords:
101 x = int(grounding_coords[0] / new_width * width)
102 y = int(grounding_coords[1] / new_height * height)
103
104 cv2.circle(grounding_img, (x, y), 10, (0, 0, 255), -1)
105 cv2.putText(grounding_img, grounding_label, (x+10, y-10),
106 cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
107 cv2.imwrite("grounding_output.png", grounding_img)
108 print("Predicted coordinates:", grounding_coords)
109 print(f"Grounding visualization saved to grounding_output.png")
110
111 # Visualize trajectory coordinates
112 if trajectory_coords:
113 x = int(trajectory_coords[0] / new_width * width)
114 y = int(trajectory_coords[1] / new_height * height)
115
116 cv2.circle(trajectory_img, (x, y), 10, (0, 0, 255), -1)
117 cv2.putText(trajectory_img, trajectory_label, (x+10, y-10),
118 cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
119 cv2.imwrite("trajectory_output.png", trajectory_img)
120 print("Predicted action:", trajectory_label)
121 print(f"Trajectory visualization saved to trajectory_output.png")
122
123except:
124 print("Error: Failed to parse coordinates or process image")1@article{liu2025infigui,
2 title={InfiGUI-R1: Advancing Multimodal GUI Agents from Reactive Actors to Deliberative Reasoners},
3 author={Liu, Yuhang and Li, Pengxiang and Xie, Congkai and Hu, Xavier and Han, Xiaotian and Zhang, Shengyu and Yang, Hongxia and Wu, Fei},
4 journal={arXiv preprint arXiv:2504.14239},
5 year={2025}
6}1@article{liu2025infiguiagent,
2 title={InfiGUIAgent: A Multimodal Generalist GUI Agent with Native Reasoning and Reflection},
3 author={Liu, Yuhang and Li, Pengxiang and Wei, Zishu and Xie, Congkai and Hu, Xueyu and Xu, Xinchen and Zhang, Shengyu and Han, Xiaotian and Yang, Hongxia and Wu, Fei},
4 journal={arXiv preprint arXiv:2501.04575},
5 year={2025}
6}