import json import numpy as np import tensorflow as tf from PIL import Image import gradio as gr # Load model model = tf.keras.models.load_model("animal_cnn.keras") # Load class names with open("class_names.json", "r") as f: class_names = json.load(f) # Preprocess function def preprocess_image(image): image = image.resize((224, 224)) image = np.array(image) / 255.0 image = np.expand_dims(image, axis=0) return image # Prediction function def predict(image): image = preprocess_image(image) predictions = model.predict(image)[0] top_index = np.argmax(predictions) confidence = float(predictions[top_index]) return f"{class_names[top_index]} ({confidence*100:.2f}%)" # Gradio UI interface = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs="text", title="🐾 Animal Classifier", description="Upload an image to detect the animal" ) interface.launch()