Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.graph_objects as go
|
|
|
4 |
|
5 |
# Set page configurations
|
6 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
@@ -48,114 +49,111 @@ def process_excel(file):
|
|
48 |
if uploaded_file:
|
49 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
|
50 |
|
51 |
-
# Sidebar
|
52 |
-
st.sidebar.header("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Function to calculate footprints
|
56 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
57 |
-
|
58 |
-
|
59 |
-
carbon_footprint = 0
|
60 |
-
|
61 |
for fiber, percentage in composition.items():
|
62 |
if fiber in fiber_impact_data:
|
63 |
data = fiber_impact_data[fiber]
|
64 |
fraction = percentage / 100
|
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 |
-
acrylic = st.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1)
|
91 |
-
viscose = st.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1)
|
92 |
-
|
93 |
-
total = cotton + polyester + nylon + acrylic + viscose
|
94 |
-
if total != 100:
|
95 |
-
st.error("Total fiber composition must equal 100%!")
|
96 |
-
return {"Conventional Cotton": cotton, "Polyester": polyester, "Nylon 6": nylon, "Acrylic": acrylic, "Viscose": viscose} if total == 100 else None
|
97 |
-
|
98 |
-
# Inputs for lifecycle details
|
99 |
-
def lifecycle_input():
|
100 |
-
st.subheader("Lifecycle Details")
|
101 |
-
washing_cycles = st.number_input("Number of Washing Cycles", min_value=0, value=30, step=1)
|
102 |
-
washing_temperature = st.selectbox("Washing Temperature", list(washing_impact_data.keys()))
|
103 |
-
use_dryer = st.checkbox("Use Tumble Dryer?")
|
104 |
-
transport_mode = st.selectbox("Transport Mode", list(transport_impact_data.keys()))
|
105 |
-
transport_distance = st.number_input("Transport Distance (km)", min_value=0, step=10, value=100)
|
106 |
-
return {
|
107 |
-
"washing_cycles": washing_cycles,
|
108 |
-
"washing_temperature": washing_temperature,
|
109 |
-
"use_dryer": use_dryer,
|
110 |
"transport_mode": transport_mode,
|
111 |
"transport_distance": transport_distance,
|
|
|
|
|
|
|
112 |
}
|
113 |
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
if comparison_mode:
|
117 |
-
|
118 |
-
st.header("Scenario 1")
|
119 |
-
composition1 = composition_input()
|
120 |
-
lifecycle1 = lifecycle_input()
|
121 |
-
|
122 |
-
st.header("Scenario 2")
|
123 |
-
composition2 = composition_input()
|
124 |
-
lifecycle2 = lifecycle_input()
|
125 |
-
|
126 |
-
if composition1 and composition2:
|
127 |
-
water_fp1, energy_fp1, carbon_fp1 = calculate_footprints(1, composition1, lifecycle1)
|
128 |
-
water_fp2, energy_fp2, carbon_fp2 = calculate_footprints(1, composition2, lifecycle2)
|
129 |
-
|
130 |
-
# Display results side by side
|
131 |
-
col1, col2 = st.columns(2)
|
132 |
-
with col1:
|
133 |
-
st.subheader("Scenario 1 Results")
|
134 |
-
st.markdown(f"- **Water Footprint**: {water_fp1:.2f} liters")
|
135 |
-
st.markdown(f"- **Energy Footprint**: {energy_fp1:.2f} MJ")
|
136 |
-
st.markdown(f"- **Carbon Footprint**: {carbon_fp1:.2f} kg CO2e")
|
137 |
-
with col2:
|
138 |
-
st.subheader("Scenario 2 Results")
|
139 |
-
st.markdown(f"- **Water Footprint**: {water_fp2:.2f} liters")
|
140 |
-
st.markdown(f"- **Energy Footprint**: {energy_fp2:.2f} MJ")
|
141 |
-
st.markdown(f"- **Carbon Footprint**: {carbon_fp2:.2f} kg CO2e")
|
142 |
-
|
143 |
-
# Visual comparison
|
144 |
-
fig = go.Figure()
|
145 |
-
fig.add_trace(go.Bar(name="Scenario 1", x=["Water", "Energy", "Carbon"], y=[water_fp1, energy_fp1, carbon_fp1]))
|
146 |
-
fig.add_trace(go.Bar(name="Scenario 2", x=["Water", "Energy", "Carbon"], y=[water_fp2, energy_fp2, carbon_fp2]))
|
147 |
-
fig.update_layout(barmode="group", title="Comparison of Scenarios")
|
148 |
-
st.plotly_chart(fig)
|
149 |
else:
|
150 |
-
#
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
else:
|
161 |
-
st.info("Please upload a dataset
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.graph_objects as go
|
4 |
+
import plotly.express as px
|
5 |
|
6 |
# Set page configurations
|
7 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
|
|
49 |
if uploaded_file:
|
50 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
|
51 |
|
52 |
+
# Sidebar for product and fiber composition inputs
|
53 |
+
st.sidebar.header("Step 2: Input Product Details")
|
54 |
+
product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.5)
|
55 |
+
|
56 |
+
st.sidebar.header("Material Composition (%)")
|
57 |
+
cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key="cotton")
|
58 |
+
polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key="polyester")
|
59 |
+
nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key="nylon")
|
60 |
+
acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key="acrylic")
|
61 |
+
viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key="viscose")
|
62 |
+
|
63 |
+
total_percentage = cotton + polyester + nylon + acrylic + viscose
|
64 |
+
if total_percentage != 100:
|
65 |
+
st.sidebar.error("The total of all fiber percentages must equal 100%!")
|
66 |
+
|
67 |
+
composition = {
|
68 |
+
"Conventional Cotton": cotton,
|
69 |
+
"Polyester": polyester,
|
70 |
+
"Nylon 6": nylon,
|
71 |
+
"Acrylic": acrylic,
|
72 |
+
"Viscose": viscose,
|
73 |
+
}
|
74 |
+
|
75 |
+
# Sidebar for lifecycle inputs
|
76 |
+
st.sidebar.header("Step 3: Input Lifecycle Details")
|
77 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
78 |
+
washing_cycles = st.sidebar.number_input("Number of Washing Cycles", min_value=0, step=1, value=30)
|
79 |
+
washing_temperature = None
|
80 |
+
use_dryer = False
|
81 |
+
transport_mode = None
|
82 |
+
transport_distance = 0
|
83 |
+
|
84 |
+
if washing_impact_data:
|
85 |
+
washing_temperature = st.sidebar.selectbox("Washing Temperature", list(washing_impact_data.keys()))
|
86 |
+
use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
|
87 |
+
|
88 |
+
if transport_impact_data:
|
89 |
+
transport_mode = st.sidebar.selectbox("Transport Mode", list(transport_impact_data.keys()))
|
90 |
+
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=10, value=100)
|
91 |
|
92 |
# Function to calculate footprints
|
93 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
94 |
+
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
95 |
+
|
|
|
|
|
96 |
for fiber, percentage in composition.items():
|
97 |
if fiber in fiber_impact_data:
|
98 |
data = fiber_impact_data[fiber]
|
99 |
fraction = percentage / 100
|
100 |
+
water_fp += data["Water (L/kg)"] * weight * fraction
|
101 |
+
energy_fp += data["Energy (MJ/kg)"] * weight * fraction
|
102 |
+
carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
|
103 |
+
|
104 |
+
if lifecycle_inputs["transport_mode"] in transport_impact_data:
|
105 |
+
carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
|
106 |
+
|
107 |
+
if lifecycle_inputs["washing_temperature"] in washing_impact_data:
|
108 |
+
washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
|
109 |
+
washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
|
110 |
+
washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
|
111 |
+
washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
|
112 |
+
dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
|
113 |
+
|
114 |
+
water_fp += washing_water
|
115 |
+
energy_fp += washing_energy
|
116 |
+
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
|
117 |
+
|
118 |
+
return water_fp, energy_fp, carbon_fp
|
119 |
+
|
120 |
+
# Results display area
|
121 |
+
st.header("Results")
|
122 |
+
|
123 |
+
if uploaded_file and total_percentage == 100:
|
124 |
+
lifecycle_inputs = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
"transport_mode": transport_mode,
|
126 |
"transport_distance": transport_distance,
|
127 |
+
"washing_temperature": washing_temperature,
|
128 |
+
"washing_cycles": washing_cycles,
|
129 |
+
"use_dryer": use_dryer,
|
130 |
}
|
131 |
|
132 |
+
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, lifecycle_inputs)
|
133 |
+
|
134 |
+
st.subheader("Calculated Footprints")
|
135 |
+
st.markdown(f"- **Water Footprint**: {water_fp:.2f} liters")
|
136 |
+
st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
|
137 |
+
st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kg CO2e")
|
138 |
+
|
139 |
+
# Visualization
|
140 |
if comparison_mode:
|
141 |
+
st.info("Comparison mode enabled. Results for two scenarios can be added here if required.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
else:
|
143 |
+
# Line chart visualization
|
144 |
+
footprint_data = pd.DataFrame({
|
145 |
+
"Footprint Type": ["Water", "Energy", "Carbon"],
|
146 |
+
"Value": [water_fp, energy_fp, carbon_fp],
|
147 |
+
})
|
148 |
+
|
149 |
+
fig = px.line(
|
150 |
+
footprint_data,
|
151 |
+
x="Footprint Type",
|
152 |
+
y="Value",
|
153 |
+
title="Footprint Trends",
|
154 |
+
markers=True,
|
155 |
+
)
|
156 |
+
st.plotly_chart(fig)
|
157 |
+
|
158 |
else:
|
159 |
+
st.info("Please upload a dataset and ensure all inputs are valid.")
|