rajanikanta2020's picture
Update app.py
0d33338 verified
raw
history blame
2.24 kB
"""
Author: Rajanikanta Mohapatra
Date: 01-04-2024
Description: This Streamlit app generates captions for uploaded images using a finetuned BLIP model.
"""
# Import necessary libraries
import streamlit as st
from transformers import AutoProcessor, BlipForConditionalGeneration
from PIL import Image
import os
# Set custom web page title and icon
st.set_page_config(page_title="Caption Generator App", page_icon="📷")
# Create a folder to save the model if it doesn't exist
saved_folder_path = "saved_model"
if not os.path.exists(saved_folder_path):
os.mkdir(saved_folder_path)
# Load processor and model
processor = AutoProcessor.from_pretrained(saved_folder_path)
model = BlipForConditionalGeneration.from_pretrained(saved_folder_path)
# Function to generate caption for the provided image
def generate_caption(image_path, target_size=(224, 224)):
"""
Generates a caption for the provided image.
Parameters:
- image_path (str): Path to the image file.
- target_size (tuple): Desired size for the image (default is (224, 224)).
Returns:
- generated_caption (str): Generated caption for the image.
"""
# Process the image
image = Image.open(image_path)
image = image.resize(target_size)
inputs = processor(images=image, return_tensors="pt")
pixel_values = inputs.pixel_values
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return generated_caption
# Streamlit app
st.title("Image Caption Generator Using BLIP")
st.markdown(
"Upload an image, and this app will generate a caption for it using a finetuned BLIP model."
)
# Upload image
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
# Process uploaded image and generate caption
if uploaded_image is not None:
st.subheader("Uploaded Image")
st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)
st.subheader("Generated Caption")
try:
generated_caption = generate_caption(uploaded_image)
st.write("Generated Caption:", generated_caption)
except Exception as e:
st.error(f"Error occurred: {e}")