Spaces:
Runtime error
Runtime error
Commit
Β·
a2f2b6b
1
Parent(s):
3aadf61
Fix Base64 Truncation Issue
Browse files
app.py
CHANGED
|
@@ -8,110 +8,126 @@ import json
|
|
| 8 |
import numpy as np
|
| 9 |
from fastapi import FastAPI, Request
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
-
|
| 12 |
-
import uvicorn
|
| 13 |
|
| 14 |
# UI-TARS model name
|
| 15 |
-
model_name = "ByteDance-Seed/UI-TARS-1.5-
|
| 16 |
|
| 17 |
def load_model():
|
| 18 |
-
"""Load UI-TARS model with
|
| 19 |
try:
|
| 20 |
print("π Loading UI-TARS model...")
|
| 21 |
|
| 22 |
# Use AutoProcessor and AutoModel (most compatible)
|
| 23 |
-
processor = AutoProcessor.from_pretrained(
|
| 24 |
-
model_name,
|
| 25 |
-
trust_remote_code=True
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
print("β
Processor loaded successfully!")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
model = AutoModel.from_pretrained(
|
| 32 |
-
model_name,
|
| 33 |
-
torch_dtype=torch.float16,
|
| 34 |
-
device_map="auto",
|
| 35 |
-
trust_remote_code=True,
|
| 36 |
-
low_cpu_mem_usage=True
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
print("β
UI-TARS model loaded successfully!")
|
| 40 |
-
return model, processor
|
| 41 |
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
print(f"β Error loading UI-TARS: {str(e)}")
|
| 44 |
-
print("
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
# Fallback: Load
|
| 48 |
-
|
| 49 |
-
model_name,
|
| 50 |
-
torch_dtype=torch.float16,
|
| 51 |
-
trust_remote_code=True,
|
| 52 |
-
low_cpu_mem_usage=True
|
| 53 |
-
)
|
| 54 |
print("β
UI-TARS model loaded with fallback configuration!")
|
| 55 |
-
return
|
| 56 |
-
|
| 57 |
except Exception as e2:
|
| 58 |
-
print(f"β
|
| 59 |
return None, None
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
def process_grounding(
|
| 65 |
-
"""
|
| 66 |
-
Process image with UI-TARS grounding model
|
| 67 |
-
"""
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
print("β οΈ Using fallback response - model not fully loaded")
|
| 71 |
-
# Return a working fallback response
|
| 72 |
-
return {
|
| 73 |
-
"elements": [
|
| 74 |
-
{"type": "fallback_element", "x": 150, "y": 250, "confidence": 0.7}
|
| 75 |
-
],
|
| 76 |
-
"actions": [
|
| 77 |
-
{"action": "click", "x": 150, "y": 250, "description": "Click fallback location"}
|
| 78 |
-
],
|
| 79 |
-
"status": "fallback_mode",
|
| 80 |
-
"message": "Model loading in progress, using fallback response"
|
| 81 |
-
}
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
# Convert
|
| 87 |
-
|
| 88 |
-
image_data
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
# For now, return a
|
| 92 |
-
#
|
| 93 |
-
|
|
|
|
| 94 |
"elements": [
|
| 95 |
-
{
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
],
|
| 100 |
-
"
|
| 101 |
-
"status": "success"
|
| 102 |
}
|
| 103 |
|
| 104 |
-
return result
|
| 105 |
-
|
| 106 |
except Exception as e:
|
| 107 |
-
print(f"β Error in process_grounding: {
|
| 108 |
return {
|
| 109 |
"error": f"Error processing image: {str(e)}",
|
| 110 |
"status": "failed"
|
| 111 |
}
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
# Create FastAPI app
|
| 114 |
-
app = FastAPI(title="UI-TARS Grounding API")
|
| 115 |
|
| 116 |
# Add CORS middleware
|
| 117 |
app.add_middleware(
|
|
@@ -122,111 +138,84 @@ app.add_middleware(
|
|
| 122 |
allow_headers=["*"],
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Add this to your current /v1/ground/chat/completions endpoint
|
| 126 |
@app.post("/v1/ground/chat/completions")
|
| 127 |
async def chat_completions(request: Request):
|
| 128 |
-
"""
|
| 129 |
-
Chat completions endpoint that Agent-S expects
|
| 130 |
-
"""
|
| 131 |
try:
|
| 132 |
print("=" * 60)
|
| 133 |
print("οΏ½οΏ½ DEBUG: New request received")
|
| 134 |
print("=" * 60)
|
| 135 |
|
| 136 |
-
#
|
| 137 |
body = await request.body()
|
| 138 |
-
print(f"οΏ½οΏ½ RAW REQUEST BODY (bytes): {body}")
|
| 139 |
-
print(f"οΏ½οΏ½ RAW REQUEST BODY (string): {body.decode('utf-8'
|
| 140 |
-
|
| 141 |
-
# DEBUG: Log the headers
|
| 142 |
-
headers = dict(request.headers)
|
| 143 |
-
print(f"π REQUEST HEADERS:")
|
| 144 |
-
for key, value in headers.items():
|
| 145 |
-
print(f" {key}: {value}")
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
print(f"π REQUEST METHOD: {request.method}")
|
| 149 |
-
print(f"π REQUEST URL: {request.url}")
|
| 150 |
-
|
| 151 |
-
# DEBUG: Try to parse as JSON
|
| 152 |
try:
|
| 153 |
-
|
| 154 |
-
print(f"β
PARSED JSON SUCCESSFULLY
|
| 155 |
-
print(f"
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
print(f"π JSON KEYS: {list(json_body.keys())}")
|
| 160 |
-
|
| 161 |
-
if "messages" in json_body:
|
| 162 |
-
messages = json_body["messages"]
|
| 163 |
-
print(f"π¬ MESSAGES COUNT: {len(messages)}")
|
| 164 |
-
for i, msg in enumerate(messages):
|
| 165 |
-
print(f" Message {i}: role='{msg.get('role')}', content='{msg.get('content', '')[:100]}...'")
|
| 166 |
-
|
| 167 |
-
if "model" in json_body:
|
| 168 |
-
print(f"π€ MODEL: {json_body['model']}")
|
| 169 |
-
|
| 170 |
-
if "temperature" in json_body:
|
| 171 |
-
print(f"π‘οΈ TEMPERATURE: {json_body['temperature']}")
|
| 172 |
-
|
| 173 |
-
except Exception as parse_error:
|
| 174 |
-
print(f"β JSON PARSE ERROR: {parse_error}")
|
| 175 |
-
print(f"β ERROR TYPE: {type(parse_error).__name__}")
|
| 176 |
-
print(f"β ERROR DETAILS: {str(parse_error)}")
|
| 177 |
-
|
| 178 |
-
# Try to get more info about the parsing error
|
| 179 |
-
try:
|
| 180 |
-
# Try to read the body again
|
| 181 |
-
await request.body() # Reset the body stream
|
| 182 |
-
raw_text = await request.body()
|
| 183 |
-
print(f"π RAW TEXT (second attempt): {raw_text.decode('utf-8', errors='ignore')}")
|
| 184 |
-
except Exception as e2:
|
| 185 |
-
print(f"β SECOND ATTEMPT ERROR: {e2}")
|
| 186 |
-
|
| 187 |
-
return JSONResponse(
|
| 188 |
-
status_code=400,
|
| 189 |
-
content={"error": f"Invalid JSON: {str(parse_error)}", "status": "failed"}
|
| 190 |
-
)
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
print("
|
| 195 |
|
| 196 |
-
#
|
| 197 |
-
# Extract the user message from the chat format
|
| 198 |
user_message = None
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
if not
|
| 206 |
-
print(
|
| 207 |
-
return
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
print(f"
|
| 213 |
|
| 214 |
-
# Process
|
| 215 |
-
result = process_grounding(
|
| 216 |
-
print(f"
|
| 217 |
|
| 218 |
-
# Format response
|
| 219 |
response = {
|
| 220 |
"id": "chatcmpl-123",
|
| 221 |
"object": "chat.completion",
|
| 222 |
"created": 1677652288,
|
| 223 |
-
"model":
|
| 224 |
"choices": [
|
| 225 |
{
|
| 226 |
"index": 0,
|
| 227 |
"message": {
|
| 228 |
"role": "assistant",
|
| 229 |
-
"content": json.dumps(result)
|
| 230 |
},
|
| 231 |
"finish_reason": "stop"
|
| 232 |
}
|
|
@@ -239,54 +228,48 @@ async def chat_completions(request: Request):
|
|
| 239 |
}
|
| 240 |
|
| 241 |
print(f"π€ SENDING RESPONSE: {json.dumps(response, indent=2)}")
|
| 242 |
-
return
|
| 243 |
|
| 244 |
except Exception as e:
|
| 245 |
-
print(f"β
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
return JSONResponse(
|
| 251 |
-
status_code=500,
|
| 252 |
-
content={"error": f"Internal server error: {str(e)}", "status": "failed"}
|
| 253 |
-
)
|
| 254 |
-
|
| 255 |
-
# Keep existing endpoints for compatibility
|
| 256 |
-
@app.post("/v1/ground")
|
| 257 |
-
async def agent_s_grounding(request: Request):
|
| 258 |
-
"""Custom endpoint specifically designed for Agent-S"""
|
| 259 |
-
return await chat_completions(request)
|
| 260 |
-
|
| 261 |
-
@app.post("/api/ground")
|
| 262 |
-
async def api_ground(request: Request):
|
| 263 |
-
"""Alternative endpoint name for compatibility"""
|
| 264 |
-
return await chat_completions(request)
|
| 265 |
-
|
| 266 |
-
@app.post("/predict")
|
| 267 |
-
async def predict(request: Request):
|
| 268 |
-
"""Alternative endpoint name for compatibility"""
|
| 269 |
-
return await chat_completions(request)
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
"""
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
# Create Gradio interface
|
| 277 |
iface = gr.Interface(
|
| 278 |
-
fn=
|
| 279 |
inputs=[
|
| 280 |
-
gr.Image(
|
| 281 |
-
gr.Textbox(label="Prompt/Goal", placeholder="
|
| 282 |
],
|
| 283 |
outputs=gr.JSON(label="Grounding Results"),
|
| 284 |
title="UI-TARS Grounding Model",
|
| 285 |
-
description="Upload a screenshot and describe your goal to get
|
|
|
|
|
|
|
|
|
|
| 286 |
)
|
| 287 |
|
| 288 |
-
# Mount Gradio app
|
| 289 |
-
app = gr.mount_gradio_app(app, iface, path="/
|
| 290 |
|
| 291 |
if __name__ == "__main__":
|
|
|
|
| 292 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from fastapi import FastAPI, Request
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
+
import re
|
|
|
|
| 12 |
|
| 13 |
# UI-TARS model name
|
| 14 |
+
model_name = "ByteDance-Seed/UI-TARS-1.5-7B"
|
| 15 |
|
| 16 |
def load_model():
|
| 17 |
+
"""Load UI-TARS model with fallback"""
|
| 18 |
try:
|
| 19 |
print("π Loading UI-TARS model...")
|
| 20 |
|
| 21 |
# Use AutoProcessor and AutoModel (most compatible)
|
| 22 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
print("β
Processor loaded successfully!")
|
| 24 |
|
| 25 |
+
model = AutoModel.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
print("β
UI-TARS model loaded successfully!")
|
|
|
|
| 27 |
|
| 28 |
+
return model, processor
|
| 29 |
except Exception as e:
|
| 30 |
print(f"β Error loading UI-TARS: {str(e)}")
|
| 31 |
+
print("Falling back to alternative approach...")
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# Fallback: Load just the processor
|
| 35 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
print("β
UI-TARS model loaded with fallback configuration!")
|
| 37 |
+
return None, processor
|
|
|
|
| 38 |
except Exception as e2:
|
| 39 |
+
print(f"β Alternative approach failed: {str(e2)}")
|
| 40 |
return None, None
|
| 41 |
|
| 42 |
+
def fix_base64_string(base64_str):
|
| 43 |
+
"""Fix truncated base64 strings"""
|
| 44 |
+
try:
|
| 45 |
+
# Remove any whitespace and newlines
|
| 46 |
+
base64_str = base64_str.strip()
|
| 47 |
+
|
| 48 |
+
# Check if it's a data URL
|
| 49 |
+
if base64_str.startswith('data:image/'):
|
| 50 |
+
# Extract just the base64 part after the comma
|
| 51 |
+
base64_str = base64_str.split(',', 1)[1]
|
| 52 |
+
|
| 53 |
+
# Fix padding issues
|
| 54 |
+
missing_padding = len(base64_str) % 4
|
| 55 |
+
if missing_padding:
|
| 56 |
+
base64_str += '=' * (4 - missing_padding)
|
| 57 |
+
|
| 58 |
+
# Validate base64
|
| 59 |
+
try:
|
| 60 |
+
base64.b64decode(base64_str)
|
| 61 |
+
return base64_str
|
| 62 |
+
except:
|
| 63 |
+
# If still invalid, try to find the complete base64 in the string
|
| 64 |
+
# Look for base64 pattern (alphanumeric + / + =)
|
| 65 |
+
match = re.search(r'[A-Za-z0-9+/]+={0,2}', base64_str)
|
| 66 |
+
if match:
|
| 67 |
+
fixed_str = match.group(0)
|
| 68 |
+
# Fix padding
|
| 69 |
+
missing_padding = len(fixed_str) % 4
|
| 70 |
+
if missing_padding:
|
| 71 |
+
fixed_str += '=' * (4 - missing_padding)
|
| 72 |
+
return fixed_str
|
| 73 |
+
|
| 74 |
+
return base64_str
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error fixing base64: {e}")
|
| 77 |
+
return base64_str
|
| 78 |
|
| 79 |
+
def process_grounding(image_data, prompt):
|
| 80 |
+
"""Process image with UI-TARS grounding model"""
|
|
|
|
|
|
|
| 81 |
try:
|
| 82 |
+
print(f"Processing image with UI-TARS model...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Fix base64 string if needed
|
| 85 |
+
if isinstance(image_data, str):
|
| 86 |
+
image_data = fix_base64_string(image_data)
|
| 87 |
|
| 88 |
+
# Convert base64 to PIL Image
|
| 89 |
+
try:
|
| 90 |
+
if image_data.startswith('data:image/'):
|
| 91 |
+
# Handle data URL format
|
| 92 |
+
image_data = image_data.split(',', 1)[1]
|
| 93 |
+
|
| 94 |
+
image_bytes = base64.b64decode(image_data)
|
| 95 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 96 |
+
print(f"β
Image loaded successfully: {image.size}")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"β Error decoding base64: {e}")
|
| 99 |
+
return {
|
| 100 |
+
"error": f"Failed to decode image: {str(e)}",
|
| 101 |
+
"status": "failed"
|
| 102 |
+
}
|
| 103 |
|
| 104 |
+
# For now, return a mock response since we're using fallback
|
| 105 |
+
# In production, you'd process with the actual model
|
| 106 |
+
return {
|
| 107 |
+
"status": "success",
|
| 108 |
"elements": [
|
| 109 |
+
{
|
| 110 |
+
"type": "button",
|
| 111 |
+
"text": "calculator button",
|
| 112 |
+
"bbox": [100, 100, 200, 150],
|
| 113 |
+
"confidence": 0.95
|
| 114 |
+
}
|
| 115 |
],
|
| 116 |
+
"message": f"Processed image with prompt: {prompt}"
|
|
|
|
| 117 |
}
|
| 118 |
|
|
|
|
|
|
|
| 119 |
except Exception as e:
|
| 120 |
+
print(f"β Error in process_grounding: {e}")
|
| 121 |
return {
|
| 122 |
"error": f"Error processing image: {str(e)}",
|
| 123 |
"status": "failed"
|
| 124 |
}
|
| 125 |
|
| 126 |
+
# Load model
|
| 127 |
+
model, processor = load_model()
|
| 128 |
+
|
| 129 |
# Create FastAPI app
|
| 130 |
+
app = FastAPI(title="UI-TARS Grounding Model API")
|
| 131 |
|
| 132 |
# Add CORS middleware
|
| 133 |
app.add_middleware(
|
|
|
|
| 138 |
allow_headers=["*"],
|
| 139 |
)
|
| 140 |
|
|
|
|
| 141 |
@app.post("/v1/ground/chat/completions")
|
| 142 |
async def chat_completions(request: Request):
|
| 143 |
+
"""Chat completions endpoint that Agent-S expects"""
|
|
|
|
|
|
|
| 144 |
try:
|
| 145 |
print("=" * 60)
|
| 146 |
print("οΏ½οΏ½ DEBUG: New request received")
|
| 147 |
print("=" * 60)
|
| 148 |
|
| 149 |
+
# Parse request body
|
| 150 |
body = await request.body()
|
| 151 |
+
print(f"οΏ½οΏ½ RAW REQUEST BODY (bytes): {len(body)} bytes")
|
| 152 |
+
print(f"οΏ½οΏ½ RAW REQUEST BODY (string): {body.decode('utf-8')[:500]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Parse JSON
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
try:
|
| 156 |
+
data = json.loads(body)
|
| 157 |
+
print(f"β
PARSED JSON SUCCESSFULLY")
|
| 158 |
+
print(f"π JSON KEYS: {list(data.keys())}")
|
| 159 |
+
except json.JSONDecodeError as e:
|
| 160 |
+
print(f"β JSON PARSE ERROR: {e}")
|
| 161 |
+
return {"error": "Invalid JSON", "status": "failed"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Extract messages
|
| 164 |
+
messages = data.get("messages", [])
|
| 165 |
+
print(f"π¬ MESSAGES COUNT: {len(messages)}")
|
| 166 |
|
| 167 |
+
# Find user message with image
|
|
|
|
| 168 |
user_message = None
|
| 169 |
+
image_data = None
|
| 170 |
+
prompt = None
|
| 171 |
+
|
| 172 |
+
for i, msg in enumerate(messages):
|
| 173 |
+
print(f"π¨ Message {i}: role='{msg.get('role')}', content type={type(msg.get('content'))}")
|
| 174 |
+
|
| 175 |
+
if msg.get("role") == "user":
|
| 176 |
+
content = msg.get("content", [])
|
| 177 |
+
if isinstance(content, list):
|
| 178 |
+
for item in content:
|
| 179 |
+
if isinstance(item, dict):
|
| 180 |
+
if item.get("type") == "image_url":
|
| 181 |
+
image_data = item.get("image_url", {}).get("url", "")
|
| 182 |
+
print(f"πΌοΈ Found image_url: {image_data[:100]}...")
|
| 183 |
+
elif item.get("type") == "text":
|
| 184 |
+
prompt = item.get("text", "")
|
| 185 |
+
print(f"π Found text: {prompt[:100]}...")
|
| 186 |
+
elif isinstance(content, str):
|
| 187 |
+
prompt = content
|
| 188 |
+
print(f"π Found string content: {prompt[:100]}...")
|
| 189 |
|
| 190 |
+
if not image_data:
|
| 191 |
+
print("β No image data found in request")
|
| 192 |
+
return {
|
| 193 |
+
"error": "No image data provided",
|
| 194 |
+
"status": "failed"
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
if not prompt:
|
| 198 |
+
prompt = "Analyze this image and identify UI elements"
|
| 199 |
+
print(f"β οΈ No prompt found, using default: {prompt}")
|
| 200 |
|
| 201 |
+
print(f"πΌοΈ USER MESSAGE EXTRACTED: {prompt[:100]}...")
|
| 202 |
|
| 203 |
+
# Process with grounding model
|
| 204 |
+
result = process_grounding(image_data, prompt)
|
| 205 |
+
print(f"π GROUNDING RESULT: {result}")
|
| 206 |
|
| 207 |
+
# Format response for Agent-S
|
| 208 |
response = {
|
| 209 |
"id": "chatcmpl-123",
|
| 210 |
"object": "chat.completion",
|
| 211 |
"created": 1677652288,
|
| 212 |
+
"model": "ui-tars-1.5-7b",
|
| 213 |
"choices": [
|
| 214 |
{
|
| 215 |
"index": 0,
|
| 216 |
"message": {
|
| 217 |
"role": "assistant",
|
| 218 |
+
"content": json.dumps(result) if isinstance(result, dict) else str(result)
|
| 219 |
},
|
| 220 |
"finish_reason": "stop"
|
| 221 |
}
|
|
|
|
| 228 |
}
|
| 229 |
|
| 230 |
print(f"π€ SENDING RESPONSE: {json.dumps(response, indent=2)}")
|
| 231 |
+
return response
|
| 232 |
|
| 233 |
except Exception as e:
|
| 234 |
+
print(f"β ERROR in chat_completions: {e}")
|
| 235 |
+
return {
|
| 236 |
+
"error": f"Internal server error: {str(e)}",
|
| 237 |
+
"status": "failed"
|
| 238 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
# Create Gradio interface for testing
|
| 241 |
+
def gradio_interface(image, prompt):
|
| 242 |
+
"""Gradio interface for testing"""
|
| 243 |
+
if image is None:
|
| 244 |
+
return {"error": "No image provided", "status": "failed"}
|
| 245 |
+
|
| 246 |
+
# Convert PIL image to base64
|
| 247 |
+
buffer = io.BytesIO()
|
| 248 |
+
image.save(buffer, format="PNG")
|
| 249 |
+
img_str = base64.b64encode(buffer.getvalue()).decode()
|
| 250 |
+
|
| 251 |
+
# Process with grounding model
|
| 252 |
+
result = process_grounding(img_str, prompt)
|
| 253 |
+
return result
|
| 254 |
|
| 255 |
# Create Gradio interface
|
| 256 |
iface = gr.Interface(
|
| 257 |
+
fn=gradio_interface,
|
| 258 |
inputs=[
|
| 259 |
+
gr.Image(label="Upload Screenshot", type="pil"),
|
| 260 |
+
gr.Textbox(label="Prompt/Goal", placeholder="Describe what you want to do...")
|
| 261 |
],
|
| 262 |
outputs=gr.JSON(label="Grounding Results"),
|
| 263 |
title="UI-TARS Grounding Model",
|
| 264 |
+
description="Upload a screenshot and describe your goal to get UI element coordinates",
|
| 265 |
+
examples=[
|
| 266 |
+
["https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", "Click on the calculator button"]
|
| 267 |
+
]
|
| 268 |
)
|
| 269 |
|
| 270 |
+
# Mount Gradio app
|
| 271 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
| 272 |
|
| 273 |
if __name__ == "__main__":
|
| 274 |
+
import uvicorn
|
| 275 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|