File size: 2,509 Bytes
ac50f17
 
2d43de5
ac50f17
2d43de5
ac50f17
2d43de5
ac50f17
 
 
2d43de5
ac50f17
2d43de5
ac50f17
2d43de5
 
ac50f17
2d43de5
 
ac50f17
2d43de5
 
ac50f17
 
 
 
2d43de5
 
 
ac50f17
 
 
 
 
 
2d43de5
 
 
 
 
ac50f17
 
2d43de5
 
 
 
 
ac50f17
 
 
 
 
 
2d43de5
ac50f17
 
 
2d43de5
ac50f17
 
 
 
2d43de5
ac50f17
 
2d43de5
ac50f17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import requests
from PIL import Image, ImageDraw, ImageFont
import os
from transformers import BlipProcessor, BlipForConditionalGeneration

# Load the BLIP model for creative caption generation
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

# Function to generate a creative caption
def generate_caption(image):
    # Prepare image for the model
    inputs = processor(images=image, return_tensors="pt")
    
    # Generate the caption
    out = model.generate(**inputs)
    
    # Decode the output to get a readable caption
    caption = processor.decode(out[0], skip_special_tokens=True)
    
    # Return the creative caption
    return caption

# Streamlit app
def main():
    st.title("Creative Image Caption Generator")
    st.write("Upload an image, and let the AI generate a creative and descriptive caption for it.")
    
    # Upload image
    uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
    if uploaded_file is not None:
        # Open the uploaded image
        image = Image.open(uploaded_file)
        
        # Display the uploaded image
        st.image(image, caption="Uploaded Image", use_column_width=True)
        
        # Generate caption button
        if st.button("Generate Creative Caption"):
            caption = generate_caption(image)
            
            # Display the generated caption
            st.write("### Generated Creative Caption: ")
            st.write(caption)
            
            # Save image with caption overlay
            if st.button("Save Image with Caption"):
                # Draw the caption on the image
                draw = ImageDraw.Draw(image)
                font = ImageFont.load_default()
                draw.text((10, 10), caption, fill="white", font=font)
                
                # Save the image with the caption
                save_path = os.path.join("saved_images", "image_with_caption.jpg")
                image.save(save_path)

                # Provide download button for the captioned image
                with open(save_path, "rb") as f:
                    st.download_button("Download Image with Caption", f, file_name="image_with_caption.jpg")

if __name__ == "__main__":
    # Create a directory to save the image
    if not os.path.exists("saved_images"):
        os.makedirs("saved_images")
        
    main()