Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
import requests
|
| 3 |
from PIL import Image, UnidentifiedImageError
|
| 4 |
from transformers import AutoTokenizer, AutoImageProcessor, VisionEncoderDecoderModel
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
# Load the model, tokenizer, and image processor with error handling
|
| 8 |
def load_model_and_components(model_name):
|
|
@@ -27,32 +27,16 @@ def generate_caption(image):
|
|
| 27 |
except Exception:
|
| 28 |
return "Please upload a valid image."
|
| 29 |
|
| 30 |
-
# Predefined images for selection
|
| 31 |
-
predefined_images_urls = [
|
| 32 |
-
"http://images.cocodataset.org/val2014/COCO_val2014_000000458153.jpg",
|
| 33 |
-
"http://images.cocodataset.org/val2014/COCO_val2014_000000000074.jpg"
|
| 34 |
-
]
|
| 35 |
-
|
| 36 |
# Predefined images for selection
|
| 37 |
image_folder = "images"
|
| 38 |
predefined_images_paths = [
|
| 39 |
os.path.join(image_folder, fname) for fname in os.listdir(image_folder) if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))
|
| 40 |
]
|
| 41 |
|
| 42 |
-
# Preload PIL.Image objects for predefined images with error handling
|
| 43 |
-
predefined_images = []
|
| 44 |
-
for path in predefined_images_paths:
|
| 45 |
-
try:
|
| 46 |
-
predefined_images.append(Image.open(path))
|
| 47 |
-
except Exception as e:
|
| 48 |
-
print(f"Error loading predefined image from {path}: {e}")
|
| 49 |
-
|
| 50 |
# Gradio app
|
| 51 |
-
def app(image=None
|
| 52 |
try:
|
| 53 |
-
if
|
| 54 |
-
image = predefined_image
|
| 55 |
-
elif image is None:
|
| 56 |
return "Please upload a valid image."
|
| 57 |
return generate_caption(image)
|
| 58 |
except Exception:
|
|
@@ -60,11 +44,6 @@ def app(image=None, predefined_image=None):
|
|
| 60 |
|
| 61 |
# Define UI
|
| 62 |
with gr.Blocks() as interface:
|
| 63 |
-
gr.Markdown("""
|
| 64 |
-
# Welcome to the LAICSI-IFES space for Vision Encoder-Decoder (VED) demonstration
|
| 65 |
-
|
| 66 |
-
### Be patient with the Swin-GPorTuguese-2 as it is heavier than the Swin-DistilBERTimbau.
|
| 67 |
-
""")
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column():
|
| 70 |
model_selector = gr.Dropdown(choices=["laicsiifes/swin-distilbertimbau", "laicsiifes/swin-gportuguese-2"],
|
|
@@ -73,7 +52,7 @@ with gr.Blocks() as interface:
|
|
| 73 |
loading_message = gr.Textbox(label="Status Message")
|
| 74 |
image_display = gr.Image(type="pil", label="Image Preview", interactive=False)
|
| 75 |
upload_button = gr.File(label="Upload an Image", file_types=["image"], type="filepath")
|
| 76 |
-
|
| 77 |
|
| 78 |
with gr.Column():
|
| 79 |
output_text = gr.Textbox(label="Generated Caption")
|
|
@@ -88,15 +67,6 @@ with gr.Blocks() as interface:
|
|
| 88 |
except Exception:
|
| 89 |
return None, "Please upload a valid image."
|
| 90 |
|
| 91 |
-
def handle_predefined_image(evt: gr.SelectData, _):
|
| 92 |
-
try:
|
| 93 |
-
if not evt:
|
| 94 |
-
return None, "Please upload a valid image."
|
| 95 |
-
pil_image = Image.open(requests.get(evt.value['image']['url'], stream=True).raw)
|
| 96 |
-
return pil_image, generate_caption(pil_image)
|
| 97 |
-
except Exception:
|
| 98 |
-
return None, "Please upload a valid image."
|
| 99 |
-
|
| 100 |
def switch_model(selected_model):
|
| 101 |
gr.Info("Loading model... Please wait.")
|
| 102 |
return "Loading model... Please wait.", None, None, None
|
|
@@ -107,8 +77,7 @@ with gr.Blocks() as interface:
|
|
| 107 |
return "Model loaded successfully.", None, None, None
|
| 108 |
|
| 109 |
model_selector.change(fn=switch_model, inputs=model_selector, outputs=[loading_message, upload_button, image_display, output_text])
|
| 110 |
-
model_selector.change(fn=load_new_model, inputs=model_selector, outputs=[loading_message,
|
| 111 |
upload_button.change(fn=handle_uploaded_image, inputs=upload_button, outputs=[image_display, output_text])
|
| 112 |
-
predefined_images_display.select(fn=handle_predefined_image, inputs=predefined_images_display, outputs=[image_display, output_text])
|
| 113 |
|
| 114 |
-
interface.launch()
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from PIL import Image, UnidentifiedImageError
|
| 3 |
from transformers import AutoTokenizer, AutoImageProcessor, VisionEncoderDecoderModel
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# Load the model, tokenizer, and image processor with error handling
|
| 8 |
def load_model_and_components(model_name):
|
|
|
|
| 27 |
except Exception:
|
| 28 |
return "Please upload a valid image."
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Predefined images for selection
|
| 31 |
image_folder = "images"
|
| 32 |
predefined_images_paths = [
|
| 33 |
os.path.join(image_folder, fname) for fname in os.listdir(image_folder) if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))
|
| 34 |
]
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Gradio app
|
| 37 |
+
def app(image=None):
|
| 38 |
try:
|
| 39 |
+
if image is None:
|
|
|
|
|
|
|
| 40 |
return "Please upload a valid image."
|
| 41 |
return generate_caption(image)
|
| 42 |
except Exception:
|
|
|
|
| 44 |
|
| 45 |
# Define UI
|
| 46 |
with gr.Blocks() as interface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Row():
|
| 48 |
with gr.Column():
|
| 49 |
model_selector = gr.Dropdown(choices=["laicsiifes/swin-distilbertimbau", "laicsiifes/swin-gportuguese-2"],
|
|
|
|
| 52 |
loading_message = gr.Textbox(label="Status Message")
|
| 53 |
image_display = gr.Image(type="pil", label="Image Preview", interactive=False)
|
| 54 |
upload_button = gr.File(label="Upload an Image", file_types=["image"], type="filepath")
|
| 55 |
+
examples = gr.Examples(predefined_images_paths, inputs=[upload_button], label="Examples")
|
| 56 |
|
| 57 |
with gr.Column():
|
| 58 |
output_text = gr.Textbox(label="Generated Caption")
|
|
|
|
| 67 |
except Exception:
|
| 68 |
return None, "Please upload a valid image."
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def switch_model(selected_model):
|
| 71 |
gr.Info("Loading model... Please wait.")
|
| 72 |
return "Loading model... Please wait.", None, None, None
|
|
|
|
| 77 |
return "Model loaded successfully.", None, None, None
|
| 78 |
|
| 79 |
model_selector.change(fn=switch_model, inputs=model_selector, outputs=[loading_message, upload_button, image_display, output_text])
|
| 80 |
+
model_selector.change(fn=load_new_model, inputs=model_selector, outputs=[loading_message, image_display, output_text])
|
| 81 |
upload_button.change(fn=handle_uploaded_image, inputs=upload_button, outputs=[image_display, output_text])
|
|
|
|
| 82 |
|
| 83 |
+
interface.launch(share=False)
|