Spaces:
Running
on
Zero
Running
on
Zero
jadechoghari
commited on
Commit
•
8fa6ab2
1
Parent(s):
e682d93
add app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,108 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
demo
|
7 |
-
demo.launch()
|
|
|
1 |
+
from typing import Optional
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
|
9 |
+
|
10 |
+
import base64, os
|
11 |
+
from utils import check_ocr_box, get_yolo_model, get_caption_model_processor, get_som_labeled_img
|
12 |
+
import torch
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
yolo_model = get_yolo_model(model_path='weights/icon_detect/best.pt')
|
16 |
+
caption_model_processor = get_caption_model_processor(model_name="florence2", model_name_or_path="weights/icon_caption_florence")
|
17 |
+
platform = 'pc'
|
18 |
+
if platform == 'pc':
|
19 |
+
draw_bbox_config = {
|
20 |
+
'text_scale': 0.8,
|
21 |
+
'text_thickness': 2,
|
22 |
+
'text_padding': 2,
|
23 |
+
'thickness': 2,
|
24 |
+
}
|
25 |
+
elif platform == 'web':
|
26 |
+
draw_bbox_config = {
|
27 |
+
'text_scale': 0.8,
|
28 |
+
'text_thickness': 2,
|
29 |
+
'text_padding': 3,
|
30 |
+
'thickness': 3,
|
31 |
+
}
|
32 |
+
elif platform == 'mobile':
|
33 |
+
draw_bbox_config = {
|
34 |
+
'text_scale': 0.8,
|
35 |
+
'text_thickness': 2,
|
36 |
+
'text_padding': 3,
|
37 |
+
'thickness': 3,
|
38 |
+
}
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
MARKDOWN = """
|
43 |
+
# OmniParser for Pure Vision Based General GUI Agent 🔥
|
44 |
+
<div>
|
45 |
+
<a href="https://arxiv.org/pdf/2408.00203">
|
46 |
+
<img src="https://img.shields.io/badge/arXiv-2408.00203-b31b1b.svg" alt="Arxiv" style="display:inline-block;">
|
47 |
+
</a>
|
48 |
+
</div>
|
49 |
+
|
50 |
+
OmniParser is a screen parsing tool to convert general GUI screen to structured elements.
|
51 |
+
"""
|
52 |
+
|
53 |
+
DEVICE = torch.device('cuda')
|
54 |
+
|
55 |
+
# @spaces.GPU
|
56 |
+
# @torch.inference_mode()
|
57 |
+
# @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
|
58 |
+
def process(
|
59 |
+
image_input,
|
60 |
+
box_threshold,
|
61 |
+
iou_threshold
|
62 |
+
) -> Optional[Image.Image]:
|
63 |
+
|
64 |
+
image_save_path = 'imgs/saved_image_demo.png'
|
65 |
+
image_input.save(image_save_path)
|
66 |
+
# import pdb; pdb.set_trace()
|
67 |
+
|
68 |
+
ocr_bbox_rslt, is_goal_filtered = check_ocr_box(image_save_path, display_img = False, output_bb_format='xyxy', goal_filtering=None, easyocr_args={'paragraph': False, 'text_threshold':0.9})
|
69 |
+
text, ocr_bbox = ocr_bbox_rslt
|
70 |
+
# print('prompt:', prompt)
|
71 |
+
dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image_save_path, yolo_model, BOX_TRESHOLD = box_threshold, output_coord_in_ratio=True, ocr_bbox=ocr_bbox,draw_bbox_config=draw_bbox_config, caption_model_processor=caption_model_processor, ocr_text=text,iou_threshold=iou_threshold)
|
72 |
+
image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
|
73 |
+
print('finish processing')
|
74 |
+
parsed_content_list = '\n'.join(parsed_content_list)
|
75 |
+
return image, str(parsed_content_list)
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown(MARKDOWN)
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column():
|
83 |
+
image_input_component = gr.Image(
|
84 |
+
type='pil', label='Upload image')
|
85 |
+
# set the threshold for removing the bounding boxes with low confidence, default is 0.05
|
86 |
+
box_threshold_component = gr.Slider(
|
87 |
+
label='Box Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.05)
|
88 |
+
# set the threshold for removing the bounding boxes with large overlap, default is 0.1
|
89 |
+
iou_threshold_component = gr.Slider(
|
90 |
+
label='IOU Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.1)
|
91 |
+
submit_button_component = gr.Button(
|
92 |
+
value='Submit', variant='primary')
|
93 |
+
with gr.Column():
|
94 |
+
image_output_component = gr.Image(type='pil', label='Image Output')
|
95 |
+
text_output_component = gr.Textbox(label='Parsed screen elements', placeholder='Text Output')
|
96 |
|
97 |
+
submit_button_component.click(
|
98 |
+
fn=process,
|
99 |
+
inputs=[
|
100 |
+
image_input_component,
|
101 |
+
box_threshold_component,
|
102 |
+
iou_threshold_component
|
103 |
+
],
|
104 |
+
outputs=[image_output_component, text_output_component]
|
105 |
+
)
|
106 |
|
107 |
+
# demo.launch(debug=False, show_error=True, share=True)
|
108 |
+
demo.launch(share=True, server_port=7861, server_name='0.0.0.0')
|