ZainMalik0925 commited on
Commit
abd0a76
·
verified ·
1 Parent(s): a140149

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -2,14 +2,24 @@ import streamlit as 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:
@@ -18,13 +28,13 @@ def process_dataset(url):
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
 
@@ -58,23 +68,23 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
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 = {
@@ -82,19 +92,19 @@ def get_inputs(prefix):
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")
@@ -103,14 +113,20 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
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"),
@@ -122,19 +138,22 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
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.")
 
2
  import pandas as pd
3
  import plotly.express as px
4
 
5
+ # Set page configuration
6
+ st.set_page_config(page_title="GreenLens AI", layout="wide")
7
+
8
+ # Title and subtitle
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; font-size: 18px;'>
13
+ A Comprehensive Tool for Assessing Water, Energy, and Carbon Footprints of Textile Products 🌍
14
+ </p>
15
+ """,
16
+ unsafe_allow_html=True,
17
+ )
18
+
19
+ # Dataset URL
20
  DATASET_URL = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/DataSet01.xlsx"
21
 
22
+ # Load dataset from Hugging Face Spaces
 
23
  @st.cache_data
24
  def process_dataset(url):
25
  try:
 
28
  transport_data = pd.read_excel(excel_content, sheet_name="Transport Impact Data")
29
  washing_data = pd.read_excel(excel_content, sheet_name="Washing Data")
30
 
31
+ # Convert data to dictionaries for calculations
32
  fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
33
  transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
34
  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")
35
  return fiber_impact_data, transport_impact_data, washing_impact_data
36
  except Exception as e:
37
+ st.error(f"Error loading dataset: {e}")
38
  return None, None, None
39
 
40
 
 
68
 
69
  # Sidebar inputs
70
  def get_inputs(prefix):
71
+ weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.0, value=0.0, step=0.01, key=f"{prefix}_weight")
72
  st.sidebar.subheader(f"{prefix} Material Composition (%)")
73
+ cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 0, step=1, key=f"{prefix}_cotton")
74
+ polyester = st.sidebar.number_input("Polyester (%)", 0, 100, 0, step=1, key=f"{prefix}_polyester")
75
+ nylon = st.sidebar.number_input("Nylon 6 (%)", 0, 100, 0, step=1, key=f"{prefix}_nylon")
76
+ acrylic = st.sidebar.number_input("Acrylic (%)", 0, 100, 0, step=1, key=f"{prefix}_acrylic")
77
+ viscose = st.sidebar.number_input("Viscose (%)", 0, 100, 0, step=1, key=f"{prefix}_viscose")
78
 
79
  if cotton + polyester + nylon + acrylic + viscose != 100:
80
  st.sidebar.error("Fiber composition must sum to 100%!")
81
 
82
  lifecycle_inputs = {
83
+ "washing_cycles": st.sidebar.number_input(f"{prefix} Washing Cycles", min_value=0, value=0, key=f"{prefix}_wash_cycles"),
84
  "washing_temperature": st.sidebar.selectbox(f"{prefix} Washing Temperature", list(washing_impact_data.keys()), key=f"{prefix}_wash_temp"),
85
  "use_dryer": st.sidebar.checkbox(f"{prefix} Use Dryer?", key=f"{prefix}_use_dryer"),
86
  "transport_mode": st.sidebar.selectbox(f"{prefix} Transport Mode", list(transport_impact_data.keys()), key=f"{prefix}_transport_mode"),
87
+ "transport_distance": st.sidebar.number_input(f"{prefix} Transport Distance (km)", min_value=0, value=0, step=10, key=f"{prefix}_transport_distance"),
88
  }
89
 
90
  composition = {
 
92
  "Polyester": polyester,
93
  "Nylon 6": nylon,
94
  "Acrylic": acrylic,
95
+ "Viscose": viscose,
96
  }
97
  return weight, composition, lifecycle_inputs
98
 
99
 
100
+ # Main application logic
 
101
  fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
102
 
103
  if fiber_impact_data and transport_impact_data and washing_impact_data:
104
  comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
105
 
106
  if comparison_mode:
107
+ # Input for two assessments
108
  col1, col2 = st.columns(2)
109
  with col1:
110
  st.subheader("Assessment 1")
 
113
  st.subheader("Assessment 2")
114
  weight2, composition2, lifecycle2 = get_inputs("Assessment 2")
115
 
116
+ # Calculate footprints for both assessments
117
  water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
118
  water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
119
 
120
+ # Display numerical comparison
121
+ st.subheader("Numerical Comparison")
122
+ st.write(f"**Assessment 1**: Water: {water1:.2f} kL, Energy: {energy1:.2f} MJ, Carbon: {carbon1:.2f} kg CO2e")
123
+ st.write(f"**Assessment 2**: Water: {water2:.2f} kL, Energy: {energy2:.2f} MJ, Carbon: {carbon2:.2f} kg CO2e")
124
+
125
+ # Bar chart comparison
126
  comparison_data = pd.DataFrame({
127
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
128
  "Assessment 1": [water1, energy1, carbon1],
129
+ "Assessment 2": [water2, energy2, carbon2],
130
  })
131
  fig = px.bar(
132
  comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
 
138
  st.plotly_chart(fig)
139
 
140
  else:
141
+ # Input for a single assessment
142
  weight, composition, lifecycle = get_inputs("Single")
143
  water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
144
 
145
+ # Display results
146
+ st.subheader("Single Assessment Results")
147
+ st.markdown(f"- **Water Footprint**: {water:.2f} kL")
148
+ st.markdown(f"- **Energy Footprint**: {energy:.2f} MJ")
149
+ st.markdown(f"- **Carbon Footprint**: {carbon:.2f} kg CO2e")
150
 
151
+ # Bar chart for single assessment
152
  result_data = pd.DataFrame({
153
  "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
154
  "Value": [water, energy, carbon]
155
  })
156
+ fig = px.bar(result_data, x="Footprint Type", y="Value", title="Single Assessment Footprint Breakdown")
157
  st.plotly_chart(fig)
158
  else:
159
  st.error("Failed to load dataset.")