File size: 4,316 Bytes
d8b7142
db3cf05
c632a11
db3cf05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c632a11
db3cf05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c632a11
 
db3cf05
 
 
c632a11
db3cf05
c632a11
 
 
db3cf05
 
 
 
c632a11
 
 
 
 
db3cf05
 
 
 
c632a11
 
db3cf05
 
 
 
c632a11
 
 
 
 
db3cf05
c632a11
 
db3cf05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c632a11
db3cf05
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import streamlit as st
from PIL import Image
from torchvision import transforms
from transformers import AutoProcessor, BlipForConditionalGeneration
import google.generativeai as genai

# Define model

# Initialize the image to caption model - BLIP
blip_processor = AutoProcessor.\
    from_pretrained("Salesforce/blip-image-captioning-base")
blip_model = BlipForConditionalGeneration.\
    from_pretrained("Salesforce/blip-image-captioning-base")

# Initialize the caption to instagram post model - gemini
GOOGLE_API_KEY = 'AIzaSyDMeFOnOflzYJ-cjedJ8ky9AE-yThcNXFk'
genai.configure(api_key=GOOGLE_API_KEY)
gemini_model = genai.GenerativeModel('gemini-pro')


def import_and_predict(image_data):
    """Creates five possible Instagram captions for an image.
    Args:
    - image_data: Image data in RGB format.
    Returns:
    - response.text: The five Instagram captions in text format.
    """
    transform = transforms.Compose([
        transforms.Resize((1080, 1080))
    ])
    uploaded_image = transform(image_data)

    # BLIP Description Generation
    inputs = blip_processor(images=uploaded_image, return_tensors="pt")
    generated_ids = blip_model.generate(**inputs,
                                        max_new_tokens=100,
                                        max_length=100)
    caption = blip_processor.\
        batch_decode(generated_ids, skip_special_tokens=True)[0].strip()

    # Gemini Caption Generation
    prompt = f"""Given the provided photo caption, generate five distinct \
                and engaging Instagram captions. Each caption must include \
                at least one emoji and one hashtag. The captions should be \
                formatted with a preceding "Caption #", followed by the \
                caption text. Ensure each caption is seperated by a blank \
                line for readability. \

                Original Caption: {caption}

                Please format your response as follows:
                **Caption 1**: [caption text] \n
                **Caption 2**: [caption text] \n
                **Caption 3**: [caption text] \n
                **Caption 4**: [caption text] \n
                **Caption 5**: [caption text] \n
                """
    response = gemini_model.generate_content(prompt)
    return response.text


# Define streamlit configurations

# Set pre-defined page configurations
st.set_page_config(page_title="Instamuse",
                   page_icon=":camera:",
                   initial_sidebar_state='auto')

# Sidebar (left side of the page)
with st.sidebar:
    st.image('insta.png')
    st.title("InstaMuse")
    st.subheader("Welcome to InstaMuse, the ultimate tool for turning your \
                 snapshots into social media sensations!")
    st.write("Start turning heads with your posts. Use InstaMuse now and \
             watch your likes soar! ")

# Main page text
st.write("""
         # InstaMuse  🌟📸

        Struggling to find the perfect words to match your pictures? Let \
        InstaMuse do the heavy lifting! With just a simple upload, our app \
        uses cutting-edge technology to analyze your image and generate a \
        witty, engaging, or inspiring caption that captures the essence of \
        your moment. \n \

        Whether you’re a selfie savant, a nature explorer, or a foodie fanatic\
        , InstaMuse is here to amplify your Instagram presence. Jazz up your \
        feed with tailored captions that resonate with your followers and \
        attract new eyes to your profile. It’s quick, easy, and fun!

        **Drag your photo here and spark some caption magic!** ✨
         """
         )

# Upload image file and process image
file = st.file_uploader("", type=["jpg", "png"])

if file is not None:
    # Create two columns for the image and the captions
    col1, col2 = st.columns(2)
    
    with col1:
        # Image column, left screen.
        st.markdown("#### Photo:")
        image = Image.open(file).convert('RGB')
        image.thumbnail((400, 400))
        st.image(image, caption='Uploaded Image')

    with col2:
        # Caption column, right screen.
        predictions = import_and_predict(image)
        st.markdown("#### Captions:")
        st.write(predictions)
else:
    st.text("Please upload an image to generate captions.")