valleeneutral/multi_utility_image_app
0
1import streamlit as st
2from PIL import Image
3from dotenv import load_dotenv
4import os
5import google.generativeai as genai
6
7# Load environment variables
8load_dotenv()
9genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
11# Initialize session state for history
12if "history" not in st.session_state:
13 st.session_state.history = []
14
15# Define app sections
16def calorie_health_tracker():
17 st.header("Calorie Health Tracker")
18 uploaded_file = st.file_uploader("Upload a food image...", type=["jpg", "jpeg", "png"])
19 if uploaded_file is not None:
20 image = Image.open(uploaded_file)
21 st.image(image, caption="Uploaded Image", use_container_width=True)
22
23 input_prompt = """
24 You are an expert in nutritionist where you need to see the food items from the image
25 and calculate the total calories, also provide the details of every food items with calories intake
26 is below format
27
28 1. Item 1 - no of calories
29 2. Item 2 - no of calories
30 ----
31 ----
32 Finally, you can also mention if the food is healthy or not with proper reason why,
33 and also mention the percentage split of the ratio of carbohydrates, fats, fibers, sugars and
34 other important information required in our diet
35 """
36 if st.button("Analyze Food"):
37 try:
38 image_data = [
39 {"mime_type": uploaded_file.type, "data": uploaded_file.getvalue()}
40 ]
41 model = genai.GenerativeModel("gemini-1.5-flash")
42 response = model.generate_content([input_prompt, image_data[0]])
43 st.subheader("Analysis Results")
44 st.write(response.text)
45
46 # Save to history
47 st.session_state.history.append({
48 "section": "Calorie Health Tracker",
49 "result": response.text
50 })
51
52 except Exception as e:
53 st.error(f"Error: {e}")
54
55def invoice_insight_extractor():
56 st.header("Invoice Insight Extractor")
57 user_query = st.text_input("Enter your question about the invoice:")
58 uploaded_file = st.file_uploader("Upload an invoice image...", type=["jpg", "jpeg", "png"])
59 if uploaded_file is not None:
60 image = Image.open(uploaded_file)
61 st.image(image, caption=f"Uploaded Invoice: {uploaded_file.name}", use_container_width=True)
62
63 if st.button("Extract and Answer"):
64 try:
65 input_prompt = """
66 You area an expert in understanding invoices. We will upload an image as invoice
67 and you will answer any following questions based on the uploaded invoice image
68 """
69 image_data = [{"mime_type": uploaded_file.type, "data": uploaded_file.getvalue()}]
70 model = genai.GenerativeModel("gemini-1.5-flash")
71 response = model.generate_content([input_prompt, image_data[0], user_query])
72 st.subheader("Response:")
73 st.write(response.text)
74
75 # Save to history
76 st.session_state.history.append({
77 "section": "Invoice Insight Extractor",
78 "question": user_query,
79 "result": response.text
80 })
81
82 except Exception as e:
83 st.error(f"Error: {e}")
84
85def image_insight_extraction():
86 st.header("Image Insight Extraction")
87 uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
88 if uploaded_file is not None:
89 image = Image.open(uploaded_file)
90 st.image(image, caption="Uploaded Image", use_container_width=True)
91
92 input_prompt = """
93 You are an expert AI trained in visual and contextual analysis. Your task is to analyze the uploaded image and provide a detailed description.
94
95 1. Identify if the image is from a movie poster or any popular media. If yes, provide the name of the movie or media.
96 2. Describe the visual elements of the image, such as objects, characters, or text visible.
97 3. Mention any specific stylistic or artistic details that help identify the context of the image.
98 4. If possible, recognize any iconic symbols, logos, or designs in the image and explain their significance.
99 5. Provide a concise summary of what the image represents.
100
101 Be as detailed and accurate as possible in your response.
102 """
103 if st.button("Get Insights"):
104 try:
105 image_data = [{"mime_type": uploaded_file.type, "data": uploaded_file.getvalue()}]
106 model = genai.GenerativeModel("gemini-1.5-flash")
107 response = model.generate_content([input_prompt, image_data[0]])
108 st.subheader("Image Analysis Results")
109 st.write(response.text)
110
111 # Save to history
112 st.session_state.history.append({
113 "section": "Image Insight Extraction",
114 "result": response.text
115 })
116
117 except Exception as e:
118 st.error(f"Error: {e}")
119
120# Sidebar navigation with dynamic highlighting
121st.set_page_config(page_title="Multi-Functional App", layout="wide")
122
123if "selected" not in st.session_state:
124 st.session_state.selected = "Calorie Health Tracker"
125
126sidebar_options = {
127 "Calorie Health Tracker": calorie_health_tracker,
128 "Invoice Insight Extractor": invoice_insight_extractor,
129 "Image Insight Extraction": image_insight_extraction,
130}
131
132# Inject custom CSS for styling sidebar buttons and centering the title
133st.markdown("""
134 <style>
135 .sidebar .sidebar-content {
136 width: 100%;
137 }
138 .stSidebarTitle {
139 text-align: center;
140 font-weight: bold;
141 font-size: 20px;
142 margin-bottom: 10px;
143 }
144 .stButton button {
145 width: 100%;
146 background-color: #f9f9f9;
147 border: 2px solid #dcdcdc;
148 border-radius: 6px;
149 color: black;
150 text-align: left;
151 padding: 10px;
152 font-size: 16px;
153 margin-bottom: 10px;
154 transition: 0.3s;
155 }
156 .stButton button:hover {
157 background-color: #e0e4eb;
158 border-color: #c0c0c0;
159 }
160 .stButton.active button {
161 background-color: #1e90ff;
162 color: white;
163 border-color: #1c86ee;
164 }
165 </style>
166""", unsafe_allow_html=True)
167
168# Render centered title and sidebar buttons
169st.sidebar.markdown('<div class="stSidebarTitle">Navigation</div>', unsafe_allow_html=True)
170for option in sidebar_options.keys():
171 is_active = st.session_state.selected == option
172 button_style = "active" if is_active else ""
173 if st.sidebar.button(option, key=option):
174 st.session_state.selected = option
175
176# Apply dynamic highlighting and render the selected app
177selected_app = st.session_state.selected
178st.sidebar.markdown(f"**Currently Active:** {selected_app}")
179sidebar_options[selected_app]()
180
181# Display history
182st.sidebar.markdown("---")
183st.sidebar.markdown("### History")
184with st.sidebar.expander("View Session History"):
185 for entry in st.session_state.history:
186 st.markdown(f"**Section:** {entry['section']}")
187 if "question" in entry:
188 st.markdown(f"**Question:** {entry['question']}")
189 st.markdown(f"**Result:** {entry['result']}")
190 st.markdown("---")
191
192 