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()