Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
def load_model():
|
8 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
+
return processor, model
|
11 |
+
|
12 |
+
processor, model = load_model()
|
13 |
+
|
14 |
+
st.title("Image Captioning with BLIP")
|
15 |
+
|
16 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
17 |
+
text_prompt = st.text_input("Enter a prompt for conditional captioning", "here...")
|
18 |
+
|
19 |
+
if uploaded_file is not None:
|
20 |
+
image = Image.open(uploaded_file).convert("RGB")
|
21 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
22 |
+
|
23 |
+
# Conditional captioning
|
24 |
+
inputs = processor(image, text_prompt, return_tensors="pt")
|
25 |
+
out = model.generate(**inputs)
|
26 |
+
conditional_caption = processor.decode(out[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Unconditional captioning
|
29 |
+
inputs = processor(image, return_tensors="pt")
|
30 |
+
out = model.generate(**inputs)
|
31 |
+
unconditional_caption = processor.decode(out[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
st.subheader("Generated Captions")
|
34 |
+
st.write(f"**Conditional Caption:** {conditional_caption}")
|
35 |
+
st.write(f"**Unconditional Caption:** {unconditional_caption}")
|