ZainMalik0925 commited on
Commit
972688e
·
verified ·
1 Parent(s): 01f8a8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -115
app.py CHANGED
@@ -1,19 +1,21 @@
1
  import streamlit as st
 
 
2
 
3
- # Set page configurations
4
  st.set_page_config(page_title="GreenLens-AI", layout="wide")
5
 
6
  # Add custom CSS for the background image
7
  def set_background():
8
  st.markdown(
9
- f"""
10
  <style>
11
- .stApp {{
12
  background: url("https://drive.google.com/uc?id=1UTGAcHDwFs-JFjG163FTOhusLf1sDMK3");
13
  background-size: cover;
14
  background-position: center;
15
  background-attachment: fixed;
16
- }}
17
  </style>
18
  """,
19
  unsafe_allow_html=True
@@ -22,34 +24,7 @@ def set_background():
22
  # Set the background
23
  set_background()
24
 
25
- import streamlit as st
26
- import pandas as pd
27
- import plotly.express as px
28
-
29
- # Set page configurations
30
- st.set_page_config(page_title="GreenLens-AI", layout="wide")
31
-
32
- # Page title and description
33
- st.markdown("<h1 style='text-align: center; color: #4CAF50;'>GreenLens-AI</h1>", unsafe_allow_html=True)
34
- st.markdown(
35
- """
36
- <p style='text-align: center; color: #4CAF50;'>
37
- A Tool for Calculating Water, Energy, and Carbon Footprints of Textile Products 🌍
38
- </p>
39
- """,
40
- unsafe_allow_html=True,
41
- )
42
-
43
- # Sidebar for file upload
44
- st.sidebar.header("Step 1: Upload Dataset")
45
- uploaded_file = st.sidebar.file_uploader("Upload your Excel file (.xlsx)", type=["xlsx"])
46
-
47
- # Initialize data containers
48
- fiber_impact_data = None
49
- transport_impact_data = None
50
- washing_impact_data = None
51
-
52
- # Function to process the uploaded Excel file
53
  @st.cache_data
54
  def process_excel(file):
55
  try:
@@ -58,24 +33,18 @@ def process_excel(file):
58
  transport_data = pd.read_excel(file, sheet_name="Transport Impact Data")
59
  washing_data = pd.read_excel(file, sheet_name="Washing Data")
60
 
61
- # Convert into dictionaries for dynamic calculations
62
  fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
63
  transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
64
  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")
65
-
66
  return fiber_impact_data, transport_impact_data, washing_impact_data
67
  except Exception as e:
68
- st.error(f"Error processing the file: {e}")
69
  return None, None, None
70
 
71
- # Process uploaded file
72
- if uploaded_file:
73
- fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
74
-
75
- # Function to calculate footprints
76
  def calculate_footprints(weight, composition, lifecycle_inputs):
77
  water_fp, energy_fp, carbon_fp = 0, 0, 0
78
-
79
  for fiber, percentage in composition.items():
80
  if fiber in fiber_impact_data:
81
  data = fiber_impact_data[fiber]
@@ -83,116 +52,94 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
83
  water_fp += data["Water (L/kg)"] * weight * fraction
84
  energy_fp += data["Energy (MJ/kg)"] * weight * fraction
85
  carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
86
-
87
  if lifecycle_inputs["transport_mode"] in transport_impact_data:
88
  carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
89
-
90
  if lifecycle_inputs["washing_temperature"] in washing_impact_data:
91
  washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
92
  washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
93
  washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
94
  washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
95
  dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
96
-
97
  water_fp += washing_water
98
  energy_fp += washing_energy
99
  carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
100
-
101
- # Convert water footprint from liters to kiloliters for visualization
102
- water_fp_kL = water_fp / 1000 # Convert liters to kiloliters
103
- return water_fp_kL, energy_fp, carbon_fp
104
-
105
- # Sidebar inputs for all scenarios
106
- def get_inputs(key_prefix):
107
- product_weight = st.sidebar.number_input(f"{key_prefix} - Product Weight (kg)", min_value=0.01, step=0.01, value=0.5, key=f"{key_prefix}_weight")
108
 
109
- st.sidebar.subheader(f"{key_prefix} - Material Composition (%)")
110
- cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key=f"{key_prefix}_cotton")
111
- polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key=f"{key_prefix}_polyester")
112
- nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key=f"{key_prefix}_nylon")
113
- acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_acrylic")
114
- viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_viscose")
115
-
116
- total_percentage = cotton + polyester + nylon + acrylic + viscose
117
- if total_percentage != 100:
118
- st.sidebar.error(f"Total composition for {key_prefix} must be 100%!")
 
 
119
 
 
 
 
 
 
 
 
 
 
 
 
120
  composition = {
121
  "Conventional Cotton": cotton,
122
  "Polyester": polyester,
123
  "Nylon 6": nylon,
124
  "Acrylic": acrylic,
125
- "Viscose": viscose,
126
  }
127
-
128
- st.sidebar.subheader(f"{key_prefix} - Lifecycle Inputs")
129
- washing_cycles = st.sidebar.number_input(f"{key_prefix} - Washing Cycles", min_value=0, step=1, value=30, key=f"{key_prefix}_wash_cycles")
130
- washing_temperature = st.sidebar.selectbox(f"{key_prefix} - Washing Temperature", list(washing_impact_data.keys()), key=f"{key_prefix}_wash_temp")
131
- use_dryer = st.sidebar.checkbox(f"{key_prefix} - Use Tumble Dryer?", key=f"{key_prefix}_use_dryer")
132
- transport_mode = st.sidebar.selectbox(f"{key_prefix} - Transport Mode", list(transport_impact_data.keys()), key=f"{key_prefix}_transport_mode")
133
- 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")
134
-
135
- lifecycle_inputs = {
136
- "washing_temperature": washing_temperature,
137
- "washing_cycles": washing_cycles,
138
- "use_dryer": use_dryer,
139
- "transport_mode": transport_mode,
140
- "transport_distance": transport_distance,
141
- }
142
-
143
- return product_weight, composition, lifecycle_inputs
144
 
145
- # Main interface
146
- if uploaded_file and fiber_impact_data and transport_impact_data and washing_impact_data:
147
  comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
148
 
149
  if comparison_mode:
150
- # Input for two assessments
151
  col1, col2 = st.columns(2)
152
  with col1:
153
  st.subheader("Assessment 1")
154
- product_weight_1, composition_1, lifecycle_inputs_1 = get_inputs("Assessment 1")
155
  with col2:
156
  st.subheader("Assessment 2")
157
- product_weight_2, composition_2, lifecycle_inputs_2 = get_inputs("Assessment 2")
158
 
159
- # Calculations for both assessments
160
- water_fp_1, energy_fp_1, carbon_fp_1 = calculate_footprints(product_weight_1, composition_1, lifecycle_inputs_1)
161
- water_fp_2, energy_fp_2, carbon_fp_2 = calculate_footprints(product_weight_2, composition_2, lifecycle_inputs_2)
162
 
163
- # Combined visualization with line chart
164
- st.subheader("Comparison of Assessments")
165
- assessment_data = pd.DataFrame({
166
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
167
- "Assessment 1": [water_fp_1, energy_fp_1, carbon_fp_1],
168
- "Assessment 2": [water_fp_2, energy_fp_2, carbon_fp_2],
169
  })
170
- fig = px.line(
171
- assessment_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
172
- x="Footprint Type",
173
- y="Value",
174
- color="Assessment",
175
- markers=True,
176
- title="Footprint Trends: Assessment 1 vs. Assessment 2"
177
- )
178
  st.plotly_chart(fig)
179
  else:
180
- # Input for single calculation
181
- product_weight, composition, lifecycle_inputs = get_inputs("")
182
- water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, lifecycle_inputs)
183
-
184
- # Display results
185
  st.subheader("Results")
186
- st.markdown(f"- **Water Footprint**: {water_fp:.2f} kL")
187
- st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
188
- st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kg CO2e")
189
-
190
- # Visualization for single scenario
191
- result_data = pd.DataFrame({
192
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
193
- "Value": [water_fp, energy_fp, carbon_fp],
194
- })
195
- fig = px.line(result_data, x="Footprint Type", y="Value", markers=True, title="Footprint Trends")
196
  st.plotly_chart(fig)
197
  else:
198
  st.info("Please upload a dataset to proceed.")
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import plotly.express as px
4
 
5
+ # Set page configurations (must be the first Streamlit command)
6
  st.set_page_config(page_title="GreenLens-AI", layout="wide")
7
 
8
  # Add custom CSS for the background image
9
  def set_background():
10
  st.markdown(
11
+ """
12
  <style>
13
+ .stApp {
14
  background: url("https://drive.google.com/uc?id=1UTGAcHDwFs-JFjG163FTOhusLf1sDMK3");
15
  background-size: cover;
16
  background-position: center;
17
  background-attachment: fixed;
18
+ }
19
  </style>
20
  """,
21
  unsafe_allow_html=True
 
24
  # Set the background
25
  set_background()
26
 
27
+ # Process uploaded Excel file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @st.cache_data
29
  def process_excel(file):
30
  try:
 
33
  transport_data = pd.read_excel(file, sheet_name="Transport Impact Data")
34
  washing_data = pd.read_excel(file, sheet_name="Washing Data")
35
 
36
+ # Convert data into dictionaries for calculations
37
  fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
38
  transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
39
  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")
 
40
  return fiber_impact_data, transport_impact_data, washing_impact_data
41
  except Exception as e:
42
+ st.error(f"Error processing file: {e}")
43
  return None, None, None
44
 
45
+ # Calculate footprints
 
 
 
 
46
  def calculate_footprints(weight, composition, lifecycle_inputs):
47
  water_fp, energy_fp, carbon_fp = 0, 0, 0
 
48
  for fiber, percentage in composition.items():
49
  if fiber in fiber_impact_data:
50
  data = fiber_impact_data[fiber]
 
52
  water_fp += data["Water (L/kg)"] * weight * fraction
53
  energy_fp += data["Energy (MJ/kg)"] * weight * fraction
54
  carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
55
+
56
  if lifecycle_inputs["transport_mode"] in transport_impact_data:
57
  carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
58
+
59
  if lifecycle_inputs["washing_temperature"] in washing_impact_data:
60
  washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
61
  washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
62
  washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
63
  washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
64
  dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
 
65
  water_fp += washing_water
66
  energy_fp += washing_energy
67
  carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
 
 
 
 
 
 
 
 
68
 
69
+ water_fp /= 1000 # Convert water from liters to kiloliters
70
+ return water_fp, energy_fp, carbon_fp
71
+
72
+ # Sidebar inputs
73
+ def get_inputs(prefix):
74
+ weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.01, value=0.5, step=0.01, key=f"{prefix}_weight")
75
+ st.sidebar.subheader(f"{prefix} Material Composition (%)")
76
+ cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 50, step=1, key=f"{prefix}_cotton")
77
+ polyester = st.sidebar.number_input("Polyester (%)", 0, 100, 30, step=1, key=f"{prefix}_polyester")
78
+ nylon = st.sidebar.number_input("Nylon 6 (%)", 0, 100, 10, step=1, key=f"{prefix}_nylon")
79
+ acrylic = st.sidebar.number_input("Acrylic (%)", 0, 100, 5, step=1, key=f"{prefix}_acrylic")
80
+ viscose = st.sidebar.number_input("Viscose (%)", 0, 100, 5, step=1, key=f"{prefix}_viscose")
81
 
82
+ if cotton + polyester + nylon + acrylic + viscose != 100:
83
+ st.sidebar.error("Fiber composition must sum to 100%!")
84
+
85
+ lifecycle_inputs = {
86
+ "washing_cycles": st.sidebar.number_input(f"{prefix} Washing Cycles", min_value=0, value=30, key=f"{prefix}_wash_cycles"),
87
+ "washing_temperature": st.sidebar.selectbox(f"{prefix} Washing Temperature", list(washing_impact_data.keys()), key=f"{prefix}_wash_temp"),
88
+ "use_dryer": st.sidebar.checkbox(f"{prefix} Use Dryer?", key=f"{prefix}_use_dryer"),
89
+ "transport_mode": st.sidebar.selectbox(f"{prefix} Transport Mode", list(transport_impact_data.keys()), key=f"{prefix}_transport_mode"),
90
+ "transport_distance": st.sidebar.number_input(f"{prefix} Transport Distance (km)", min_value=0, value=100, step=10, key=f"{prefix}_transport_distance")
91
+ }
92
+
93
  composition = {
94
  "Conventional Cotton": cotton,
95
  "Polyester": polyester,
96
  "Nylon 6": nylon,
97
  "Acrylic": acrylic,
98
+ "Viscose": viscose
99
  }
100
+ return weight, composition, lifecycle_inputs
101
+
102
+ # Main App Logic
103
+ st.sidebar.header("Step 1: Upload Dataset")
104
+ uploaded_file = st.sidebar.file_uploader("Upload your Excel file (.xlsx)", type=["xlsx"])
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ if uploaded_file:
107
+ fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
108
  comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
109
 
110
  if comparison_mode:
 
111
  col1, col2 = st.columns(2)
112
  with col1:
113
  st.subheader("Assessment 1")
114
+ weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
115
  with col2:
116
  st.subheader("Assessment 2")
117
+ weight2, composition2, lifecycle2 = get_inputs("Assessment 2")
118
 
119
+ water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
120
+ water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
 
121
 
122
+ st.subheader("Comparison")
123
+ df = pd.DataFrame({
 
124
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
125
+ "Assessment 1": [water1, energy1, carbon1],
126
+ "Assessment 2": [water2, energy2, carbon2],
127
  })
128
+ fig = px.line(df.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
129
+ x="Footprint Type", y="Value", color="Assessment", markers=True,
130
+ title="Comparison of Assessments")
 
 
 
 
 
131
  st.plotly_chart(fig)
132
  else:
133
+ weight, composition, lifecycle = get_inputs("Single")
134
+ water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
 
 
 
135
  st.subheader("Results")
136
+ st.markdown(f"- **Water Footprint**: {water:.2f} kL\n"
137
+ f"- **Energy Footprint**: {energy:.2f} MJ\n"
138
+ f"- **Carbon Footprint**: {carbon:.2f} kg CO2e")
139
+ fig = px.line(pd.DataFrame({
 
 
140
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
141
+ "Value": [water, energy, carbon]
142
+ }), x="Footprint Type", y="Value", markers=True, title="Footprint Trends")
 
143
  st.plotly_chart(fig)
144
  else:
145
  st.info("Please upload a dataset to proceed.")