File size: 2,607 Bytes
89f852d
 
 
 
 
2ac26ce
89f852d
 
2ac26ce
89f852d
 
 
2ac26ce
 
 
89f852d
2ac26ce
 
 
 
 
 
89f852d
 
 
 
2ac26ce
 
89f852d
 
2ac26ce
 
89f852d
 
2ac26ce
89f852d
2ac26ce
89f852d
2ac26ce
89f852d
 
 
2ac26ce
89f852d
 
 
2ac26ce
 
89f852d
 
 
 
 
 
 
 
 
2ac26ce
89f852d
 
 
2ac26ce
89f852d
 
 
 
 
 
 
 
2ac26ce
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
import gradio as gr
from PIL import Image
import random

# -------------------------------
# Classification logic
# -------------------------------
def classify_item(image, description):
    categories = ["Recyclable", "Compostable", "Trash", "Harmful"]

    if description:
        desc = description.lower()

        # Compostable: fruits & vegetables
        if "banana" in desc or "apple" in desc or "fruit" in desc or "vegetable" in desc or "food" in desc or "peel" in desc or "leaf" in desc:
            category = "Compostable"

        # Harmful: medical + batteries
        elif "syringe" in desc or "needle" in desc or "battery" in desc or "cell" in desc:
            category = "Harmful"

        # Recyclable
        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"

        # Trash
        elif "pizza box" in desc or "styrofoam" in desc or "chip bag" in desc:
            category = "Trash"

        # Fallback
        else:
            category = random.choice(categories)

    elif image:
        # Placeholder – replace with ML image model later
        category = random.choice(categories)

    else:
        return "No input", "⚠️ Please upload an image or type a description."

    # Eco 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.",
        "Harmful": "⚠️ Special disposal needed. Take syringes, needles, and batteries to hazardous waste collection centers."
    }

    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, Trash, or Harmful.")

    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', 'syringe')")

    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()