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 os
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
# Add custom CSS for the app background and highlighted text
|
|
@@ -65,7 +64,7 @@ def process_dataset(url):
|
|
| 65 |
return None, None, None
|
| 66 |
|
| 67 |
# Calculate footprints
|
| 68 |
-
def calculate_footprints(weight, composition, lifecycle_inputs):
|
| 69 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
| 70 |
for fiber, percentage in composition.items():
|
| 71 |
if fiber in fiber_impact_data:
|
|
@@ -94,8 +93,42 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
|
|
| 94 |
water_fp /= 1000
|
| 95 |
return water_fp, energy_fp, carbon_fp
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.0, value=0.0, step=0.01, key=f"{prefix}_weight")
|
| 100 |
st.sidebar.markdown(f"<h3 style='color: green;'>{prefix} Material Composition (%)</h3>", unsafe_allow_html=True)
|
| 101 |
cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 0, step=1, key=f"{prefix}_cotton")
|
|
@@ -128,40 +161,6 @@ def get_inputs(prefix):
|
|
| 128 |
}
|
| 129 |
return weight, composition, lifecycle_inputs
|
| 130 |
|
| 131 |
-
# Adjust graph styling
|
| 132 |
-
def style_figure(fig):
|
| 133 |
-
fig.update_layout(
|
| 134 |
-
plot_bgcolor="rgba(27, 27, 27, 0.8)", # 20% transparency
|
| 135 |
-
paper_bgcolor="rgba(27, 27, 27, 0.8)", # 20% transparency
|
| 136 |
-
font=dict(color="white"), # Font color set to white
|
| 137 |
-
title_font=dict(size=18, color="white"), # Title font white
|
| 138 |
-
xaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
|
| 139 |
-
yaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
|
| 140 |
-
)
|
| 141 |
-
fig.update_traces(marker=dict(color="white", line=dict(color="gray", width=1))) # Simulate 3D effect with border
|
| 142 |
-
return fig
|
| 143 |
-
|
| 144 |
-
# Generate recommendations using Groq API
|
| 145 |
-
def generate_recommendations(water, energy, carbon):
|
| 146 |
-
try:
|
| 147 |
-
client = Groq(api_key="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")
|
| 148 |
-
prompt = (
|
| 149 |
-
f"The environmental impact values for a textile product are as follows:\n"
|
| 150 |
-
f"Water Footprint: {water:.2f} kL\n"
|
| 151 |
-
f"Energy Footprint: {energy:.2f} MJ\n"
|
| 152 |
-
f"Carbon Footprint: {carbon:.2f} kg CO2e\n"
|
| 153 |
-
f"Provide recommendations to lower these impacts."
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
response = client.chat.completions.create(
|
| 157 |
-
messages=[{"role": "user", "content": prompt}],
|
| 158 |
-
model="llama-3.3-70b-versatile",
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
return response.choices[0].message.content
|
| 162 |
-
except Exception as e:
|
| 163 |
-
return f"Error generating recommendations: {e}"
|
| 164 |
-
|
| 165 |
# Main application logic
|
| 166 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
|
| 167 |
|
|
@@ -169,16 +168,16 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
|
|
| 169 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
| 170 |
|
| 171 |
if comparison_mode:
|
| 172 |
-
# Input for two assessments
|
| 173 |
col1, col2 = st.columns(2)
|
| 174 |
with col1:
|
| 175 |
-
weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
|
| 176 |
with col2:
|
| 177 |
-
weight2, composition2, lifecycle2 = get_inputs("Assessment 2")
|
| 178 |
|
| 179 |
-
# Calculate footprints for both assessments
|
| 180 |
-
water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
|
| 181 |
-
water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
|
| 182 |
|
| 183 |
# Display numerical comparison
|
| 184 |
st.markdown(f"""
|
|
@@ -204,9 +203,9 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
|
|
| 204 |
)
|
| 205 |
st.plotly_chart(style_figure(fig))
|
| 206 |
else:
|
| 207 |
-
# Input for a single assessment
|
| 208 |
-
weight, composition, lifecycle = get_inputs("Single")
|
| 209 |
-
water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
|
| 210 |
|
| 211 |
# Display results
|
| 212 |
st.markdown(f"""
|
|
@@ -236,4 +235,4 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
|
|
| 236 |
</div>
|
| 237 |
""", unsafe_allow_html=True)
|
| 238 |
else:
|
| 239 |
-
st.error("Failed to
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Add custom CSS for the app background and highlighted text
|
|
|
|
| 64 |
return None, None, None
|
| 65 |
|
| 66 |
# Calculate footprints
|
| 67 |
+
def calculate_footprints(weight, composition, lifecycle_inputs, fiber_impact_data, transport_impact_data, washing_impact_data):
|
| 68 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
| 69 |
for fiber, percentage in composition.items():
|
| 70 |
if fiber in fiber_impact_data:
|
|
|
|
| 93 |
water_fp /= 1000
|
| 94 |
return water_fp, energy_fp, carbon_fp
|
| 95 |
|
| 96 |
+
# Adjust graph styling
|
| 97 |
+
def style_figure(fig):
|
| 98 |
+
fig.update_layout(
|
| 99 |
+
plot_bgcolor="rgba(27, 27, 27, 0.8)",
|
| 100 |
+
paper_bgcolor="rgba(27, 27, 27, 0.8)",
|
| 101 |
+
font=dict(color="white"),
|
| 102 |
+
title_font=dict(size=18, color="white"),
|
| 103 |
+
xaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
|
| 104 |
+
yaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
|
| 105 |
+
)
|
| 106 |
+
fig.update_traces(marker=dict(color="white", line=dict(color="gray", width=1)))
|
| 107 |
+
return fig
|
| 108 |
+
|
| 109 |
+
# Generate recommendations using Groq API
|
| 110 |
+
def generate_recommendations(water, energy, carbon):
|
| 111 |
+
try:
|
| 112 |
+
client = Groq(api_key="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")
|
| 113 |
+
prompt = (
|
| 114 |
+
f"The environmental impact values for a textile product are as follows:\n"
|
| 115 |
+
f"Water Footprint: {water:.2f} kL\n"
|
| 116 |
+
f"Energy Footprint: {energy:.2f} MJ\n"
|
| 117 |
+
f"Carbon Footprint: {carbon:.2f} kg CO2e\n"
|
| 118 |
+
f"Provide recommendations to lower these impacts."
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
response = client.chat.completions.create(
|
| 122 |
+
messages=[{"role": "user", "content": prompt}],
|
| 123 |
+
model="llama-3.3-70b-versatile",
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
return response.choices[0].message.content
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return f"Error generating recommendations: {e}"
|
| 129 |
+
|
| 130 |
+
# Sidebar inputs - FIXED: Now returns data dictionaries as parameters
|
| 131 |
+
def get_inputs(prefix, transport_impact_data, washing_impact_data):
|
| 132 |
weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.0, value=0.0, step=0.01, key=f"{prefix}_weight")
|
| 133 |
st.sidebar.markdown(f"<h3 style='color: green;'>{prefix} Material Composition (%)</h3>", unsafe_allow_html=True)
|
| 134 |
cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 0, step=1, key=f"{prefix}_cotton")
|
|
|
|
| 161 |
}
|
| 162 |
return weight, composition, lifecycle_inputs
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# Main application logic
|
| 165 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
|
| 166 |
|
|
|
|
| 168 |
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
| 169 |
|
| 170 |
if comparison_mode:
|
| 171 |
+
# Input for two assessments - FIXED: Pass data dictionaries
|
| 172 |
col1, col2 = st.columns(2)
|
| 173 |
with col1:
|
| 174 |
+
weight1, composition1, lifecycle1 = get_inputs("Assessment 1", transport_impact_data, washing_impact_data)
|
| 175 |
with col2:
|
| 176 |
+
weight2, composition2, lifecycle2 = get_inputs("Assessment 2", transport_impact_data, washing_impact_data)
|
| 177 |
|
| 178 |
+
# Calculate footprints for both assessments - FIXED: Pass data dictionaries
|
| 179 |
+
water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1, fiber_impact_data, transport_impact_data, washing_impact_data)
|
| 180 |
+
water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2, fiber_impact_data, transport_impact_data, washing_impact_data)
|
| 181 |
|
| 182 |
# Display numerical comparison
|
| 183 |
st.markdown(f"""
|
|
|
|
| 203 |
)
|
| 204 |
st.plotly_chart(style_figure(fig))
|
| 205 |
else:
|
| 206 |
+
# Input for a single assessment - FIXED: Pass data dictionaries
|
| 207 |
+
weight, composition, lifecycle = get_inputs("Single", transport_impact_data, washing_impact_data)
|
| 208 |
+
water, energy, carbon = calculate_footprints(weight, composition, lifecycle, fiber_impact_data, transport_impact_data, washing_impact_data)
|
| 209 |
|
| 210 |
# Display results
|
| 211 |
st.markdown(f"""
|
|
|
|
| 235 |
</div>
|
| 236 |
""", unsafe_allow_html=True)
|
| 237 |
else:
|
| 238 |
+
st.error("Failed to load dataset.")
|