Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,84 @@
|
|
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
|
|
|
7 |
|
8 |
# Set Page Configurations
|
9 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
10 |
-
|
11 |
-
# Custom Styling for the App
|
12 |
st.markdown(
|
13 |
"""
|
14 |
-
<style>
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
.stSlider > div {
|
19 |
-
background-color: #50C878 !important; /* Green slider bar */
|
20 |
-
}
|
21 |
-
h1, h2, p {
|
22 |
-
text-align: center;
|
23 |
-
color: #4CAF50;
|
24 |
-
}
|
25 |
-
</style>
|
26 |
-
""",
|
27 |
-
unsafe_allow_html=True
|
28 |
-
)
|
29 |
-
|
30 |
-
# Title and Tagline
|
31 |
-
st.markdown("<h1>GreenLens-AI</h1>", unsafe_allow_html=True)
|
32 |
-
st.markdown("<p>A Sustainable Tool for Calculating Carbon, Energy, and Ecological Footprints 🌱</p>", unsafe_allow_html=True)
|
33 |
|
34 |
# Google Drive Dataset Link
|
35 |
-
DATASET_URL = "https://drive.google.com/uc?id=
|
36 |
-
|
37 |
-
# Function to fetch
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
total_size = int(response.headers.get('content-length', 0))
|
44 |
downloaded_size = 0
|
45 |
chunks = []
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
|
52 |
pdf_content = b"".join(chunks)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
return None
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
"""Process the PDF file and extract relevant data."""
|
62 |
-
st.info("Processing dataset...")
|
63 |
-
extracted_data = {}
|
64 |
-
|
65 |
-
for page in pdf_reader.pages:
|
66 |
-
text = page.extract_text()
|
67 |
-
|
68 |
-
# Example parsing rules (adjust based on the actual file format):
|
69 |
-
if "Global average water footprint of cotton fabric" in text:
|
70 |
-
extracted_data["Cotton"] = {"Water": 10000, "Energy": 60, "Carbon": 3.18}
|
71 |
-
if "Water footprint by region: China" in text:
|
72 |
-
extracted_data["China"] = {"Water": 6000}
|
73 |
-
|
74 |
-
st.success("Dataset processed successfully!")
|
75 |
-
return extracted_data
|
76 |
-
|
77 |
-
# Step 1: Fetch dataset from Google Drive
|
78 |
-
pdf_reader = fetch_pdf_from_drive(DATASET_URL)
|
79 |
-
if pdf_reader:
|
80 |
-
fiber_impact_data = process_pdf_data(pdf_reader)
|
81 |
-
st.write(fiber_impact_data) # Debugging: Display parsed data
|
82 |
-
else:
|
83 |
-
st.stop()
|
84 |
|
85 |
# Sidebar for User Inputs
|
86 |
st.sidebar.header("Input Product Details")
|
|
|
|
|
87 |
product_type = st.sidebar.selectbox("Product Type", ["T-shirt", "Jeans", "Shirt", "Carpet"])
|
88 |
product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.25)
|
89 |
|
@@ -95,10 +90,10 @@ nylon_percent = st.sidebar.slider("Nylon (%)", 0, 100, 10)
|
|
95 |
acrylic_percent = st.sidebar.slider("Acrylic (%)", 0, 100, 5)
|
96 |
viscose_percent = st.sidebar.slider("Viscose (%)", 0, 100, 5)
|
97 |
|
98 |
-
#
|
99 |
total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
|
100 |
if total_percentage != 100:
|
101 |
-
st.sidebar.
|
102 |
|
103 |
# Lifecycle Inputs
|
104 |
st.sidebar.header("Lifecycle Details")
|
@@ -109,18 +104,50 @@ transport_mode = st.sidebar.selectbox("Transport Mode", ["Plane", "Ship", "Train
|
|
109 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
110 |
|
111 |
# Function to calculate footprints
|
112 |
-
def calculate_footprints(weight, composition):
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
114 |
for fiber, percentage in composition.items():
|
115 |
if fiber in fiber_impact_data:
|
116 |
-
|
117 |
fraction = percentage / 100
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
-
#
|
124 |
composition = {
|
125 |
"Cotton": cotton_percent,
|
126 |
"Polyester": polyester_percent,
|
@@ -129,22 +156,28 @@ composition = {
|
|
129 |
"Viscose": viscose_percent,
|
130 |
}
|
131 |
|
132 |
-
|
133 |
-
|
|
|
134 |
|
135 |
# Display results
|
136 |
st.subheader("Calculated Results")
|
137 |
-
st.markdown(f"
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
149 |
else:
|
150 |
-
st.
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import requests
|
4 |
from io import BytesIO
|
5 |
from PyPDF2 import PdfReader
|
6 |
+
import plotly.graph_objects as go
|
7 |
|
8 |
# Set Page Configurations
|
9 |
st.set_page_config(page_title="GreenLens-AI", layout="wide")
|
10 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>GreenLens-AI</h1>", unsafe_allow_html=True)
|
|
|
11 |
st.markdown(
|
12 |
"""
|
13 |
+
<p style='text-align: center; color: #4CAF50; font-size: 18px;'>
|
14 |
+
A Comprehensive Tool for Calculating Water, Energy, and Carbon Footprints of Textile Products 🌍
|
15 |
+
</p>
|
16 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Google Drive Dataset Link
|
19 |
+
DATASET_URL = "https://drive.google.com/uc?id=1JMECXBOPU5UD9hdEUA0uv1g_Qm1CkWYn"
|
20 |
+
|
21 |
+
# Step 1: Function to fetch and process PDF dataset
|
22 |
+
@st.cache_data
|
23 |
+
def fetch_and_process_pdf(url):
|
24 |
+
"""Fetch the PDF dataset from Google Drive and process it."""
|
25 |
+
progress_text = "Fetching dataset from Google Drive..."
|
26 |
+
progress_bar = st.progress(0)
|
27 |
+
|
28 |
+
try:
|
29 |
+
# Download the file
|
30 |
+
response = requests.get(url, stream=True)
|
31 |
total_size = int(response.headers.get('content-length', 0))
|
32 |
downloaded_size = 0
|
33 |
chunks = []
|
34 |
|
35 |
+
for chunk in response.iter_content(chunk_size=8192):
|
36 |
+
downloaded_size += len(chunk)
|
37 |
+
progress_bar.progress(min(1.0, downloaded_size / total_size), text=progress_text)
|
38 |
+
chunks.append(chunk)
|
39 |
|
40 |
pdf_content = b"".join(chunks)
|
41 |
+
progress_bar.progress(1.0, text="Processing PDF...")
|
42 |
+
|
43 |
+
# Parse the PDF content
|
44 |
+
pdf_reader = PdfReader(BytesIO(pdf_content))
|
45 |
+
pdf_text = ""
|
46 |
+
for page in pdf_reader.pages:
|
47 |
+
pdf_text += page.extract_text()
|
48 |
+
|
49 |
+
# Extract necessary data dynamically from the PDF text
|
50 |
+
# Assume the PDF contains data structured like this:
|
51 |
+
# "FIBER_NAME, WATER (L/kg), ENERGY (MJ/kg), CARBON (kgCO2e/kg)"
|
52 |
+
lines = pdf_text.split("\n")
|
53 |
+
fiber_impact_data = {}
|
54 |
+
for line in lines:
|
55 |
+
parts = line.split(",")
|
56 |
+
if len(parts) == 4: # Ensure line is properly formatted
|
57 |
+
fiber, water, energy, carbon = parts
|
58 |
+
try:
|
59 |
+
fiber_impact_data[fiber.strip()] = {
|
60 |
+
"Water": float(water.strip()),
|
61 |
+
"Energy": float(energy.strip()),
|
62 |
+
"Carbon": float(carbon.strip()),
|
63 |
+
}
|
64 |
+
except ValueError:
|
65 |
+
# Skip lines that don't have valid numerical data
|
66 |
+
continue
|
67 |
+
|
68 |
+
st.success("Dataset loaded successfully!")
|
69 |
+
return fiber_impact_data
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
st.error(f"Error fetching or processing dataset: {e}")
|
73 |
return None
|
74 |
|
75 |
+
# Load dataset dynamically from Google Drive
|
76 |
+
fiber_impact_data = fetch_and_process_pdf(DATASET_URL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Sidebar for User Inputs
|
79 |
st.sidebar.header("Input Product Details")
|
80 |
+
|
81 |
+
# Input Section: Product-Specific Inputs
|
82 |
product_type = st.sidebar.selectbox("Product Type", ["T-shirt", "Jeans", "Shirt", "Carpet"])
|
83 |
product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.25)
|
84 |
|
|
|
90 |
acrylic_percent = st.sidebar.slider("Acrylic (%)", 0, 100, 5)
|
91 |
viscose_percent = st.sidebar.slider("Viscose (%)", 0, 100, 5)
|
92 |
|
93 |
+
# Validation check: Percentages must add up to 100%
|
94 |
total_percentage = cotton_percent + polyester_percent + nylon_percent + acrylic_percent + viscose_percent
|
95 |
if total_percentage != 100:
|
96 |
+
st.sidebar.error("The total of all fiber percentages must equal 100%!")
|
97 |
|
98 |
# Lifecycle Inputs
|
99 |
st.sidebar.header("Lifecycle Details")
|
|
|
104 |
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=50)
|
105 |
|
106 |
# Function to calculate footprints
|
107 |
+
def calculate_footprints(weight, composition, lifecycle_inputs):
|
108 |
+
# Initialize footprints
|
109 |
+
water_footprint = 0
|
110 |
+
energy_footprint = 0
|
111 |
+
carbon_footprint = 0
|
112 |
+
|
113 |
+
# Fiber contributions
|
114 |
for fiber, percentage in composition.items():
|
115 |
if fiber in fiber_impact_data:
|
116 |
+
data = fiber_impact_data[fiber]
|
117 |
fraction = percentage / 100
|
118 |
+
water_footprint += data["Water"] * weight * fraction
|
119 |
+
energy_footprint += data["Energy"] * weight * fraction
|
120 |
+
carbon_footprint += data["Carbon"] * weight * fraction
|
121 |
+
|
122 |
+
# Transportation impacts
|
123 |
+
transport_factor = {
|
124 |
+
"Plane": 1.102,
|
125 |
+
"Ship": 0.011,
|
126 |
+
"Train": 0.05,
|
127 |
+
"Truck": 0.25,
|
128 |
+
}[lifecycle_inputs["transport_mode"]]
|
129 |
+
|
130 |
+
transport_emissions = transport_factor * lifecycle_inputs["transport_distance"] * weight
|
131 |
+
carbon_footprint += transport_emissions
|
132 |
+
|
133 |
+
# Washing and drying impacts
|
134 |
+
washing_energy = {"Cold": 0.02, "30°C": 0.1, "40°C": 0.2, "60°C": 0.5}
|
135 |
+
dryer_energy = 0.5 if lifecycle_inputs["use_dryer"] else 0
|
136 |
+
carbon_footprint += (washing_energy[lifecycle_inputs["washing_temperature"]] * lifecycle_inputs["washing_cycles"] * 0.05)
|
137 |
+
energy_footprint += dryer_energy * lifecycle_inputs["washing_cycles"]
|
138 |
+
|
139 |
+
return water_footprint, energy_footprint, carbon_footprint
|
140 |
+
|
141 |
+
# User inputs as a dictionary
|
142 |
+
user_inputs = {
|
143 |
+
"transport_mode": transport_mode,
|
144 |
+
"transport_distance": transport_distance,
|
145 |
+
"washing_temperature": washing_temperature,
|
146 |
+
"washing_cycles": washing_cycles,
|
147 |
+
"use_dryer": use_dryer,
|
148 |
+
}
|
149 |
|
150 |
+
# Collect the composition dictionary
|
151 |
composition = {
|
152 |
"Cotton": cotton_percent,
|
153 |
"Polyester": polyester_percent,
|
|
|
156 |
"Viscose": viscose_percent,
|
157 |
}
|
158 |
|
159 |
+
# Run Calculations
|
160 |
+
if fiber_impact_data and total_percentage == 100:
|
161 |
+
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
|
162 |
|
163 |
# Display results
|
164 |
st.subheader("Calculated Results")
|
165 |
+
st.markdown(f"""
|
166 |
+
- **Water Footprint**: {water_fp:.2f} liters
|
167 |
+
- **Energy Footprint**: {energy_fp:.2f} MJ
|
168 |
+
- **Carbon Footprint**: {carbon_fp:.2f} kgCO2e
|
169 |
+
""")
|
170 |
+
|
171 |
+
# 3D Visualization
|
172 |
+
fig = go.Figure()
|
173 |
+
fig.add_trace(go.Bar(
|
174 |
+
x=["Water Footprint", "Energy Footprint", "Carbon Footprint"],
|
175 |
+
y=[water_fp, energy_fp, carbon_fp],
|
176 |
+
text=[f"{water_fp:.2f} L", f"{energy_fp:.2f} MJ", f"{carbon_fp:.2f} kgCO2e"],
|
177 |
+
textposition='auto',
|
178 |
+
marker=dict(color=["blue", "orange", "green"])
|
179 |
+
))
|
180 |
+
fig.update_layout(title="Footprint Breakdown", xaxis_title="Footprint Type", yaxis_title="Value")
|
181 |
+
st.plotly_chart(fig)
|
182 |
else:
|
183 |
+
st.error("Ensure dataset is loaded and composition sums to 100%.")
|