shkumar156 commited on
Commit
e34d869
·
verified ·
1 Parent(s): 34e459d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -14,21 +14,44 @@ SUPPORTED_BREEDS = [
14
  model = tf.keras.models.load_model("cat_breed_model.keras")
15
 
16
  def predict(image: Image.Image):
17
- # Preprocess image
18
- img = image.resize((224, 224)).convert("RGB")
19
- x = np.array(img) / 255.0
20
- x = np.expand_dims(x, axis=0)
21
- preds = model.predict(x)[0] # shape: (12,)
22
- # Map each breed to its confidence
23
- result = {breed: float(conf) for breed, conf in zip(SUPPORTED_BREEDS, preds)}
24
- return result
 
 
 
 
25
 
26
  iface = gr.Interface(
27
  fn=predict,
28
  inputs=gr.Image(type="pil"),
29
  outputs=[gr.Label(num_top_classes=1, label="Predicted Breed")],
30
  title="Cat Breed Classifier",
31
- description="Upload a cat image to classify its breed."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
34
  if __name__ == "__main__":
 
14
  model = tf.keras.models.load_model("cat_breed_model.keras")
15
 
16
  def predict(image: Image.Image):
17
+ try:
18
+ if image is None:
19
+ return {breed: 0.0 for breed in SUPPORTED_BREEDS}
20
+ img = image.resize((224, 224)).convert("RGB")
21
+ x = np.array(img) / 255.0
22
+ x = np.expand_dims(x, axis=0)
23
+ preds = model.predict(x)[0] # shape: (12,)
24
+ result = {breed: float(conf) for breed, conf in zip(SUPPORTED_BREEDS, preds)}
25
+ return result
26
+ except Exception as e:
27
+ print(f"Prediction error: {e}")
28
+ return {breed: 0.0 for breed in SUPPORTED_BREEDS}
29
 
30
  iface = gr.Interface(
31
  fn=predict,
32
  inputs=gr.Image(type="pil"),
33
  outputs=[gr.Label(num_top_classes=1, label="Predicted Breed")],
34
  title="Cat Breed Classifier",
35
+ description="Upload a cat image to classify its breed.",
36
+ article="""
37
+ **Note:**
38
+ This project can detect only the following 12 cat breeds:
39
+
40
+ - Abyssinian
41
+ - Bengal
42
+ - Birman
43
+ - Bombay
44
+ - British Shorthair
45
+ - Egyptian Mau
46
+ - Maine Coon
47
+ - Persian
48
+ - Ragdoll
49
+ - Russian Blue
50
+ - Siamese
51
+ - Sphynx
52
+
53
+ *Please upload a clear image of a cat belonging to one of these breeds for best results.*
54
+ """
55
  )
56
 
57
  if __name__ == "__main__":