Spaces:
Sleeping
Sleeping
gokaygokay
commited on
Commit
•
ade70cf
1
Parent(s):
2bbbd6c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
+
import requests
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import matplotlib.patches as patches
|
7 |
+
import numpy as np
|
8 |
+
import random
|
9 |
+
|
10 |
+
# Load model and processor
|
11 |
+
model_id = 'microsoft/Florence-2-large'
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
|
13 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
14 |
+
|
15 |
+
def run_example(task_prompt, image, text_input=None):
|
16 |
+
prompt = task_prompt if text_input is None else task_prompt + text_input
|
17 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
18 |
+
generated_ids = model.generate(
|
19 |
+
input_ids=inputs["input_ids"],
|
20 |
+
pixel_values=inputs["pixel_values"],
|
21 |
+
max_new_tokens=1024,
|
22 |
+
early_stopping=False,
|
23 |
+
do_sample=False,
|
24 |
+
num_beams=3,
|
25 |
+
)
|
26 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
27 |
+
parsed_answer = processor.post_process_generation(
|
28 |
+
generated_text,
|
29 |
+
task=task_prompt,
|
30 |
+
image_size=(image.width, image.height)
|
31 |
+
)
|
32 |
+
return parsed_answer
|
33 |
+
|
34 |
+
def plot_bbox(image, data):
|
35 |
+
fig, ax = plt.subplots()
|
36 |
+
ax.imshow(image)
|
37 |
+
for bbox, label in zip(data['bboxes'], data['labels']):
|
38 |
+
x1, y1, x2, y2 = bbox
|
39 |
+
rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none')
|
40 |
+
ax.add_patch(rect)
|
41 |
+
plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
42 |
+
plt.axis('off')
|
43 |
+
plt.show()
|
44 |
+
|
45 |
+
def draw_polygons(image, prediction, fill_mask=False):
|
46 |
+
draw = ImageDraw.Draw(image)
|
47 |
+
colormap = ['blue', 'orange', 'green', 'purple', 'brown', 'pink', 'gray', 'olive', 'cyan', 'red']
|
48 |
+
for polygons, label in zip(prediction['polygons'], prediction['labels']):
|
49 |
+
color = random.choice(colormap)
|
50 |
+
fill_color = color if fill_mask else None
|
51 |
+
for polygon in polygons:
|
52 |
+
draw.polygon(polygon, outline=color, fill=fill_color)
|
53 |
+
draw.text((polygon[0][0], polygon[0][1]), label, fill=color)
|
54 |
+
image.show()
|
55 |
+
|
56 |
+
def gradio_interface(image, task_prompt, text_input):
|
57 |
+
result = run_example(task_prompt, image, text_input)
|
58 |
+
if task_prompt in ['<OD>', '<OPEN_VOCABULARY_DETECTION>']:
|
59 |
+
plot_bbox(image, result)
|
60 |
+
elif task_prompt in ['<REFERRING_EXPRESSION_SEGMENTATION>', '<REGION_TO_SEGMENTATION>']:
|
61 |
+
draw_polygons(image, result, fill_mask=True)
|
62 |
+
return result
|
63 |
+
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
gr.Markdown("## Florence Model Advanced Tasks")
|
66 |
+
with gr.Row():
|
67 |
+
image_input = gr.Image(type="pil")
|
68 |
+
task_input = gr.Dropdown(label="Select Task", choices=[
|
69 |
+
'<CAPTION>', '<DETAILED_CAPTION>', '<MORE_DETAILED_CAPTION>',
|
70 |
+
'<OD>', '<DENSE_REGION_CAPTION>', '<REGION_PROPOSAL>',
|
71 |
+
'<CAPTION_TO_PHRASE_GROUNDING>', '<REFERRING_EXPRESSION_SEGMENTATION>',
|
72 |
+
'<REGION_TO_SEGMENTATION>', '<OPEN_VOCABULARY_DETECTION>',
|
73 |
+
'<REGION_TO_CATEGORY>', '<REGION_TO_DESCRIPTION>', '<OCR>', '<OCR_WITH_REGION>'
|
74 |
+
])
|
75 |
+
text_input = gr.Textbox(label="Optional Text Input", placeholder="Enter text here if required by the task")
|
76 |
+
submit_btn = gr.Button("Run Task")
|
77 |
+
output = gr.Textbox(label="Output")
|
78 |
+
|
79 |
+
submit_btn.click(fn=gradio_interface, inputs=[image_input, task_input, text_input], outputs=output)
|
80 |
+
|
81 |
+
demo.launch()
|