amnakhan1122 commited on
Commit
841f21f
·
verified ·
1 Parent(s): 12ec9fc

Update image_caption.py

Browse files
Files changed (1) hide show
  1. image_caption.py +25 -0
image_caption.py CHANGED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ from transformers import BlipProcessor, BlipForConditionalGeneration
4
+ from PIL import Image
5
+ import torch
6
+
7
+ st.title("AI Image Caption Generator")
8
+ st.write("Upload an image and get a caption generated by an AI model!")
9
+
10
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
11
+
12
+ if uploaded_file:
13
+ image = Image.open(uploaded_file).convert('RGB')
14
+ st.image(image, caption="Uploaded Image", use_column_width=True)
15
+
16
+ st.write("Generating caption...")
17
+
18
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
19
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
20
+
21
+ inputs = processor(image, return_tensors="pt")
22
+ out = model.generate(**inputs)
23
+ caption = processor.decode(out[0], skip_special_tokens=True)
24
+
25
+ st.success(f"📝 Caption: {caption}")