Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
-
import plotly.graph_objects as go
|
5 |
import requests
|
6 |
from io import BytesIO
|
7 |
from PyPDF2 import PdfReader
|
@@ -17,7 +16,7 @@ st.markdown(
|
|
17 |
body {
|
18 |
background-color: #d4edda; /* Light green background */
|
19 |
}
|
20 |
-
.stSlider > div {
|
21 |
background-color: #50C878 !important; /* Green slider bar */
|
22 |
}
|
23 |
h1, h2, p {
|
@@ -31,16 +30,16 @@ st.markdown(
|
|
31 |
|
32 |
# Title and Tagline
|
33 |
st.markdown("<h1>GreenLens-AI</h1>", unsafe_allow_html=True)
|
34 |
-
st.markdown("<p>A Sustainable Tool for Calculating Carbon, Energy, and Ecological Footprints
|
35 |
|
36 |
# Google Drive Dataset Link
|
37 |
DATASET_URL = "https://drive.google.com/uc?id=1QY9yv2mhz4n8bOTi4ahbjBpapltqXV6D"
|
38 |
|
39 |
-
# Function to fetch
|
40 |
@st.cache_data
|
41 |
-
def
|
42 |
-
"""Fetch the dataset from Google Drive."""
|
43 |
-
progress_text = "
|
44 |
progress_bar = st.progress(0)
|
45 |
|
46 |
try:
|
@@ -55,13 +54,38 @@ def fetch_dataset(url):
|
|
55 |
chunks.append(chunk)
|
56 |
|
57 |
pdf_content = b"".join(chunks)
|
58 |
-
progress_bar.progress(1.0, text="
|
|
|
59 |
return PdfReader(BytesIO(pdf_content))
|
|
|
60 |
except Exception as e:
|
61 |
st.error(f"Error fetching dataset: {e}")
|
62 |
return None
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# Sidebar for User Inputs
|
67 |
st.sidebar.header("Input Product Details")
|
@@ -79,7 +103,7 @@ viscose_percent = st.sidebar.slider("Viscose (%)", 0, 100, 5)
|
|
79 |
# Validate percentage
|
80 |
total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
|
81 |
if total_percentage != 100:
|
82 |
-
st.sidebar.warning("
|
83 |
|
84 |
# Lifecycle Inputs
|
85 |
st.sidebar.header("Lifecycle Details")
|
@@ -89,36 +113,19 @@ use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
|
|
89 |
transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train", "Truck"])
|
90 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
91 |
|
92 |
-
# Mock Fiber Impact Data
|
93 |
-
fiber_impact_data = {
|
94 |
-
"Cotton": {"Water": 10000, "Energy": 60, "Carbon": 3.18},
|
95 |
-
"Polyester": {"Water": 62, "Energy": 125, "Carbon": 4.8},
|
96 |
-
"Nylon": {"Water": 70, "Energy": 120.47, "Carbon": 5.4},
|
97 |
-
"Acrylic": {"Water": 50, "Energy": 175, "Carbon": 6.2},
|
98 |
-
"Viscose": {"Water": 200, "Energy": 100, "Carbon": 4.2},
|
99 |
-
}
|
100 |
-
|
101 |
# Function to calculate footprints
|
102 |
-
def calculate_footprints(weight, composition
|
103 |
-
|
104 |
-
energy_footprint = 0
|
105 |
-
carbon_footprint = 0
|
106 |
-
|
107 |
for fiber, percentage in composition.items():
|
108 |
if fiber in fiber_impact_data:
|
109 |
-
|
110 |
fraction = percentage / 100
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
114 |
|
115 |
-
|
116 |
-
transport_factor = {"Plane": 1.102, "Ship": 0.011, "Train": 0.05, "Truck": 0.25}[lifecycle_inputs["transport_mode"]]
|
117 |
-
carbon_footprint += transport_factor * lifecycle_inputs["transport_distance"] * weight
|
118 |
-
|
119 |
-
return water_footprint, energy_footprint, carbon_footprint
|
120 |
-
|
121 |
-
# Composition dictionary
|
122 |
composition = {
|
123 |
"Cotton": cotton_percent,
|
124 |
"Polyester": polyester_percent,
|
@@ -127,34 +134,22 @@ composition = {
|
|
127 |
"Viscose": viscose_percent,
|
128 |
}
|
129 |
|
130 |
-
user_inputs = {
|
131 |
-
"transport_mode": transport_mode,
|
132 |
-
"transport_distance": transport_distance,
|
133 |
-
"washing_temperature": washing_temperature,
|
134 |
-
"washing_cycles": washing_cycles,
|
135 |
-
"use_dryer": use_dryer,
|
136 |
-
}
|
137 |
-
|
138 |
-
# Perform calculations
|
139 |
if total_percentage == 100:
|
140 |
-
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition
|
141 |
|
142 |
# Display results
|
143 |
st.subheader("Calculated Results")
|
144 |
-
st.markdown(f""
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
st.plotly_chart(fig_cfp, use_container_width=True)
|
157 |
-
st.plotly_chart(fig_ep, use_container_width=True)
|
158 |
-
st.plotly_chart(fig_ef, use_container_width=True)
|
159 |
else:
|
160 |
-
st.
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
from PyPDF2 import PdfReader
|
|
|
16 |
body {
|
17 |
background-color: #d4edda; /* Light green background */
|
18 |
}
|
19 |
+
.stSlider > div {
|
20 |
background-color: #50C878 !important; /* Green slider bar */
|
21 |
}
|
22 |
h1, h2, p {
|
|
|
30 |
|
31 |
# Title and Tagline
|
32 |
st.markdown("<h1>GreenLens-AI</h1>", unsafe_allow_html=True)
|
33 |
+
st.markdown("<p>A Sustainable Tool for Calculating Carbon, Energy, and Ecological Footprints 🌱</p>", unsafe_allow_html=True)
|
34 |
|
35 |
# Google Drive Dataset Link
|
36 |
DATASET_URL = "https://drive.google.com/uc?id=1QY9yv2mhz4n8bOTi4ahbjBpapltqXV6D"
|
37 |
|
38 |
+
# Function to fetch dataset from Google Drive
|
39 |
@st.cache_data
|
40 |
+
def fetch_pdf_from_drive(url):
|
41 |
+
"""Fetch the dataset (PDF) from Google Drive."""
|
42 |
+
progress_text = "Downloading dataset from Google Drive..."
|
43 |
progress_bar = st.progress(0)
|
44 |
|
45 |
try:
|
|
|
54 |
chunks.append(chunk)
|
55 |
|
56 |
pdf_content = b"".join(chunks)
|
57 |
+
progress_bar.progress(1.0, text="Download Complete")
|
58 |
+
st.success("Dataset downloaded successfully!")
|
59 |
return PdfReader(BytesIO(pdf_content))
|
60 |
+
|
61 |
except Exception as e:
|
62 |
st.error(f"Error fetching dataset: {e}")
|
63 |
return None
|
64 |
|
65 |
+
# Function to extract data from the PDF
|
66 |
+
@st.cache_data
|
67 |
+
def process_pdf_data(pdf_reader):
|
68 |
+
"""Extract relevant data from the PDF."""
|
69 |
+
extracted_data = {}
|
70 |
+
for page in pdf_reader.pages:
|
71 |
+
text = page.extract_text()
|
72 |
+
# Example: Extract fiber impact data or other metrics
|
73 |
+
if "Global average water footprint of cotton fabric" in text:
|
74 |
+
extracted_data["Cotton"] = {"Water": 10000, "Energy": 60, "Carbon": 3.18}
|
75 |
+
if "Water footprint by region: China" in text:
|
76 |
+
extracted_data["China"] = {"Water": 6000}
|
77 |
+
return extracted_data
|
78 |
+
|
79 |
+
# Fetch and process the dataset
|
80 |
+
st.info("Fetching dataset. Please wait...")
|
81 |
+
pdf_reader = fetch_pdf_from_drive(DATASET_URL)
|
82 |
+
if pdf_reader:
|
83 |
+
fiber_impact_data = process_pdf_data(pdf_reader)
|
84 |
+
if not fiber_impact_data:
|
85 |
+
st.error("Failed to extract data from the PDF. Please check the PDF structure.")
|
86 |
+
else:
|
87 |
+
st.error("Failed to fetch the dataset. Check your internet connection or the dataset link.")
|
88 |
+
fiber_impact_data = {}
|
89 |
|
90 |
# Sidebar for User Inputs
|
91 |
st.sidebar.header("Input Product Details")
|
|
|
103 |
# Validate percentage
|
104 |
total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
|
105 |
if total_percentage != 100:
|
106 |
+
st.sidebar.warning("Material percentages must sum to 100%!")
|
107 |
|
108 |
# Lifecycle Inputs
|
109 |
st.sidebar.header("Lifecycle Details")
|
|
|
113 |
transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train", "Truck"])
|
114 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
# Function to calculate footprints
|
117 |
+
def calculate_footprints(weight, composition):
|
118 |
+
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
|
|
|
|
|
|
119 |
for fiber, percentage in composition.items():
|
120 |
if fiber in fiber_impact_data:
|
121 |
+
impacts = fiber_impact_data[fiber]
|
122 |
fraction = percentage / 100
|
123 |
+
water_fp += impacts["Water"] * weight * fraction
|
124 |
+
energy_fp += impacts["Energy"] * weight * fraction
|
125 |
+
carbon_fp += impacts["Carbon"] * weight * fraction
|
126 |
+
return water_fp, energy_fp, carbon_fp
|
127 |
|
128 |
+
# Perform calculations
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
composition = {
|
130 |
"Cotton": cotton_percent,
|
131 |
"Polyester": polyester_percent,
|
|
|
134 |
"Viscose": viscose_percent,
|
135 |
}
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
if total_percentage == 100:
|
138 |
+
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition)
|
139 |
|
140 |
# Display results
|
141 |
st.subheader("Calculated Results")
|
142 |
+
st.markdown(f"- **Water Footprint**: {water_fp:.2f} liters")
|
143 |
+
st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
|
144 |
+
st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kgCO2e")
|
145 |
+
|
146 |
+
# Separate Graphs for each footprint
|
147 |
+
fig_water = px.bar(x=["Water Footprint"], y=[water_fp], labels={"x": "Type", "y": "Liters"}, title="Water Footprint")
|
148 |
+
fig_energy = px.bar(x=["Energy Footprint"], y=[energy_fp], labels={"x": "Type", "y": "MJ"}, title="Energy Footprint")
|
149 |
+
fig_carbon = px.bar(x=["Carbon Footprint"], y=[carbon_fp], labels={"x": "Type", "y": "kgCO2e"}, title="Carbon Footprint")
|
150 |
+
|
151 |
+
st.plotly_chart(fig_water, use_container_width=True)
|
152 |
+
st.plotly_chart(fig_energy, use_container_width=True)
|
153 |
+
st.plotly_chart(fig_carbon, use_container_width=True)
|
|
|
|
|
|
|
154 |
else:
|
155 |
+
st.warning("Ensure that material composition totals 100%.")
|