CoolFace
Apppublic

mohAhmad/Temporary

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py38 linesDownload Raw Back to root
1import streamlit as st2from transformers import BlipProcessor, BlipForConditionalGeneration3from PIL import Image4import torch5 6# Title and description7st.title("Image Captioning App")8st.write("This app converts an uploaded image into a text description using the BLIP model.")9 10# Load model and processor11@st.cache_resource12def load_model():13    processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")14    model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")15    return processor, model16 17processor, model = load_model()18 19# Upload image20uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])21 22if uploaded_file is not None:23    image = Image.open(uploaded_file).convert("RGB")24    st.image(image, caption="Uploaded Image", use_column_width=True)25 26    # Preprocess the image27    inputs = processor(image, return_tensors="pt")28 29    # Generate the caption (inference)30    generated_ids = model.generate(**inputs)31 32    # Decode the generated caption33    generated_text = processor.decode(generated_ids[0], skip_special_tokens=True)34 35    # Display the generated caption36    st.write("Generated Caption:")37    st.success(generated_text)38