isana25 commited on
Commit
1e635e1
·
verified ·
1 Parent(s): fbb8092

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -2,12 +2,13 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
- # Load your model from Hugging Face repo (or local file if testing)
7
  model = tf.keras.models.load_model("animal_classifier.keras")
8
 
9
- # Dynamically get class names from model output layer
10
- class_names = list(model.class_names) if hasattr(model, 'class_names') else ["dog", "lion", "tiger"] # fallback if not stored in model
11
 
12
  def predict_image(image):
13
  img = image.resize((224, 224))
@@ -18,12 +19,14 @@ def predict_image(image):
18
  confidence = np.max(preds)
19
  predicted_index = np.argmax(preds)
20
 
21
- threshold = 0.5
 
22
  if confidence < threshold:
23
  return "Image not recognized as any animal in the dataset"
24
  else:
25
  return class_names[predicted_index]
26
 
27
- demo = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text", title="MobileNetV2 Animal Classifier")
 
28
 
29
  demo.launch()
 
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
+ import json
6
 
7
+ # Load model and class names JSON
8
  model = tf.keras.models.load_model("animal_classifier.keras")
9
 
10
+ with open("class_names.json", "r") as f:
11
+ class_names = json.load(f)
12
 
13
  def predict_image(image):
14
  img = image.resize((224, 224))
 
19
  confidence = np.max(preds)
20
  predicted_index = np.argmax(preds)
21
 
22
+ threshold = 0.5 # minimum confidence to accept prediction
23
+
24
  if confidence < threshold:
25
  return "Image not recognized as any animal in the dataset"
26
  else:
27
  return class_names[predicted_index]
28
 
29
+ demo = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text",
30
+ title="MobileNetV2 Animal Classifier")
31
 
32
  demo.launch()