Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import plotly.graph_objects as go
|
4 |
import time
|
5 |
|
@@ -13,6 +14,45 @@ st.markdown(
|
|
13 |
</p>
|
14 |
""", unsafe_allow_html=True)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# Sidebar for User Inputs
|
17 |
st.sidebar.header("Input Product Details")
|
18 |
|
@@ -41,27 +81,21 @@ use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
|
|
41 |
transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train", "Truck"])
|
42 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
"
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
}
|
60 |
-
|
61 |
-
# Washing and drying emission factors
|
62 |
-
washing_energy = {"Cold": 0.02, "30°C": 0.1, "40°C": 0.2, "60°C": 0.5} # Energy per cycle (MJ)
|
63 |
-
dryer_energy = 0.5 # Additional energy per drying cycle (MJ)
|
64 |
-
dryer_carbon = 0.3 # Additional carbon emissions per drying cycle (kgCO2e)
|
65 |
|
66 |
# Function to calculate footprints
|
67 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
@@ -72,11 +106,12 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
|
|
72 |
|
73 |
# Fiber contributions
|
74 |
for fiber, percentage in composition.items():
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
|
81 |
# Transportation impacts
|
82 |
transport_factor = transport_emissions_factors[lifecycle_inputs["transport_mode"]]
|
@@ -112,11 +147,11 @@ composition = {
|
|
112 |
|
113 |
# Run Calculations and Display Progress Bar
|
114 |
if total_percentage == 100:
|
115 |
-
|
116 |
progress_bar = st.progress(0)
|
117 |
for i in range(1, 101):
|
118 |
time.sleep(0.01)
|
119 |
-
progress_bar.progress(i
|
120 |
|
121 |
# Call the calculation function
|
122 |
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import requests
|
4 |
import plotly.graph_objects as go
|
5 |
import time
|
6 |
|
|
|
14 |
</p>
|
15 |
""", unsafe_allow_html=True)
|
16 |
|
17 |
+
# Google Drive Dataset Link
|
18 |
+
DATASET_URL = "https://drive.google.com/uc?id=1QY9yv2mhz4n8bOTi4ahbjBpapltqXV6D"
|
19 |
+
|
20 |
+
# Step 1: Function to fetch and load dataset
|
21 |
+
@st.cache_data
|
22 |
+
def fetch_and_load_dataset(url):
|
23 |
+
"""Fetch the dataset from Google Drive."""
|
24 |
+
progress_text = "Fetching dataset from Google Drive..."
|
25 |
+
progress_bar = st.progress(0)
|
26 |
+
|
27 |
+
# Download process
|
28 |
+
try:
|
29 |
+
response = requests.get(url, stream=True)
|
30 |
+
total_size = int(response.headers.get('content-length', 0))
|
31 |
+
downloaded_size = 0
|
32 |
+
chunks = []
|
33 |
+
|
34 |
+
for data in response.iter_content(chunk_size=8192):
|
35 |
+
downloaded_size += len(data)
|
36 |
+
progress_bar.progress(min(1.0, downloaded_size / total_size), text=progress_text)
|
37 |
+
chunks.append(data)
|
38 |
+
|
39 |
+
file_content = b''.join(chunks)
|
40 |
+
df = pd.read_excel(file_content, engine="openpyxl") # Assuming an Excel file
|
41 |
+
st.success("Dataset loaded successfully!")
|
42 |
+
return df
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"Error fetching dataset: {e}")
|
46 |
+
return None
|
47 |
+
|
48 |
+
# Load dataset dynamically from Google Drive
|
49 |
+
dataset = fetch_and_load_dataset(DATASET_URL)
|
50 |
+
|
51 |
+
# Display the dataset preview
|
52 |
+
if dataset is not None:
|
53 |
+
st.subheader("Dataset Preview")
|
54 |
+
st.dataframe(dataset.head())
|
55 |
+
|
56 |
# Sidebar for User Inputs
|
57 |
st.sidebar.header("Input Product Details")
|
58 |
|
|
|
81 |
transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train", "Truck"])
|
82 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
83 |
|
84 |
+
# Step 2: Extract data dynamically from the loaded dataset
|
85 |
+
@st.cache_data
|
86 |
+
def extract_fiber_impact(dataset):
|
87 |
+
"""Extract relevant data for fiber impacts from the dataset."""
|
88 |
+
fiber_data = {}
|
89 |
+
for _, row in dataset.iterrows():
|
90 |
+
fiber_name = row["Fiber"]
|
91 |
+
water = row["Water Footprint"]
|
92 |
+
energy = row["Energy Footprint"]
|
93 |
+
carbon = row["Carbon Footprint"]
|
94 |
+
fiber_data[fiber_name] = {"Water": water, "Energy": energy, "Carbon": carbon}
|
95 |
+
return fiber_data
|
96 |
+
|
97 |
+
# Load fiber impact data dynamically
|
98 |
+
fiber_impact_data = extract_fiber_impact(dataset)
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# Function to calculate footprints
|
101 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
|
|
106 |
|
107 |
# Fiber contributions
|
108 |
for fiber, percentage in composition.items():
|
109 |
+
if fiber in fiber_impact_data:
|
110 |
+
data = fiber_impact_data[fiber]
|
111 |
+
fraction = percentage / 100
|
112 |
+
water_footprint += data["Water"] * weight * fraction
|
113 |
+
energy_footprint += data["Energy"] * weight * fraction
|
114 |
+
carbon_footprint += data["Carbon"] * weight * fraction
|
115 |
|
116 |
# Transportation impacts
|
117 |
transport_factor = transport_emissions_factors[lifecycle_inputs["transport_mode"]]
|
|
|
147 |
|
148 |
# Run Calculations and Display Progress Bar
|
149 |
if total_percentage == 100:
|
150 |
+
st.write("Processing calculations...")
|
151 |
progress_bar = st.progress(0)
|
152 |
for i in range(1, 101):
|
153 |
time.sleep(0.01)
|
154 |
+
progress_bar.progress(i)
|
155 |
|
156 |
# Call the calculation function
|
157 |
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
|