Update app.py
Browse files
app.py
CHANGED
@@ -34,24 +34,30 @@ def generate_caption(image, model_name):
|
|
34 |
|
35 |
# Predefined images for selection
|
36 |
image_folder = "images"
|
37 |
-
|
38 |
-
os.path.join(image_folder, fname)
|
|
|
|
|
39 |
]
|
40 |
|
41 |
-
#
|
42 |
-
def
|
43 |
if image is None:
|
44 |
-
return
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Define UI
|
48 |
with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange")) as interface:
|
49 |
gr.Markdown("""
|
50 |
# Welcome to the LAICSI-IFES space for Vision Encoder-Decoder (VED) demonstration
|
51 |
-
|
52 |
---
|
53 |
-
|
54 |
-
### Be patient with the Swin-GPorTuguese-2 as it is heavier than the Swin-DistilBERTimbau.
|
55 |
""")
|
56 |
with gr.Row(variant='panel'):
|
57 |
with gr.Column():
|
@@ -61,12 +67,14 @@ with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange"
|
|
61 |
label="Select Model"
|
62 |
)
|
63 |
|
64 |
-
gr.Markdown("""
|
|
|
|
|
|
|
65 |
|
66 |
with gr.Row(variant='panel'):
|
67 |
with gr.Column():
|
68 |
-
image_display = gr.Image(type="pil", label="Image Preview",
|
69 |
-
upload_button = gr.File(label="Upload an Image", file_types=["image"], type="filepath", height=100)
|
70 |
with gr.Column():
|
71 |
output_text = gr.Textbox(label="Generated Caption")
|
72 |
generate_button = gr.Button("Generate")
|
@@ -74,22 +82,20 @@ with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange"
|
|
74 |
gr.Markdown("""---""")
|
75 |
|
76 |
with gr.Row(variant='panel'):
|
77 |
-
examples = gr.Examples(
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
# Define
|
80 |
-
|
81 |
-
if image is None:
|
82 |
-
return None
|
83 |
-
pil_image = Image.open(image).convert("RGB")
|
84 |
-
return pil_image
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
return "Please upload an image to generate a caption."
|
89 |
-
return generate_caption(image, selected_model)
|
90 |
|
91 |
-
model_selector.change(fn=lambda _: (None, None, None), inputs=[model_selector], outputs=[image_display, upload_button, output_text])
|
92 |
-
upload_button.change(fn=handle_uploaded_image, inputs=upload_button, outputs=image_display)
|
93 |
generate_button.click(fn=handle_generate_button, inputs=[image_display, model_selector], outputs=output_text)
|
94 |
|
95 |
-
interface.launch(share=False)
|
|
|
34 |
|
35 |
# Predefined images for selection
|
36 |
image_folder = "images"
|
37 |
+
predefined_images = [
|
38 |
+
Image.open(os.path.join(image_folder, fname)).convert("RGB")
|
39 |
+
for fname in os.listdir(image_folder) \
|
40 |
+
if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.ppm'))
|
41 |
]
|
42 |
|
43 |
+
# Define components logic
|
44 |
+
def handle_uploaded_image(image):
|
45 |
if image is None:
|
46 |
+
return None
|
47 |
+
pil_image = image.convert("RGB")
|
48 |
+
return pil_image, None
|
49 |
+
|
50 |
+
def handle_generate_button(image, selected_model):
|
51 |
+
if image is None:
|
52 |
+
return "Please upload an image to generate a caption."
|
53 |
+
return generate_caption(image, selected_model)
|
54 |
|
55 |
# Define UI
|
56 |
with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange")) as interface:
|
57 |
gr.Markdown("""
|
58 |
# Welcome to the LAICSI-IFES space for Vision Encoder-Decoder (VED) demonstration
|
|
|
59 |
---
|
60 |
+
### Select an available model: Swin-DistilBERTimbau (168M) or Swin-GPorTuguese-2 (240M)
|
|
|
61 |
""")
|
62 |
with gr.Row(variant='panel'):
|
63 |
with gr.Column():
|
|
|
67 |
label="Select Model"
|
68 |
)
|
69 |
|
70 |
+
gr.Markdown("""
|
71 |
+
---
|
72 |
+
### Upload image or example images below, and click `Generate`
|
73 |
+
""")
|
74 |
|
75 |
with gr.Row(variant='panel'):
|
76 |
with gr.Column():
|
77 |
+
image_display = gr.Image(type="pil", label="Image Preview", image_mode="RGB", height=400)
|
|
|
78 |
with gr.Column():
|
79 |
output_text = gr.Textbox(label="Generated Caption")
|
80 |
generate_button = gr.Button("Generate")
|
|
|
82 |
gr.Markdown("""---""")
|
83 |
|
84 |
with gr.Row(variant='panel'):
|
85 |
+
examples = gr.Examples(
|
86 |
+
examples=predefined_images,
|
87 |
+
fn=handle_uploaded_image,
|
88 |
+
inputs=[image_display],
|
89 |
+
outputs=[image_display, output_text],
|
90 |
+
label="Examples"
|
91 |
+
)
|
92 |
|
93 |
+
# Define actions
|
94 |
+
model_selector.change(fn=lambda: (None, None), outputs=[image_display, output_text])
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
image_display.upload(fn=handle_uploaded_image, inputs=[image_display], outputs=[image_display, output_text])
|
97 |
+
image_display.clear(fn=lambda: None, outputs=[output_text])
|
|
|
|
|
98 |
|
|
|
|
|
99 |
generate_button.click(fn=handle_generate_button, inputs=[image_display, model_selector], outputs=output_text)
|
100 |
|
101 |
+
interface.launch(share=False)
|