Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,31 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# Force legacy Keras logic
|
| 3 |
-
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import tensorflow as tf
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
model =
|
| 12 |
|
| 13 |
-
# Labels for your waste classification
|
| 14 |
labels = ["Cardboard", "Glass", "Metal", "Paper", "Plastic", "Trash"]
|
| 15 |
|
| 16 |
def classify_waste(image):
|
| 17 |
-
#
|
| 18 |
img = image.resize((224, 224))
|
| 19 |
img_array = np.array(img) / 255.0
|
| 20 |
img_array = np.expand_dims(img_array, axis=0)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
predictions = model.predict(img_array)[0]
|
| 24 |
|
| 25 |
-
#
|
| 26 |
return {labels[i]: float(predictions[i]) for i in range(len(labels))}
|
| 27 |
|
| 28 |
-
# Setup the interface
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=classify_waste,
|
| 31 |
inputs=gr.Image(type="pil"),
|
| 32 |
outputs=gr.Label(num_top_classes=3),
|
| 33 |
title="♻️ AI Waste Classifier",
|
| 34 |
-
description="Upload an image
|
| 35 |
)
|
| 36 |
|
| 37 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Standard loading - TF 2.12 will handle your .h5 file correctly
|
| 7 |
+
model = tf.keras.models.load_model("model.h5", compile=False)
|
| 8 |
|
|
|
|
| 9 |
labels = ["Cardboard", "Glass", "Metal", "Paper", "Plastic", "Trash"]
|
| 10 |
|
| 11 |
def classify_waste(image):
|
| 12 |
+
# Resize and normalize
|
| 13 |
img = image.resize((224, 224))
|
| 14 |
img_array = np.array(img) / 255.0
|
| 15 |
img_array = np.expand_dims(img_array, axis=0)
|
| 16 |
|
| 17 |
+
# Predict
|
| 18 |
predictions = model.predict(img_array)[0]
|
| 19 |
|
| 20 |
+
# Create dictionary for Gradio
|
| 21 |
return {labels[i]: float(predictions[i]) for i in range(len(labels))}
|
| 22 |
|
|
|
|
| 23 |
demo = gr.Interface(
|
| 24 |
fn=classify_waste,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
outputs=gr.Label(num_top_classes=3),
|
| 27 |
title="♻️ AI Waste Classifier",
|
| 28 |
+
description="Upload an image to identify waste categories."
|
| 29 |
)
|
| 30 |
|
| 31 |
demo.launch()
|