Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,67 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
from PIL import Image
|
4 |
-
from io import BytesIO
|
5 |
-
import torch
|
6 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
7 |
import os
|
|
|
8 |
|
9 |
-
# Load BLIP model for caption generation
|
10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
11 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
12 |
|
13 |
-
# Function to generate caption
|
14 |
def generate_caption(image):
|
|
|
15 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
16 |
out = model.generate(**inputs)
|
|
|
|
|
17 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
|
|
|
|
18 |
return caption
|
19 |
|
20 |
# Streamlit app
|
21 |
def main():
|
22 |
-
st.title("Image Caption Generator")
|
23 |
-
|
|
|
24 |
# Upload image
|
25 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
26 |
if uploaded_file is not None:
|
27 |
# Open the uploaded image
|
28 |
image = Image.open(uploaded_file)
|
29 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
30 |
|
31 |
-
#
|
32 |
-
|
|
|
|
|
|
|
33 |
caption = generate_caption(image)
|
34 |
-
st.write("Generated Caption: ", caption)
|
35 |
|
36 |
-
#
|
|
|
|
|
|
|
|
|
37 |
if st.button("Save Image with Caption"):
|
38 |
# Draw the caption on the image
|
39 |
draw = ImageDraw.Draw(image)
|
40 |
font = ImageFont.load_default()
|
41 |
draw.text((10, 10), caption, fill="white", font=font)
|
42 |
|
43 |
-
# Save the image
|
44 |
save_path = os.path.join("saved_images", "image_with_caption.jpg")
|
45 |
image.save(save_path)
|
46 |
|
47 |
-
# Provide download button
|
48 |
with open(save_path, "rb") as f:
|
49 |
st.download_button("Download Image with Caption", f, file_name="image_with_caption.jpg")
|
50 |
|
51 |
if __name__ == "__main__":
|
|
|
52 |
if not os.path.exists("saved_images"):
|
53 |
os.makedirs("saved_images")
|
|
|
54 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
4 |
import os
|
5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
6 |
|
7 |
+
# Load the BLIP model for creative caption generation
|
8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
|
11 |
+
# Function to generate a creative caption
|
12 |
def generate_caption(image):
|
13 |
+
# Prepare image for the model
|
14 |
inputs = processor(images=image, return_tensors="pt")
|
15 |
+
|
16 |
+
# Generate the caption
|
17 |
out = model.generate(**inputs)
|
18 |
+
|
19 |
+
# Decode the output to get a readable caption
|
20 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
# Return the creative caption
|
23 |
return caption
|
24 |
|
25 |
# Streamlit app
|
26 |
def main():
|
27 |
+
st.title("Creative Image Caption Generator")
|
28 |
+
st.write("Upload an image, and let the AI generate a creative and descriptive caption for it.")
|
29 |
+
|
30 |
# Upload image
|
31 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
32 |
if uploaded_file is not None:
|
33 |
# Open the uploaded image
|
34 |
image = Image.open(uploaded_file)
|
|
|
35 |
|
36 |
+
# Display the uploaded image
|
37 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
38 |
+
|
39 |
+
# Generate caption button
|
40 |
+
if st.button("Generate Creative Caption"):
|
41 |
caption = generate_caption(image)
|
|
|
42 |
|
43 |
+
# Display the generated caption
|
44 |
+
st.write("### Generated Creative Caption: ")
|
45 |
+
st.write(caption)
|
46 |
+
|
47 |
+
# Save image with caption overlay
|
48 |
if st.button("Save Image with Caption"):
|
49 |
# Draw the caption on the image
|
50 |
draw = ImageDraw.Draw(image)
|
51 |
font = ImageFont.load_default()
|
52 |
draw.text((10, 10), caption, fill="white", font=font)
|
53 |
|
54 |
+
# Save the image with the caption
|
55 |
save_path = os.path.join("saved_images", "image_with_caption.jpg")
|
56 |
image.save(save_path)
|
57 |
|
58 |
+
# Provide download button for the captioned image
|
59 |
with open(save_path, "rb") as f:
|
60 |
st.download_button("Download Image with Caption", f, file_name="image_with_caption.jpg")
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
# Create a directory to save the image
|
64 |
if not os.path.exists("saved_images"):
|
65 |
os.makedirs("saved_images")
|
66 |
+
|
67 |
main()
|