Spaces:
Runtime error
Runtime error
Commit
Β·
48483a7
1
Parent(s):
2053bec
test input
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
-
#from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
4 |
-
# from qwen_vl_utils import process_vision_info
|
5 |
import torch
|
6 |
import base64
|
7 |
from PIL import Image, ImageDraw
|
@@ -17,14 +15,12 @@ from transformers import AutoModelForCausalLM
|
|
17 |
|
18 |
|
19 |
models = {
|
20 |
-
#"OS-Copilot/OS-Atlas-Base-7B": Qwen2VLForConditionalGeneration.from_pretrained("OS-Copilot/OS-Atlas-Base-7B", torch_dtype="auto", device_map="auto"),
|
21 |
"deepseek-ai/deepseek-vl2-tiny": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2-tiny", trust_remote_code=True),
|
22 |
#"deepseek-ai/deepseek-vl2-small": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2-small", trust_remote_code=True),
|
23 |
#"deepseek-ai/deepseek-vl2": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2", trust_remote_code=True)
|
24 |
}
|
25 |
|
26 |
processors = {
|
27 |
-
#"OS-Copilot/OS-Atlas-Base-7B": AutoProcessor.from_pretrained("OS-Copilot/OS-Atlas-Base-7B")
|
28 |
"deepseek-ai/deepseek-vl2-tiny": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2-tiny",),
|
29 |
#"deepseek-ai/deepseek-vl2-small": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2-small",),
|
30 |
#"deepseek-ai/deepseek-vl2": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2",),
|
@@ -110,69 +106,21 @@ def deepseek(image, text_input, model_id):
|
|
110 |
det_pattern = r"<\|det\|>\[\[(.+)]]<\|\/det\|>"
|
111 |
|
112 |
det_content = re.search(det_pattern, answer).group(1)
|
113 |
-
|
114 |
-
|
115 |
-
bbox = [[bbox[0] * w, bbox[1] * h, bbox[2] * w, bbox[3] * h]]
|
116 |
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
119 |
|
120 |
|
121 |
@spaces.GPU
|
122 |
def run_example(image, text_input, model_id="OS-Copilot/OS-Atlas-Base-7B"):
|
123 |
-
|
124 |
return deepseek(image, text_input, model_id)
|
125 |
|
126 |
-
|
127 |
-
def run_example_old(image, text_input, model_id="OS-Copilot/OS-Atlas-Base-7B"):
|
128 |
-
model = models[model_id].eval()
|
129 |
-
processor = processors[model_id]
|
130 |
-
prompt = f"In this UI screenshot, what is the position of the element corresponding to the command \"{text_input}\" (with bbox)?"
|
131 |
-
messages = [
|
132 |
-
{
|
133 |
-
"role": "user",
|
134 |
-
"content": [
|
135 |
-
{"type": "image", "image": f"data:image;base64,{image_to_base64(image)}"},
|
136 |
-
{"type": "text", "text": prompt},
|
137 |
-
],
|
138 |
-
}
|
139 |
-
]
|
140 |
-
|
141 |
-
text = processor.apply_chat_template(
|
142 |
-
messages, tokenize=False, add_generation_prompt=True
|
143 |
-
)
|
144 |
-
image_inputs, video_inputs = process_vision_info(messages)
|
145 |
-
inputs = processor(
|
146 |
-
text=[text],
|
147 |
-
images=image_inputs,
|
148 |
-
videos=video_inputs,
|
149 |
-
padding=True,
|
150 |
-
return_tensors="pt",
|
151 |
-
)
|
152 |
-
inputs = inputs.to("cuda")
|
153 |
-
|
154 |
-
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
155 |
-
generated_ids_trimmed = [
|
156 |
-
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
157 |
-
]
|
158 |
-
output_text = processor.batch_decode(
|
159 |
-
generated_ids_trimmed, skip_special_tokens=False, clean_up_tokenization_spaces=False
|
160 |
-
)
|
161 |
-
print(output_text)
|
162 |
-
text = output_text[0]
|
163 |
-
|
164 |
-
object_ref_pattern = r"<\|object_ref_start\|>(.*?)<\|object_ref_end\|>"
|
165 |
-
box_pattern = r"<\|box_start\|>(.*?)<\|box_end\|>"
|
166 |
-
|
167 |
-
object_ref = re.search(object_ref_pattern, text).group(1)
|
168 |
-
box_content = re.search(box_pattern, text).group(1)
|
169 |
-
|
170 |
-
boxes = [tuple(map(int, pair.strip("()").split(','))) for pair in box_content.split("),(")]
|
171 |
-
boxes = [[boxes[0][0], boxes[0][1], boxes[1][0], boxes[1][1]]]
|
172 |
-
|
173 |
-
scaled_boxes = rescale_bounding_boxes(boxes, image.width, image.height)
|
174 |
-
return object_ref, scaled_boxes, draw_bounding_boxes(image, scaled_boxes)
|
175 |
-
|
176 |
css = """
|
177 |
#output {
|
178 |
height: 500px;
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
|
|
|
|
3 |
import torch
|
4 |
import base64
|
5 |
from PIL import Image, ImageDraw
|
|
|
15 |
|
16 |
|
17 |
models = {
|
|
|
18 |
"deepseek-ai/deepseek-vl2-tiny": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2-tiny", trust_remote_code=True),
|
19 |
#"deepseek-ai/deepseek-vl2-small": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2-small", trust_remote_code=True),
|
20 |
#"deepseek-ai/deepseek-vl2": AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-vl2", trust_remote_code=True)
|
21 |
}
|
22 |
|
23 |
processors = {
|
|
|
24 |
"deepseek-ai/deepseek-vl2-tiny": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2-tiny",),
|
25 |
#"deepseek-ai/deepseek-vl2-small": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2-small",),
|
26 |
#"deepseek-ai/deepseek-vl2": DeepseekVLV2Processor.from_pretrained("deepseek-ai/deepseek-vl2",),
|
|
|
106 |
det_pattern = r"<\|det\|>\[\[(.+)]]<\|\/det\|>"
|
107 |
|
108 |
det_content = re.search(det_pattern, answer).group(1)
|
109 |
+
if det_content is None:
|
110 |
+
return text_input, [], image
|
|
|
111 |
|
112 |
+
bbox = [ int(v.strip()) for v in det_content.split(",")]
|
113 |
+
#w, h = image.size
|
114 |
+
#bbox = [[bbox[0] * w, bbox[1] * h, bbox[2] * w, bbox[3] * h]]
|
115 |
+
|
116 |
+
scaled_boxes = rescale_bounding_boxes([scaled_boxes], image.width, image.height)
|
117 |
+
return text_input, scaled_boxes, draw_bounding_boxes(image, scaled_boxes)
|
118 |
|
119 |
|
120 |
@spaces.GPU
|
121 |
def run_example(image, text_input, model_id="OS-Copilot/OS-Atlas-Base-7B"):
|
|
|
122 |
return deepseek(image, text_input, model_id)
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
css = """
|
125 |
#output {
|
126 |
height: 500px;
|