UNESCO / app.py
mohAhmad's picture
Update app.py
beaa2f5 verified
import gradio as gr
from transformers import pipeline
from PIL import Image
# Load the waste classification model
classifier = pipeline(
"image-classification",
model="watersplash/waste-classification"
)
# Recycling instructions & fun facts
waste_instructions = {
"plastic": "โ™ป๏ธ Place in plastic recycling bin. Avoid single-use plastics where possible.",
"glass": "๐Ÿพ Clean and recycle in glass bin. Broken glass should be wrapped safely before disposal.",
"paper": "๐Ÿ“„ Recycle clean paper. Avoid recycling wet or oily paper.",
"metal": "๐Ÿฅซ Recycle cans after rinsing. Large scrap metal should go to a recycling center.",
"cardboard": "๐Ÿ“ฆ Flatten boxes before recycling.",
"organic": "๐ŸŒฑ Compost food waste and garden scraps.",
"trash": "๐Ÿ—‘๏ธ Not recyclable. Dispose properly."
}
fun_facts = {
"plastic": "It can take up to 500 years for plastic to decompose!",
"glass": "Glass is 100% recyclable and can be recycled endlessly without loss of quality.",
"paper": "Recycling 1 ton of paper saves 17 trees and 7,000 gallons of water.",
"metal": "Aluminum cans can be recycled and back on shelves in as little as 60 days.",
"cardboard": "Cardboard can be recycled up to 5โ€“7 times before the fibers weaken.",
"organic": "Composting reduces landfill waste and returns nutrients to the soil.",
"trash": "Some waste cannot be recycled โ€” reduce and reuse where possible."
}
# Function to process uploaded image
def identify_waste(image):
# Resize image to prevent memory issues
image = image.resize((224, 224))
# Run classification
results = classifier(image)
label = results[0]["label"].lower()
if label in waste_instructions:
return f"๐Ÿ—‚๏ธ Detected Waste Type: {label.capitalize()}\n\nโœ… {waste_instructions[label]}\n\n๐Ÿ’ก Fun Fact: {fun_facts[label]}"
else:
return f"๐Ÿ—‚๏ธ Detected Waste Type: {label}\n\nโš ๏ธ No recycling info available. Please dispose responsibly."
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# โ™ป๏ธ Smart Waste Identifier")
gr.Markdown("Upload an image to identify the type of waste and get recycling instructions.")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Waste Image")
btn = gr.Button("Identify Waste")
with gr.Column():
output_text = gr.Textbox(label="Result", lines=6)
btn.click(fn=identify_waste, inputs=image_input, outputs=output_text)
gr.Markdown("---")
gr.Markdown("### โ„น๏ธ This app classifies input images into categories and provides recycling tips or fun facts.")
# Launch the app (no enable_queue, since older Gradio version)
demo.launch()