ZainMalik0925 commited on
Commit
723d673
·
verified ·
1 Parent(s): 972688e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -74
app.py CHANGED
@@ -2,29 +2,30 @@ 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
22
- )
23
 
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,18 +34,24 @@ def process_excel(file):
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,94 +59,116 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
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.")
 
2
  import pandas as pd
3
  import plotly.express as px
4
 
5
+ # Set page configurations
6
  st.set_page_config(page_title="GreenLens-AI", layout="wide")
7
 
8
+ # Page title and description
9
+ st.markdown("<h1 style='text-align: center; color: #4CAF50;'>GreenLens-AI</h1>", unsafe_allow_html=True)
10
+ st.markdown(
11
+ """
12
+ <p style='text-align: center; color: #4CAF50;'>
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 process_excel(file):
31
  try:
 
34
  transport_data = pd.read_excel(file, sheet_name="Transport Impact Data")
35
  washing_data = pd.read_excel(file, sheet_name="Washing Data")
36
 
37
+ # Convert into dictionaries for dynamic calculations
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 processing the file: {e}")
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
+ # Function to calculate footprints
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]
 
59
  water_fp += data["Water (L/kg)"] * weight * fraction
60
  energy_fp += data["Energy (MJ/kg)"] * weight * fraction
61
  carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
62
+
63
  if lifecycle_inputs["transport_mode"] in transport_impact_data:
64
  carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
65
+
66
  if lifecycle_inputs["washing_temperature"] in washing_impact_data:
67
  washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
68
  washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
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 footprint from liters to kiloliters for visualization
78
+ water_fp_kL = water_fp / 1000 # Convert liters to kiloliters
79
+ return water_fp_kL, energy_fp, carbon_fp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
+ # Sidebar inputs for all scenarios
82
+ def get_inputs(key_prefix):
83
+ 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")
84
+
85
+ st.sidebar.subheader(f"{key_prefix} - Material Composition (%)")
86
+ cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key=f"{key_prefix}_cotton")
87
+ polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key=f"{key_prefix}_polyester")
88
+ nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key=f"{key_prefix}_nylon")
89
+ acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_acrylic")
90
+ viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_viscose")
91
+
92
+ total_percentage = cotton + polyester + nylon + acrylic + viscose
93
+ if total_percentage != 100:
94
+ st.sidebar.error(f"Total composition for {key_prefix} must be 100%!")
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
+ # Main interface
122
+ if uploaded_file and fiber_impact_data and transport_impact_data and washing_impact_data:
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
+ product_weight_1, composition_1, lifecycle_inputs_1 = get_inputs("Assessment 1")
131
  with col2:
132
  st.subheader("Assessment 2")
133
+ product_weight_2, composition_2, lifecycle_inputs_2 = get_inputs("Assessment 2")
134
 
135
+ # Calculations for both assessments
136
+ water_fp_1, energy_fp_1, carbon_fp_1 = calculate_footprints(product_weight_1, composition_1, lifecycle_inputs_1)
137
+ water_fp_2, energy_fp_2, carbon_fp_2 = calculate_footprints(product_weight_2, composition_2, lifecycle_inputs_2)
138
 
139
+ # Combined visualization with line chart
140
+ st.subheader("Comparison of Assessments")
141
+ assessment_data = pd.DataFrame({
142
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
143
+ "Assessment 1": [water_fp_1, energy_fp_1, carbon_fp_1],
144
+ "Assessment 2": [water_fp_2, energy_fp_2, carbon_fp_2],
145
  })
146
+ fig = px.line(
147
+ assessment_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
148
+ x="Footprint Type",
149
+ y="Value",
150
+ color="Assessment",
151
+ markers=True,
152
+ title="Footprint Trends: Assessment 1 vs. Assessment 2"
153
+ )
154
  st.plotly_chart(fig)
155
  else:
156
+ # Input for single calculation
157
+ product_weight, composition, lifecycle_inputs = get_inputs("")
158
+ water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, lifecycle_inputs)
159
+
160
+ # Display results
161
  st.subheader("Results")
162
+ st.markdown(f"- **Water Footprint**: {water_fp:.2f} kL")
163
+ st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
164
+ st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kg CO2e")
165
+
166
+ # Visualization for single scenario
167
+ result_data = pd.DataFrame({
168
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
169
+ "Value": [water_fp, energy_fp, carbon_fp],
170
+ })
171
+ fig = px.line(result_data, x="Footprint Type", y="Value", markers=True, title="Footprint Trends")
172
  st.plotly_chart(fig)
173
  else:
174
  st.info("Please upload a dataset to proceed.")