Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoPro
|
|
| 4 |
from qwen_vl_utils import process_vision_info
|
| 5 |
import torch
|
| 6 |
import base64
|
| 7 |
-
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
import re
|
| 10 |
|
|
@@ -27,6 +27,14 @@ def image_to_base64(image):
|
|
| 27 |
return img_str
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@spaces.GPU
|
| 31 |
def run_example(image, text_input, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
| 32 |
model = models[model_id].eval()
|
|
@@ -67,7 +75,7 @@ def run_example(image, text_input, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
|
| 67 |
pattern = r'\[\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\]'
|
| 68 |
matches = re.findall(pattern, str(output_text))
|
| 69 |
parsed_boxes = [[int(num) for num in match] for match in matches]
|
| 70 |
-
return output_text, parsed_boxes
|
| 71 |
|
| 72 |
css = """
|
| 73 |
#output {
|
|
@@ -89,7 +97,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 89 |
with gr.Column():
|
| 90 |
model_output_text = gr.Textbox(label="Model Output Text")
|
| 91 |
parsed_boxes = gr.Textbox(label="Parsed Boxes")
|
|
|
|
| 92 |
|
| 93 |
-
submit_btn.click(run_example, [input_img, text_input, model_selector], [model_output_text, parsed_boxes])
|
| 94 |
|
| 95 |
demo.launch(debug=True)
|
|
|
|
| 4 |
from qwen_vl_utils import process_vision_info
|
| 5 |
import torch
|
| 6 |
import base64
|
| 7 |
+
from PIL import Image, ImageDraw
|
| 8 |
from io import BytesIO
|
| 9 |
import re
|
| 10 |
|
|
|
|
| 27 |
return img_str
|
| 28 |
|
| 29 |
|
| 30 |
+
def draw_bounding_boxes(image, bounding_boxes, outline_color="red", line_width=2):
|
| 31 |
+
draw = ImageDraw.Draw(image)
|
| 32 |
+
for box in bounding_boxes:
|
| 33 |
+
xmin, xmax, ymin, ymax = box
|
| 34 |
+
draw.rectangle([xmin, ymin, xmax, ymax], outline=outline_color, width=line_width)
|
| 35 |
+
return image
|
| 36 |
+
|
| 37 |
+
|
| 38 |
@spaces.GPU
|
| 39 |
def run_example(image, text_input, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
| 40 |
model = models[model_id].eval()
|
|
|
|
| 75 |
pattern = r'\[\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\]'
|
| 76 |
matches = re.findall(pattern, str(output_text))
|
| 77 |
parsed_boxes = [[int(num) for num in match] for match in matches]
|
| 78 |
+
return output_text, parsed_boxes, draw_bounding_boxes(image, parsed_boxes)
|
| 79 |
|
| 80 |
css = """
|
| 81 |
#output {
|
|
|
|
| 97 |
with gr.Column():
|
| 98 |
model_output_text = gr.Textbox(label="Model Output Text")
|
| 99 |
parsed_boxes = gr.Textbox(label="Parsed Boxes")
|
| 100 |
+
annotated_image = gr.Image(label="Annotated Picture")
|
| 101 |
|
| 102 |
+
submit_btn.click(run_example, [input_img, text_input, model_selector], [model_output_text, parsed_boxes, annotated_image])
|
| 103 |
|
| 104 |
demo.launch(debug=True)
|