Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,56 +2,35 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
5 |
-
# Set page
|
6 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
A Tool for Calculating Water, Energy, and Carbon Footprints of Textile Products 🌍
|
14 |
-
</p>
|
15 |
-
""",
|
16 |
-
unsafe_allow_html=True,
|
17 |
-
)
|
18 |
-
|
19 |
-
# Sidebar for file upload
|
20 |
-
st.sidebar.header("Step 1: Upload Dataset")
|
21 |
-
uploaded_file = st.sidebar.file_uploader("Upload your Excel file (.xlsx)", type=["xlsx"])
|
22 |
-
|
23 |
-
# Initialize data containers
|
24 |
-
fiber_impact_data = None
|
25 |
-
transport_impact_data = None
|
26 |
-
washing_impact_data = None
|
27 |
-
|
28 |
-
# Function to process the uploaded Excel file
|
29 |
@st.cache_data
|
30 |
-
def
|
31 |
try:
|
32 |
-
excel_content = pd.ExcelFile(
|
33 |
-
fiber_data = pd.read_excel(
|
34 |
-
transport_data = pd.read_excel(
|
35 |
-
washing_data = pd.read_excel(
|
36 |
-
|
37 |
-
# Convert into dictionaries for
|
38 |
fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
|
39 |
transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
|
40 |
washing_impact_data = washing_data.set_index("Washing Temperature")[["Water (L/kg)", "Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"]].to_dict(orient="index")
|
41 |
-
|
42 |
return fiber_impact_data, transport_impact_data, washing_impact_data
|
43 |
except Exception as e:
|
44 |
-
st.error(f"Error
|
45 |
return None, None, None
|
46 |
|
47 |
-
# Process uploaded file
|
48 |
-
if uploaded_file:
|
49 |
-
fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
|
50 |
|
51 |
-
#
|
52 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
53 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
54 |
-
|
55 |
for fiber, percentage in composition.items():
|
56 |
if fiber in fiber_impact_data:
|
57 |
data = fiber_impact_data[fiber]
|
@@ -69,106 +48,93 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
|
|
69 |
washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
|
70 |
washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
|
71 |
dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
|
72 |
-
|
73 |
water_fp += washing_water
|
74 |
energy_fp += washing_energy
|
75 |
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
|
76 |
|
77 |
-
# Convert water
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
# Sidebar inputs
|
82 |
-
def get_inputs(
|
83 |
-
|
84 |
-
|
85 |
-
st.sidebar.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
composition = {
|
97 |
"Conventional Cotton": cotton,
|
98 |
"Polyester": polyester,
|
99 |
"Nylon 6": nylon,
|
100 |
"Acrylic": acrylic,
|
101 |
-
"Viscose": viscose
|
102 |
-
}
|
103 |
-
|
104 |
-
st.sidebar.subheader(f"{key_prefix} - Lifecycle Inputs")
|
105 |
-
washing_cycles = st.sidebar.number_input(f"{key_prefix} - Washing Cycles", min_value=0, step=1, value=30, key=f"{key_prefix}_wash_cycles")
|
106 |
-
washing_temperature = st.sidebar.selectbox(f"{key_prefix} - Washing Temperature", list(washing_impact_data.keys()), key=f"{key_prefix}_wash_temp")
|
107 |
-
use_dryer = st.sidebar.checkbox(f"{key_prefix} - Use Tumble Dryer?", key=f"{key_prefix}_use_dryer")
|
108 |
-
transport_mode = st.sidebar.selectbox(f"{key_prefix} - Transport Mode", list(transport_impact_data.keys()), key=f"{key_prefix}_transport_mode")
|
109 |
-
transport_distance = st.sidebar.number_input(f"{key_prefix} - Transport Distance (km)", min_value=0, step=10, value=100, key=f"{key_prefix}_transport_distance")
|
110 |
-
|
111 |
-
lifecycle_inputs = {
|
112 |
-
"washing_temperature": washing_temperature,
|
113 |
-
"washing_cycles": washing_cycles,
|
114 |
-
"use_dryer": use_dryer,
|
115 |
-
"transport_mode": transport_mode,
|
116 |
-
"transport_distance": transport_distance,
|
117 |
}
|
118 |
-
|
119 |
-
return product_weight, composition, lifecycle_inputs
|
120 |
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
123 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
124 |
-
|
125 |
if comparison_mode:
|
126 |
-
# Input for two assessments
|
127 |
col1, col2 = st.columns(2)
|
128 |
with col1:
|
129 |
st.subheader("Assessment 1")
|
130 |
-
|
131 |
with col2:
|
132 |
st.subheader("Assessment 2")
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
# Combined visualization with line chart
|
140 |
st.subheader("Comparison of Assessments")
|
141 |
-
|
142 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
143 |
-
"Assessment 1": [
|
144 |
-
"Assessment 2": [
|
145 |
})
|
146 |
-
fig = px.
|
147 |
-
|
148 |
x="Footprint Type",
|
149 |
y="Value",
|
150 |
color="Assessment",
|
151 |
-
|
152 |
-
title="Footprint Trends: Assessment 1 vs. Assessment 2"
|
153 |
)
|
154 |
st.plotly_chart(fig)
|
|
|
155 |
else:
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
# Display results
|
161 |
st.subheader("Results")
|
162 |
-
st.markdown(f"- **Water Footprint**: {
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
# Visualization for single scenario
|
167 |
result_data = pd.DataFrame({
|
168 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
169 |
-
"Value": [
|
170 |
})
|
171 |
-
fig = px.
|
172 |
st.plotly_chart(fig)
|
173 |
else:
|
174 |
-
st.
|
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
5 |
+
# Set page configuration (must be the first Streamlit command)
|
6 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
7 |
|
8 |
+
# Dataset URL from Hugging Face Spaces
|
9 |
+
DATASET_URL = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/DataSet01.xlsx"
|
10 |
+
|
11 |
+
|
12 |
+
# Process dataset from Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
@st.cache_data
|
14 |
+
def process_dataset(url):
|
15 |
try:
|
16 |
+
excel_content = pd.ExcelFile(url)
|
17 |
+
fiber_data = pd.read_excel(excel_content, sheet_name="Fiber Impact Data")
|
18 |
+
transport_data = pd.read_excel(excel_content, sheet_name="Transport Impact Data")
|
19 |
+
washing_data = pd.read_excel(excel_content, sheet_name="Washing Data")
|
20 |
+
|
21 |
+
# Convert data into dictionaries for calculations
|
22 |
fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
|
23 |
transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
|
24 |
washing_impact_data = washing_data.set_index("Washing Temperature")[["Water (L/kg)", "Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"]].to_dict(orient="index")
|
|
|
25 |
return fiber_impact_data, transport_impact_data, washing_impact_data
|
26 |
except Exception as e:
|
27 |
+
st.error(f"Error accessing the dataset: {e}")
|
28 |
return None, None, None
|
29 |
|
|
|
|
|
|
|
30 |
|
31 |
+
# Calculate footprints
|
32 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
33 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
|
|
34 |
for fiber, percentage in composition.items():
|
35 |
if fiber in fiber_impact_data:
|
36 |
data = fiber_impact_data[fiber]
|
|
|
48 |
washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
|
49 |
washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
|
50 |
dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
|
|
|
51 |
water_fp += washing_water
|
52 |
energy_fp += washing_energy
|
53 |
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
|
54 |
|
55 |
+
water_fp /= 1000 # Convert water from liters to kiloliters
|
56 |
+
return water_fp, energy_fp, carbon_fp
|
57 |
+
|
58 |
+
|
59 |
+
# Sidebar inputs
|
60 |
+
def get_inputs(prefix):
|
61 |
+
weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.01, value=0.5, step=0.01, key=f"{prefix}_weight")
|
62 |
+
st.sidebar.subheader(f"{prefix} Material Composition (%)")
|
63 |
+
cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key=f"{prefix}_cotton")
|
64 |
+
polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key=f"{prefix}_polyester")
|
65 |
+
nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key=f"{prefix}_nylon")
|
66 |
+
acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key=f"{prefix}_acrylic")
|
67 |
+
viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key=f"{prefix}_viscose")
|
68 |
+
|
69 |
+
if cotton + polyester + nylon + acrylic + viscose != 100:
|
70 |
+
st.sidebar.error("Fiber composition must sum to 100%!")
|
71 |
+
|
72 |
+
lifecycle_inputs = {
|
73 |
+
"washing_cycles": st.sidebar.number_input(f"{prefix} Washing Cycles", min_value=0, value=30, key=f"{prefix}_wash_cycles"),
|
74 |
+
"washing_temperature": st.sidebar.selectbox(f"{prefix} Washing Temperature", list(washing_impact_data.keys()), key=f"{prefix}_wash_temp"),
|
75 |
+
"use_dryer": st.sidebar.checkbox(f"{prefix} Use Dryer?", key=f"{prefix}_use_dryer"),
|
76 |
+
"transport_mode": st.sidebar.selectbox(f"{prefix} Transport Mode", list(transport_impact_data.keys()), key=f"{prefix}_transport_mode"),
|
77 |
+
"transport_distance": st.sidebar.number_input(f"{prefix} Transport Distance (km)", min_value=0, value=100, step=10, key=f"{prefix}_transport_distance")
|
78 |
+
}
|
79 |
+
|
80 |
composition = {
|
81 |
"Conventional Cotton": cotton,
|
82 |
"Polyester": polyester,
|
83 |
"Nylon 6": nylon,
|
84 |
"Acrylic": acrylic,
|
85 |
+
"Viscose": viscose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
}
|
87 |
+
return weight, composition, lifecycle_inputs
|
|
|
88 |
|
89 |
+
|
90 |
+
# Main App Logic
|
91 |
+
st.sidebar.header("Step 1: Configuration")
|
92 |
+
fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
|
93 |
+
|
94 |
+
if fiber_impact_data and transport_impact_data and washing_impact_data:
|
95 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
96 |
+
|
97 |
if comparison_mode:
|
|
|
98 |
col1, col2 = st.columns(2)
|
99 |
with col1:
|
100 |
st.subheader("Assessment 1")
|
101 |
+
weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
|
102 |
with col2:
|
103 |
st.subheader("Assessment 2")
|
104 |
+
weight2, composition2, lifecycle2 = get_inputs("Assessment 2")
|
105 |
+
|
106 |
+
water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
|
107 |
+
water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
|
108 |
+
|
|
|
|
|
109 |
st.subheader("Comparison of Assessments")
|
110 |
+
comparison_data = pd.DataFrame({
|
111 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
112 |
+
"Assessment 1": [water1, energy1, carbon1],
|
113 |
+
"Assessment 2": [water2, energy2, carbon2]
|
114 |
})
|
115 |
+
fig = px.bar(
|
116 |
+
comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
|
117 |
x="Footprint Type",
|
118 |
y="Value",
|
119 |
color="Assessment",
|
120 |
+
title="Comparison of Assessments"
|
|
|
121 |
)
|
122 |
st.plotly_chart(fig)
|
123 |
+
|
124 |
else:
|
125 |
+
weight, composition, lifecycle = get_inputs("Single")
|
126 |
+
water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
|
127 |
+
|
|
|
|
|
128 |
st.subheader("Results")
|
129 |
+
st.markdown(f"- **Water Footprint**: {water:.2f} kL\n"
|
130 |
+
f"- **Energy Footprint**: {energy:.2f} MJ\n"
|
131 |
+
f"- **Carbon Footprint**: {carbon:.2f} kg CO2e")
|
132 |
+
|
|
|
133 |
result_data = pd.DataFrame({
|
134 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
135 |
+
"Value": [water, energy, carbon]
|
136 |
})
|
137 |
+
fig = px.bar(result_data, x="Footprint Type", y="Value", title="Footprint Breakdown")
|
138 |
st.plotly_chart(fig)
|
139 |
else:
|
140 |
+
st.error("Failed to load dataset.")
|