Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from groq import Groq
|
| 3 |
import streamlit as st
|
| 4 |
import pandas as pd
|
| 5 |
import plotly.express as px
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Add custom CSS for the app background and highlighted text
|
| 8 |
def add_background():
|
|
@@ -64,23 +64,6 @@ def process_dataset(url):
|
|
| 64 |
st.error(f"Error loading dataset: {e}")
|
| 65 |
return None, None, None
|
| 66 |
|
| 67 |
-
# Function to get recommendations from Groq
|
| 68 |
-
def get_groq_recommendations(water_fp, energy_fp, carbon_fp):
|
| 69 |
-
if water_fp == 0 and energy_fp == 0 and carbon_fp == 0:
|
| 70 |
-
return None
|
| 71 |
-
|
| 72 |
-
# Send the query to Groq
|
| 73 |
-
chat_completion = client.chat.completions.create(
|
| 74 |
-
messages=[
|
| 75 |
-
{
|
| 76 |
-
"role": "user",
|
| 77 |
-
"content": f"Based on the environmental impact data: Water: {water_fp:.2f} kL, Energy: {energy_fp:.2f} MJ, Carbon: {carbon_fp:.2f} kg CO2e, what are the recommendations to lower the environmental impacts?"
|
| 78 |
-
}
|
| 79 |
-
],
|
| 80 |
-
model="llama-3.3-70b-versatile",
|
| 81 |
-
)
|
| 82 |
-
return chat_completion.choices[0].message.content
|
| 83 |
-
|
| 84 |
# Calculate footprints
|
| 85 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
| 86 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
|
@@ -158,6 +141,27 @@ def style_figure(fig):
|
|
| 158 |
fig.update_traces(marker=dict(color="white", line=dict(color="gray", width=1))) # Simulate 3D effect with border
|
| 159 |
return fig
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Main application logic
|
| 162 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
|
| 163 |
|
|
@@ -195,21 +199,10 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
|
|
| 195 |
comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
|
| 196 |
x="Footprint Type",
|
| 197 |
y="Value",
|
| 198 |
-
|
| 199 |
title="Comparison of Assessments"
|
| 200 |
)
|
| 201 |
st.plotly_chart(style_figure(fig))
|
| 202 |
-
|
| 203 |
-
# Get Groq recommendations if necessary
|
| 204 |
-
recommendations1 = get_groq_recommendations(water1, energy1, carbon1)
|
| 205 |
-
recommendations2 = get_groq_recommendations(water2, energy2, carbon2)
|
| 206 |
-
if recommendations1:
|
| 207 |
-
st.subheader("Recommendations for Assessment 1")
|
| 208 |
-
st.write(recommendations1)
|
| 209 |
-
if recommendations2:
|
| 210 |
-
st.subheader("Recommendations for Assessment 2")
|
| 211 |
-
st.write(recommendations2)
|
| 212 |
-
|
| 213 |
else:
|
| 214 |
# Input for a single assessment
|
| 215 |
weight, composition, lifecycle = get_inputs("Single")
|
|
@@ -230,11 +223,17 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
|
|
| 230 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
| 231 |
"Value": [water, energy, carbon]
|
| 232 |
})
|
| 233 |
-
fig = px.bar(result_data, x="Footprint Type", y="Value", title="
|
| 234 |
st.plotly_chart(style_figure(fig))
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
st.
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 8 |
def add_background():
|
|
|
|
| 64 |
st.error(f"Error loading dataset: {e}")
|
| 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
|
|
|
|
| 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=os.environ.get("GROQ_API_KEY"))
|
| 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 |
|
|
|
|
| 199 |
comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
|
| 200 |
x="Footprint Type",
|
| 201 |
y="Value",
|
| 202 |
+
color="Assessment",
|
| 203 |
title="Comparison of Assessments"
|
| 204 |
)
|
| 205 |
st.plotly_chart(style_figure(fig))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
else:
|
| 207 |
# Input for a single assessment
|
| 208 |
weight, composition, lifecycle = get_inputs("Single")
|
|
|
|
| 223 |
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
| 224 |
"Value": [water, energy, carbon]
|
| 225 |
})
|
| 226 |
+
fig = px.bar(result_data, x="Footprint Type", y="Value", title="Single Assessment Footprint Breakdown")
|
| 227 |
st.plotly_chart(style_figure(fig))
|
| 228 |
|
| 229 |
+
# Generate recommendations if impact values are not zero
|
| 230 |
+
if water > 0 or energy > 0 or carbon > 0:
|
| 231 |
+
recommendations = generate_recommendations(water, energy, carbon)
|
| 232 |
+
st.markdown(f"""
|
| 233 |
+
<div class="highlight">
|
| 234 |
+
<h2>Recommendations to Lower Environmental Impacts</h2>
|
| 235 |
+
<p>{recommendations}</p>
|
| 236 |
+
</div>
|
| 237 |
+
""", unsafe_allow_html=True)
|
| 238 |
+
else:
|
| 239 |
+
st.error("Failed to load dataset.")
|