Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the processor and model
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_model():
|
| 9 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 10 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 11 |
+
return processor, model
|
| 12 |
+
|
| 13 |
+
# Function to generate captions
|
| 14 |
+
def generate_caption(image,max_new_tokens=20):
|
| 15 |
+
processor, model = load_model()
|
| 16 |
+
inputs = processor(image, return_tensors="pt")
|
| 17 |
+
out = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 18 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
| 19 |
+
|
| 20 |
+
# Streamlit UI
|
| 21 |
+
st.title("Image Captioning with BLIP")
|
| 22 |
+
|
| 23 |
+
# Upload image
|
| 24 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 25 |
+
|
| 26 |
+
if uploaded_file is not None:
|
| 27 |
+
# Display the uploaded image with size 400x400
|
| 28 |
+
image = Image.open(uploaded_file).convert('RGB')
|
| 29 |
+
resized_image = image.resize((400, 400))
|
| 30 |
+
st.image(resized_image, caption="Uploaded Image", use_column_width=False)
|
| 31 |
+
|
| 32 |
+
# Generate caption
|
| 33 |
+
if st.button("Generate Caption"):
|
| 34 |
+
caption = generate_caption(image)
|
| 35 |
+
st.write(f"**Caption:** {caption}")
|