ZainMalik0925 commited on
Commit
4572e8b
·
verified ·
1 Parent(s): 4d6edad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -118
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import requests
4
  from io import BytesIO
5
  import plotly.graph_objects as go
6
 
@@ -18,148 +17,163 @@ st.markdown(
18
  unsafe_allow_html=True,
19
  )
20
 
21
- # Google Drive Dataset Link
22
- DATASET_URL = "https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/23453236/59122b29-51ec-4aab-8b41-5301f001a94b/DataSet01.xlsx"
23
-
24
- # Function to fetch and process Excel dataset
25
  @st.cache_data
26
- def fetch_and_process_excel(url):
27
- """Fetch and extract data from the provided .xlsx file."""
28
  try:
29
- st.info("Fetching dataset from Google Drive...")
30
-
31
- # Download the Excel file
32
- response = requests.get(url)
33
- if response.status_code != 200:
34
- raise Exception("Failed to download the file. Check the URL or permissions.")
35
 
36
- # Load the Excel file into a BytesIO object
37
- excel_content = BytesIO(response.content)
 
 
 
 
38
 
39
- # Read all relevant sheets into pandas DataFrames
40
  fiber_data = pd.read_excel(excel_content, sheet_name="Fiber Impact Data")
41
  transport_data = pd.read_excel(excel_content, sheet_name="Transport Impact Data")
42
  washing_data = pd.read_excel(excel_content, sheet_name="Washing Data")
43
 
44
- # Verify required columns exist in all sheets
45
  if not {"Fiber Type", "Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"}.issubset(fiber_data.columns):
46
- raise Exception("Required columns missing in 'Fiber Impact Data'.")
47
  if not {"Transport Mode", "CFP (kg CO2e/km)"}.issubset(transport_data.columns):
48
- raise Exception("Required columns missing in 'Transport Impact Data'.")
49
  if not {"Washing Temperature", "Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"}.issubset(
50
  washing_data.columns
51
  ):
52
- raise Exception("Required columns missing in 'Washing Data'.")
53
 
54
- # Process Fiber Impact Data
55
  fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(
56
  orient="index"
57
  )
58
-
59
- # Process Transport Impact Data
60
  transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
61
-
62
- # Process Washing Data
63
  washing_impact_data = washing_data.set_index("Washing Temperature")[
64
  ["Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"]
65
  ].to_dict(orient="index")
66
 
67
- st.success("Dataset loaded successfully!")
68
  return fiber_impact_data, transport_impact_data, washing_impact_data
69
 
70
  except Exception as e:
71
- st.error(f"Error loading or processing the Excel file: {e}")
72
- return None, None, None
73
-
74
-
75
- # Load data dynamically from Google Drive
76
- fiber_impact_data, transport_impact_data, washing_impact_data = fetch_and_process_excel(DATASET_URL)
77
-
78
- # Sidebar for User Inputs
79
- st.sidebar.header("Input Product Details")
80
-
81
- product_type = st.sidebar.selectbox("Product Type", ["T-shirt", "Jeans", "Shirt", "Carpet"])
82
- product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.25)
83
-
84
- # Fiber Composition Input
85
- st.sidebar.subheader("Material Composition (%)")
86
- cotton_percent = st.sidebar.slider("Cotton (%)", 0, 100, 50)
87
- polyester_percent = st.sidebar.slider("Polyester (%)", 0, 100, 30)
88
- nylon_percent = st.sidebar.slider("Nylon (%)", 0, 100, 10)
89
- acrylic_percent = st.sidebar.slider("Acrylic (%)", 0, 100, 5)
90
- viscose_percent = st.sidebar.slider("Viscose (%)", 0, 100, 5)
91
-
92
- # Validation check: Percentages must add up to 100%
93
- total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
94
- if total_percentage != 100:
95
- st.sidebar.error("The total of all fiber percentages must equal 100%!")
96
-
97
- st.sidebar.write(f"Total Material Percentage: {total_percentage}%")
98
-
99
- # Lifecycle Inputs
100
- st.sidebar.header("Lifecycle Details")
101
- washing_cycles = st.sidebar.number_input("Number of Washing Cycles", min_value=0, step=10, value=30)
102
- washing_temperature = st.sidebar.selectbox("Washing Temperature", list(washing_impact_data.keys()) if washing_impact_data else [])
103
- use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
104
- transport_mode = st.sidebar.selectbox("Transport Mode", list(transport_impact_data.keys()) if transport_impact_data else [])
105
- transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
106
-
107
- # Function to calculate footprints
108
- def calculate_footprints(weight, composition, lifecycle_inputs):
109
- """Calculate water, energy, and carbon footprints."""
110
- try:
111
- # Initialize footprints
112
- water_footprint = 0
113
- energy_footprint = 0
114
- carbon_footprint = 0
115
-
116
- # Fiber contributions
117
- for fiber, percentage in composition.items():
118
- if fiber in fiber_impact_data:
119
- data = fiber_impact_data[fiber]
120
- fraction = percentage / 100
121
- water_footprint += data["Water (L/kg)"] * weight * fraction
122
- energy_footprint += data["Energy (MJ/kg)"] * weight * fraction
123
- carbon_footprint += data["Carbon (kg CO2e/kg)"] * weight * fraction
124
-
125
- # Transportation impacts
126
- transport_factor = transport_impact_data.get(lifecycle_inputs["transport_mode"], 0)
127
- carbon_footprint += transport_factor * lifecycle_inputs["transport_distance"] * weight
128
-
129
- # Washing and drying impacts
130
- washing_data = washing_impact_data.get(lifecycle_inputs["washing_temperature"], {})
131
- washing_energy = washing_data.get("Energy Use (MJ/wash)", 0) * lifecycle_inputs["washing_cycles"]
132
- washing_carbon = washing_data.get("Carbon (kg CO2e/wash)", 0) * lifecycle_inputs["washing_cycles"]
133
- dryer_carbon = washing_data.get("Dryer CFP (kg CO2e/cycle)", 0) if lifecycle_inputs["use_dryer"] else 0
134
-
135
- energy_footprint += washing_energy
136
- carbon_footprint += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
137
-
138
- return water_footprint, energy_footprint, carbon_footprint
139
-
140
- except Exception as e:
141
- st.error(f"Error in calculations: {e}")
142
  return None, None, None
143
 
144
 
145
- # User inputs as a dictionary
146
- user_inputs = {
147
- "transport_mode": transport_mode,
148
- "transport_distance": transport_distance,
149
- "washing_temperature": washing_temperature,
150
- "washing_cycles": washing_cycles,
151
- "use_dryer": use_dryer,
152
- }
153
-
154
- # Collect the composition dictionary
155
- composition = {
156
- "Cotton": cotton_percent,
157
- "Polyester": polyester_percent,
158
- "Nylon": nylon_percent,
159
- "Acrylic": acrylic_percent,
160
- "Viscose": viscose_percent,
161
- }
162
-
163
- # Run Calculations if conditions are met
164
- if fiber_impact_data and total_percentage == 100:
165
- water_fp, energy_fp, carb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
 
3
  from io import BytesIO
4
  import plotly.graph_objects as go
5
 
 
17
  unsafe_allow_html=True,
18
  )
19
 
20
+ # Function to process Excel dataset
 
 
 
21
  @st.cache_data
22
+ def process_excel(file):
23
+ """Process data from the uploaded .xlsx file."""
24
  try:
25
+ # Load Excel file
26
+ excel_content = BytesIO(file.read())
 
 
 
 
27
 
28
+ # Verify required sheets exist
29
+ sheets = pd.ExcelFile(excel_content).sheet_names
30
+ required_sheets = ["Fiber Impact Data", "Transport Impact Data", "Washing Data"]
31
+ for sheet in required_sheets:
32
+ if sheet not in sheets:
33
+ raise Exception(f"Required sheet '{sheet}' is missing from the Excel file.")
34
 
35
+ # Read data from Excel sheets
36
  fiber_data = pd.read_excel(excel_content, sheet_name="Fiber Impact Data")
37
  transport_data = pd.read_excel(excel_content, sheet_name="Transport Impact Data")
38
  washing_data = pd.read_excel(excel_content, sheet_name="Washing Data")
39
 
40
+ # Validate required columns in each sheet
41
  if not {"Fiber Type", "Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"}.issubset(fiber_data.columns):
42
+ raise Exception("Required columns are missing in 'Fiber Impact Data' sheet.")
43
  if not {"Transport Mode", "CFP (kg CO2e/km)"}.issubset(transport_data.columns):
44
+ raise Exception("Required columns are missing in 'Transport Impact Data' sheet.")
45
  if not {"Washing Temperature", "Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"}.issubset(
46
  washing_data.columns
47
  ):
48
+ raise Exception("Required columns are missing in 'Washing Data' sheet.")
49
 
50
+ # Convert data into dictionaries for calculations
51
  fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(
52
  orient="index"
53
  )
 
 
54
  transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
 
 
55
  washing_impact_data = washing_data.set_index("Washing Temperature")[
56
  ["Energy Use (MJ/wash)", "Carbon (kg CO2e/wash)", "Dryer CFP (kg CO2e/cycle)"]
57
  ].to_dict(orient="index")
58
 
59
+ st.success("File successfully uploaded and processed!")
60
  return fiber_impact_data, transport_impact_data, washing_impact_data
61
 
62
  except Exception as e:
63
+ st.error(f"Error processing the Excel file: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  return None, None, None
65
 
66
 
67
+ # File uploader in Streamlit sidebar
68
+ uploaded_file = st.sidebar.file_uploader("Upload Dataset to Initiate", type=["xlsx"])
69
+
70
+ if uploaded_file is not None:
71
+ # Process the uploaded file
72
+ fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
73
+
74
+ # Sidebar for User Inputs
75
+ st.sidebar.header("Input Product Details")
76
+
77
+ product_type = st.sidebar.selectbox("Product Type", ["T-shirt", "Jeans", "Shirt", "Carpet"])
78
+ product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.25)
79
+
80
+ # Fiber Composition Input
81
+ st.sidebar.subheader("Material Composition (%)")
82
+ cotton_percent = st.sidebar.slider("Cotton (%)", 0, 100, 100)
83
+ polyester_percent = st.sidebar.slider("Polyester (%)", 0, 100, 0)
84
+ nylon_percent = st.sidebar.slider("Nylon (%)", 0, 100, 0)
85
+ acrylic_percent = st.sidebar.slider("Acrylic (%)", 0, 100, 0)
86
+ viscose_percent = st.sidebar.slider("Viscose (%)", 0, 100, 0)
87
+
88
+ # Validate composition percentages
89
+ total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
90
+ if total_percentage != 100:
91
+ st.sidebar.error("The total of all fiber percentages must equal 100%!")
92
+ st.sidebar.write(f"Total Material Percentage: {total_percentage}%")
93
+
94
+ # Lifecycle Inputs
95
+ st.sidebar.header("Lifecycle Details")
96
+ washing_cycles = st.sidebar.number_input("Number of Washing Cycles", min_value=0, step=10, value=30)
97
+ washing_temperature = st.sidebar.selectbox("Washing Temperature", list(washing_impact_data.keys()) if washing_impact_data else [])
98
+ use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
99
+ transport_mode = st.sidebar.selectbox("Transport Mode", list(transport_impact_data.keys()) if transport_impact_data else [])
100
+ transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
101
+
102
+ # Function to calculate footprints
103
+ def calculate_footprints(weight, composition, lifecycle_inputs):
104
+ """Calculate water, energy, and carbon footprints."""
105
+ try:
106
+ # Initialize footprints
107
+ water_footprint = 0
108
+ energy_footprint = 0
109
+ carbon_footprint = 0
110
+
111
+ # Fiber contributions
112
+ for fiber, percentage in composition.items():
113
+ if fiber in fiber_impact_data:
114
+ data = fiber_impact_data[fiber]
115
+ fraction = percentage / 100
116
+ water_footprint += data["Water (L/kg)"] * weight * fraction
117
+ energy_footprint += data["Energy (MJ/kg)"] * weight * fraction
118
+ carbon_footprint += data["Carbon (kg CO2e/kg)"] * weight * fraction
119
+
120
+ # Transportation impacts
121
+ transport_factor = transport_impact_data.get(lifecycle_inputs["transport_mode"], 0)
122
+ carbon_footprint += transport_factor * lifecycle_inputs["transport_distance"] * weight
123
+
124
+ # Washing and drying impacts
125
+ washing_data = washing_impact_data.get(lifecycle_inputs["washing_temperature"], {})
126
+ washing_energy = washing_data.get("Energy Use (MJ/wash)", 0) * lifecycle_inputs["washing_cycles"]
127
+ washing_carbon = washing_data.get("Carbon (kg CO2e/wash)", 0) * lifecycle_inputs["washing_cycles"]
128
+ dryer_carbon = washing_data.get("Dryer CFP (kg CO2e/cycle)", 0) if lifecycle_inputs["use_dryer"] else 0
129
+
130
+ energy_footprint += washing_energy
131
+ carbon_footprint += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
132
+
133
+ return water_footprint, energy_footprint, carbon_footprint
134
+
135
+ except Exception as e:
136
+ st.error(f"Error in calculations: {e}")
137
+ return None, None, None
138
+
139
+ # Composition dictionary
140
+ composition = {
141
+ "Cotton": cotton_percent,
142
+ "Polyester": polyester_percent,
143
+ "Nylon": nylon_percent,
144
+ "Acrylic": acrylic_percent,
145
+ "Viscose": viscose_percent,
146
+ }
147
+
148
+ # Perform calculations
149
+ if total_percentage == 100:
150
+ water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, {
151
+ "transport_mode": transport_mode,
152
+ "transport_distance": transport_distance,
153
+ "washing_temperature": washing_temperature,
154
+ "washing_cycles": washing_cycles,
155
+ "use_dryer": use_dryer,
156
+ })
157
+
158
+ if water_fp is not None:
159
+ # Display results
160
+ st.subheader("Calculated Results")
161
+ st.markdown(f"""
162
+ - **Water Footprint**: {water_fp:.2f} liters
163
+ - **Energy Footprint**: {energy_fp:.2f} MJ
164
+ - **Carbon Footprint**: {carbon_fp:.2f} kgCO2e
165
+ """)
166
+
167
+ # Visualization
168
+ fig = go.Figure()
169
+ fig.add_trace(go.Bar(
170
+ x=["Water Footprint", "Energy Footprint", "Carbon Footprint"],
171
+ y=[water_fp, energy_fp, carbon_fp],
172
+ text=[f"{water_fp:.2f} L", f"{energy_fp:.2f} MJ", f"{carbon_fp:.2f} kgCO2e"],
173
+ textposition="auto",
174
+ marker=dict(color=["blue", "orange", "green"])
175
+ ))
176
+ fig.update_layout(title="Footprint Breakdown", xaxis_title="Footprint Type", yaxis_title="Value")
177
+ st.plotly_chart(fig)
178
+ else:
179
+ st.info("Please upload a valid Dataset to Begin")