File size: 1,225 Bytes
6799494
4824b47
6799494
 
 
4824b47
 
 
6799494
4824b47
6799494
 
4824b47
 
6799494
 
 
 
4824b47
6799494
 
 
 
 
 
139e2a4
4824b47
6799494
4824b47
 
139e2a4
 
4824b47
6799494
139e2a4
4824b47
6799494
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import torch

# Title and description
st.title("Image Captioning App")
st.write("This app converts an uploaded image into a text description using the BLIP model.")

# Load model and processor
@st.cache_resource
def load_model():
    processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
    model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
    return processor, model

processor, model = load_model()

# Upload image
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert("RGB")
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Preprocess the image
    inputs = processor(image, return_tensors="pt")

    # Generate the caption (inference)
    generated_ids = model.generate(**inputs)

    # Decode the generated caption
    generated_text = processor.decode(generated_ids[0], skip_special_tokens=True)

    # Display the generated caption
    st.write("Generated Caption:")
    st.success(generated_text)