-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (81 loc) · 2.87 KB
/
app.py
File metadata and controls
96 lines (81 loc) · 2.87 KB
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
from dotenv import load_dotenv
import streamlit as st
import os
from PIL import Image
import google.generativeai as genai
# Load environment variables from .env file
load_dotenv()
# Configure Gemini API
api_key = os.getenv("GENAI_API_KEY") # Ensure GENAI_API_KEY is set in .env
if not api_key:
st.error("API key not found. Please set the GENAI_API_KEY environment variable.")
st.stop()
genai.configure(api_key=api_key)
# Initialize Gemini model
model = genai.GenerativeModel("gemini-1.5-flash")
# Function to get Gemini response for image analysis
def get_gemini_response(input_text, image):
try:
# Convert PIL image to bytes
img_bytes = image.convert("RGB")
# Generate content using Gemini
response = model.generate_content([input_text, img_bytes])
if response and response.candidates:
parts = response.candidates[0].content.parts
return ' '.join(part.text for part in parts)
else:
return "No meaningful response generated."
except Exception as e:
return f"Error: {e}"
import streamlit as st
from PIL import Image
# Page configuration must come first
st.set_page_config(page_title="NexLens Image Analysis", layout="wide")
# Page header with centered title
st.markdown(
"""
<div style='text-align: center;'>
<h1 style='color:#4F8BF9;'>🔍 NexLens</h1>
<p style='font-size:18px;'>Ask me anything!</p>
</div>
""",
unsafe_allow_html=True,
)
st.markdown("""
<style>
.footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #f0f2f6;
color: #000;
text-align: center;
padding: 12px 10px;
font-size: 14px;
z-index: 9999;
}
/* Push page content up above footer */
.block-container {
padding-bottom: 70px !important;
}
</style>
<div class="footer">
NexLens Image Analysis by <b>Deepak Gowda H R</b>, Mandya Technologies Ltd, Mandya, Karnataka, India • Copyright © 2025
</div>
""", unsafe_allow_html=True)
# Input section
st.header("🧠 NexLens Computer Vision Application")
input_prompt = st.text_input("📝 Input Prompt:", key="input", placeholder="Describe what you want to know about the image.")
uploaded_file = st.file_uploader("📁 Upload an Image:", type=["jpg", "jpeg", "png"])
# Optional: Display uploaded image in small size
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", width=300)
# Button to analyze the image
if st.button("Analyze Image"):
with st.spinner("Analyzing the image..."):
response = get_gemini_response(input_prompt, image)
# Display the response
st.subheader("Analysis Result:")
st.write(response)