Update app.py
Browse files
app.py
CHANGED
|
@@ -1,396 +1,349 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import openai
|
| 3 |
-
import os
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
api_key =
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
client
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
return
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
self.
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
def
|
| 199 |
-
"""
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
# Clear the input box immediately for better UX
|
| 350 |
-
yield history + [{"role": "user", "content": message}], ""
|
| 351 |
-
|
| 352 |
-
# Start streaming the response
|
| 353 |
-
final_response = None
|
| 354 |
-
for response in respond(message, history, system_msg, max_tok, temp, top_p_val, image_data):
|
| 355 |
-
final_response = response
|
| 356 |
-
yield final_response, ""
|
| 357 |
-
|
| 358 |
-
def update_screen_and_status(img, status_msg):
|
| 359 |
-
return img, gr.update(value=f"<div class='device-info'>{status_msg}</div>") if status_msg else gr.update()
|
| 360 |
-
|
| 361 |
-
# Connect UI components to functions
|
| 362 |
-
demo.load(refresh_devices_ui, outputs=device_dropdown)
|
| 363 |
-
refresh_btn.click(refresh_devices_ui, outputs=device_dropdown)
|
| 364 |
-
connect_btn.click(connect_device_ui, inputs=device_dropdown, outputs=[device_status, device_screen])
|
| 365 |
-
|
| 366 |
-
prompt_preset.change(lambda preset: system_prompts.get(preset, ""), inputs=prompt_preset, outputs=system_message)
|
| 367 |
-
|
| 368 |
-
# Chat submission logic
|
| 369 |
-
msg.submit(chat_with_optional_screen, [msg, chatbot, system_message, max_tokens, temperature, top_p, include_screen], [chatbot, msg])
|
| 370 |
-
submit_btn.click(chat_with_optional_screen, [msg, chatbot, system_message, max_tokens, temperature, top_p, include_screen], [chatbot, msg])
|
| 371 |
-
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 372 |
-
|
| 373 |
-
# Device action logic
|
| 374 |
-
screenshot_btn.click(device_manager.capture_screenshot, outputs=[device_screen, device_status])
|
| 375 |
-
back_btn.click(lambda: device_manager.device_action(device_manager.press_key, "back"), outputs=[device_screen])
|
| 376 |
-
home_btn.click(lambda: device_manager.device_action(device_manager.press_key, "home"), outputs=[device_screen])
|
| 377 |
-
tap_btn.click(lambda x, y: device_manager.device_action(device_manager.send_tap, x, y), [tap_x, tap_y], device_screen)
|
| 378 |
-
send_text_btn.click(lambda text: device_manager.device_action(device_manager.send_text, text), [text_input], device_screen).then(lambda: "", outputs=text_input)
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
return demo
|
| 382 |
-
|
| 383 |
-
if __name__ == "__main__":
|
| 384 |
-
print("===== Application Startup =====")
|
| 385 |
-
if not os.getenv("nebiusaistudioapikey"):
|
| 386 |
-
print("β οΈ Warning: NEBIUSAISTUDIOAPIKEY environment variable not set! API calls will fail.")
|
| 387 |
-
else:
|
| 388 |
-
print("β
Nebius AI Studio API key found.")
|
| 389 |
-
|
| 390 |
-
if ADB_AVAILABLE:
|
| 391 |
-
print("β
pure-python-adb library is available.")
|
| 392 |
-
|
| 393 |
-
app = create_android_interface()
|
| 394 |
-
|
| 395 |
-
print("\nπ Starting Gradio interface...")
|
| 396 |
app.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import asyncio
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
|
| 9 |
+
# --- ADB Library Integration ---
|
| 10 |
+
try:
|
| 11 |
+
from ppadb.client import Client as AdbClient
|
| 12 |
+
ADB_AVAILABLE = True
|
| 13 |
+
except ImportError:
|
| 14 |
+
ADB_AVAILABLE = False
|
| 15 |
+
print("β Critical Error: pure-python-adb library not found. Android integration will not work.")
|
| 16 |
+
print(" Please install it with: pip install pure-python-adb")
|
| 17 |
+
|
| 18 |
+
"""
|
| 19 |
+
Nebius AI Studio OpenAI-compatible API integration with Mistral model + Android Integration
|
| 20 |
+
(Enhanced with asyncio for robust async operation in Gradio and improved error handling for HF Spaces)
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
# --- OpenAI Client Initialization ---
|
| 24 |
+
def get_openai_client():
|
| 25 |
+
api_key = os.getenv("nebiusaistudioapikey") # Standard practice is all-caps for env vars
|
| 26 |
+
if not api_key:
|
| 27 |
+
raise ValueError("NEBIUSAISTUDIOAPIKEY environment variable not set in your Space secrets.")
|
| 28 |
+
return openai.OpenAI(api_key=api_key, base_url="https://api.studio.nebius.ai/v1/")
|
| 29 |
+
|
| 30 |
+
client = None
|
| 31 |
+
|
| 32 |
+
# --- AI Chat Function ---
|
| 33 |
+
async def respond(message, history, system_message, max_tokens, temperature, top_p, image_data=None):
|
| 34 |
+
global client
|
| 35 |
+
max_history = 20
|
| 36 |
+
history = history[-max_history:]
|
| 37 |
+
|
| 38 |
+
if client is None:
|
| 39 |
+
try:
|
| 40 |
+
client = get_openai_client()
|
| 41 |
+
except ValueError as e:
|
| 42 |
+
yield history + [{"role": "assistant", "content": f"Configuration Error: {str(e)}"}]
|
| 43 |
+
return
|
| 44 |
+
|
| 45 |
+
messages = [{"role": "system", "content": system_message}]
|
| 46 |
+
for role, content in history:
|
| 47 |
+
messages.append({"role": role, "content": content})
|
| 48 |
+
|
| 49 |
+
user_content = [{"type": "text", "text": message}]
|
| 50 |
+
if image_data:
|
| 51 |
+
# Vision models prefer the image first
|
| 52 |
+
user_content.insert(0, {"type": "image_url", "image_url": {"url": image_data}})
|
| 53 |
+
|
| 54 |
+
messages.append({"role": "user", "content": user_content})
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
response = await asyncio.to_thread(
|
| 58 |
+
client.chat.completions.create,
|
| 59 |
+
model="mistralai/Mistral-Small-3.1-24B-Instruct-2503", # NOTE: Ensure this model supports vision
|
| 60 |
+
messages=messages,
|
| 61 |
+
max_tokens=max_tokens,
|
| 62 |
+
temperature=temperature,
|
| 63 |
+
top_p=top_p,
|
| 64 |
+
stream=True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# The user message is already in the history before calling respond
|
| 68 |
+
# We just need to append the assistant's response part by part
|
| 69 |
+
partial_message = ""
|
| 70 |
+
for chunk in response:
|
| 71 |
+
if chunk.choices[0].delta.content is not None:
|
| 72 |
+
partial_message += chunk.choices[0].delta.content
|
| 73 |
+
yield history + [("user", message), ("assistant", partial_message)]
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
yield history + [("user", message), ("assistant", f"API Error: {str(e)}")]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# --- Android Device Management (Refactored for Async) ---
|
| 80 |
+
class AndroidDeviceManager:
|
| 81 |
+
def __init__(self):
|
| 82 |
+
self.adb_client = None
|
| 83 |
+
self.adb_server_running = False
|
| 84 |
+
self.current_device_serial = None
|
| 85 |
+
self.current_ppadb_device = None
|
| 86 |
+
self.latest_screenshot_uri = None
|
| 87 |
+
|
| 88 |
+
if ADB_AVAILABLE:
|
| 89 |
+
try:
|
| 90 |
+
# Check for ADB server connection on initialization
|
| 91 |
+
self.adb_client = AdbClient(host="127.0.0.1", port=5037)
|
| 92 |
+
self.adb_client.version() # This will raise an exception if server isn't running
|
| 93 |
+
self.adb_server_running = True
|
| 94 |
+
print("β
ADB server found and client initialized.")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
self.adb_client = None
|
| 97 |
+
self.adb_server_running = False
|
| 98 |
+
print(f"β οΈ ADB server not found on localhost:5037. Reason: {e}")
|
| 99 |
+
print(" Will operate in 'Simulated Device' mode only. This is expected on Hugging Face Spaces.")
|
| 100 |
+
|
| 101 |
+
def _create_mock_device_for_demo(self):
|
| 102 |
+
"""Creates and sets up the simulated device."""
|
| 103 |
+
self.current_device_serial = "demo_device_cloud (Simulated)"
|
| 104 |
+
self.current_ppadb_device = None
|
| 105 |
+
try:
|
| 106 |
+
demo_img = Image.new('RGB', (1080, 2340), color='#f0f2f5')
|
| 107 |
+
draw = ImageDraw.Draw(demo_img)
|
| 108 |
+
# Use a default font to avoid file not found errors on servers
|
| 109 |
+
font_h1 = ImageFont.load_default(size=60)
|
| 110 |
+
font_p = ImageFont.load_default(size=45)
|
| 111 |
+
draw.rectangle([0, 0, 1080, 100], fill='#4a90e2')
|
| 112 |
+
draw.text((40, 150), "Demo App", fill='black', font=font_h1)
|
| 113 |
+
draw.rectangle([100, 300, 980, 450], fill='#ffffff', outline='gray', width=3)
|
| 114 |
+
draw.text((400, 345), "Login", fill='black', font=font_p)
|
| 115 |
+
self.latest_screenshot_uri = self._image_to_data_uri(demo_img)
|
| 116 |
+
return True, "Connected to Simulated Device."
|
| 117 |
+
except Exception as e:
|
| 118 |
+
print(f"Error creating demo device: {e}")
|
| 119 |
+
return False, "Failed to create Simulated Device."
|
| 120 |
+
|
| 121 |
+
def get_connected_devices(self):
|
| 122 |
+
"""BLOCKING: Gets device serial numbers. Must be run in a thread."""
|
| 123 |
+
demo_option = "demo_device_cloud (Simulated)"
|
| 124 |
+
if not self.adb_server_running:
|
| 125 |
+
return [demo_option]
|
| 126 |
+
try:
|
| 127 |
+
devices = self.adb_client.devices()
|
| 128 |
+
return [d.serial for d in devices] + [demo_option]
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"β ADB command failed: {e}. Falling back to demo mode.")
|
| 131 |
+
self.adb_server_running = False # Mark as not running to avoid retries
|
| 132 |
+
return [demo_option]
|
| 133 |
+
|
| 134 |
+
def connect_device(self, device_serial):
|
| 135 |
+
"""BLOCKING: Connects to a device. Must be run in a thread."""
|
| 136 |
+
if not device_serial:
|
| 137 |
+
return False, "No device selected."
|
| 138 |
+
if "demo_device_cloud" in device_serial:
|
| 139 |
+
return self._create_mock_device_for_demo()
|
| 140 |
+
|
| 141 |
+
if not self.adb_server_running:
|
| 142 |
+
return False, "ADB Server not available. Cannot connect to physical devices."
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
device = self.adb_client.device(device_serial)
|
| 146 |
+
if device:
|
| 147 |
+
self.current_ppadb_device = device
|
| 148 |
+
self.current_device_serial = device_serial
|
| 149 |
+
print(f"β
Successfully connected to {device_serial}")
|
| 150 |
+
return True, f"Connected to {device_serial}"
|
| 151 |
+
else:
|
| 152 |
+
raise RuntimeError("Device not found by client.")
|
| 153 |
+
except Exception as e:
|
| 154 |
+
self.current_device_serial = None
|
| 155 |
+
self.current_ppadb_device = None
|
| 156 |
+
return False, f"Failed to connect to {device_serial}: {e}"
|
| 157 |
+
|
| 158 |
+
def _image_to_data_uri(self, img, max_width=800):
|
| 159 |
+
"""Resizes a PIL image and converts to a base64 data URI."""
|
| 160 |
+
if img.width > max_width:
|
| 161 |
+
ratio = max_width / float(img.width)
|
| 162 |
+
height = int(float(img.height) * ratio)
|
| 163 |
+
img = img.resize((max_width, height), Image.Resampling.LANCZOS)
|
| 164 |
+
|
| 165 |
+
buffered = io.BytesIO()
|
| 166 |
+
img.save(buffered, format="PNG")
|
| 167 |
+
return f"data:image/png;base64,{base64.b64encode(buffered.getvalue()).decode()}"
|
| 168 |
+
|
| 169 |
+
def capture_screenshot(self):
|
| 170 |
+
"""BLOCKING: Captures a screenshot. Must be run in a thread."""
|
| 171 |
+
if not self.current_device_serial: return None, "No device connected."
|
| 172 |
+
if "demo_device_cloud" in self.current_device_serial:
|
| 173 |
+
# Re-create demo image to simulate a fresh capture
|
| 174 |
+
self._create_mock_device_for_demo()
|
| 175 |
+
return self.latest_screenshot_uri, "Captured from Simulated Device."
|
| 176 |
+
if not self.current_ppadb_device: return None, "Device not properly connected."
|
| 177 |
+
|
| 178 |
+
try:
|
| 179 |
+
result_bytes = self.current_ppadb_device.screencap()
|
| 180 |
+
img = Image.open(io.BytesIO(result_bytes))
|
| 181 |
+
self.latest_screenshot_uri = self._image_to_data_uri(img)
|
| 182 |
+
return self.latest_screenshot_uri, "Screenshot captured successfully."
|
| 183 |
+
except Exception as e:
|
| 184 |
+
return None, f"Screenshot error: {e}"
|
| 185 |
+
|
| 186 |
+
def get_device_info(self):
|
| 187 |
+
"""BLOCKING: Gets device info. Must be run in a thread."""
|
| 188 |
+
if not self.current_device_serial: return "No device connected"
|
| 189 |
+
if "demo_device_cloud" in self.current_device_serial: return "Model: Simulated Android\nOS: 14.0"
|
| 190 |
+
if not self.current_ppadb_device: return "ADB device object not found."
|
| 191 |
+
try:
|
| 192 |
+
model = self.current_ppadb_device.shell('getprop ro.product.model').strip()
|
| 193 |
+
version = self.current_ppadb_device.shell('getprop ro.build.version.release').strip()
|
| 194 |
+
return f"Model: {model}\nOS: {version}"
|
| 195 |
+
except Exception as e:
|
| 196 |
+
return f"Error getting device info: {e}"
|
| 197 |
+
|
| 198 |
+
def perform_action(self, action_name, *args):
|
| 199 |
+
"""BLOCKING: Generic action executor. Must be run in a thread."""
|
| 200 |
+
if not self.current_ppadb_device:
|
| 201 |
+
return self.latest_screenshot_uri, "Action failed: No real device connected."
|
| 202 |
+
try:
|
| 203 |
+
action_map = {
|
| 204 |
+
'tap': lambda x, y: self.current_ppadb_device.shell(f'input tap {int(x)} {int(y)}'),
|
| 205 |
+
'text': lambda text: self.current_ppadb_device.input_text(text),
|
| 206 |
+
'key': lambda key: self.current_ppadb_device.shell(f'input keyevent {{"back": 4, "home": 3}.get(key, 0)}')
|
| 207 |
+
}
|
| 208 |
+
action_map[action_name](*args)
|
| 209 |
+
# Give UI time to update before screenshot
|
| 210 |
+
asyncio.run(asyncio.sleep(1.0)) # Use asyncio.sleep in sync context
|
| 211 |
+
return self.capture_screenshot()
|
| 212 |
+
except Exception as e:
|
| 213 |
+
return self.latest_screenshot_uri, f"Action '{action_name}' failed: {e}"
|
| 214 |
+
|
| 215 |
+
# Initialize device manager
|
| 216 |
+
device_manager = AndroidDeviceManager()
|
| 217 |
+
|
| 218 |
+
# --- MAIN GRADIO UI ---
|
| 219 |
+
def create_android_interface():
|
| 220 |
+
with gr.Blocks(css=".gradio-container{font-family:'Segoe UI',sans-serif}", title="Android App Review Agent") as demo:
|
| 221 |
+
gr.HTML("<h1>π€ Android App Review Agent</h1>")
|
| 222 |
+
|
| 223 |
+
with gr.Row():
|
| 224 |
+
with gr.Column(scale=2):
|
| 225 |
+
chatbot = gr.Chatbot(height=550, label="AI Assistant", show_copy_button=True)
|
| 226 |
+
with gr.Row():
|
| 227 |
+
msg = gr.Textbox(placeholder="Ask about the screen...", show_label=False, scale=4)
|
| 228 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 229 |
+
with gr.Row():
|
| 230 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 231 |
+
include_screen = gr.Checkbox(label="Include screen in prompt", value=True)
|
| 232 |
+
|
| 233 |
+
with gr.Column(scale=2):
|
| 234 |
+
gr.HTML("<h3>π± Android Device</h3>")
|
| 235 |
+
with gr.Row():
|
| 236 |
+
refresh_btn = gr.Button("π Refresh Devices", scale=1)
|
| 237 |
+
device_dropdown = gr.Dropdown(label="Connected Devices", scale=2, interactive=True)
|
| 238 |
+
connect_btn = gr.Button("π Connect", variant="primary")
|
| 239 |
+
device_status = gr.HTML(f"<div style='padding:10px;border-radius:5px;background-color:#f0f0f0;'>Status: App Loaded</div>")
|
| 240 |
+
device_screen = gr.Image(label="Device Screen", height=600, interactive=False)
|
| 241 |
+
with gr.Row():
|
| 242 |
+
back_btn = gr.Button("β¬
οΈ Back")
|
| 243 |
+
home_btn = gr.Button("π Home")
|
| 244 |
+
screenshot_btn = gr.Button("πΈ Capture", variant="primary")
|
| 245 |
+
|
| 246 |
+
with gr.Column(scale=1):
|
| 247 |
+
gr.HTML("<h3>βοΈ Controls</h3>")
|
| 248 |
+
system_prompts = {
|
| 249 |
+
"Android App Reviewer": "You are an expert QA Engineer. Analyze the provided app screen. Report potential bugs, UI/UX improvements, and usability issues in a concise markdown report.",
|
| 250 |
+
"General Assistant": "You are a helpful AI assistant. Provide accurate responses to the user's query.",
|
| 251 |
+
}
|
| 252 |
+
system_message = gr.Textbox(value=system_prompts["Android App Reviewer"], label="System Prompt", lines=8)
|
| 253 |
+
gr.Accordion("ποΈ AI Settings", open=False)
|
| 254 |
+
max_tokens = gr.Slider(1, 4096, 2048, label="Max Tokens")
|
| 255 |
+
temperature = gr.Slider(0.0, 1.0, 0.5, label="Temperature")
|
| 256 |
+
top_p = gr.Slider(0.0, 1.0, 0.95, label="Top-p")
|
| 257 |
+
with gr.Accordion("π Device Interaction", open=True):
|
| 258 |
+
with gr.Row():
|
| 259 |
+
tap_x = gr.Number(label="X", value=540)
|
| 260 |
+
tap_y = gr.Number(label="Y", value=1170)
|
| 261 |
+
tap_btn = gr.Button("π Tap")
|
| 262 |
+
text_input = gr.Textbox(label="Text to Send")
|
| 263 |
+
send_text_btn = gr.Button("β¨οΈ Send Text")
|
| 264 |
+
|
| 265 |
+
# --- ASYNC EVENT HANDLERS ---
|
| 266 |
+
async def async_refresh_devices():
|
| 267 |
+
devices = await asyncio.to_thread(device_manager.get_connected_devices)
|
| 268 |
+
current_val = device_manager.current_device_serial
|
| 269 |
+
new_value = current_val if current_val in devices else devices[0] if devices else None
|
| 270 |
+
return gr.Dropdown(choices=devices, value=new_value)
|
| 271 |
+
|
| 272 |
+
async def async_connect_device(device_id):
|
| 273 |
+
if not device_id:
|
| 274 |
+
return "<div>Status: Please select a device.</div>", None
|
| 275 |
+
|
| 276 |
+
success, message = await asyncio.to_thread(device_manager.connect_device, device_id)
|
| 277 |
+
color = "#28a745" if success else "#dc3545"
|
| 278 |
+
status_html = f"<div style='padding:10px;border-radius:5px;background-color:{color};color:white;'><strong>Status:</strong> {message}</div>"
|
| 279 |
+
|
| 280 |
+
if success:
|
| 281 |
+
info = await asyncio.to_thread(device_manager.get_device_info)
|
| 282 |
+
status_html += f"<div style='padding:10px;margin-top:5px;border-radius:5px;background-color:#f0f0f0;'>{info.replace(chr(10), '<br>')}</div>"
|
| 283 |
+
screen_uri, _ = await asyncio.to_thread(device_manager.capture_screenshot)
|
| 284 |
+
return status_html, screen_uri
|
| 285 |
+
else:
|
| 286 |
+
return status_html, None
|
| 287 |
+
|
| 288 |
+
async def async_chat_stream(message, history, sys_msg, max_tok, temp, top_p_val, use_screen):
|
| 289 |
+
if not message:
|
| 290 |
+
return history, ""
|
| 291 |
+
|
| 292 |
+
# Immediately update UI
|
| 293 |
+
yield history + [("user", message)], ""
|
| 294 |
+
|
| 295 |
+
image_data = device_manager.latest_screenshot_uri if use_screen else None
|
| 296 |
+
|
| 297 |
+
# Stream the response
|
| 298 |
+
async for response in respond(message, history, sys_msg, max_tok, temp, top_p_val, image_data):
|
| 299 |
+
yield response, ""
|
| 300 |
+
|
| 301 |
+
async def async_capture_and_update():
|
| 302 |
+
screen_uri, status_msg = await asyncio.to_thread(device_manager.capture_screenshot)
|
| 303 |
+
color = "#28a745" if "success" in status_msg.lower() else "#ffc107"
|
| 304 |
+
status_html = f"<div style='padding:10px;border-radius:5px;background-color:{color};color:white;'>{status_msg}</div>"
|
| 305 |
+
return screen_uri, status_html
|
| 306 |
+
|
| 307 |
+
async def async_device_action(action, *params):
|
| 308 |
+
screen_uri, status_msg = await asyncio.to_thread(device_manager.perform_action, action, *params)
|
| 309 |
+
status_html = f"<div style='padding:10px;border-radius:5px;background-color:#007bff;color:white;'>{status_msg}</div>"
|
| 310 |
+
return screen_uri, status_html
|
| 311 |
+
|
| 312 |
+
# --- UI Event Wiring ---
|
| 313 |
+
async def initial_load():
|
| 314 |
+
"""Runs when the app first loads."""
|
| 315 |
+
dropdown_update = await async_refresh_devices()
|
| 316 |
+
# If no ADB server, automatically connect to the demo device for a better UX
|
| 317 |
+
if not device_manager.adb_server_running:
|
| 318 |
+
status_update, screen_update = await async_connect_device("demo_device_cloud (Simulated)")
|
| 319 |
+
return dropdown_update, status_update, screen_update
|
| 320 |
+
return dropdown_update, gr.HTML(), gr.Image()
|
| 321 |
+
|
| 322 |
+
demo.load(initial_load, outputs=[device_dropdown, device_status, device_screen])
|
| 323 |
+
|
| 324 |
+
refresh_btn.click(async_refresh_devices, outputs=device_dropdown)
|
| 325 |
+
connect_btn.click(async_connect_device, inputs=device_dropdown, outputs=[device_status, device_screen])
|
| 326 |
+
|
| 327 |
+
msg.submit(async_chat_stream, [msg, chatbot, system_message, max_tokens, temperature, top_p, include_screen], [chatbot, msg])
|
| 328 |
+
submit_btn.click(async_chat_stream, [msg, chatbot, system_message, max_tokens, temperature, top_p, include_screen], [chatbot, msg])
|
| 329 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 330 |
+
|
| 331 |
+
screenshot_btn.click(async_capture_and_update, outputs=[device_screen, device_status])
|
| 332 |
+
back_btn.click(lambda: async_device_action("key", "back"), outputs=[device_screen, device_status])
|
| 333 |
+
home_btn.click(lambda: async_device_action("key", "home"), outputs=[device_screen, device_status])
|
| 334 |
+
tap_btn.click(lambda x, y: async_device_action("tap", x, y), [tap_x, tap_y], [device_screen, device_status])
|
| 335 |
+
send_text_btn.click(lambda t: async_device_action("text", t), [text_input], [device_screen, device_status]).then(lambda: "", outputs=text_input)
|
| 336 |
+
|
| 337 |
+
return demo
|
| 338 |
+
|
| 339 |
+
if __name__ == "__main__":
|
| 340 |
+
print("===== Application Startup =====")
|
| 341 |
+
if not os.getenv("nebiusaistudioapikey"):
|
| 342 |
+
print("β οΈ Warning: NEBIUSAISTUDIOAPIKEY environment variable not set! API calls will fail.")
|
| 343 |
+
else:
|
| 344 |
+
print("β
Nebius AI Studio API key found.")
|
| 345 |
+
|
| 346 |
+
app = create_android_interface()
|
| 347 |
+
|
| 348 |
+
print("\nπ Starting Gradio interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
app.launch()
|