Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Author: Rajanikanta Mohapatra
|
| 3 |
+
Date: 01-04-2024
|
| 4 |
+
|
| 5 |
+
Description: This Streamlit app generates captions for uploaded images using a finetuned BLIP model.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
# Import necessary libraries
|
| 9 |
+
import streamlit as st
|
| 10 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
# Set custom web page title and icon
|
| 15 |
+
st.set_page_config(page_title="Caption Generator App", page_icon="📷")
|
| 16 |
+
|
| 17 |
+
# Create a folder to save the model if it doesn't exist
|
| 18 |
+
saved_folder_path = "saved_model"
|
| 19 |
+
if not os.path.exists(saved_folder_path):
|
| 20 |
+
os.mkdir(saved_folder_path)
|
| 21 |
+
|
| 22 |
+
# Load processor and model
|
| 23 |
+
processor = AutoProcessor.from_pretrained(saved_folder_path)
|
| 24 |
+
model = BlipForConditionalGeneration.from_pretrained(saved_folder_path)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Function to generate caption for the provided image
|
| 28 |
+
def generate_caption(image_path, target_size=(224, 224)):
|
| 29 |
+
"""
|
| 30 |
+
Generates a caption for the provided image.
|
| 31 |
+
|
| 32 |
+
Parameters:
|
| 33 |
+
- image_path (str): Path to the image file.
|
| 34 |
+
- target_size (tuple): Desired size for the image (default is (224, 224)).
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
- generated_caption (str): Generated caption for the image.
|
| 38 |
+
"""
|
| 39 |
+
# Process the image
|
| 40 |
+
image = Image.open(image_path)
|
| 41 |
+
image = image.resize(target_size)
|
| 42 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 43 |
+
pixel_values = inputs.pixel_values
|
| 44 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
| 45 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 46 |
+
return generated_caption
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Streamlit app
|
| 50 |
+
st.title("Image Caption Generator Using BLIP")
|
| 51 |
+
st.markdown(
|
| 52 |
+
"Upload an image, and this app will generate a caption for it using a finetuned BLIP model."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Upload image
|
| 56 |
+
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 57 |
+
|
| 58 |
+
# Process uploaded image and generate caption
|
| 59 |
+
if uploaded_image is not None:
|
| 60 |
+
st.subheader("Uploaded Image")
|
| 61 |
+
st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)
|
| 62 |
+
st.subheader("Generated Caption")
|
| 63 |
+
try:
|
| 64 |
+
generated_caption = generate_caption(uploaded_image)
|
| 65 |
+
st.write("Generated Caption:", generated_caption)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
st.error(f"Error occurred: {e}")
|
| 68 |
+
|
| 69 |
+
|