swayam-the-coder commited on
Commit
5000866
β€’
1 Parent(s): e3b1a59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import googlemaps
4
+ from datetime import datetime
5
+ from PIL import Image as PILImage
6
+ import folium
7
+ from streamlit_folium import folium_static
8
+ import os
9
+ from dotenv import load_dotenv
10
+ from streamlit_option_menu import option_menu
11
+ import plotly.graph_objs as go
12
+ import re
13
+
14
+ # Load environment variables
15
+ load_dotenv()
16
+
17
+ # Initialize APIs
18
+ MAP_API_KEY = os.getenv('MAP_API_KEY')
19
+ GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
20
+
21
+ # Configure the page
22
+ st.set_page_config(
23
+ page_title="TerraPulse",
24
+ page_icon="🌍",
25
+ layout="wide",
26
+ initial_sidebar_state="expanded"
27
+ )
28
+
29
+ # Custom CSS for typography, headings, and styling
30
+ st.markdown(
31
+ """
32
+ <style>
33
+ body {
34
+ background-color: #f5f7fa;
35
+ font-family: 'Poppins', sans-serif;
36
+ }
37
+ h1, h2, h3 {
38
+ color: #008080;
39
+ font-weight: 700;
40
+ }
41
+ h1 {
42
+ font-size: 3em;
43
+ text-align: center;
44
+ margin-bottom: 20px;
45
+ }
46
+ h2 {
47
+ font-size: 2.2em;
48
+ margin-top: 20px;
49
+ }
50
+ h3 {
51
+ font-size: 1.8em;
52
+ }
53
+ .stApp {
54
+ padding: 20px;
55
+ border-radius: 10px;
56
+ background-color: #f5f7fa;
57
+ }
58
+ .stButton>button {
59
+ background-color: #008080;
60
+ color: white;
61
+ border: none;
62
+ padding: 12px 24px;
63
+ font-size: 18px;
64
+ border-radius: 10px;
65
+ transition: background-color 0.3s;
66
+ }
67
+ .stButton>button:hover {
68
+ background-color: #006666;
69
+ }
70
+ .stTextInput>div>div>input {
71
+ border-radius: 10px;
72
+ border: 1px solid #ccc;
73
+ padding: 12px;
74
+ font-size: 18px;
75
+ }
76
+ .stSidebar > div {
77
+ background-color: rgba(255, 255, 255, 0.95);
78
+ padding: 20px;
79
+ border-radius: 10px;
80
+ }
81
+ .chat-message {
82
+ font-size: 18px;
83
+ font-weight: bold;
84
+ color: #008080;
85
+ }
86
+ </style>
87
+ """,
88
+ unsafe_allow_html=True
89
+ )
90
+
91
+ # Sidebar with option menu
92
+ selected_option = option_menu(
93
+ menu_title="🌎 TerraPulse",
94
+ options=["Home", "Waste-wise", "EcoRoute: Sustainable Travel Planner"],
95
+ icons=["house", "recycle", "globe"],
96
+ menu_icon="cast",
97
+ default_index=0,
98
+ orientation="horizontal",
99
+ styles={
100
+ "container": {"padding": "5!important", "background-color": "#e0f7fa"},
101
+ "icon": {"color": "#006666", "font-size": "25px"},
102
+ "nav-link": {"font-size": "20px", "text-align": "center", "margin":"0px", "--hover-color": "#e0f7fa"},
103
+ "nav-link-selected": {"background-color": "#008080"},
104
+ }
105
+ )
106
+
107
+ # Home page
108
+ if selected_option == "Home":
109
+ st.title("🌍 Welcome to TerraPulse")
110
+ st.markdown(
111
+ """
112
+ **TerraPulse** is your go-to application for a sustainable future. 🌱
113
+ Whether you're looking to classify waste for proper disposal or planning an eco-friendly route for your next trip, TerraPulse has got you covered.
114
+ **Features:**
115
+ - **♻️ Waste-wise:** Upload images of trash items, and TerraPulse will classify them into recyclables, compostables, hazardous materials, and general waste.
116
+ - **🌍 EcoRoute:** Plan your travel with the environment in mind. Get the most sustainable routes, transportation suggestions, and carbon footprint estimates.
117
+ **Let's work together for a cleaner and greener planet!** πŸŒπŸ’š
118
+ """
119
+ )
120
+
121
+ # Load Gemini Pro Vision model
122
+ @st.cache_resource
123
+ def load_model():
124
+ if not GOOGLE_API_KEY:
125
+ st.error("Google API Key not found in .env file.")
126
+ st.stop()
127
+ genai.configure(api_key=GOOGLE_API_KEY)
128
+ return genai.GenerativeModel('gemini-1.5-flash')
129
+
130
+ # Analyze image function
131
+ def analyze_image(image, prompt):
132
+ model = load_model()
133
+ try:
134
+ response = model.generate_content([prompt, image])
135
+ return response.text
136
+ except Exception as e:
137
+ st.error(f"An error occurred during analysis: {str(e)}")
138
+ return None
139
+
140
+ def parse_modes_and_footprints(response_text):
141
+ # Regular expression to match the rows of the table
142
+ row_pattern = re.compile(r'\| (.+?) \| ([\d.]+) \|')
143
+
144
+ # Find all rows in the table
145
+ matches = row_pattern.findall(response_text)
146
+
147
+ modes = []
148
+ carbon_footprints = []
149
+
150
+ for match in matches:
151
+ mode = match[0].strip()
152
+ footprint = float(match[1].strip())
153
+ modes.append(mode)
154
+ carbon_footprints.append(footprint)
155
+
156
+ if not modes or not carbon_footprints:
157
+ raise ValueError("No valid data found in the response text")
158
+
159
+ return modes, carbon_footprints
160
+
161
+
162
+ # Waste-wise section
163
+ if selected_option == "Waste-wise":
164
+ st.title("♻️ Waste-wise")
165
+
166
+ st.subheader("πŸ“€ Upload Image")
167
+ uploaded_files = st.file_uploader("Choose trash images...", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
168
+
169
+ prompt = "Analyze the image of trash items. Classify the waste into categories such as recyclables, compostables, hazardous materials, and general waste. Based on the classification, guide the user on which specific color dustbin (e.g., recycling, compost, hazardous, or landfill) to dispose of the items."
170
+
171
+ if uploaded_files:
172
+ analyze_button = st.button("πŸ” Analyze Image")
173
+ for uploaded_file in uploaded_files:
174
+ col1, col2 = st.columns(2)
175
+
176
+ with col1:
177
+ st.subheader("πŸ–ΌοΈ Uploaded Image")
178
+ image = PILImage.open(uploaded_file)
179
+ st.image(image, caption="Uploaded Image", use_column_width=True)
180
+
181
+ with col2:
182
+ st.subheader("🧠 Image Analysis")
183
+ if analyze_button:
184
+ with st.spinner("Analyzing the image..."):
185
+ analysis = analyze_image(image, prompt)
186
+ if analysis:
187
+ st.markdown(analysis)
188
+ else:
189
+ st.info("Click 'Analyze Image' to start the analysis.")
190
+
191
+ # EcoRoute section
192
+ if selected_option == "EcoRoute: Sustainable Travel Planner":
193
+ st.title("🌍 EcoRoute: Sustainable Travel Planner")
194
+
195
+ gmaps = googlemaps.Client(key=MAP_API_KEY)
196
+ model = load_model()
197
+
198
+ # User inputs
199
+ start_location = st.text_input("Enter your start location")
200
+ destination_location = st.text_input("Enter your destination location")
201
+ no_of_people = st.selectbox(
202
+ "Choose the number of people",
203
+ ["1", "2", "3-6", "6-10", "10+"]
204
+ )
205
+
206
+ def calculate_trees(carbon_footprint):
207
+ """Calculate the number of trees required to offset the carbon footprint."""
208
+ carbon_per_tree = 0.02177 # Metric tons of COβ‚‚ absorbed per year by one tree
209
+ return carbon_footprint / carbon_per_tree
210
+
211
+ if st.button("Find Eco-Friendly Route"):
212
+ if start_location and destination_location:
213
+ # Geocoding the start and end locations
214
+ geocode_start = gmaps.geocode(start_location)
215
+ geocode_end = gmaps.geocode(destination_location)
216
+
217
+ if geocode_start and geocode_end:
218
+ start_coords = geocode_start[0]['geometry']['location']
219
+ end_coords = geocode_end[0]['geometry']['location']
220
+
221
+ # Prompt Google Gemini API to suggest an eco-friendly mode of transport
222
+ prompt = f"""You are an eco-friendly mode of transport suggestor. Your job is to provide me with the best routes in bullet points in a comprehensive manner. Suggest the most eco-friendly mode of transport between {start_location} and {destination_location}. Here is the number of people traveling: {no_of_people}. Consider all the above parameters to provide the result in the below format:
223
+ Distance: [distance]
224
+ Mode 1: Train
225
+ - Time: 2h 30m
226
+ - Feasibility: High
227
+ - Route: Detailed route description
228
+ - Carbon footprint (unit): 30.0
229
+ Mode 2: Bus
230
+ - Time: 3h 00m
231
+ - Feasibility: Medium
232
+ - Route: Detailed route description
233
+ - Carbon footprint (unit): 50.0
234
+ Mode 3: Car
235
+ - Time: 1h 15m
236
+ - Feasibility: High
237
+ - Route: Detailed route description
238
+ - Carbon footprint (unit): 120.0
239
+ Use this exact format. carbon footprint output should only be a single number in float format, nothing else. Generate a table of the different modes of transport vs their carbon footprint.
240
+ Similarly, using your own knowledge, provide eco-friendly routes and the most eco-friendly option along with the estimated carbon footprint.
241
+ """
242
+
243
+ response = model.generate_content([prompt])
244
+ eco_friendly_modes = response.text.strip()
245
+
246
+ st.write(f"**Suggested Eco-Friendly Modes:**\n{eco_friendly_modes}")
247
+
248
+ try:
249
+ modes, carbon_footprints = parse_modes_and_footprints(eco_friendly_modes)
250
+
251
+ # Plotting the pie chart using Plotly
252
+ if modes and carbon_footprints:
253
+ fig = go.Figure(data=[go.Pie(labels=modes, values=carbon_footprints)])
254
+ fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20)
255
+ fig.update_layout(title="Carbon Footprint Distribution by Mode of Transport", margin=dict(l=0, r=0, t=40, b=0))
256
+
257
+ # Adjust layout to ensure map and pie chart do not overlap
258
+ col1, col2 = st.columns([2, 1])
259
+
260
+ with col1:
261
+ # Displaying the map
262
+ st.subheader("EcoRoute Map View")
263
+ # Create a map centered at the midpoint
264
+ midpoint = [(start_coords['lat'] + end_coords['lat']) / 2, (start_coords['lng'] + end_coords['lng']) / 2]
265
+ m = folium.Map(location=midpoint, zoom_start=8)
266
+
267
+ folium.Marker([start_coords['lat'], start_coords['lng']], popup=start_location, icon=folium.Icon(color='green')).add_to(m)
268
+ folium.Marker([end_coords['lat'], end_coords['lng']], popup=destination_location, icon=folium.Icon(color='red')).add_to(m)
269
+
270
+ # Draw a line between the start and end locations
271
+ folium.PolyLine(locations=[(start_coords['lat'], start_coords['lng']), (end_coords['lat'], end_coords['lng'])], color='blue').add_to(m)
272
+
273
+ folium_static(m)
274
+
275
+ with col2:
276
+ st.plotly_chart(fig, use_container_width=True)
277
+
278
+ except ValueError as e:
279
+ st.error(f"Error parsing the response: {str(e)}")
280
+
281
+ else:
282
+ st.error("Could not geocode one or both locations. Please check the input.")
283
+ else:
284
+ st.error("Please enter both the start and destination locations.")