Spaces:
Sleeping
Sleeping
add interface logic
Browse files
app.py
CHANGED
@@ -1,15 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from PIL import Image, ImageDraw
|
4 |
from transformers import AutoProcessor
|
5 |
from modeling_florence2 import Florence2ForConditionalGeneration
|
6 |
-
from configuration_florence2 import Florence2Config
|
7 |
import io
|
8 |
import matplotlib.pyplot as plt
|
9 |
import matplotlib.patches as patches
|
10 |
import numpy as np
|
11 |
import random
|
12 |
-
import copy
|
13 |
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
@@ -28,6 +26,9 @@ TASK_PROMPTS = {
|
|
28 |
"Region Proposal": "<REGION_PROPOSAL>"
|
29 |
}
|
30 |
|
|
|
|
|
|
|
31 |
colormap = ['blue','orange','green','purple','brown','pink','gray','olive','cyan','red',
|
32 |
'lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue']
|
33 |
|
@@ -78,11 +79,10 @@ def process_image(image, task):
|
|
78 |
|
79 |
return parsed_answer
|
80 |
|
81 |
-
|
82 |
def main_process(image, task):
|
83 |
result = process_image(image, task)
|
84 |
|
85 |
-
if task in
|
86 |
if task == "OCR with Region":
|
87 |
output_image = draw_ocr_bboxes(image.copy(), result[TASK_PROMPTS[task]])
|
88 |
else:
|
@@ -93,7 +93,7 @@ def main_process(image, task):
|
|
93 |
return None, gr.update(visible=False), str(result), gr.update(visible=True)
|
94 |
|
95 |
def reset_outputs():
|
96 |
-
return None, gr.update(visible=False), None, gr.update(visible=
|
97 |
|
98 |
with gr.Blocks(title="Florence-2 Demo") as iface:
|
99 |
gr.Markdown("# Florence-2 Demo")
|
@@ -101,17 +101,22 @@ with gr.Blocks(title="Florence-2 Demo") as iface:
|
|
101 |
|
102 |
with gr.Column():
|
103 |
image_input = gr.Image(type="pil", label="Input Image")
|
104 |
-
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task")
|
105 |
|
106 |
with gr.Row():
|
107 |
submit_button = gr.Button("Process")
|
108 |
reset_button = gr.Button("Reset")
|
109 |
|
110 |
output_image = gr.Image(label="Processed Image", visible=False)
|
111 |
-
output_text = gr.Textbox(label="Output", visible=
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
submit_button.click(
|
114 |
-
fn=
|
115 |
inputs=[image_input, task_dropdown],
|
116 |
outputs=[output_image, output_image, output_text, output_text]
|
117 |
)
|
@@ -121,5 +126,11 @@ with gr.Blocks(title="Florence-2 Demo") as iface:
|
|
121 |
inputs=[],
|
122 |
outputs=[output_image, output_image, output_text, output_text]
|
123 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
from transformers import AutoProcessor
|
5 |
from modeling_florence2 import Florence2ForConditionalGeneration
|
|
|
6 |
import io
|
7 |
import matplotlib.pyplot as plt
|
8 |
import matplotlib.patches as patches
|
9 |
import numpy as np
|
10 |
import random
|
|
|
11 |
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
26 |
"Region Proposal": "<REGION_PROPOSAL>"
|
27 |
}
|
28 |
|
29 |
+
IMAGE_TASKS = ["Object Detection", "Dense Region Caption", "Region Proposal", "OCR with Region"]
|
30 |
+
TEXT_TASKS = ["Caption", "Detailed Caption", "More Detailed Caption", "OCR"]
|
31 |
+
|
32 |
colormap = ['blue','orange','green','purple','brown','pink','gray','olive','cyan','red',
|
33 |
'lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue']
|
34 |
|
|
|
79 |
|
80 |
return parsed_answer
|
81 |
|
|
|
82 |
def main_process(image, task):
|
83 |
result = process_image(image, task)
|
84 |
|
85 |
+
if task in IMAGE_TASKS:
|
86 |
if task == "OCR with Region":
|
87 |
output_image = draw_ocr_bboxes(image.copy(), result[TASK_PROMPTS[task]])
|
88 |
else:
|
|
|
93 |
return None, gr.update(visible=False), str(result), gr.update(visible=True)
|
94 |
|
95 |
def reset_outputs():
|
96 |
+
return None, gr.update(visible=False), None, gr.update(visible=True)
|
97 |
|
98 |
with gr.Blocks(title="Florence-2 Demo") as iface:
|
99 |
gr.Markdown("# Florence-2 Demo")
|
|
|
101 |
|
102 |
with gr.Column():
|
103 |
image_input = gr.Image(type="pil", label="Input Image")
|
104 |
+
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task", value="Caption")
|
105 |
|
106 |
with gr.Row():
|
107 |
submit_button = gr.Button("Process")
|
108 |
reset_button = gr.Button("Reset")
|
109 |
|
110 |
output_image = gr.Image(label="Processed Image", visible=False)
|
111 |
+
output_text = gr.Textbox(label="Output", visible=True)
|
112 |
+
|
113 |
+
def process_and_update(image, task):
|
114 |
+
if image is None:
|
115 |
+
return None, gr.update(visible=False), "Please upload an image first.", gr.update(visible=True)
|
116 |
+
return main_process(image, task)
|
117 |
|
118 |
submit_button.click(
|
119 |
+
fn=process_and_update,
|
120 |
inputs=[image_input, task_dropdown],
|
121 |
outputs=[output_image, output_image, output_text, output_text]
|
122 |
)
|
|
|
126 |
inputs=[],
|
127 |
outputs=[output_image, output_image, output_text, output_text]
|
128 |
)
|
129 |
+
|
130 |
+
task_dropdown.change(
|
131 |
+
fn=lambda task: (gr.update(visible=task in IMAGE_TASKS), gr.update(visible=task in TEXT_TASKS)),
|
132 |
+
inputs=[task_dropdown],
|
133 |
+
outputs=[output_image, output_text]
|
134 |
+
)
|
135 |
|
136 |
iface.launch()
|