File size: 1,706 Bytes
34e459d
 
 
 
88bae0f
34e459d
82b0ec9
88bae0f
 
34e459d
82b0ec9
34e459d
 
 
e34d869
 
82b0ec9
88bae0f
82b0ec9
e34d869
82b0ec9
e34d869
88bae0f
82b0ec9
88bae0f
82b0ec9
 
88bae0f
52b4bf7
 
82b0ec9
34e459d
82b0ec9
34e459d
 
82b0ec9
 
 
d337b9f
 
e34d869
82b0ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e34d869
34e459d
 
 
82b0ec9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import json

# 🔹 Load class names from JSON file
with open("cat_class_names.json", "r") as f:
    SUPPORTED_CLASSES = json.load(f)

# Load the trained model
model = tf.keras.models.load_model("cat_breed_model.keras")

def predict(image: Image.Image):
    try:
        if image is None:
            return {cls: 0.0 for cls in SUPPORTED_CLASSES}

        # Preprocess the image
        img = image.resize((224, 224)).convert("RGB")
        x = np.array(img) / 255.0
        x = np.expand_dims(x, axis=0)

        # Predict
        preds = model.predict(x)[0]  # shape: (13,)
        result = {cls: float(conf) for cls, conf in zip(SUPPORTED_CLASSES, preds)}
        return result

    except Exception as e:
        print(f"Prediction error: {e}")
        return {cls: 0.0 for cls in SUPPORTED_CLASSES}

# Define Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=1, label="Predicted Class"),
    title="Cat Breed Classifier",
    description="Upload a cat image to classify its breed. "
    "Upload a clear image of the cat with minimal background for best results.",
    article="""
**Supported Cat Breeds:**
- Abyssinian
- Bengal
- Birman
- Bombay
- British Shorthair
- Egyptian Mau
- Maine Coon
- Persian
- Ragdoll
- Russian Blue
- Siamese
- Sphynx

⚠️ **Note:** If you upload a cat image from a breed that is **not listed above** (e.g., Devon Rex, Turkish Van), the model may still return a prediction, but it will likely be for the most **visually similar** breed it was trained on.
"""
)

if __name__ == "__main__":
    iface.launch()