Spaces:
Sleeping
Sleeping
File size: 9,472 Bytes
2edd2bd cb34c8b 2edd2bd cb34c8b 2edd2bd 4c2c8cc 2edd2bd c083376 2edd2bd b2fe23d fd48294 4fb700c 2edd2bd d8a792a 2edd2bd 002fec0 2edd2bd cb34c8b e1ab9ed 2edd2bd 6ae98f3 2edd2bd 6ae98f3 2edd2bd a1e2899 fd48294 a1e2899 fd48294 a1e2899 fd48294 cb34c8b 4462e8e 77a3ee3 4462e8e b69f42b cb34c8b 4462e8e 77a3ee3 4462e8e b69f42b e1ab9ed 614cb20 e1ab9ed 614cb20 e1ab9ed 3464e2f 614cb20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import os
import streamlit as st
from groq import Groq
from ultralytics import YOLO
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
from PIL import Image
import numpy as np
# Set the Groq API key
os.environ["GROQ_API_KEY"] = "key"
# Initialize Groq client
client = Groq(api_key=os.environ.get("key"))
# Carbon footprint reduction data (kg CO2 per kg recycled)
carbon_reduction_data = {
"plastic bottle": 3.8,
"metal container": 9.0,
"burnable waste": 2.0,
"glass bottle": 0.5,
"paper": 1.3,
"plastic bag": 2.5,
"wood": 1.7,
"rubber": 6.0,
}
# ADE20K class label mapping for SegFormer
ade20k_labels = {
17: "plastic bottle",
36: "glass bottle",
49: "paper",
72: "wood",
85: "metal container",
108: "burnable waste",
120: "plastic bag",
150: "rubber",
}
# Predefined list of clutter objects with emojis
predefined_clutter_items = {
"plastic bottle": "π§΄",
"metal container": "π’οΈ",
"burnable waste": "π₯",
"glass bottle": "πΎ",
"paper": "π",
"plastic bag": "ποΈ",
"wood": "πͺ΅",
"rubber": "π",
}
# Load YOLOv8 model
@st.cache_resource
def load_yolo_model():
return YOLO("yolov8n.pt")
model = load_yolo_model()
# Load SegFormer model and feature extractor
@st.cache_resource
def load_segformer_model():
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
return feature_extractor, model
segformer_extractor, segformer_model = load_segformer_model()
# Function to call Groq LLM for recycling suggestions
def get_recycling_suggestions_from_groq(item, quantity):
prompt = (
f"You are an expert in recycling and sustainability. "
f"Suggest profitable and eco-friendly uses for {quantity} kg of {item}, "
f"including household uses, ways to monetize them, and calculate carbon footprint reduction. "
f"Keep your response concise and practical. Add emojis to enhance clarity."
)
try:
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
stream=False,
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"Error fetching suggestions: {e}"
# Function to get DIY steps from Groq
def get_diy_steps_from_groq(item):
prompt = (
f"Provide step-by-step DIY instructions to create '{item}' in a concise and practical way. "
f"Focus on clear bullet points and minimal resources."
)
try:
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
stream=False,
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"Error fetching DIY instructions: {e}"
# Sidebar
st.sidebar.markdown(
"""
<div style="text-align: center;">
<h2 style="color: #004d40;">β»οΈ Navigation</h2>
<p style="color: #00796b;">Use the app to identify waste items and generate recycling suggestions.</p>
</div>
""",
unsafe_allow_html=True,
)
action = st.sidebar.radio("Choose an action:", ["Upload Image", "Get Suggestions for Items"])
# Main app
st.markdown(
"""
<div style="text-align: center; background-color: #004d40; padding: 20px; border-radius: 10px;">
<h1 style="color: #ffffff;">β»οΈ Recycle-Smart-PK</h1>
<p style="font-size: 18px; color: #ffffff;">Powered by LLM π</p>
</div>
""",
unsafe_allow_html=True,
)
if action == "Upload Image":
st.markdown(
"""
<div style="text-align: center; background-color: #e3f2fd; padding: 10px; border-radius: 5px;">
<h3 style="color: #01579b;">Upload an image of waste, and we'll identify items, suggest recycling ideas, and calculate carbon footprint reduction!</h3>
</div>
""",
unsafe_allow_html=True,
)
uploaded_image = st.file_uploader("Upload an image of the waste:", type=["jpg", "jpeg", "png"])
if uploaded_image:
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_container_width=True)
st.write("### YOLOv8: Detecting Waste Items...")
yolo_results = model.predict(image, conf=0.1)
yolo_detected_items = [model.model.names[int(pred[5])] for pred in yolo_results[0].boxes.data.tolist()]
st.write("### SegFormer: Analyzing Segmentation...")
segformer_inputs = segformer_extractor(images=image, return_tensors="pt")
segformer_outputs = segformer_model(**segformer_inputs)
segmentation_map = segformer_outputs.logits.argmax(dim=1).squeeze().numpy()
segformer_detected_items = [
ade20k_labels[class_id]
for class_id in np.unique(segmentation_map)
if class_id in ade20k_labels
]
combined_items = set(yolo_detected_items + segformer_detected_items)
if combined_items:
st.write("### Combined Results:")
st.write(", ".join(combined_items))
total_carbon_reduction = 0
for item in combined_items:
st.markdown(f"**Recycling Idea for {item}:**")
response = get_recycling_suggestions_from_groq(item, 1)
carbon_reduction = max(0.5, min(2.5, carbon_reduction_data.get(item.lower(), 0) * 1))
total_carbon_reduction += carbon_reduction
st.write(response)
st.markdown(
f"""<p style="color: #2e7d32;">π Carbon Footprint Reduction: {carbon_reduction:.2f} kg COβ</p>""",
unsafe_allow_html=True,
)
st.write("---")
st.markdown(
f"""<div style="padding: 15px; text-align: center; background-color: #004d40; color: #ffffff; border-radius: 5px;">
π Total Carbon Footprint Reduction: <b>{total_carbon_reduction:.2f} kg COβ saved</b>
</div>""",
unsafe_allow_html=True,
)
else:
st.error("No recognizable waste items detected.")
elif action == "Get Suggestions for Items":
st.markdown(
"""
<div style="text-align: center; background-color: #fff3e0; padding: 10px; border-radius: 5px;">
<h3 style="color: #ff6f00;">Select clutter items for recycling suggestions:</h3>
</div>
""",
unsafe_allow_html=True,
)
selected_items = []
quantities = {}
cols = st.columns(len(predefined_clutter_items))
for i, (item, emoji) in enumerate(predefined_clutter_items.items()):
with cols[i]:
if st.checkbox(f"{emoji} {item.title()}", key=item):
selected_items.append(item)
quantities[item] = st.number_input(f"{item} (kg):", min_value=0.0, step=0.1, key=f"qty_{item}")
if selected_items and st.button("Generate Suggestions"):
total_carbon_reduction = 0
st.write("### β»οΈ Recycling Suggestions and Impact:")
for item, quantity in quantities.items():
if quantity > 0:
response = get_recycling_suggestions_from_groq(item, quantity)
carbon_reduction = max(0.5, min(2.5, carbon_reduction_data.get(item.lower(), 0) * quantity))
total_carbon_reduction += carbon_reduction
st.markdown(f"**{item} ({quantity} kg)**")
st.write(response)
st.markdown(
f"""<p style="color: #2e7d32;">π Carbon Footprint Reduction: {carbon_reduction:.2f} kg COβ</p>""",
unsafe_allow_html=True,
)
st.write("---")
st.markdown(
f"""<div style="padding: 15px; text-align: center; background-color: #004d40; color: #ffffff; border-radius: 5px;">
π Total Carbon Footprint Reduction: <b>{total_carbon_reduction:.2f} kg COβ saved</b>
</div>""",
unsafe_allow_html=True,
)
# Add session state for DIY instructions
if "diy_suggestion" not in st.session_state:
st.session_state.diy_suggestion = ""
suggestion = st.text_input("Enter a suggestion to get DIY instructions:", key="diy_input")
if st.button("Generate DIY Instructions"):
if suggestion:
st.session_state.diy_suggestion = get_diy_steps_from_groq(suggestion)
if st.session_state.diy_suggestion:
st.markdown(
f"""<div style="padding: 10px; background-color: #f0f4c3; color: #33691e; border-radius: 5px;">
<h4>π DIY Instructions:</h4>
{st.session_state.diy_suggestion}
</div>""",
unsafe_allow_html=True,
)
# Motivational Message
st.markdown(
"""
<div style="text-align: center; padding: 20px; background-color: #dcedc8; border-radius: 10px;">
<h3 style="color: #33691e;">π Let's Keep Our Planet Green!</h3>
<p style="color: #2e7d32;">Recycling is not just an action but a responsibility. Together, we can make a difference. β»οΈπ</p>
</div>
""",
unsafe_allow_html=True,
)
|