Stremly commited on
Commit
823c6b6
·
verified ·
1 Parent(s): 14672e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py CHANGED
@@ -20,7 +20,26 @@ processor = AutoProcessor.from_pretrained(
20
  use_fast=True,
21
  )
22
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def draw_point(image: Image.Image, point=None, radius: int = 5):
25
  """Overlay a red dot on the screenshot where the model clicked."""
26
  img = image.copy()
@@ -55,6 +74,13 @@ def navigate(screenshot, task: str, platform: str, history):
55
  else:
56
  messages = history
57
 
 
 
 
 
 
 
 
58
  prompt_header = (
59
  "You are a GUI agent. You are given a task and your action history, with screenshots."
60
  "You need to perform the next action to complete the task. \n\n## Output Format\n```\nThought: ...\nAction: ...\n```\n\n## Action Space\n\nclick(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\nleft_double(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\nright_single(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\ndrag(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e', end_box='\u003c|box_start|\u003e(x3, y3)\u003c|box_end|\u003e')\nhotkey(key='')\ntype(content='') #If you want to submit your input, use \"\\n\" at the end of `content`.\nscroll(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e', direction='down or up or right or left')\nwait() #Sleep for 5s and take a screenshot to check for any changes.\nfinished(content='xxx') # Use escape characters \\', \\\", and \\n in content part to ensure we can parse the content in normal python string format.\n\n\n## Note\n- Use English in `Thought` part.\n- Write a small plan and finally summarize your next action (with its target element) in one sentence in `Thought` part.\n\n"
 
20
  use_fast=True,
21
  )
22
 
23
+ def load_base64_to_pil(base64_string):
24
+ """
25
+ Loads a Base64 encoded image string into a PIL Image object.
26
+
27
+ Args:
28
+ base64_string (str): The Base64 encoded string of the image.
29
 
30
+ Returns:
31
+ PIL.Image.Image: The loaded PIL Image object.
32
+ """
33
+ # If the Base64 string includes a data URI prefix (e.g., "data:image/png;base64,"),
34
+ # you should strip it before decoding.
35
+ if ',' in base64_string:
36
+ base64_string = base64_string.split(',')[1]
37
+
38
+ decoded_bytes = base64.b64decode(base64_string)
39
+ image_stream = BytesIO(decoded_bytes)
40
+ pil_image = Image.open(image_stream)
41
+ return pil_image
42
+
43
  def draw_point(image: Image.Image, point=None, radius: int = 5):
44
  """Overlay a red dot on the screenshot where the model clicked."""
45
  img = image.copy()
 
74
  else:
75
  messages = history
76
 
77
+ for message in messages:
78
+ if message['role'] == 'user' and isinstance(message.get('content'), list):
79
+ for item in message['content']:
80
+ if item.get('type') == 'image_url' and isinstance(item.get('image_url'), str):
81
+ # This is a base64 string, convert it to a PIL image
82
+ item['image_url'] = load_base64_to_pil(item['image_url'])
83
+
84
  prompt_header = (
85
  "You are a GUI agent. You are given a task and your action history, with screenshots."
86
  "You need to perform the next action to complete the task. \n\n## Output Format\n```\nThought: ...\nAction: ...\n```\n\n## Action Space\n\nclick(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\nleft_double(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\nright_single(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e')\ndrag(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e', end_box='\u003c|box_start|\u003e(x3, y3)\u003c|box_end|\u003e')\nhotkey(key='')\ntype(content='') #If you want to submit your input, use \"\\n\" at the end of `content`.\nscroll(start_box='\u003c|box_start|\u003e(x1, y1)\u003c|box_end|\u003e', direction='down or up or right or left')\nwait() #Sleep for 5s and take a screenshot to check for any changes.\nfinished(content='xxx') # Use escape characters \\', \\\", and \\n in content part to ensure we can parse the content in normal python string format.\n\n\n## Note\n- Use English in `Thought` part.\n- Write a small plan and finally summarize your next action (with its target element) in one sentence in `Thought` part.\n\n"