Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from PIL import Image, UnidentifiedImageError
|
| 3 |
+
from transformers import AutoTokenizer, AutoImageProcessor, VisionEncoderDecoderModel
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load the model, tokenizer, and image processor with error handling
|
| 7 |
+
def load_model_and_components(model_name):
|
| 8 |
+
try:
|
| 9 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
image_processor = AutoImageProcessor.from_pretrained(model_name)
|
| 12 |
+
return model, tokenizer, image_processor
|
| 13 |
+
except Exception as e:
|
| 14 |
+
raise RuntimeError(f"Error loading model components: {e}")
|
| 15 |
+
|
| 16 |
+
current_model_name = "laicsiifes/swin-distilbertimbau"
|
| 17 |
+
model, tokenizer, image_processor = load_model_and_components(current_model_name)
|
| 18 |
+
|
| 19 |
+
# Function to process the image and generate a caption
|
| 20 |
+
def generate_caption(image):
|
| 21 |
+
try:
|
| 22 |
+
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
| 23 |
+
generated_ids = model.generate(pixel_values)
|
| 24 |
+
caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 25 |
+
return caption
|
| 26 |
+
except Exception:
|
| 27 |
+
return "Please upload a valid image."
|
| 28 |
+
|
| 29 |
+
# Predefined images for selection
|
| 30 |
+
predefined_images_urls = [
|
| 31 |
+
"http://images.cocodataset.org/val2014/COCO_val2014_000000458153.jpg",
|
| 32 |
+
"http://images.cocodataset.org/val2014/COCO_val2014_000000000074.jpg"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
# Preload PIL.Image objects for predefined images with error handling
|
| 36 |
+
predefined_images = []
|
| 37 |
+
for url in predefined_images_urls:
|
| 38 |
+
try:
|
| 39 |
+
predefined_images.append(Image.open(requests.get(url, stream=True).raw))
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error loading predefined image from {url}: {e}")
|
| 42 |
+
|
| 43 |
+
# Gradio app
|
| 44 |
+
def app(image=None, predefined_image=None):
|
| 45 |
+
try:
|
| 46 |
+
if predefined_image is not None:
|
| 47 |
+
image = predefined_image
|
| 48 |
+
elif image is None:
|
| 49 |
+
return "Please upload a valid image."
|
| 50 |
+
return generate_caption(image)
|
| 51 |
+
except Exception:
|
| 52 |
+
return "Please upload a valid image."
|
| 53 |
+
|
| 54 |
+
# Define UI
|
| 55 |
+
with gr.Blocks() as interface:
|
| 56 |
+
gr.Markdown("""
|
| 57 |
+
# Welcome to the LAICSI-IFES space for Vision Encoder-Decoder (VED) demonstration
|
| 58 |
+
|
| 59 |
+
### Be patient with the Swin-GPorTuguese-2 as it is heavier than the Swin-DistilBERTimbau.
|
| 60 |
+
""")
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column():
|
| 63 |
+
model_selector = gr.Dropdown(choices=["laicsiifes/swin-distilbertimbau", "laicsiifes/swin-gportuguese-2"],
|
| 64 |
+
value="laicsiifes/swin-distilbertimbau",
|
| 65 |
+
label="Select Model")
|
| 66 |
+
loading_message = gr.Textbox(label="Status Message")
|
| 67 |
+
image_display = gr.Image(type="pil", label="Image Preview", interactive=False)
|
| 68 |
+
upload_button = gr.File(label="Upload an Image", file_types=["image"], type="filepath")
|
| 69 |
+
predefined_images_display = gr.Gallery(predefined_images_urls, label="Choose a Predefined Image")
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
output_text = gr.Textbox(label="Generated Caption")
|
| 73 |
+
|
| 74 |
+
# Define logic
|
| 75 |
+
def handle_uploaded_image(image):
|
| 76 |
+
try:
|
| 77 |
+
if image is None:
|
| 78 |
+
return None, "Please upload a valid image."
|
| 79 |
+
pil_image = Image.open(image)
|
| 80 |
+
return pil_image, generate_caption(pil_image)
|
| 81 |
+
except Exception:
|
| 82 |
+
return None, "Please upload a valid image."
|
| 83 |
+
|
| 84 |
+
def handle_predefined_image(evt: gr.SelectData, _):
|
| 85 |
+
try:
|
| 86 |
+
if not evt:
|
| 87 |
+
return None, "Please upload a valid image."
|
| 88 |
+
pil_image = Image.open(requests.get(evt.value['image']['url'], stream=True).raw)
|
| 89 |
+
return pil_image, generate_caption(pil_image)
|
| 90 |
+
except Exception:
|
| 91 |
+
return None, "Please upload a valid image."
|
| 92 |
+
|
| 93 |
+
def switch_model(selected_model):
|
| 94 |
+
gr.Info("Loading model... Please wait.")
|
| 95 |
+
return "Loading model... Please wait.", None, None, None
|
| 96 |
+
|
| 97 |
+
def load_new_model(selected_model):
|
| 98 |
+
global model, tokenizer, image_processor
|
| 99 |
+
model, tokenizer, image_processor = load_model_and_components(selected_model)
|
| 100 |
+
return "Model loaded successfully.", None, None, None
|
| 101 |
+
|
| 102 |
+
model_selector.change(fn=switch_model, inputs=model_selector, outputs=[loading_message, upload_button, image_display, output_text])
|
| 103 |
+
model_selector.change(fn=load_new_model, inputs=model_selector, outputs=[loading_message, upload_button, image_display, output_text])
|
| 104 |
+
upload_button.change(fn=handle_uploaded_image, inputs=upload_button, outputs=[image_display, output_text])
|
| 105 |
+
predefined_images_display.select(fn=handle_predefined_image, inputs=predefined_images_display, outputs=[image_display, output_text])
|
| 106 |
+
|
| 107 |
+
interface.launch()
|