File size: 10,740 Bytes
5000866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import streamlit as st
import google.generativeai as genai
import googlemaps
from datetime import datetime
from PIL import Image as PILImage
import folium
from streamlit_folium import folium_static
import os
from dotenv import load_dotenv
from streamlit_option_menu import option_menu
import plotly.graph_objs as go
import re

# Load environment variables
load_dotenv()

# Initialize APIs
MAP_API_KEY = os.getenv('MAP_API_KEY')
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')

# Configure the page
st.set_page_config(
    page_title="TerraPulse", 
    page_icon="🌍", 
    layout="wide", 
    initial_sidebar_state="expanded"
)

# Custom CSS for typography, headings, and styling
st.markdown(
    """
    <style>
    body {
        background-color: #f5f7fa;
        font-family: 'Poppins', sans-serif;
    }
    h1, h2, h3 {
        color: #008080;
        font-weight: 700;
    }
    h1 {
        font-size: 3em;
        text-align: center;
        margin-bottom: 20px;
    }
    h2 {
        font-size: 2.2em;
        margin-top: 20px;
    }
    h3 {
        font-size: 1.8em;
    }
    .stApp {
        padding: 20px;
        border-radius: 10px;
        background-color: #f5f7fa;
    }
    .stButton>button {
        background-color: #008080;
        color: white;
        border: none;
        padding: 12px 24px;
        font-size: 18px;
        border-radius: 10px;
        transition: background-color 0.3s;
    }
    .stButton>button:hover {
        background-color: #006666;
    }
    .stTextInput>div>div>input {
        border-radius: 10px;
        border: 1px solid #ccc;
        padding: 12px;
        font-size: 18px;
    }
    .stSidebar > div {
        background-color: rgba(255, 255, 255, 0.95);
        padding: 20px;
        border-radius: 10px;
    }
    .chat-message {
        font-size: 18px;
        font-weight: bold;
        color: #008080;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# Sidebar with option menu
selected_option = option_menu(
    menu_title="🌎 TerraPulse",  
    options=["Home", "Waste-wise", "EcoRoute: Sustainable Travel Planner"],  
    icons=["house", "recycle", "globe"],  
    menu_icon="cast",  
    default_index=0,  
    orientation="horizontal",
    styles={
        "container": {"padding": "5!important", "background-color": "#e0f7fa"},
        "icon": {"color": "#006666", "font-size": "25px"}, 
        "nav-link": {"font-size": "20px", "text-align": "center", "margin":"0px", "--hover-color": "#e0f7fa"},
        "nav-link-selected": {"background-color": "#008080"},
    }
)

# Home page
if selected_option == "Home":
    st.title("🌍 Welcome to TerraPulse")
    st.markdown(
        """
        **TerraPulse** is your go-to application for a sustainable future. 🌱  
        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.
        **Features:**
        - **♻️ Waste-wise:** Upload images of trash items, and TerraPulse will classify them into recyclables, compostables, hazardous materials, and general waste.  
        - **🌍 EcoRoute:** Plan your travel with the environment in mind. Get the most sustainable routes, transportation suggestions, and carbon footprint estimates.
        **Let's work together for a cleaner and greener planet!** πŸŒπŸ’š
        """
    )

# Load Gemini Pro Vision model
@st.cache_resource
def load_model():
    if not GOOGLE_API_KEY:
        st.error("Google API Key not found in .env file.")
        st.stop()
    genai.configure(api_key=GOOGLE_API_KEY)
    return genai.GenerativeModel('gemini-1.5-flash')

# Analyze image function
def analyze_image(image, prompt):
    model = load_model()
    try:
        response = model.generate_content([prompt, image])
        return response.text
    except Exception as e:
        st.error(f"An error occurred during analysis: {str(e)}")
        return None
    
def parse_modes_and_footprints(response_text):
    # Regular expression to match the rows of the table
    row_pattern = re.compile(r'\| (.+?) \| ([\d.]+) \|')
    
    # Find all rows in the table
    matches = row_pattern.findall(response_text)
    
    modes = []
    carbon_footprints = []
    
    for match in matches:
        mode = match[0].strip()
        footprint = float(match[1].strip())
        modes.append(mode)
        carbon_footprints.append(footprint)
    
    if not modes or not carbon_footprints:
        raise ValueError("No valid data found in the response text")
    
    return modes, carbon_footprints


# Waste-wise section
if selected_option == "Waste-wise":
    st.title("♻️ Waste-wise")

    st.subheader("πŸ“€ Upload Image")
    uploaded_files = st.file_uploader("Choose trash images...", type=["jpg", "jpeg", "png"], accept_multiple_files=True)

    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."

    if uploaded_files:
        analyze_button = st.button("πŸ” Analyze Image")
        for uploaded_file in uploaded_files:
            col1, col2 = st.columns(2)

            with col1:
                st.subheader("πŸ–ΌοΈ Uploaded Image")
                image = PILImage.open(uploaded_file)
                st.image(image, caption="Uploaded Image", use_column_width=True)

            with col2:
                st.subheader("🧠 Image Analysis")
                if analyze_button:
                    with st.spinner("Analyzing the image..."):
                        analysis = analyze_image(image, prompt)
                        if analysis:
                            st.markdown(analysis)
                else:
                    st.info("Click 'Analyze Image' to start the analysis.")

# EcoRoute section
if selected_option == "EcoRoute: Sustainable Travel Planner":
    st.title("🌍 EcoRoute: Sustainable Travel Planner")

    gmaps = googlemaps.Client(key=MAP_API_KEY)
    model = load_model()

    # User inputs
    start_location = st.text_input("Enter your start location")
    destination_location = st.text_input("Enter your destination location")
    no_of_people = st.selectbox(
        "Choose the number of people",
        ["1", "2", "3-6", "6-10", "10+"]
    )

    def calculate_trees(carbon_footprint):
        """Calculate the number of trees required to offset the carbon footprint."""
        carbon_per_tree = 0.02177  # Metric tons of COβ‚‚ absorbed per year by one tree
        return carbon_footprint / carbon_per_tree

    if st.button("Find Eco-Friendly Route"):
        if start_location and destination_location:
            # Geocoding the start and end locations
            geocode_start = gmaps.geocode(start_location)
            geocode_end = gmaps.geocode(destination_location)
            
            if geocode_start and geocode_end:
                start_coords = geocode_start[0]['geometry']['location']
                end_coords = geocode_end[0]['geometry']['location']

                # Prompt Google Gemini API to suggest an eco-friendly mode of transport
                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:
Distance: [distance]
Mode 1: Train
- Time: 2h 30m
- Feasibility: High
- Route: Detailed route description
- Carbon footprint (unit): 30.0
Mode 2: Bus
- Time: 3h 00m
- Feasibility: Medium
- Route: Detailed route description
- Carbon footprint (unit): 50.0
Mode 3: Car
- Time: 1h 15m
- Feasibility: High
- Route: Detailed route description
- Carbon footprint (unit): 120.0
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.
Similarly, using your own knowledge, provide eco-friendly routes and the most eco-friendly option along with the estimated carbon footprint.
"""

                response = model.generate_content([prompt])
                eco_friendly_modes = response.text.strip()

                st.write(f"**Suggested Eco-Friendly Modes:**\n{eco_friendly_modes}")

                try:
                    modes, carbon_footprints = parse_modes_and_footprints(eco_friendly_modes)

                    # Plotting the pie chart using Plotly
                    if modes and carbon_footprints:
                        fig = go.Figure(data=[go.Pie(labels=modes, values=carbon_footprints)])
                        fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20)
                        fig.update_layout(title="Carbon Footprint Distribution by Mode of Transport", margin=dict(l=0, r=0, t=40, b=0))

                        # Adjust layout to ensure map and pie chart do not overlap
                        col1, col2 = st.columns([2, 1])

                        with col1:
                            # Displaying the map
                            st.subheader("EcoRoute Map View")
                            # Create a map centered at the midpoint
                            midpoint = [(start_coords['lat'] + end_coords['lat']) / 2, (start_coords['lng'] + end_coords['lng']) / 2]
                            m = folium.Map(location=midpoint, zoom_start=8)

                            folium.Marker([start_coords['lat'], start_coords['lng']], popup=start_location, icon=folium.Icon(color='green')).add_to(m)
                            folium.Marker([end_coords['lat'], end_coords['lng']], popup=destination_location, icon=folium.Icon(color='red')).add_to(m)

                            # Draw a line between the start and end locations
                            folium.PolyLine(locations=[(start_coords['lat'], start_coords['lng']), (end_coords['lat'], end_coords['lng'])], color='blue').add_to(m)

                            folium_static(m)

                        with col2:
                            st.plotly_chart(fig, use_container_width=True)
                    
                except ValueError as e:
                    st.error(f"Error parsing the response: {str(e)}")
            
            else:
                st.error("Could not geocode one or both locations. Please check the input.")
        else:
            st.error("Please enter both the start and destination locations.")