Our models are trained based on Jedi models via an efficient continual training strategy, which enhances the models' text dragging performance while perserving their original click-based performance.
For details of how to employ the models, please refer to our repo examples.
Below is the code of a quick demo (demo.png can be found at here):
# pip install openai pillow transformers
# start the vllm server:
'''
vllm serve osunlp/GUI-Drag-7B \
--host 0.0.0.0 \
--port 8000 \
--max-model-len 16384 \
--tensor-parallel-size 2
'''
import base64
import json
import re
from pathlib import Path
import io
from openai import OpenAI
from PIL import Image, ImageDraw
from transformers.models.qwen2_vl.image_processing_qwen2_vl_fast import smart_resize as qwen_smart_resize
MODEL_ID = "osunlp/GUI-Drag-7B"
BASE_URL = "http://localhost:8000/v1"
FN_CALL_TEMPLATE = """You are a helpful assistant.
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{"type": "function", "function": {{"name": "computer_use", "description": "Use a mouse and keyboard to interact with a computer, and take screenshots.\n* This is an interface to a desktop GUI. You do not have access to a terminal or applications menu. You must click on desktop icons to start applications.\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. E.g. if you click on Firefox and a window doesn't open, try wait and taking another screenshot.\n* The screen's resolution is {width}x{height}.\n* Whenever you intend to move the cursor to click on an element like an icon, you should consult a screenshot to determine the coordinates of the element before moving the cursor.\n* If you tried clicking on a program or link but it failed to load, even after waiting, try adjusting your cursor position so that the tip of the cursor visually falls on the element that you want to click.\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`: Performs key down presses on the arguments passed in order, then performs key releases in reverse order.\n* `type`: Type a string of text on the keyboard.\n* `mouse_move`: Move the cursor to a specified (x, y) pixel coordinate on the screen.\n* `left_click`: Click the left mouse button.\n* `left_click_drag`: Click and drag the cursor to a specified (x, y) pixel coordinate on the screen.\n* `right_click`: Click the right mouse button.\n* `middle_click`: Click the middle mouse button.\n* `double_click`: Double-click the left mouse button.\n* `scroll`: Performs a scroll of the mouse scroll wheel.\n* `wait`: Wait specified seconds for the change to happen.\n* `terminate`: Terminate the current task and report its completion status.", "enum": ["key", "type", "mouse_move", "left_click", "left_click_drag", "right_click", "middle_click", "double_click", "scroll", "wait", "terminate"], "type": "string"}}, "keys": {{"description": "Required only by `action=key`.", "type": "array"}}, "text": {{"description": "Required only by `action=type`.", "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=mouse_move`, `action=left_click_drag`, `action=left_click`, `action=right_click`, `action=double_click`.", "type": "array"}}, "pixels": {{"description": "The amount of scrolling to perform. Positive values scroll up, negative values scroll down. Required only by `action=scroll`.", "type": "number"}}, "time": {{"description": "The seconds to wait. Required only by `action=wait`.", "type": "number"}}, "status": {{"description": "The status of the task. Required only by `action=terminate`.", "type": "string", "enum": ["success", "failure"]}}}}, "required": ["action"], "type": "object"}}}}}}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{{"name": <function-name>, "arguments": <args-json-object>}}
</tool_call>
"""
IMAGE_PATH = Path("demo.png")
INSTRUCTION = "Drag to select the last sentence."
def encode_image(image: Image) -> str:
"""Encode PIL image to base64 string"""
output_buffer = io.BytesIO()
image.save(output_buffer, format="PNG")
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data).decode("utf-8")
return base64_str
def resize_coordinates(coord, size_pred, size_to_be_mapped):
return (
round(coord[0] * size_to_be_mapped[0] / size_pred[0]),
round(coord[1] * size_to_be_mapped[1] / size_pred[1]),
)
def process_simple_drag_response(parsed):
if len(parsed) < 2:
return None
first = json.loads(parsed[0])
second = json.loads(parsed[1])
if first["arguments"]["action"] not in ("mouse_move", "left_click"):
return None
if second["arguments"]["action"] != "left_click_drag":
return None
return first["arguments"].get("coordinate"), second["arguments"].get("coordinate")
def draw_drag(image: Image.Image, start, end, output_path: Path):
draw = ImageDraw.Draw(image)
draw.ellipse((start[0]-10, start[1]-10, start[0]+10, start[1]+10), outline="lime", width=3)
draw.ellipse((end[0]-10, end[1]-10, end[0]+10, end[1]+10), outline="red", width=3)
draw.line((*start, *end), fill="yellow", width=4)
image.save(output_path)
def main():
image = Image.open(IMAGE_PATH)
resized_h, resized_w = qwen_smart_resize(
image.height,
image.width,
max_pixels=2116800,
min_pixels=12544,
)
messages = [
{
"role": "system",
"content": [{"type": "text", "text": FN_CALL_TEMPLATE.format(width=resized_w, height=resized_h)}],
},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encode_image(image)}"}},
{"type": "text", "text": INSTRUCTION},
],
},
]
client = OpenAI(base_url=BASE_URL, api_key="EMPTY")
resp = client.chat.completions.create(
model=MODEL_ID,
messages=messages,
temperature=0.1,
max_tokens=1024,
)
text = resp.choices[0].message.content
parsed = re.findall(r"<tool_call>\s*(\{.*?\})\s*</tool_call>", text, flags=re.DOTALL)
drag = process_simple_drag_response(parsed)
if not drag:
print("No drag action detected.")
return
start_resized, end_resized = drag
start = resize_coordinates(start_resized, (resized_w, resized_h), image.size)
end = resize_coordinates(end_resized, (resized_w, resized_h), image.size)
print("Predicted drag:", start, "โ", end)
draw_drag(image.copy(), start, end, IMAGE_PATH.with_name("GUI-Drag-7B_demo.png"))
if __name__ == "__main__":
main()
- Downloads last month
- 61
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
๐
Ask for provider support