Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,9 @@ import json
|
|
| 4 |
import time
|
| 5 |
import shutil
|
| 6 |
import uuid
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import unicodedata
|
| 9 |
-
import gc
|
| 10 |
from io import BytesIO
|
| 11 |
from typing import Tuple, Optional, List, Dict, Any
|
| 12 |
|
|
@@ -21,18 +21,18 @@ from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
|
| 21 |
from qwen_vl_utils import process_vision_info
|
| 22 |
|
| 23 |
# -----------------------------------------------------------------------------
|
| 24 |
-
# 1. CONSTANTS &
|
| 25 |
# -----------------------------------------------------------------------------
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
"Fara-7B": "microsoft/Fara-7B",
|
| 30 |
"UI-TARS-1.5-7B": "ByteDance-Seed/UI-TARS-1.5-7B"
|
| 31 |
}
|
| 32 |
|
| 33 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
|
| 35 |
-
# System Prompt
|
| 36 |
OS_SYSTEM_PROMPT = """You are a GUI agent. You are given a task and a screenshot of the current status.
|
| 37 |
You need to generate the next action to complete the task.
|
| 38 |
|
|
@@ -50,87 +50,100 @@ Examples:
|
|
| 50 |
"""
|
| 51 |
|
| 52 |
# -----------------------------------------------------------------------------
|
| 53 |
-
# 2. MODEL MANAGEMENT
|
| 54 |
# -----------------------------------------------------------------------------
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
self.processor = None
|
| 61 |
-
|
| 62 |
-
def load_model(self, model_key):
|
| 63 |
-
model_id = MODELS.get(model_key)
|
| 64 |
-
if not model_id:
|
| 65 |
-
raise ValueError(f"Unknown model: {model_key}")
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
torch.cuda.empty_cache()
|
| 81 |
-
print("Previous model unloaded.")
|
| 82 |
-
|
| 83 |
-
print(f"Loading {model_id}...")
|
| 84 |
-
try:
|
| 85 |
-
self.processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 86 |
-
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 87 |
-
model_id,
|
| 88 |
-
trust_remote_code=True,
|
| 89 |
-
torch_dtype=torch.bfloat16 if DEVICE == "cuda" else torch.float32,
|
| 90 |
-
device_map="auto" if DEVICE == "cuda" else None,
|
| 91 |
-
)
|
| 92 |
-
if DEVICE == "cpu":
|
| 93 |
-
self.model.to("cpu")
|
| 94 |
-
self.model.eval()
|
| 95 |
-
self.current_model_id = model_id
|
| 96 |
-
print(f"Successfully loaded {model_key}")
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"Error loading model {model_id}: {e}")
|
| 99 |
-
raise e
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
-
image_inputs, video_inputs = process_vision_info(messages)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
padding=True,
|
| 116 |
-
return_tensors="pt",
|
| 117 |
-
)
|
| 118 |
-
inputs = inputs.to(self.model.device)
|
| 119 |
-
|
| 120 |
-
# Generate
|
| 121 |
-
with torch.no_grad():
|
| 122 |
-
generated_ids = self.model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# -----------------------------------------------------------------------------
|
| 136 |
# 3. PARSING & VISUALIZATION LOGIC
|
|
@@ -151,17 +164,13 @@ def get_navigation_prompt(task, image):
|
|
| 151 |
]
|
| 152 |
|
| 153 |
def parse_tool_calls(response: str) -> list[dict]:
|
| 154 |
-
"""
|
| 155 |
-
Parses <tool_call>{JSON}</tool_call> tags.
|
| 156 |
-
Also attempts fallback regex for plain coordinate output just in case.
|
| 157 |
-
"""
|
| 158 |
actions = []
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
json_matches = re.findall(r"<tool_call>(.*?)</tool_call>", response, re.DOTALL)
|
| 162 |
-
for match in json_matches:
|
| 163 |
try:
|
| 164 |
-
|
|
|
|
| 165 |
args = data.get("arguments", {})
|
| 166 |
coords = args.get("coordinate", [])
|
| 167 |
action_type = args.get("action", "unknown")
|
|
@@ -173,28 +182,14 @@ def parse_tool_calls(response: str) -> list[dict]:
|
|
| 173 |
"x": float(coords[0]),
|
| 174 |
"y": float(coords[1]),
|
| 175 |
"text": text_content,
|
| 176 |
-
"
|
| 177 |
})
|
| 178 |
-
except:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
# 2. Fallback: Search for any [x, y] or (x, y) pattern if JSON parsing yielded nothing
|
| 182 |
-
if not actions:
|
| 183 |
-
# Regex for [123, 456] or (123, 456)
|
| 184 |
-
coord_matches = re.findall(r"[\[\(](\d+(?:\.\d+)?),\s*(\d+(?:\.\d+)?)[\]\)]", response)
|
| 185 |
-
for x, y in coord_matches:
|
| 186 |
-
actions.append({
|
| 187 |
-
"type": "click", # Assume click for raw coords
|
| 188 |
-
"x": float(x),
|
| 189 |
-
"y": float(y),
|
| 190 |
-
"text": "",
|
| 191 |
-
"source": "regex"
|
| 192 |
-
})
|
| 193 |
|
| 194 |
return actions
|
| 195 |
|
| 196 |
def create_localized_image(original_image: Image.Image, actions: list[dict]) -> Optional[Image.Image]:
|
| 197 |
-
"""Draws markers on the image based on parsed pixel coordinates."""
|
| 198 |
if not actions:
|
| 199 |
return None
|
| 200 |
|
|
@@ -220,7 +215,7 @@ def create_localized_image(original_image: Image.Image, actions: list[dict]) ->
|
|
| 220 |
x = act['x']
|
| 221 |
y = act['y']
|
| 222 |
|
| 223 |
-
#
|
| 224 |
if x <= 1.0 and y <= 1.0 and x > 0:
|
| 225 |
pixel_x = int(x * width)
|
| 226 |
pixel_y = int(y * height)
|
|
@@ -232,16 +227,9 @@ def create_localized_image(original_image: Image.Image, actions: list[dict]) ->
|
|
| 232 |
color = colors.get(action_type, 'green')
|
| 233 |
|
| 234 |
# Draw Target
|
| 235 |
-
r = 12
|
| 236 |
-
draw.ellipse(
|
| 237 |
-
|
| 238 |
-
outline=color,
|
| 239 |
-
width=4
|
| 240 |
-
)
|
| 241 |
-
draw.ellipse(
|
| 242 |
-
[pixel_x - 3, pixel_y - 3, pixel_x + 3, pixel_y + 3],
|
| 243 |
-
fill=color
|
| 244 |
-
)
|
| 245 |
|
| 246 |
# Label
|
| 247 |
label_text = f"{action_type}"
|
|
@@ -249,44 +237,36 @@ def create_localized_image(original_image: Image.Image, actions: list[dict]) ->
|
|
| 249 |
label_text += f": '{act['text']}'"
|
| 250 |
|
| 251 |
text_pos = (pixel_x + 15, pixel_y - 10)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
draw.rectangle(bbox, fill="black")
|
| 256 |
-
draw.text(text_pos, label_text, fill="white", font=font)
|
| 257 |
-
else:
|
| 258 |
-
draw.text(text_pos, label_text, fill="black") # fallback
|
| 259 |
|
| 260 |
return img_copy
|
| 261 |
|
| 262 |
# -----------------------------------------------------------------------------
|
| 263 |
-
# 4. GRADIO LOGIC
|
| 264 |
# -----------------------------------------------------------------------------
|
| 265 |
|
| 266 |
@spaces.GPU(duration=120)
|
| 267 |
-
def process_screenshot(
|
| 268 |
if input_numpy_image is None:
|
| 269 |
return "⚠️ Please upload an image first.", None
|
| 270 |
|
| 271 |
-
#
|
|
|
|
|
|
|
|
|
|
| 272 |
input_pil_image = array_to_image(input_numpy_image)
|
| 273 |
-
|
| 274 |
-
# Build Prompt
|
| 275 |
prompt = get_navigation_prompt(task, input_pil_image)
|
| 276 |
|
| 277 |
-
# Generate
|
| 278 |
-
print(f"Generating
|
| 279 |
-
|
| 280 |
-
raw_response = model_manager.generate(model_choice, prompt, max_new_tokens=500)
|
| 281 |
-
except Exception as e:
|
| 282 |
-
return f"Error generating response: {str(e)}", None
|
| 283 |
-
|
| 284 |
print(f"Raw Output:\n{raw_response}")
|
| 285 |
|
| 286 |
-
# Parse
|
| 287 |
actions = parse_tool_calls(raw_response)
|
| 288 |
|
| 289 |
-
# Visualize
|
| 290 |
output_image = input_pil_image
|
| 291 |
if actions:
|
| 292 |
visualized = create_localized_image(input_pil_image, actions)
|
|
@@ -296,13 +276,13 @@ def process_screenshot(model_choice: str, input_numpy_image: np.ndarray, task: s
|
|
| 296 |
return raw_response, output_image
|
| 297 |
|
| 298 |
# -----------------------------------------------------------------------------
|
| 299 |
-
# 5.
|
| 300 |
# -----------------------------------------------------------------------------
|
| 301 |
|
| 302 |
-
title = "CUA
|
| 303 |
description = """
|
| 304 |
-
|
| 305 |
-
|
| 306 |
"""
|
| 307 |
|
| 308 |
custom_css = """
|
|
@@ -315,6 +295,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 315 |
|
| 316 |
with gr.Row():
|
| 317 |
with gr.Column():
|
|
|
|
|
|
|
| 318 |
# Model Selector
|
| 319 |
model_selector = gr.Dropdown(
|
| 320 |
label="Choose CUA Model",
|
|
@@ -323,7 +305,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 323 |
interactive=True
|
| 324 |
)
|
| 325 |
|
| 326 |
-
input_image = gr.Image(label="Upload Screenshot", height=500)
|
| 327 |
task_input = gr.Textbox(
|
| 328 |
label="Task Instruction",
|
| 329 |
placeholder="e.g. Input the server address readyforquantum.com...",
|
|
@@ -338,18 +319,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 338 |
# Wire up the button
|
| 339 |
submit_btn.click(
|
| 340 |
fn=process_screenshot,
|
| 341 |
-
inputs=[
|
| 342 |
outputs=[output_text, output_image]
|
| 343 |
)
|
| 344 |
|
| 345 |
-
# Example for quick testing
|
| 346 |
gr.Examples(
|
| 347 |
examples=[
|
| 348 |
-
["
|
|
|
|
| 349 |
],
|
| 350 |
-
inputs=[
|
| 351 |
label="Quick Examples"
|
| 352 |
)
|
| 353 |
|
| 354 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 355 |
demo.queue().launch()
|
|
|
|
| 4 |
import time
|
| 5 |
import shutil
|
| 6 |
import uuid
|
| 7 |
+
import gc
|
| 8 |
import tempfile
|
| 9 |
import unicodedata
|
|
|
|
| 10 |
from io import BytesIO
|
| 11 |
from typing import Tuple, Optional, List, Dict, Any
|
| 12 |
|
|
|
|
| 21 |
from qwen_vl_utils import process_vision_info
|
| 22 |
|
| 23 |
# -----------------------------------------------------------------------------
|
| 24 |
+
# 1. CONSTANTS & CONFIGURATION
|
| 25 |
# -----------------------------------------------------------------------------
|
| 26 |
|
| 27 |
+
# Map display names to Hugging Face Repo IDs
|
| 28 |
+
MODEL_MAP = {
|
| 29 |
"Fara-7B": "microsoft/Fara-7B",
|
| 30 |
"UI-TARS-1.5-7B": "ByteDance-Seed/UI-TARS-1.5-7B"
|
| 31 |
}
|
| 32 |
|
| 33 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
|
| 35 |
+
# System Prompt
|
| 36 |
OS_SYSTEM_PROMPT = """You are a GUI agent. You are given a task and a screenshot of the current status.
|
| 37 |
You need to generate the next action to complete the task.
|
| 38 |
|
|
|
|
| 50 |
"""
|
| 51 |
|
| 52 |
# -----------------------------------------------------------------------------
|
| 53 |
+
# 2. GLOBAL MODEL STATE MANAGEMENT
|
| 54 |
# -----------------------------------------------------------------------------
|
| 55 |
|
| 56 |
+
# Global variables to track the currently loaded model
|
| 57 |
+
CURRENT_MODEL = None
|
| 58 |
+
CURRENT_PROCESSOR = None
|
| 59 |
+
CURRENT_MODEL_ID = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
def load_model(model_key: str):
|
| 62 |
+
"""
|
| 63 |
+
Dynamically loads the requested model.
|
| 64 |
+
Unloads the previous model to free up GPU memory if a switch occurs.
|
| 65 |
+
"""
|
| 66 |
+
global CURRENT_MODEL, CURRENT_PROCESSOR, CURRENT_MODEL_ID
|
| 67 |
+
|
| 68 |
+
target_repo_id = MODEL_MAP[model_key]
|
| 69 |
+
|
| 70 |
+
# If the requested model is already loaded, do nothing
|
| 71 |
+
if CURRENT_MODEL is not None and CURRENT_MODEL_ID == target_repo_id:
|
| 72 |
+
print(f"Model {model_key} is already loaded.")
|
| 73 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
print(f"--- Switching Model to {model_key} ({target_repo_id}) ---")
|
| 76 |
+
|
| 77 |
+
# 1. Unload existing model to free GPU memory
|
| 78 |
+
if CURRENT_MODEL is not None:
|
| 79 |
+
print("Unloading current model...")
|
| 80 |
+
del CURRENT_MODEL
|
| 81 |
+
del CURRENT_PROCESSOR
|
| 82 |
+
CURRENT_MODEL = None
|
| 83 |
+
CURRENT_PROCESSOR = None
|
| 84 |
+
gc.collect()
|
| 85 |
+
torch.cuda.empty_cache()
|
| 86 |
+
print("Memory cleared.")
|
| 87 |
+
|
| 88 |
+
# 2. Load new model
|
| 89 |
+
try:
|
| 90 |
+
print(f"Loading processor for {target_repo_id}...")
|
| 91 |
+
processor = AutoProcessor.from_pretrained(target_repo_id, trust_remote_code=True)
|
| 92 |
|
| 93 |
+
print(f"Loading model weights for {target_repo_id}...")
|
| 94 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 95 |
+
target_repo_id,
|
| 96 |
+
trust_remote_code=True,
|
| 97 |
+
torch_dtype=torch.bfloat16 if DEVICE == "cuda" else torch.float32,
|
| 98 |
+
device_map="auto" if DEVICE == "cuda" else None,
|
| 99 |
)
|
|
|
|
| 100 |
|
| 101 |
+
if DEVICE == "cpu":
|
| 102 |
+
model.to("cpu")
|
| 103 |
+
|
| 104 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# Update global state
|
| 107 |
+
CURRENT_MODEL = model
|
| 108 |
+
CURRENT_PROCESSOR = processor
|
| 109 |
+
CURRENT_MODEL_ID = target_repo_id
|
| 110 |
+
print(f"Successfully loaded {model_key}.")
|
| 111 |
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"Error loading model {target_repo_id}: {e}")
|
| 114 |
+
raise e
|
| 115 |
+
|
| 116 |
+
def generate_response(messages: list[dict], max_new_tokens=512):
|
| 117 |
+
"""
|
| 118 |
+
Runs generation using the currently loaded global model.
|
| 119 |
+
"""
|
| 120 |
+
if CURRENT_MODEL is None or CURRENT_PROCESSOR is None:
|
| 121 |
+
raise ValueError("No model loaded.")
|
| 122 |
+
|
| 123 |
+
text = CURRENT_PROCESSOR.apply_chat_template(
|
| 124 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 125 |
+
)
|
| 126 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 127 |
+
|
| 128 |
+
inputs = CURRENT_PROCESSOR(
|
| 129 |
+
text=[text],
|
| 130 |
+
images=image_inputs,
|
| 131 |
+
videos=video_inputs,
|
| 132 |
+
padding=True,
|
| 133 |
+
return_tensors="pt",
|
| 134 |
+
)
|
| 135 |
+
inputs = inputs.to(CURRENT_MODEL.device)
|
| 136 |
|
| 137 |
+
with torch.no_grad():
|
| 138 |
+
generated_ids = CURRENT_MODEL.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 139 |
+
|
| 140 |
+
generated_ids_trimmed = [
|
| 141 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
return CURRENT_PROCESSOR.batch_decode(
|
| 145 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 146 |
+
)[0]
|
| 147 |
|
| 148 |
# -----------------------------------------------------------------------------
|
| 149 |
# 3. PARSING & VISUALIZATION LOGIC
|
|
|
|
| 164 |
]
|
| 165 |
|
| 166 |
def parse_tool_calls(response: str) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
actions = []
|
| 168 |
+
matches = re.findall(r"<tool_call>(.*?)</tool_call>", response, re.DOTALL)
|
| 169 |
|
| 170 |
+
for match in matches:
|
|
|
|
|
|
|
| 171 |
try:
|
| 172 |
+
json_str = match.strip()
|
| 173 |
+
data = json.loads(json_str)
|
| 174 |
args = data.get("arguments", {})
|
| 175 |
coords = args.get("coordinate", [])
|
| 176 |
action_type = args.get("action", "unknown")
|
|
|
|
| 182 |
"x": float(coords[0]),
|
| 183 |
"y": float(coords[1]),
|
| 184 |
"text": text_content,
|
| 185 |
+
"raw_json": data
|
| 186 |
})
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"Error parsing tool call: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
return actions
|
| 191 |
|
| 192 |
def create_localized_image(original_image: Image.Image, actions: list[dict]) -> Optional[Image.Image]:
|
|
|
|
| 193 |
if not actions:
|
| 194 |
return None
|
| 195 |
|
|
|
|
| 215 |
x = act['x']
|
| 216 |
y = act['y']
|
| 217 |
|
| 218 |
+
# Determine if coords are normalized or absolute
|
| 219 |
if x <= 1.0 and y <= 1.0 and x > 0:
|
| 220 |
pixel_x = int(x * width)
|
| 221 |
pixel_y = int(y * height)
|
|
|
|
| 227 |
color = colors.get(action_type, 'green')
|
| 228 |
|
| 229 |
# Draw Target
|
| 230 |
+
r = 12
|
| 231 |
+
draw.ellipse([pixel_x - r, pixel_y - r, pixel_x + r, pixel_y + r], outline=color, width=4)
|
| 232 |
+
draw.ellipse([pixel_x - 3, pixel_y - 3, pixel_x + 3, pixel_y + 3], fill=color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
# Label
|
| 235 |
label_text = f"{action_type}"
|
|
|
|
| 237 |
label_text += f": '{act['text']}'"
|
| 238 |
|
| 239 |
text_pos = (pixel_x + 15, pixel_y - 10)
|
| 240 |
+
bbox = draw.textbbox(text_pos, label_text, font=font)
|
| 241 |
+
draw.rectangle(bbox, fill="black")
|
| 242 |
+
draw.text(text_pos, label_text, fill="white", font=font)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
return img_copy
|
| 245 |
|
| 246 |
# -----------------------------------------------------------------------------
|
| 247 |
+
# 4. GRADIO PROCESSING LOGIC
|
| 248 |
# -----------------------------------------------------------------------------
|
| 249 |
|
| 250 |
@spaces.GPU(duration=120)
|
| 251 |
+
def process_screenshot(input_numpy_image: np.ndarray, task: str, selected_model_key: str) -> Tuple[str, Optional[Image.Image]]:
|
| 252 |
if input_numpy_image is None:
|
| 253 |
return "⚠️ Please upload an image first.", None
|
| 254 |
|
| 255 |
+
# 1. Ensure correct model is loaded
|
| 256 |
+
load_model(selected_model_key)
|
| 257 |
+
|
| 258 |
+
# 2. Prepare Data
|
| 259 |
input_pil_image = array_to_image(input_numpy_image)
|
|
|
|
|
|
|
| 260 |
prompt = get_navigation_prompt(task, input_pil_image)
|
| 261 |
|
| 262 |
+
# 3. Generate
|
| 263 |
+
print(f"Generating with {selected_model_key}...")
|
| 264 |
+
raw_response = generate_response(prompt, max_new_tokens=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
print(f"Raw Output:\n{raw_response}")
|
| 266 |
|
| 267 |
+
# 4. Parse & Visualize
|
| 268 |
actions = parse_tool_calls(raw_response)
|
| 269 |
|
|
|
|
| 270 |
output_image = input_pil_image
|
| 271 |
if actions:
|
| 272 |
visualized = create_localized_image(input_pil_image, actions)
|
|
|
|
| 276 |
return raw_response, output_image
|
| 277 |
|
| 278 |
# -----------------------------------------------------------------------------
|
| 279 |
+
# 5. UI SETUP
|
| 280 |
# -----------------------------------------------------------------------------
|
| 281 |
|
| 282 |
+
title = "Computer Use Agent (CUA) Playground 🖥️"
|
| 283 |
description = """
|
| 284 |
+
Analyze GUI screenshots and generate action coordinates using State-of-the-Art Vision Language Models.
|
| 285 |
+
Supported Models: **Microsoft Fara-7B** and **ByteDance UI-TARS-1.5-7B**.
|
| 286 |
"""
|
| 287 |
|
| 288 |
custom_css = """
|
|
|
|
| 295 |
|
| 296 |
with gr.Row():
|
| 297 |
with gr.Column():
|
| 298 |
+
input_image = gr.Image(label="Upload Screenshot", height=500)
|
| 299 |
+
|
| 300 |
# Model Selector
|
| 301 |
model_selector = gr.Dropdown(
|
| 302 |
label="Choose CUA Model",
|
|
|
|
| 305 |
interactive=True
|
| 306 |
)
|
| 307 |
|
|
|
|
| 308 |
task_input = gr.Textbox(
|
| 309 |
label="Task Instruction",
|
| 310 |
placeholder="e.g. Input the server address readyforquantum.com...",
|
|
|
|
| 319 |
# Wire up the button
|
| 320 |
submit_btn.click(
|
| 321 |
fn=process_screenshot,
|
| 322 |
+
inputs=[input_image, task_input, model_selector],
|
| 323 |
outputs=[output_text, output_image]
|
| 324 |
)
|
| 325 |
|
|
|
|
| 326 |
gr.Examples(
|
| 327 |
examples=[
|
| 328 |
+
["./assets/google.png", "Search for 'Hugging Face'", "Fara-7B"],
|
| 329 |
+
["./assets/google.png", "Click the Sign In button", "UI-TARS-1.5-7B"],
|
| 330 |
],
|
| 331 |
+
inputs=[input_image, task_input, model_selector],
|
| 332 |
label="Quick Examples"
|
| 333 |
)
|
| 334 |
|
| 335 |
if __name__ == "__main__":
|
| 336 |
+
# Pre-load the default model on startup to speed up first inference (optional)
|
| 337 |
+
# load_model("Fara-7B")
|
| 338 |
demo.queue().launch()
|