dk00069 commited on
Commit
669aec5
·
verified ·
1 Parent(s): b0885a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -1,33 +1,37 @@
1
  import os
 
2
  os.environ["TF_USE_LEGACY_KERAS"] = "1"
3
 
4
  import gradio as gr
5
  import numpy as np
6
  import tensorflow as tf
7
- import tf_keras # This is the secret weapon
8
 
9
- # Use tf_keras instead of tf.keras to load the model
10
- model = tf_keras.models.load_model(
11
- "model.h5",
12
- compile=False
13
- )
14
 
 
15
  labels = ["Cardboard", "Glass", "Metal", "Paper", "Plastic", "Trash"]
16
 
17
  def classify_waste(image):
18
- # Match the 224x224 size from your error log
19
  img = image.resize((224, 224))
20
  img_array = np.array(img) / 255.0
21
  img_array = np.expand_dims(img_array, axis=0)
 
 
22
  predictions = model.predict(img_array)[0]
 
 
23
  return {labels[i]: float(predictions[i]) for i in range(len(labels))}
24
 
 
25
  demo = gr.Interface(
26
  fn=classify_waste,
27
  inputs=gr.Image(type="pil"),
28
  outputs=gr.Label(num_top_classes=3),
29
- title="Waste Classifier",
30
- description="Upload a waste image to classify it."
31
  )
32
 
33
  demo.launch()
 
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
+ import tf_keras # This is the dedicated legacy library
9
 
10
+ # Load the model using the legacy-specific loader
11
+ model = tf_keras.models.load_model("model.h5", compile=False)
 
 
 
12
 
13
+ # Labels for your waste classification
14
  labels = ["Cardboard", "Glass", "Metal", "Paper", "Plastic", "Trash"]
15
 
16
  def classify_waste(image):
17
+ # Preprocessing: Match the 224x224 input size from your error log
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
+ # Run prediction
23
  predictions = model.predict(img_array)[0]
24
+
25
+ # Return top results in Gradio format
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 of waste to see the classification."
35
  )
36
 
37
  demo.launch()