Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,198 +1,72 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import requests
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
DATASET_URL = "https://drive.google.com/file/d/1JMECXBOPU5UD9hdEUA0uv1g_Qm1CkWYn/view?usp=drive_link"
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
if response.status_code != 200:
|
34 |
-
raise Exception("Failed to download the file. Check the URL or permissions.")
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
st.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
}
|
62 |
-
except ValueError:
|
63 |
-
st.warning(f"Invalid data format in line: {line}")
|
64 |
-
continue
|
65 |
-
|
66 |
-
# Log parsed dataset
|
67 |
-
st.write("Parsed Fiber Impact Data:", fiber_impact_data)
|
68 |
-
return fiber_impact_data
|
69 |
-
|
70 |
-
except Exception as e:
|
71 |
-
st.error(f"Error loading or processing the PDF: {e}")
|
72 |
-
return None
|
73 |
-
|
74 |
-
# Load dataset dynamically
|
75 |
-
fiber_impact_data = fetch_and_process_pdf(DATASET_URL)
|
76 |
-
|
77 |
-
# Sidebar for User Inputs
|
78 |
-
st.sidebar.header("Input Product Details")
|
79 |
-
|
80 |
-
# Input Section: Product-Specific Inputs
|
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}%") # Debugging sidebar inputs
|
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", ["Cold", "30°C", "40°C", "60°C"])
|
103 |
-
use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
|
104 |
-
transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train", "Truck"])
|
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 |
-
# Log inputs
|
112 |
-
st.write("Inputs to function:", weight, composition, lifecycle_inputs)
|
113 |
-
|
114 |
-
# Initialize footprints
|
115 |
-
water_footprint = 0
|
116 |
-
energy_footprint = 0
|
117 |
-
carbon_footprint = 0
|
118 |
-
|
119 |
-
# Fiber contributions
|
120 |
-
for fiber, percentage in composition.items():
|
121 |
-
if fiber in fiber_impact_data:
|
122 |
-
data = fiber_impact_data[fiber]
|
123 |
-
fraction = percentage / 100
|
124 |
-
water_footprint += data["Water"] * weight * fraction
|
125 |
-
energy_footprint += data["Energy"] * weight * fraction
|
126 |
-
carbon_footprint += data["Carbon"] * weight * fraction
|
127 |
-
else:
|
128 |
-
st.warning(f"No data found for fiber: {fiber}")
|
129 |
-
|
130 |
-
# Transportation impacts
|
131 |
-
transport_factor = {
|
132 |
-
"Plane": 1.102,
|
133 |
-
"Ship": 0.011,
|
134 |
-
"Train": 0.05,
|
135 |
-
"Truck": 0.25,
|
136 |
-
}.get(lifecycle_inputs["transport_mode"], 0)
|
137 |
-
carbon_footprint += transport_factor * lifecycle_inputs["transport_distance"] * weight
|
138 |
-
|
139 |
-
# Washing and drying impacts
|
140 |
-
washing_energy = {"Cold": 0.02, "30°C": 0.1, "40°C": 0.2, "60°C": 0.5}
|
141 |
-
dryer_energy = 0.5 if lifecycle_inputs["use_dryer"] else 0
|
142 |
-
carbon_footprint += (
|
143 |
-
washing_energy[lifecycle_inputs["washing_temperature"]] * lifecycle_inputs["washing_cycles"] * 0.05
|
144 |
-
)
|
145 |
-
energy_footprint += dryer_energy * lifecycle_inputs["washing_cycles"]
|
146 |
-
|
147 |
-
# Log final results
|
148 |
-
st.write("Calculated Results:", water_footprint, energy_footprint, carbon_footprint)
|
149 |
-
return water_footprint, energy_footprint, carbon_footprint
|
150 |
-
|
151 |
-
except Exception as e:
|
152 |
-
st.error(f"Error in calculations: {e}")
|
153 |
-
return None, None, None
|
154 |
-
|
155 |
-
# User inputs as a dictionary
|
156 |
-
user_inputs = {
|
157 |
-
"transport_mode": transport_mode,
|
158 |
-
"transport_distance": transport_distance,
|
159 |
-
"washing_temperature": washing_temperature,
|
160 |
-
"washing_cycles": washing_cycles,
|
161 |
-
"use_dryer": use_dryer,
|
162 |
-
}
|
163 |
-
|
164 |
-
# Collect the composition dictionary
|
165 |
-
composition = {
|
166 |
-
"Cotton": cotton_percent,
|
167 |
-
"Polyester": polyester_percent,
|
168 |
-
"Nylon": nylon_percent,
|
169 |
-
"Acrylic": acrylic_percent,
|
170 |
-
"Viscose": viscose_percent,
|
171 |
-
}
|
172 |
-
|
173 |
-
# Run Calculations if conditions are met
|
174 |
-
if fiber_impact_data and total_percentage == 100:
|
175 |
-
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
|
176 |
-
|
177 |
-
if water_fp is not None:
|
178 |
-
# Display results
|
179 |
-
st.subheader("Calculated Results")
|
180 |
-
st.markdown(f"""
|
181 |
-
- **Water Footprint**: {water_fp:.2f} liters
|
182 |
-
- **Energy Footprint**: {energy_fp:.2f} MJ
|
183 |
-
- **Carbon Footprint**: {carbon_fp:.2f} kgCO2e
|
184 |
-
""")
|
185 |
-
|
186 |
-
# Visualization
|
187 |
-
fig = go.Figure()
|
188 |
-
fig.add_trace(go.Bar(
|
189 |
-
x=["Water Footprint", "Energy Footprint", "Carbon Footprint"],
|
190 |
-
y=[water_fp, energy_fp, carbon_fp],
|
191 |
-
text=[f"{water_fp:.2f} L", f"{energy_fp:.2f} MJ", f"{carbon_fp:.2f} kgCO2e"],
|
192 |
-
textposition="auto",
|
193 |
-
marker=dict(color=["blue", "orange", "green"])
|
194 |
-
))
|
195 |
-
fig.update_layout(title="Footprint Breakdown", xaxis_title="Footprint Type", yaxis_title="Value")
|
196 |
-
st.plotly_chart(fig)
|
197 |
-
else:
|
198 |
-
st.error("Ensure dataset is loaded and the composition sums to 100%.")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import requests
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Function to load the data from the Google Sheets link
|
7 |
+
def load_data_from_google_sheets(sheet_url):
|
8 |
+
sheet_id = sheet_url.split('/d/')[1].split('/')[0]
|
9 |
+
export_url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=xlsx"
|
10 |
+
|
11 |
+
# Downloading the sheet content
|
12 |
+
response = requests.get(export_url)
|
13 |
+
|
14 |
+
if response.status_code == 200:
|
15 |
+
file = io.BytesIO(response.content)
|
16 |
+
data = pd.read_excel(file)
|
17 |
+
return data
|
18 |
+
else:
|
19 |
+
st.error("Failed to load the dataset. Please check the link.")
|
20 |
+
return None
|
|
|
21 |
|
22 |
+
# Function to calculate CFP, Energy Footprints, Water Footprints, and Ecological Footprints
|
23 |
+
def calculate_footprints(data, user_inputs):
|
24 |
+
# Logic for calculation based on user inputs and loaded dataset
|
25 |
+
# For the sake of example, let's assume you are calculating the CFP for the user's input
|
26 |
+
# Here, we can write the necessary calculations as per the LCA logic
|
27 |
+
cfp = user_inputs['carbon_emission'] * data['emission_factor'].mean() # Example calculation
|
28 |
+
energy_fp = user_inputs['energy_consumed'] * data['energy_factor'].mean()
|
29 |
+
water_fp = user_inputs['water_used'] * data['water_factor'].mean()
|
30 |
+
ecological_fp = user_inputs['ecological_impact'] * data['ecological_factor'].mean()
|
31 |
+
|
32 |
+
return cfp, energy_fp, water_fp, ecological_fp
|
33 |
+
|
34 |
+
# Streamlit App Logic
|
35 |
+
def main():
|
36 |
+
st.title("Life Cycle Assessment (LCA) Calculator")
|
37 |
+
|
38 |
+
# Load the dataset from the provided link
|
39 |
+
sheet_url = "https://docs.google.com/spreadsheets/d/1CfuCrqRvu1LjBVmJh9b7mZiHBJ2kvJ0t/edit?usp=drive_link&ouid=108672166866225546443&rtpof=true&sd=true"
|
40 |
+
data = load_data_from_google_sheets(sheet_url)
|
41 |
+
|
42 |
+
if data is not None:
|
43 |
+
st.write("Dataset loaded successfully.")
|
44 |
|
45 |
+
# User Input for LCA variables
|
46 |
+
st.subheader("Input Parameters")
|
|
|
|
|
47 |
|
48 |
+
carbon_emission = st.number_input("Carbon Emission (kg CO2)", min_value=0.0)
|
49 |
+
energy_consumed = st.number_input("Energy Consumed (kWh)", min_value=0.0)
|
50 |
+
water_used = st.number_input("Water Used (liters)", min_value=0.0)
|
51 |
+
ecological_impact = st.number_input("Ecological Impact (units)", min_value=0.0)
|
52 |
+
|
53 |
+
user_inputs = {
|
54 |
+
"carbon_emission": carbon_emission,
|
55 |
+
"energy_consumed": energy_consumed,
|
56 |
+
"water_used": water_used,
|
57 |
+
"ecological_impact": ecological_impact
|
58 |
+
}
|
59 |
+
|
60 |
+
if st.button("Calculate LCA"):
|
61 |
+
# Perform LCA calculations
|
62 |
+
cfp, energy_fp, water_fp, ecological_fp = calculate_footprints(data, user_inputs)
|
63 |
+
|
64 |
+
# Display results
|
65 |
+
st.subheader("Results")
|
66 |
+
st.write(f"Total Carbon Footprint (CFP): {cfp} kg CO2")
|
67 |
+
st.write(f"Total Energy Footprint: {energy_fp} kWh")
|
68 |
+
st.write(f"Total Water Footprint: {water_fp} liters")
|
69 |
+
st.write(f"Total Ecological Footprint: {ecological_fp} units")
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|