import streamlit as st from transformers import BlipProcessor, BlipForConditionalGeneration from PIL import Image # Cache model & processor to avoid reloading every time @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() # Streamlit App st.set_page_config(page_title="🖼️ Image Caption Generator", page_icon="🖼️", layout="centered") st.title("🖼️ Image Caption Generator") st.write("Upload an image and get a descriptive caption generated by AI.") uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file: image = Image.open(uploaded_file).convert('RGB') st.image(image, caption='Uploaded Image', use_column_width=True) if st.button("Generate Caption"): with st.spinner("Generating caption..."): inputs = processor(image, return_tensors="pt") out = model.generate(**inputs) caption = processor.decode(out[0], skip_special_tokens=True) st.subheader("📋 Generated Caption:") st.write(f"**{caption}**")