Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load model
|
| 6 |
+
model = tf.keras.models.load_model("model.h5")
|
| 7 |
+
|
| 8 |
+
# Labels
|
| 9 |
+
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs',
|
| 10 |
+
'metal', 'organic', 'paper', 'plastic']
|
| 11 |
+
|
| 12 |
+
# Image preprocessing and prediction
|
| 13 |
+
def predict_image(img):
|
| 14 |
+
img = tf.image.resize(img, (224, 224))
|
| 15 |
+
img = tf.cast(img, tf.float32) / 255.0
|
| 16 |
+
img = tf.expand_dims(img, axis=0)
|
| 17 |
+
preds = model.predict(img)
|
| 18 |
+
pred_idx = np.argmax(preds)
|
| 19 |
+
confidence = float(np.max(preds)) * 100
|
| 20 |
+
return {class_names[pred_idx]: confidence}
|
| 21 |
+
|
| 22 |
+
# Gradio app
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=predict_image,
|
| 25 |
+
inputs=gr.Image(type="numpy"),
|
| 26 |
+
outputs=gr.Label(num_top_classes=3),
|
| 27 |
+
title="♻️ Waste Classifier",
|
| 28 |
+
description="Classify waste into 9 categories: batteries, clothes, e-waste, glass, light bulbs, metal, organic, paper, plastic."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
demo.launch()
|