Spaces:
Sleeping
Sleeping
File size: 9,250 Bytes
01f8a8c 972688e 01f8a8c e7b455f f7d942c e7b455f f7d942c ed6c776 e7b455f ed6c776 f7d942c abd0a76 f7d942c a68b099 bc1fd3e abd0a76 bc1fd3e abd0a76 a140149 abd0a76 6dd1918 a140149 6dd1918 a140149 abd0a76 74989d0 aa39642 1858b46 aa39642 6dd1918 abd0a76 6dd1918 a140149 1858b46 4a96b97 1858b46 4a96b97 723d673 4a96b97 723d673 4a96b97 972688e a140149 abd0a76 e7b455f abd0a76 a140149 abd0a76 a140149 abd0a76 a140149 53b9d1f abd0a76 723d673 a140149 a2acbd6 33ec56b bc1fd3e ed6c776 a68b099 3b7ba3d bc1fd3e 3b7ba3d bc1fd3e a140149 abd0a76 a140149 53b9d1f a140149 1858b46 abd0a76 53b9d1f a140149 53b9d1f a140149 abd0a76 a140149 abd0a76 ed6c776 a140149 e7b455f 1858b46 abd0a76 a140149 abd0a76 ed6c776 e7b455f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
import streamlit as st
import pandas as pd
import plotly.express as px
# Add custom CSS for the app background and text styling
def add_background():
background_url = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/BCK1.jpg"
css = f"""
<style>
.stApp {{
background-image: url("{background_url}");
background-size: cover;
background-position: center;
background-attachment: fixed;
}}
.highlight {{
background-color: rgba(27, 27, 27, 0.7); /* 70% Opaque Black Background */
padding: 10px;
border-radius: 5px;
margin-bottom: 15px;
color: white;
}}
</style>
"""
st.markdown(css, unsafe_allow_html=True)
# Set page configuration
st.set_page_config(page_title="GreenLens AI", layout="wide")
# Call the background function to load the background silently
add_background()
# Title and subtitle with updated color to white
st.markdown("<h1 style='text-align: center; color: white;'>GreenLens AI</h1>", unsafe_allow_html=True)
st.markdown(
"""
<p style='text-align: center; color: white; font-size: 18px;'>
A Comprehensive Tool for Assessing Water, Energy, and Carbon Footprints of Textile Products 🌍
</p>
""",
unsafe_allow_html=True,
)
# Dataset URL
DATASET_URL = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/DataSet01.xlsx"
# Load dataset from Hugging Face Spaces
@st.cache_data
def process_dataset(url):
try:
excel_content = pd.ExcelFile(url)
fiber_data = pd.read_excel(excel_content, sheet_name="Fiber Impact Data")
transport_data = pd.read_excel(excel_content, sheet_name="Transport Impact Data")
washing_data = pd.read_excel(excel_content, sheet_name="Washing Data")
# Convert data to dictionaries for calculations
fiber_impact_data = fiber_data.set_index("Fiber Type")[["Water (L/kg)", "Energy (MJ/kg)", "Carbon (kg CO2e/kg)"]].to_dict(orient="index")
transport_impact_data = transport_data.set_index("Transport Mode")["CFP (kg CO2e/km)"].to_dict()
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")
return fiber_impact_data, transport_impact_data, washing_impact_data
except Exception as e:
st.error(f"Error loading dataset: {e}")
return None, None, None
# Calculate footprints
def calculate_footprints(weight, composition, lifecycle_inputs):
water_fp, energy_fp, carbon_fp = 0, 0, 0
for fiber, percentage in composition.items():
if fiber in fiber_impact_data:
data = fiber_impact_data[fiber]
fraction = percentage / 100
water_fp += data["Water (L/kg)"] * weight * fraction
energy_fp += data["Energy (MJ/kg)"] * weight * fraction
carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
if lifecycle_inputs["transport_mode"] in transport_impact_data:
carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
if lifecycle_inputs["washing_temperature"] in washing_impact_data:
washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
water_fp += washing_water
energy_fp += washing_energy
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
water_fp /= 1000 # Convert water from liters to kiloliters
return water_fp, energy_fp, carbon_fp
# Sidebar inputs
def get_inputs(prefix):
weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.0, value=0.0, step=0.01, key=f"{prefix}_weight")
st.sidebar.markdown(f"<h3 style='color: black;'>{prefix} Material Composition (%)</h3>", unsafe_allow_html=True)
cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 0, step=1, key=f"{prefix}_cotton")
polyester = st.sidebar.number_input("Polyester (%)", 0, 100, 0, step=1, key=f"{prefix}_polyester")
nylon = st.sidebar.number_input("Nylon 6 (%)", 0, 100, 0, step=1, key=f"{prefix}_nylon")
acrylic = st.sidebar.number_input("Acrylic (%)", 0, 100, 0, step=1, key=f"{prefix}_acrylic")
viscose = st.sidebar.number_input("Viscose (%)", 0, 100, 0, step=1, key=f"{prefix}_viscose")
if cotton + polyester + nylon + acrylic + viscose != 100:
st.sidebar.error("Fiber composition must sum to 100%!")
lifecycle_inputs = {
"washing_cycles": st.sidebar.number_input(f"{prefix} Washing Cycles", min_value=0, value=0, key=f"{prefix}_wash_cycles"),
"washing_temperature": st.sidebar.selectbox(f"{prefix} Washing Temperature", list(washing_impact_data.keys()), key=f"{prefix}_wash_temp"),
"use_dryer": st.sidebar.checkbox(f"{prefix} Use Dryer?", key=f"{prefix}_use_dryer"),
"transport_mode": st.sidebar.selectbox(f"{prefix} Transport Mode", list(transport_impact_data.keys()), key=f"{prefix}_transport_mode"),
"transport_distance": st.sidebar.number_input(f"{prefix} Transport Distance (km)", min_value=0, value=0, step=10, key=f"{prefix}_transport_distance"),
}
composition = {
"Conventional Cotton": cotton,
"Polyester": polyester,
"Nylon 6": nylon,
"Acrylic": acrylic,
"Viscose": viscose,
}
return weight, composition, lifecycle_inputs
# Adjust graph styling
def style_figure(fig):
fig.update_layout(
plot_bgcolor="rgba(27,27,27,0.8)", # 20% transparency
paper_bgcolor="rgba(27,27,27,0.8)", # 20% transparency
font=dict(color="white"), # Font color set to white
title_font=dict(size=18, color="white"), # Title font white
xaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
yaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white"))
)
fig.update_traces(marker=dict(color="white", line=dict(color="gray", width=1))) # Simulate 3D effect with border
return fig
# Main application logic
fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
if fiber_impact_data and transport_impact_data and washing_impact_data:
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
if comparison_mode:
# Input for two assessments
col1, col2 = st.columns(2)
with col1:
weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
with col2:
weight2, composition2, lifecycle2 = get_inputs("Assessment 2")
# Calculate footprints for both assessments
water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
# Display numerical comparison
st.markdown(f"""
<div class="highlight">
<h2>Numerical Comparison</h2>
<p>**Assessment 1**: Water: {water1:.2f} kL, Energy: {energy1:.2f} MJ, Carbon: {carbon1:.2f} kg CO2e</p>
<p>**Assessment 2**: Water: {water2:.2f} kL, Energy: {energy2:.2f} MJ, Carbon: {carbon2:.2f} kg CO2e</p>
</div>
""", unsafe_allow_html=True)
# Bar chart comparison
comparison_data = pd.DataFrame({
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
"Assessment 1": [water1, energy1, carbon1],
"Assessment 2": [water2, energy2, carbon2],
})
fig = px.bar(
comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
x="Footprint Type",
y="Value",
color_discrete_sequence=["white"],
title="Comparison of Assessments"
)
st.plotly_chart(style_figure(fig))
else:
# Input for a single assessment
weight, composition, lifecycle = get_inputs("Single")
water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
# Display results
st.markdown(f"""
<div class="highlight">
<h2>Single Assessment Results</h2>
<p>- **Water Footprint**: {water:.2f} kL</p>
<p>- **Energy Footprint**: {energy:.2f} MJ</p>
<p>- **Carbon Footprint**: {carbon:.2f} kg CO2e</p>
</div>
""", unsafe_allow_html=True)
# Bar chart for single assessment
result_data = pd.DataFrame({
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
"Value": [water, energy, carbon]
})
fig = px.bar(result_data, x="Footprint Type", y="Value", title="Single Assessment Footprint Breakdown")
st.plotly_chart(style_figure(fig))
else:
st.error("Failed to load dataset.")
|