Commit
·
31bb28b
1
Parent(s):
45ba166
add groundcua.py
Browse files- groundcua.py +76 -0
groundcua.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
GroundCUA Helper Module
|
| 3 |
+
Contains constants and utility functions for GroundNext models.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
GROUNDNEXT_SYSTEM_PROMPT = """You are a helpful assistant.
|
| 7 |
+
|
| 8 |
+
# Tools
|
| 9 |
+
|
| 10 |
+
You may call one or more functions to assist with the user query.
|
| 11 |
+
|
| 12 |
+
You are provided with function signatures within <tools></tools> XML tags:
|
| 13 |
+
<tools>
|
| 14 |
+
{{"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"}}}}}}
|
| 15 |
+
</tools>
|
| 16 |
+
|
| 17 |
+
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
|
| 18 |
+
<tool_call>
|
| 19 |
+
{{"name": <function-name>, "arguments": <args-json-object>}}
|
| 20 |
+
</tool_call>"""
|
| 21 |
+
|
| 22 |
+
# Default generation parameters
|
| 23 |
+
DEFAULT_TEMPERATURE = 0.0
|
| 24 |
+
DEFAULT_MAX_NEW_TOKENS = 64
|
| 25 |
+
MIN_PIXELS = 78_400
|
| 26 |
+
MAX_PIXELS = 6_000_000
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def prepare_image(image, min_pixels=MIN_PIXELS, max_pixels=MAX_PIXELS):
|
| 30 |
+
"""
|
| 31 |
+
Resize image using smart_resize for optimal model performance.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
image: PIL Image object
|
| 35 |
+
min_pixels: Minimum number of pixels (default: 78,400)
|
| 36 |
+
max_pixels: Maximum number of pixels (default: 6,000,000)
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Resized PIL Image and (width, height) tuple
|
| 40 |
+
"""
|
| 41 |
+
from qwen_vl_utils.vision_process import smart_resize
|
| 42 |
+
|
| 43 |
+
width, height = image.size
|
| 44 |
+
resized_height, resized_width = smart_resize(
|
| 45 |
+
height, width, min_pixels=min_pixels, max_pixels=max_pixels
|
| 46 |
+
)
|
| 47 |
+
resized_image = image.resize((resized_width, resized_height))
|
| 48 |
+
return resized_image, (resized_width, resized_height)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def create_messages(instruction, image, width, height):
|
| 52 |
+
"""
|
| 53 |
+
Create message format for GroundNext models.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
instruction: Text instruction for grounding task
|
| 57 |
+
image: PIL Image object
|
| 58 |
+
width: Image width
|
| 59 |
+
height: Image height
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
List of messages in the correct format
|
| 63 |
+
"""
|
| 64 |
+
return [
|
| 65 |
+
{
|
| 66 |
+
"role": "system",
|
| 67 |
+
"content": GROUNDNEXT_SYSTEM_PROMPT.format(width=width, height=height)
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"role": "user",
|
| 71 |
+
"content": [
|
| 72 |
+
{"type": "image", "image": image},
|
| 73 |
+
{"type": "text", "text": instruction},
|
| 74 |
+
],
|
| 75 |
+
}
|
| 76 |
+
]
|