File size: 2,727 Bytes
cc77c9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import google.generativeai as genai

import io
import os

from dotenv import load_dotenv
from PIL import Image

load_dotenv()

# Configure the API key
genai.configure(api_key= "AIzaSyBaxMCjBV5fBlsKUmFb-8SGgkiirv1ZKck")

# Set up the model
model = genai.GenerativeModel('gemini-1.5-flash-latest')

def get_gemini_response(image_blob, prompt):
    response = model.generate_content([prompt, image_blob])
    return response.text

# Streamlit app
st.set_page_config(page_title="Image Insights Generator", page_icon="📷", layout="wide")

# Sidebar with instructions and additional information
st.sidebar.title("Instructions")
st.sidebar.write("""

1. Upload an image using the file uploader.

2. Enter a question about the image in the text input box.

3. Click the "Generate Insights" button to get AI insights about the image.

""")
st.sidebar.markdown("---")
st.sidebar.caption("Created with Streamlit and Gemini Pro Vision")

# Main content
st.title("📷 Image Insights Generator")
st.write("Upload an image and ask a question about it. Our AI will analyze the image and provide insights!")

# File uploader for image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Text input for the prompt
    prompt = st.text_input("What would you like to know about this image?", placeholder="Enter your question here...")

    if st.button("Generate Insights"):
        with st.spinner("Analyzing the image..."):
            # Prepare the image for the Gemini API
            img_byte_arr = io.BytesIO()
            image.save(img_byte_arr, format='PNG')
            img_byte_arr = img_byte_arr.getvalue()

            # Create a Blob from the image data
            image_blob = {
                "mime_type": "image/png",
                "data": img_byte_arr
            }

            # Progress bar
            progress_bar = st.progress(0)

            for i in range(1, 101):
                progress_bar.progress(i)
                if i == 100:
                    # Get the response from Gemini Pro Vision
                    response = get_gemini_response(image_blob, prompt)

                    # Display the response
                    st.subheader("🔍 AI Insights:")
                    st.write(response)
                    st.balloons()  # Add some celebration!

# Custom CSS for styling
st.markdown("""

<style>

body {

    font-family: 'Arial', sans-serif;

}

</style>

""", unsafe_allow_html=True)