Update app.py
Browse files
app.py
CHANGED
@@ -2,17 +2,8 @@ import gradio as gr
|
|
2 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
3 |
import spaces
|
4 |
|
5 |
-
import requests
|
6 |
-
import copy
|
7 |
-
|
8 |
-
from PIL import Image, ImageDraw, ImageFont
|
9 |
import io
|
10 |
-
|
11 |
-
import matplotlib.patches as patches
|
12 |
-
|
13 |
-
import random
|
14 |
-
import numpy as np
|
15 |
-
|
16 |
import subprocess
|
17 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
18 |
|
@@ -26,18 +17,8 @@ processors = {
|
|
26 |
'J-LAB/Florence_2_L_FluxiAI_Product_Caption': AutoProcessor.from_pretrained('J-LAB/Florence_2_L_FluxiAI_Product_Caption', trust_remote_code=True)
|
27 |
}
|
28 |
|
29 |
-
|
30 |
DESCRIPTION = "# [Florence-2 Product Describe by Fluxi IA](https://huggingface.co/microsoft/Florence-2-large)"
|
31 |
|
32 |
-
colormap = ['blue','orange','green','purple','brown','pink','gray','olive','cyan','red',
|
33 |
-
'lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue']
|
34 |
-
|
35 |
-
def fig_to_pil(fig):
|
36 |
-
buf = io.BytesIO()
|
37 |
-
fig.savefig(buf, format='png')
|
38 |
-
buf.seek(0)
|
39 |
-
return Image.open(buf)
|
40 |
-
|
41 |
@spaces.GPU
|
42 |
def process_image(image, task_prompt, text_input=None, model_id='J-LAB/Florence_2_B_FluxiAI_Product_Caption'):
|
43 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
@@ -59,64 +40,7 @@ def process_image(image, task_prompt, text_input=None, model_id='J-LAB/Florence_
|
|
59 |
# Convert newline characters to HTML line breaks
|
60 |
output_text = output_text.replace("\n\n", "<br><br>").replace("\n", "<br>")
|
61 |
|
62 |
-
return output_text
|
63 |
-
|
64 |
-
def plot_bbox(image, data):
|
65 |
-
fig, ax = plt.subplots()
|
66 |
-
ax.imshow(image)
|
67 |
-
for bbox, label in zip(data['bboxes'], data['labels']):
|
68 |
-
x1, y1, x2, y2 = bbox
|
69 |
-
rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none')
|
70 |
-
ax.add_patch(rect)
|
71 |
-
plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
72 |
-
ax.axis('off')
|
73 |
-
return fig
|
74 |
-
|
75 |
-
def draw_polygons(image, prediction, fill_mask=False):
|
76 |
-
|
77 |
-
draw = ImageDraw.Draw(image)
|
78 |
-
scale = 1
|
79 |
-
for polygons, label in zip(prediction['polygons'], prediction['labels']):
|
80 |
-
color = random.choice(colormap)
|
81 |
-
fill_color = random.choice(colormap) if fill_mask else None
|
82 |
-
for _polygon in polygons:
|
83 |
-
_polygon = np.array(_polygon).reshape(-1, 2)
|
84 |
-
if len(_polygon) < 3:
|
85 |
-
print('Invalid polygon:', _polygon)
|
86 |
-
continue
|
87 |
-
_polygon = (_polygon * scale).reshape(-1).tolist()
|
88 |
-
if fill_mask:
|
89 |
-
draw.polygon(_polygon, outline=color, fill=fill_color)
|
90 |
-
else:
|
91 |
-
draw.polygon(_polygon, outline=color)
|
92 |
-
draw.text((_polygon[0] + 8, _polygon[1] + 2), label, fill=color)
|
93 |
-
return image
|
94 |
-
|
95 |
-
def convert_to_od_format(data):
|
96 |
-
bboxes = data.get('bboxes', [])
|
97 |
-
labels = data.get('bboxes_labels', [])
|
98 |
-
od_results = {
|
99 |
-
'bboxes': bboxes,
|
100 |
-
'labels': labels
|
101 |
-
}
|
102 |
-
return od_results
|
103 |
-
|
104 |
-
def draw_ocr_bboxes(image, prediction):
|
105 |
-
scale = 1
|
106 |
-
draw = ImageDraw.Draw(image)
|
107 |
-
bboxes, labels = prediction['quad_boxes'], prediction['labels']
|
108 |
-
for box, label in zip(bboxes, labels):
|
109 |
-
color = random.choice(colormap)
|
110 |
-
new_box = (np.array(box) * scale).tolist()
|
111 |
-
draw.polygon(new_box, width=3, outline=color)
|
112 |
-
draw.text((new_box[0]+8, new_box[1]+2),
|
113 |
-
"{}".format(label),
|
114 |
-
align="right",
|
115 |
-
fill=color)
|
116 |
-
return image
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
css = """
|
122 |
#output {
|
@@ -126,13 +50,10 @@ css = """
|
|
126 |
}
|
127 |
"""
|
128 |
|
129 |
-
|
130 |
-
single_task_list =[
|
131 |
'Product Caption', 'More Detailed Caption'
|
132 |
]
|
133 |
|
134 |
-
|
135 |
-
|
136 |
with gr.Blocks(css=css) as demo:
|
137 |
gr.Markdown(DESCRIPTION)
|
138 |
with gr.Tab(label="Florence-2 Image Captioning"):
|
@@ -146,8 +67,7 @@ with gr.Blocks(css=css) as demo:
|
|
146 |
submit_btn = gr.Button(value="Submit")
|
147 |
with gr.Column():
|
148 |
output_text = gr.HTML(label="Output Text")
|
149 |
-
output_img = gr.Image(label="Output Image")
|
150 |
|
151 |
-
submit_btn.click(process_image, [input_img, task_prompt, text_input, model_selector], [output_text
|
152 |
|
153 |
demo.launch(debug=True)
|
|
|
2 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
3 |
import spaces
|
4 |
|
|
|
|
|
|
|
|
|
5 |
import io
|
6 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
7 |
import subprocess
|
8 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
9 |
|
|
|
17 |
'J-LAB/Florence_2_L_FluxiAI_Product_Caption': AutoProcessor.from_pretrained('J-LAB/Florence_2_L_FluxiAI_Product_Caption', trust_remote_code=True)
|
18 |
}
|
19 |
|
|
|
20 |
DESCRIPTION = "# [Florence-2 Product Describe by Fluxi IA](https://huggingface.co/microsoft/Florence-2-large)"
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
@spaces.GPU
|
23 |
def process_image(image, task_prompt, text_input=None, model_id='J-LAB/Florence_2_B_FluxiAI_Product_Caption'):
|
24 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
|
|
40 |
# Convert newline characters to HTML line breaks
|
41 |
output_text = output_text.replace("\n\n", "<br><br>").replace("\n", "<br>")
|
42 |
|
43 |
+
return output_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
css = """
|
46 |
#output {
|
|
|
50 |
}
|
51 |
"""
|
52 |
|
53 |
+
single_task_list = [
|
|
|
54 |
'Product Caption', 'More Detailed Caption'
|
55 |
]
|
56 |
|
|
|
|
|
57 |
with gr.Blocks(css=css) as demo:
|
58 |
gr.Markdown(DESCRIPTION)
|
59 |
with gr.Tab(label="Florence-2 Image Captioning"):
|
|
|
67 |
submit_btn = gr.Button(value="Submit")
|
68 |
with gr.Column():
|
69 |
output_text = gr.HTML(label="Output Text")
|
|
|
70 |
|
71 |
+
submit_btn.click(process_image, [input_img, task_prompt, text_input, model_selector], [output_text])
|
72 |
|
73 |
demo.launch(debug=True)
|