-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
130 lines (111 loc) · 5.72 KB
/
app.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
import google.generativeai as genai
import PIL.Image
import streamlit as st
import io
api_key = st.secrets["GEMINI_API_KEY"] # Fetches the API key from Streamlit secrets
# Function to get the Gemini AI response
def get_gemini_response(api_key, prompt, image_bytes):
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content([prompt, image_bytes])
return response.text
# Function to auto-generate a caption and a structured description for the uploaded image
def generate_image_summary(api_key, image_bytes):
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
# Generate a one-line caption and a concise description for the image
caption_response = model.generate_content(["Provide a short, one-line caption describing the image.", image_bytes])
description_response = model.generate_content(["Provide a detailed yet brief, structured summary with appropriate emojis to make it engaging and easy to read.", image_bytes])
return caption_response.text, description_response.text
# Initialize the Streamlit app
st.set_page_config(page_title="📸 InsightLens 🔍", layout="centered")
# Title and subtitle with animations (fade-in and glowing effect)
title_color = "#1c4966" # Static color for both light and dark themes
st.markdown(f"""
<style>
@keyframes fadeIn {{
0% {{ opacity: 0; }}
100% {{ opacity: 1; }}
}}
.fade-in {{
animation: fadeIn 2s ease-in-out;
}}
.glowing {{
color: {title_color}; /* Title color dynamically set */
text-shadow:
0 0 10px #00A6FF, /* Bright blue glow */
0 0 20px #00A6FF,
0 0 30px #00A6FF,
0 0 40px #00A6FF,
0 0 50px #00A6FF,
0 0 60px #00A6FF;
animation: glowing 1.5s infinite alternate;
}}
@keyframes glowing {{
0% {{ text-shadow: 0 0 10px #00A6FF, 0 0 20px #00A6FF, 0 0 30px #00A6FF, 0 0 40px #00A6FF; }}
50% {{ text-shadow: 0 0 20px #00A6FF, 0 0 30px #00A6FF, 0 0 40px #00A6FF, 0 0 50px #00A6FF; }}
100% {{ text-shadow: 0 0 10px #00A6FF, 0 0 20px #00A6FF, 0 0 30px #00A6FF, 0 0 40px #00A6FF; }}
}}
</style>
<h1 class="fade-in glowing" style="text-align: center; font-weight: bold;">🖼️ InsightLens 🤖</h1>
<h3 class="fade-in" style="text-align: center; color: #FF5722;">Upload an image to explore its details and ask questions!</h3>
""", unsafe_allow_html=True)
st.write("---")
st.write("### Step 1: Upload a photo, get a quick overview, and dive into details! 🌟")
# File uploader to allow users to upload an image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
image = None
# Create session state to hold caption and description to prevent re-generation
if "caption" not in st.session_state:
st.session_state.caption = ""
if "description" not in st.session_state:
st.session_state.description = ""
if uploaded_file is not None and api_key:
try:
# Open the uploaded image with a fade-in effect
image = PIL.Image.open(uploaded_file)
st.image(image, use_container_width=True)
# Convert image to bytes
image_bytes = io.BytesIO()
image.save(image_bytes, format="PNG")
image_bytes = image_bytes.getvalue()
# Generate a brief caption and a structured description only once
if not st.session_state.caption and not st.session_state.description:
caption, description = generate_image_summary(api_key, image_bytes)
st.session_state.caption = caption
st.session_state.description = description
# Display the caption and description from session state
st.markdown(f"<p class='fade-in' style='color: #FF5733; font-size: 24px; text-align: center; font-weight: bold;'>{st.session_state.caption}</p>", unsafe_allow_html=True)
st.markdown(f"<p class='fade-in' style='color: #4CAF50; font-size: 18px;'>🔍 Image Details:\n\n{st.session_state.description}</p>", unsafe_allow_html=True)
except Exception as e:
if "HARM_CATEGORY_SEXUALLY_EXPLICIT" in str(e):
st.error("🚫 InsightLens couldn't generate details due to sensitive content detected. Try a different image!")
else:
st.error(f"🔄 Something went wrong! Error: {e}")
st.write("---")
# Input prompt for the user's question, with a placeholder text
st.write("### Step 2: Ask a Question About the Image")
prompt = st.text_input("Ask something about the image (e.g., 'What is happening here?'):", key="input")
# Button to submit the request with an animation effect
submit = st.button("🔍 Get Answer")
# If the submit button is clicked, configure the API key and get the Gemini AI response
if submit and api_key and image is not None:
try:
# Convert image to bytes
image_bytes = io.BytesIO()
image.save(image_bytes, format="PNG")
image_bytes = image_bytes.getvalue()
response = get_gemini_response(api_key, prompt, image_bytes)
st.subheader("💡 AI's Response:")
st.write(response)
st.balloons() # Celebration balloons for an interactive touch!
except Exception as e:
st.error(f"An error occurred: {e}")
# Show footer
st.markdown("---")
st.markdown(
"<div style='text-align: center; color: #7f8c8d; font-size: 16px;'>"
"<p style='text-align: center;'>🔮 <strong>|Brought to Life By</strong> - Harshal Kumawat| 🤖</p>"
"</div>",
unsafe_allow_html=True
)