GreenPrint_AI / src /streamlit_app.py
Parishri07's picture
Update src/streamlit_app.py
b7f50c7 verified
import streamlit as st
import plotly.express as px
from model.predictor import predict_footprint
from utils.reducer import suggest_reduction
st.set_page_config(page_title="GreenPrint AI ๐ŸŒฑ", layout="centered")
# Custom CSS for green button
st.markdown("""
<style>
div.stButton > button:first-child {
background-color: #4CAF50;
color: white;
border: none;
padding: 8px 20px;
border-radius: 8px;
font-size: 16px;
transition: 0.3s;
}
div.stButton > button:first-child:hover {
background-color: #45a049;
color: white;
border: none;
}
</style>
""", unsafe_allow_html=True)
st.markdown("""
<h1 style='text-align: center; white-space: nowrap; overflow-x: auto; font-size: 2.2rem;'>
๐ŸŒฟ GreenPrint AI: Carbon Footprint Detector
</h1>
""", unsafe_allow_html=True)
# Emission factors (same as used in dataset creation)
EMISSION_FACTORS = {
"Car Travel": 0.2, # kg COโ‚‚ per km
"Electricity": 0.5, # kg COโ‚‚ per unit (kWh)
"Meat Consumption": 27 / 7, # Assuming avg 1 kg per 7 meals
"Flights": 250 # kg COโ‚‚ per flight
}
# User Input
with st.form("activity_form"):
car_km = st.number_input("๐Ÿš— Car travel per week (km)", 0, 1000, 50)
electricity = st.number_input("๐Ÿ’ก Electricity usage per month (units)", 0, 1000, 200)
meat = st.number_input("๐Ÿ– Meat meals per week", 0, 21, 7)
flights = st.number_input("โœˆ๏ธ Flights per year", 0, 20, 1)
submitted = st.form_submit_button("Calculate")
if submitted:
user_input = {
"car_km_per_week": car_km,
"electricity_units_per_month": electricity,
"meat_meals_per_week": meat,
"flights_per_year": flights
}
result = predict_footprint(user_input)
st.success(f"Estimated Annual Carbon Footprint: **{result:.2f} kg COโ‚‚**")
# Activity-wise contribution calculation
yearly_values = {
"Car Travel": car_km * 52 * EMISSION_FACTORS["Car Travel"],
"Electricity": electricity * 12 * EMISSION_FACTORS["Electricity"],
"Meat Consumption": meat * 52 * EMISSION_FACTORS["Meat Consumption"],
"Flights": flights * EMISSION_FACTORS["Flights"]
}
# Pie chart with green shades and percent labels
st.markdown("### ๐Ÿงพ Activity-wise Contribution to Your Footprint")
green_colors = ['#006400', '#228B22', '#32CD32', '#7CFC00'] # dark to light green
fig = px.pie(
names=yearly_values.keys(),
values=yearly_values.values(),
title="Your Carbon Footprint Breakdown",
color_discrete_sequence=green_colors,
hole=0.3
)
fig.update_traces(textinfo='label+percent', textfont_size=16)
st.plotly_chart(fig, use_container_width=True)
st.markdown("---")
st.subheader("โ™ป๏ธ Suggestions to Reduce Your Footprint")
for tip in suggest_reduction(user_input):
st.markdown(f"- {tip}")
st.markdown("---")
st.markdown("<div style='text-align:center;'>Made with ๐Ÿ’š by Parishri</div>", unsafe_allow_html=True)