Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load image classifier (general-purpose)
|
| 6 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 7 |
+
|
| 8 |
+
# Define recyclable classes (based on common outputs of the model)
|
| 9 |
+
RECYCLABLE_CLASSES = {
|
| 10 |
+
"plastic bottle", "water bottle", "can", "glass", "cup",
|
| 11 |
+
"paper", "newspaper", "cardboard", "box", "carton", "tin"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def classify_trash(image):
|
| 15 |
+
results = classifier(image)
|
| 16 |
+
top_label = results[0]['label'].lower()
|
| 17 |
+
confidence = results[0]['score']
|
| 18 |
+
|
| 19 |
+
is_recyclable = any(recycle_word in top_label for recycle_word in RECYCLABLE_CLASSES)
|
| 20 |
+
|
| 21 |
+
label = "♻️ Recyclable" if is_recyclable else "🗑️ Not Recyclable"
|
| 22 |
+
return f"{label}\nDetected: {top_label}\nConfidence: {confidence:.2%}"
|
| 23 |
+
|
| 24 |
+
# Gradio interface
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=classify_trash,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=gr.Textbox(label="Classification"),
|
| 29 |
+
title="♻️ Trash Classifier: Recyclable or Not?",
|
| 30 |
+
description="Upload an image of an object (like a bottle, banana peel, or can) and find out if it is recyclable.",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch()
|