Views
No views yet
1import ast
2import torch
3from PIL import Image, ImageDraw
4from qwen_vl_utils import process_vision_info
5from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
6
7def draw_point(image_input, point=None, radius=5):
8 if isinstance(image_input, str):
9 image = Image.open(BytesIO(requests.get(image_input).content)) if image_input.startswith('http') else Image.open(image_input)
10 else:
11 image = image_input
12
13 if point:
14 x, y = point[0] * image.width, point[1] * image.height
15 ImageDraw.Draw(image).ellipse((x - radius, y - radius, x + radius, y + radius), fill='red')
16 display(image)
17 return
18
19model = Qwen2VLForConditionalGeneration.from_pretrained(
20 "showlab/ShowUI-2B",
21 torch_dtype=torch.bfloat16,
22 device_map="auto"
23)
24
25min_pixels = 256*28*28
26max_pixels = 1344*28*28
27
28processor = AutoProcessor.from_pretrained("showlab/ShowUI-2B", min_pixels=min_pixels, max_pixels=max_pixels)1img_url = 'examples/web_dbd7514b-9ca3-40cd-b09a-990f7b955da1.png'
2query = "Nahant"
3
4
5_SYSTEM = "Based on the screenshot of the page, I give a text description and you give its corresponding location. The coordinate represents a clickable location [x, y] for an element, which is a relative coordinate on the screenshot, scaled from 0 to 1."
6messages = [
7 {
8 "role": "user",
9 "content": [
10 {"type": "text", "text": _SYSTEM},
11 {"type": "image", "image": img_url, "min_pixels": min_pixels, "max_pixels": max_pixels},
12 {"type": "text", "text": query}
13 ],
14 }
15]
16
17text = processor.apply_chat_template(
18 messages, tokenize=False, add_generation_prompt=True,
19)
20image_inputs, video_inputs = process_vision_info(messages)
21inputs = processor(
22 text=[text],
23 images=image_inputs,
24 videos=video_inputs,
25 padding=True,
26 return_tensors="pt",
27)
28inputs = inputs.to("cuda")
29
30generated_ids = model.generate(**inputs, max_new_tokens=128)
31generated_ids_trimmed = [
32 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
33]
34output_text = processor.batch_decode(
35 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
36)[0]
37
38click_xy = ast.literal_eval(output_text)
39# [0.73, 0.21]
40
41draw_point(img_url, click_xy, 10)1_NAV_SYSTEM = """You are an assistant trained to navigate the {_APP} screen.
2Given a task instruction, a screen observation, and an action history sequence,
3output the next action and wait for the next observation.
4Here is the action space:
5{_ACTION_SPACE}
6"""
7
8_NAV_FORMAT = """
9Format the action as a dictionary with the following keys:
10{'action': 'ACTION_TYPE', 'value': 'element', 'position': [x,y]}
11
12If value or position is not applicable, set it as `None`.
13Position might be [[x1,y1], [x2,y2]] if the action requires a start and end position.
14Position represents the relative coordinates on the screenshot and should be scaled to a range of 0-1.
15"""
16
17action_map = {
18'web': """
191. `CLICK`: Click on an element, value is not applicable and the position [x,y] is required.
202. `INPUT`: Type a string into an element, value is a string to type and the position [x,y] is required.
213. `SELECT`: Select a value for an element, value is not applicable and the position [x,y] is required.
224. `HOVER`: Hover on an element, value is not applicable and the position [x,y] is required.
235. `ANSWER`: Answer the question, value is the answer and the position is not applicable.
246. `ENTER`: Enter operation, value and position are not applicable.
257. `SCROLL`: Scroll the screen, value is the direction to scroll and the position is not applicable.
268. `SELECT_TEXT`: Select some text content, value is not applicable and position [[x1,y1], [x2,y2]] is the start and end position of the select operation.
279. `COPY`: Copy the text, value is the text to copy and the position is not applicable.
28""",
29
30'phone': """
311. `INPUT`: Type a string into an element, value is a string to type and the position [x,y] is required.
322. `SWIPE`: Swipe the screen, value is not applicable and the position [[x1,y1], [x2,y2]] is the start and end position of the swipe operation.
333. `TAP`: Tap on an element, value is not applicable and the position [x,y] is required.
344. `ANSWER`: Answer the question, value is the status (e.g., 'task complete') and the position is not applicable.
355. `ENTER`: Enter operation, value and position are not applicable.
36"""
37}1img_url = 'examples/chrome.png'
2split='web'
3system_prompt = _NAV_SYSTEM.format(_APP=split, _ACTION_SPACE=action_map[split]) + _NAV_FORMAT
4query = "Search the weather for the New York city."
5
6messages = [
7 {
8 "role": "user",
9 "content": [
10 {"type": "text", "text": system_prompt},
11 {"type": "text", "text": f'Task: {query}'},
12 # {"type": "text", "text": PAST_ACTION},
13 {"type": "image", "image": img_url, "min_pixels": min_pixels, "max_pixels": max_pixels},
14 ],
15 }
16]
17
18text = processor.apply_chat_template(
19 messages, tokenize=False, add_generation_prompt=True,
20)
21image_inputs, video_inputs = process_vision_info(messages)
22inputs = processor(
23 text=[text],
24 images=image_inputs,
25 videos=video_inputs,
26 padding=True,
27 return_tensors="pt",
28)
29inputs = inputs.to("cuda")
30
31generated_ids = model.generate(**inputs, max_new_tokens=128)
32generated_ids_trimmed = [
33 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
34]
35output_text = processor.batch_decode(
36 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
37)[0]
38
39print(output_text)
40# {'action': 'CLICK', 'value': None, 'position': [0.49, 0.42]},
41# {'action': 'INPUT', 'value': 'weather for New York city', 'position': [0.49, 0.42]},
42# {'action': 'ENTER', 'value': None, 'position': None}@misc{lin2024showui,
title={ShowUI: One Vision-Language-Action Model for GUI Visual Agent},
author={Kevin Qinghong Lin and Linjie Li and Difei Gao and Zhengyuan Yang and Shiwei Wu and Zechen Bai and Weixian Lei and Lijuan Wang and Mike Zheng Shou},
year={2024},
eprint={2411.17465},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2411.17465},
}