Spaces:
Configuration error
Configuration error
import gradio as gr from PIL import Image import random # ------------------------------- # Simple classification logic # ------------------------------- def classify_item(image, description): categories = ["Recyclable", "Compostable", "Trash"] if description: desc = description.lower() if "banana" in desc or "food" in desc or "peel" in desc or "leaf" in desc: category = "Compostable" elif "plastic" in desc or "bottle" in desc or "can" in desc or "metal" in desc: category = "Recyclable" elif "paper" in desc and "greasy" not in desc: category = "Recyclable" elif "pizza box" in desc or "styrofoam" in desc or "chip bag" in desc: category = "Trash" else: category = random.choice(categories) elif image: # Placeholder – replace with ML model if you train one category = random.choice(categories) else: return "No input", "⚠️ Please upload an image or type a description." # Tips tips = { "Recyclable": "♻️ Rinse before recycling. Check local rules for plastics.", "Compostable": "🌱 Add to compost bin or green waste collection.", "Trash": "🗑️ Not recyclable. Consider reusable alternatives." } return category, tips.get(category, "Check local disposal guidelines.") # ------------------------------- # Gradio UI # ------------------------------- with gr.Blocks() as demo: gr.Markdown("# 🌍 EcoSort: Smart Waste Classifier") gr.Markdown("Upload an **image** or type a **description** to check if it's Recyclable, Compostable, or Trash.") with gr.Row(): image_input = gr.Image(type="pil", label="Upload Image") text_input = gr.Textbox(label="Or type a description (e.g., 'banana peel', 'plastic bottle')") output_label = gr.Label(label="Prediction") output_tip = gr.Textbox(label="Eco-Friendly Tip", interactive=False) btn = gr.Button("Classify") btn.click(fn=classify_item, inputs=[image_input, text_input], outputs=[output_label, output_tip]) demo.launch()
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# -------------------------------
|
| 6 |
+
# Simple classification logic
|
| 7 |
+
# -------------------------------
|
| 8 |
+
def classify_item(image, description):
|
| 9 |
+
categories = ["Recyclable", "Compostable", "Trash"]
|
| 10 |
+
|
| 11 |
+
if description:
|
| 12 |
+
desc = description.lower()
|
| 13 |
+
if "banana" in desc or "food" in desc or "peel" in desc or "leaf" in desc:
|
| 14 |
+
category = "Compostable"
|
| 15 |
+
elif "plastic" in desc or "bottle" in desc or "can" in desc or "metal" in desc:
|
| 16 |
+
category = "Recyclable"
|
| 17 |
+
elif "paper" in desc and "greasy" not in desc:
|
| 18 |
+
category = "Recyclable"
|
| 19 |
+
elif "pizza box" in desc or "styrofoam" in desc or "chip bag" in desc:
|
| 20 |
+
category = "Trash"
|
| 21 |
+
else:
|
| 22 |
+
category = random.choice(categories)
|
| 23 |
+
elif image:
|
| 24 |
+
# Placeholder – replace with ML model if you train one
|
| 25 |
+
category = random.choice(categories)
|
| 26 |
+
else:
|
| 27 |
+
return "No input", "⚠️ Please upload an image or type a description."
|
| 28 |
+
|
| 29 |
+
# Tips
|
| 30 |
+
tips = {
|
| 31 |
+
"Recyclable": "♻️ Rinse before recycling. Check local rules for plastics.",
|
| 32 |
+
"Compostable": "🌱 Add to compost bin or green waste collection.",
|
| 33 |
+
"Trash": "🗑️ Not recyclable. Consider reusable alternatives."
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
return category, tips.get(category, "Check local disposal guidelines.")
|
| 37 |
+
|
| 38 |
+
# -------------------------------
|
| 39 |
+
# Gradio UI
|
| 40 |
+
# -------------------------------
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# 🌍 EcoSort: Smart Waste Classifier")
|
| 43 |
+
gr.Markdown("Upload an **image** or type a **description** to check if it's Recyclable, Compostable, or Trash.")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 47 |
+
text_input = gr.Textbox(label="Or type a description (e.g., 'banana peel', 'plastic bottle')")
|
| 48 |
+
|
| 49 |
+
output_label = gr.Label(label="Prediction")
|
| 50 |
+
output_tip = gr.Textbox(label="Eco-Friendly Tip", interactive=False)
|
| 51 |
+
|
| 52 |
+
btn = gr.Button("Classify")
|
| 53 |
+
btn.click(fn=classify_item, inputs=[image_input, text_input], outputs=[output_label, output_tip])
|
| 54 |
+
|
| 55 |
+
demo.launch()
|