tahzaya commited on
Commit
de394d4
·
verified ·
1 Parent(s): 4dd0d0c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import numpy as np
4
+ import cv2
5
+ import tensorflow as tf
6
+
7
+ # Load model
8
+ model = tf.keras.models.load_model("trash_sorter.keras")
9
+
10
+ def classify_waste(image):
11
+ # Preprocess image
12
+ img = cv2.resize(image, (224, 224))
13
+ img = img / 255.0
14
+ img = np.expand_dims(img, axis=0)
15
+
16
+ # Predict
17
+ predictions = model.predict(img, verbose=0)[0]
18
+
19
+ # Get results
20
+ class_names = ["🗑️ ORGANIC", "♻️ RECYCLABLE"]
21
+ confidence = np.max(predictions)
22
+ predicted_class = class_names[np.argmax(predictions)]
23
+
24
+ # Format output
25
+ result = f"""
26
+ ## {predicted_class}
27
+
28
+ **Confidence:** {confidence:.2%}
29
+
30
+ **Breakdown:**
31
+ - Organic: {predictions[0]:.2%}
32
+ - Recyclable: {predictions[1]:.2%}
33
+
34
+ **What to do:**
35
+ { "Compost or dispose in green bin" if predicted_class == "🗑️ ORGANIC" else "Clean and place in recycling bin" }
36
+ """
37
+
38
+ return result
39
+
40
+ # Create interface
41
+ demo = gr.Interface(
42
+ fn=classify_waste,
43
+ inputs=gr.Image(type="numpy"),
44
+ outputs=gr.Markdown(),
45
+ title="🗑️ AI Trash Sorter",
46
+ description="Upload a photo of waste to classify as Organic or Recyclable",
47
+ examples=[
48
+ ["example_organic.jpg"],
49
+ ["example_recyclable.jpg"]
50
+ ]
51
+ )
52
+
53
+ # Launch
54
+ if __name__ == "__main__":
55
+ demo.launch()