ZainMalik0925 commited on
Commit
5982535
·
verified ·
1 Parent(s): e039532

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -62
app.py CHANGED
@@ -1,10 +1,8 @@
1
- from groq import Groq
2
- import os
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 recommendations styling
8
  def add_background():
9
  background_url = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/BCK2.jpg"
10
  css = f"""
@@ -16,19 +14,12 @@ def add_background():
16
  background-attachment: fixed;
17
  }}
18
  .highlight {{
19
- background-color: rgba(27, 27, 27, 0.8); /* 80% opaque black */
20
  padding: 10px;
21
  border-radius: 5px;
22
  margin-bottom: 15px;
23
  color: white;
24
  }}
25
- .recommendation {{
26
- background-color: rgba(27, 27, 27, 0.8); /* 80% opaque black */
27
- padding: 10px;
28
- border-radius: 5px;
29
- margin-top: 15px;
30
- color: white;
31
- }}
32
  </style>
33
  """
34
  st.markdown(css, unsafe_allow_html=True)
@@ -71,49 +62,91 @@ def process_dataset(url):
71
  st.error(f"Error loading dataset: {e}")
72
  return None, None, None
73
 
74
- # Initialize Groq API client
75
- client = Groq(
76
- api_key="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"
77
- )
78
-
79
- # Function to get concise recommendations from Groq
80
- def get_groq_recommendations(water_fp, energy_fp, carbon_fp):
81
- if water_fp == 0 and energy_fp == 0 and carbon_fp == 0:
82
- return None
83
- # Send the query to Groq
84
- chat_completion = client.chat.completions.create(
85
- messages=[
86
- {
87
- "role": "user",
88
- "content": f"Given water usage: {water_fp:.2f} kL, energy use: {energy_fp:.2f} MJ, and carbon emissions: {carbon_fp:.2f} kg CO2e, provide concise recommendations to reduce these impacts."
89
- }
90
- ],
91
- model="llama-3.3-70b-versatile",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  )
93
- return chat_completion.choices[0].message.content
94
-
95
- # Display recommendations with specified styling
96
- def display_recommendations(recommendations):
97
- if recommendations:
98
- st.markdown(
99
- f"""
100
- <div class="recommendation">
101
- <h3>Recommendations</h3>
102
- <p>{recommendations}</p>
103
- </div>
104
- """,
105
- unsafe_allow_html=True,
106
- )
107
 
108
- # Sidebar inputs and calculations remain unchanged
109
- # ...
110
 
111
- # Main application logic: Ensure all recommendations are displayed with correct styling
112
  if fiber_impact_data and transport_impact_data and washing_impact_data:
113
  comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
114
 
115
  if comparison_mode:
116
- # Comparison mode (for two assessments)
117
  col1, col2 = st.columns(2)
118
  with col1:
119
  weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
@@ -124,10 +157,7 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
124
  water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
125
  water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
126
 
127
- # Get and display recommendations
128
- recommendations1 = get_groq_recommendations(water1, energy1, carbon1)
129
- recommendations2 = get_groq_recommendations(water2, energy2, carbon2)
130
-
131
  st.markdown(f"""
132
  <div class="highlight">
133
  <h2>Numerical Comparison</h2>
@@ -136,19 +166,27 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
136
  </div>
137
  """, unsafe_allow_html=True)
138
 
139
- if recommendations1:
140
- display_recommendations(recommendations1)
141
- if recommendations2:
142
- display_recommendations(recommendations2)
 
 
 
 
 
 
 
 
 
 
143
 
144
  else:
145
- # Single assessment mode
146
  weight, composition, lifecycle = get_inputs("Single")
147
  water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
148
 
149
- # Get and display recommendations
150
- recommendations = get_groq_recommendations(water, energy, carbon)
151
-
152
  st.markdown(f"""
153
  <div class="highlight">
154
  <h2>Single Assessment Results</h2>
@@ -158,8 +196,12 @@ if fiber_impact_data and transport_impact_data and washing_impact_data:
158
  </div>
159
  """, unsafe_allow_html=True)
160
 
161
- if recommendations:
162
- display_recommendations(recommendations)
163
-
 
 
 
 
164
  else:
165
  st.error("Failed to load dataset.")
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
 
5
+ # Add custom CSS for the app background and highlighted text
6
  def add_background():
7
  background_url = "https://huggingface.co/spaces/ZainMalik0925/GreenLensAI_LCA/resolve/main/BCK2.jpg"
8
  css = f"""
 
14
  background-attachment: fixed;
15
  }}
16
  .highlight {{
17
+ background-color: rgba(27, 27, 27, 0.7); /* 70% opaque black */
18
  padding: 10px;
19
  border-radius: 5px;
20
  margin-bottom: 15px;
21
  color: white;
22
  }}
 
 
 
 
 
 
 
23
  </style>
24
  """
25
  st.markdown(css, unsafe_allow_html=True)
 
62
  st.error(f"Error loading dataset: {e}")
63
  return None, None, None
64
 
65
+ # Calculate footprints
66
+ def calculate_footprints(weight, composition, lifecycle_inputs):
67
+ water_fp, energy_fp, carbon_fp = 0, 0, 0
68
+ for fiber, percentage in composition.items():
69
+ if fiber in fiber_impact_data:
70
+ data = fiber_impact_data[fiber]
71
+ fraction = percentage / 100
72
+ water_fp += data["Water (L/kg)"] * weight * fraction
73
+ energy_fp += data["Energy (MJ/kg)"] * weight * fraction
74
+ carbon_fp += data["Carbon (kg CO2e/kg)"] * weight * fraction
75
+
76
+ # Add transport impact
77
+ if lifecycle_inputs["transport_mode"] in transport_impact_data:
78
+ carbon_fp += transport_impact_data[lifecycle_inputs["transport_mode"]] * lifecycle_inputs["transport_distance"] * weight
79
+
80
+ # Add washing impact
81
+ if lifecycle_inputs["washing_temperature"] in washing_impact_data:
82
+ washing_data = washing_impact_data[lifecycle_inputs["washing_temperature"]]
83
+ washing_water = washing_data["Water (L/kg)"] * lifecycle_inputs["washing_cycles"]
84
+ washing_energy = washing_data["Energy Use (MJ/wash)"] * lifecycle_inputs["washing_cycles"]
85
+ washing_carbon = washing_data["Carbon (kg CO2e/wash)"] * lifecycle_inputs["washing_cycles"]
86
+ dryer_carbon = washing_data["Dryer CFP (kg CO2e/cycle)"] if lifecycle_inputs["use_dryer"] else 0
87
+ water_fp += washing_water
88
+ energy_fp += washing_energy
89
+ carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
90
+
91
+ # Convert water from liters to kiloliters
92
+ water_fp /= 1000
93
+ return water_fp, energy_fp, carbon_fp
94
+
95
+ # Sidebar inputs
96
+ def get_inputs(prefix):
97
+ weight = st.sidebar.number_input(f"{prefix} Product Weight (kg)", min_value=0.0, value=0.0, step=0.01, key=f"{prefix}_weight")
98
+ st.sidebar.markdown(f"<h3 style='color: white;'>{prefix} Material Composition (%)</h3>", unsafe_allow_html=True)
99
+ cotton = st.sidebar.number_input("Conventional Cotton (%)", 0, 100, 0, step=1, key=f"{prefix}_cotton")
100
+ polyester = st.sidebar.number_input("Polyester (%)", 0, 100, 0, step=1, key=f"{prefix}_polyester")
101
+ nylon = st.sidebar.number_input("Nylon 6 (%)", 0, 100, 0, step=1, key=f"{prefix}_nylon")
102
+ acrylic = st.sidebar.number_input("Acrylic (%)", 0, 100, 0, step=1, key=f"{prefix}_acrylic")
103
+ viscose = st.sidebar.number_input("Viscose (%)", 0, 100, 0, step=1, key=f"{prefix}_viscose")
104
+
105
+ if cotton + polyester + nylon + acrylic + viscose != 100:
106
+ st.sidebar.error("Fiber composition must sum to 100%!")
107
+
108
+ st.sidebar.markdown(f"<h3 style='color: white;'>{prefix} Transport Inputs</h3>", unsafe_allow_html=True)
109
+ transport_mode = st.sidebar.selectbox(f"{prefix} Transport Mode", list(transport_impact_data.keys()), key=f"{prefix}_transport_mode")
110
+ transport_distance = st.sidebar.number_input(f"{prefix} Transport Distance (km)", min_value=0, value=0, step=10, key=f"{prefix}_transport_distance")
111
+
112
+ lifecycle_inputs = {
113
+ "washing_cycles": st.sidebar.number_input(f"{prefix} Washing Cycles", min_value=0, value=0, key=f"{prefix}_wash_cycles"),
114
+ "washing_temperature": st.sidebar.selectbox(f"{prefix} Washing Temperature", list(washing_impact_data.keys()), key=f"{prefix}_wash_temp"),
115
+ "use_dryer": st.sidebar.checkbox(f"{prefix} Use Dryer?", key=f"{prefix}_use_dryer"),
116
+ "transport_mode": transport_mode,
117
+ "transport_distance": transport_distance,
118
+ }
119
+
120
+ composition = {
121
+ "Conventional Cotton": cotton,
122
+ "Polyester": polyester,
123
+ "Nylon 6": nylon,
124
+ "Acrylic": acrylic,
125
+ "Viscose": viscose,
126
+ }
127
+ return weight, composition, lifecycle_inputs
128
+
129
+ # Adjust graph styling
130
+ def style_figure(fig):
131
+ fig.update_layout(
132
+ plot_bgcolor="rgba(27, 27, 27, 0.8)", # 20% transparency
133
+ paper_bgcolor="rgba(27, 27, 27, 0.8)", # 20% transparency
134
+ font=dict(color="white"), # Font color set to white
135
+ title_font=dict(size=18, color="white"), # Title font white
136
+ xaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
137
+ yaxis=dict(title_font=dict(color="white"), tickfont=dict(color="white")),
138
  )
139
+ fig.update_traces(marker=dict(color="white", line=dict(color="gray", width=1))) # Simulate 3D effect with border
140
+ return fig
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
+ # Main application logic
143
+ fiber_impact_data, transport_impact_data, washing_impact_data = process_dataset(DATASET_URL)
144
 
 
145
  if fiber_impact_data and transport_impact_data and washing_impact_data:
146
  comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
147
 
148
  if comparison_mode:
149
+ # Input for two assessments
150
  col1, col2 = st.columns(2)
151
  with col1:
152
  weight1, composition1, lifecycle1 = get_inputs("Assessment 1")
 
157
  water1, energy1, carbon1 = calculate_footprints(weight1, composition1, lifecycle1)
158
  water2, energy2, carbon2 = calculate_footprints(weight2, composition2, lifecycle2)
159
 
160
+ # Display numerical comparison
 
 
 
161
  st.markdown(f"""
162
  <div class="highlight">
163
  <h2>Numerical Comparison</h2>
 
166
  </div>
167
  """, unsafe_allow_html=True)
168
 
169
+ # Bar chart comparison
170
+ comparison_data = pd.DataFrame({
171
+ "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
172
+ "Assessment 1": [water1, energy1, carbon1],
173
+ "Assessment 2": [water2, energy2, carbon2],
174
+ })
175
+ fig = px.bar(
176
+ comparison_data.melt(id_vars="Footprint Type", var_name="Assessment", value_name="Value"),
177
+ x="Footprint Type",
178
+ y="Value",
179
+ color_discrete_sequence=["white"],
180
+ title="Comparison of Assessments"
181
+ )
182
+ st.plotly_chart(style_figure(fig))
183
 
184
  else:
185
+ # Input for a single assessment
186
  weight, composition, lifecycle = get_inputs("Single")
187
  water, energy, carbon = calculate_footprints(weight, composition, lifecycle)
188
 
189
+ # Display results
 
 
190
  st.markdown(f"""
191
  <div class="highlight">
192
  <h2>Single Assessment Results</h2>
 
196
  </div>
197
  """, unsafe_allow_html=True)
198
 
199
+ # Bar chart for single assessment
200
+ result_data = pd.DataFrame({
201
+ "Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
202
+ "Value": [water, energy, carbon]
203
+ })
204
+ fig = px.bar(result_data, x="Footprint Type", y="Value", title="Single Assessment Footprint Breakdown")
205
+ st.plotly_chart(style_figure(fig))
206
  else:
207
  st.error("Failed to load dataset.")