Yugesh-S/Image-Description-Generator-App
0
1import streamlit as st2from transformers import BlipProcessor, BlipForConditionalGeneration3from PIL import Image4import torch5 6# Initialize the BLIP model and processor7processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")8model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")9 10# Function to interact with the model and get the description11def get_image_description(image, prompt):12 inputs = processor(image, prompt, return_tensors="pt")13 out = model.generate(**inputs)14 description = processor.decode(out[0], skip_special_tokens=True)15 return description16 17# Streamlit UI18st.title("Image Description using Hugging Face Models")19 20# File uploader for image21uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])22 23# Text input for prompt24prompt = st.text_input("Enter your prompt", value="Describe this image")25 26if uploaded_image is not None:27 # Open and resize the uploaded image28 image = Image.open(uploaded_image)29 resized_image = image.resize((400, 400)) # Resize the image to 400x400 pixels30 31 # Display image and description side by side using columns32 col1, col2 = st.columns([2, 3])33 34 with col1:35 # Display the resized image on the left36 st.image(resized_image, caption="Uploaded Image", use_column_width=True)37 38 with col2:39 # Display the description on the right40 if st.button("Get Description"):41 description = get_image_description(image, prompt)42 st.info(description) # Display answer in an info box43 