Mujtaba29 commited on
Commit
e1ab9ed
·
verified ·
1 Parent(s): 08e7dcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -3
app.py CHANGED
@@ -10,7 +10,7 @@ import numpy as np
10
  os.environ["GROQ_API_KEY"] = "key"
11
 
12
  # Initialize Groq client
13
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
14
 
15
  # Carbon footprint reduction data (kg CO2 per kg recycled)
16
  carbon_reduction_data = {
@@ -64,7 +64,7 @@ def load_segformer_model():
64
 
65
  segformer_extractor, segformer_model = load_segformer_model()
66
 
67
- # Function to call Groq LLM
68
  def get_recycling_suggestions_from_groq(item, quantity):
69
  prompt = (
70
  f"You are an expert in recycling and sustainability. "
@@ -121,7 +121,65 @@ st.markdown(
121
  unsafe_allow_html=True,
122
  )
123
 
124
- if action == "Get Suggestions for Items":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  st.markdown(
126
  """
127
  <div style="text-align: center; background-color: #fff3e0; padding: 10px; border-radius: 5px;">
 
10
  os.environ["GROQ_API_KEY"] = "key"
11
 
12
  # Initialize Groq client
13
+ client = Groq(api_key=os.environ.get("key"))
14
 
15
  # Carbon footprint reduction data (kg CO2 per kg recycled)
16
  carbon_reduction_data = {
 
64
 
65
  segformer_extractor, segformer_model = load_segformer_model()
66
 
67
+ # Function to call Groq LLM for recycling suggestions
68
  def get_recycling_suggestions_from_groq(item, quantity):
69
  prompt = (
70
  f"You are an expert in recycling and sustainability. "
 
121
  unsafe_allow_html=True,
122
  )
123
 
124
+ if action == "Upload Image":
125
+ st.markdown(
126
+ """
127
+ <div style="text-align: center; background-color: #e3f2fd; padding: 10px; border-radius: 5px;">
128
+ <h3 style="color: #01579b;">Upload an image of waste, and we'll identify items, suggest recycling ideas, and calculate carbon footprint reduction!</h3>
129
+ </div>
130
+ """,
131
+ unsafe_allow_html=True,
132
+ )
133
+ uploaded_image = st.file_uploader("Upload an image of the waste:", type=["jpg", "jpeg", "png"])
134
+
135
+ if uploaded_image:
136
+ image = Image.open(uploaded_image)
137
+ st.image(image, caption="Uploaded Image", use_container_width=True)
138
+
139
+ st.write("### YOLOv8: Detecting Waste Items...")
140
+ yolo_results = model.predict(image, conf=0.1)
141
+ yolo_detected_items = [model.model.names[int(pred[5])] for pred in yolo_results[0].boxes.data.tolist()]
142
+
143
+ st.write("### SegFormer: Analyzing Segmentation...")
144
+ segformer_inputs = segformer_extractor(images=image, return_tensors="pt")
145
+ segformer_outputs = segformer_model(**segformer_inputs)
146
+ segmentation_map = segformer_outputs.logits.argmax(dim=1).squeeze().numpy()
147
+ segformer_detected_items = [
148
+ ade20k_labels[class_id]
149
+ for class_id in np.unique(segmentation_map)
150
+ if class_id in ade20k_labels
151
+ ]
152
+
153
+ combined_items = set(yolo_detected_items + segformer_detected_items)
154
+
155
+ if combined_items:
156
+ st.write("### Combined Results:")
157
+ st.write(", ".join(combined_items))
158
+
159
+ total_carbon_reduction = 0
160
+ for item in combined_items:
161
+ st.markdown(f"**Recycling Idea for {item}:**")
162
+ response = get_recycling_suggestions_from_groq(item, 1)
163
+ carbon_reduction = max(0.5, min(2.5, carbon_reduction_data.get(item.lower(), 0) * 1))
164
+ total_carbon_reduction += carbon_reduction
165
+
166
+ st.write(response)
167
+ st.markdown(
168
+ f"""<p style="color: #2e7d32;">🌍 Carbon Footprint Reduction: {carbon_reduction:.2f} kg CO₂</p>""",
169
+ unsafe_allow_html=True,
170
+ )
171
+ st.write("---")
172
+
173
+ st.markdown(
174
+ f"""<div style="padding: 15px; text-align: center; background-color: #004d40; color: #ffffff; border-radius: 5px;">
175
+ 🌟 Total Carbon Footprint Reduction: <b>{total_carbon_reduction:.2f} kg CO₂ saved</b>
176
+ </div>""",
177
+ unsafe_allow_html=True,
178
+ )
179
+ else:
180
+ st.error("No recognizable waste items detected.")
181
+
182
+ elif action == "Get Suggestions for Items":
183
  st.markdown(
184
  """
185
  <div style="text-align: center; background-color: #fff3e0; padding: 10px; border-radius: 5px;">