cat_vs_dog / app.py
mramjad's picture
Update app.py
8e65ee6 verified
import gradio as gr
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import tempfile
# Load model
model = load_model('cats_dogs_model.h5')
CLASS_NAMES = ['cat', 'dog']
def predict_image(img):
# Preprocess image
img = img.resize((224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
# Make prediction
predictions = model.predict(img_array)
predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
confidence = float(np.max(predictions[0]))
return {predicted_class: confidence, "other_class": 1 - confidence}
# Create Gradio interface
interface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil", label="Upload Pet Image"),
outputs=gr.Label(num_top_classes=2),
title="Cat vs Dog Classifier 🐱 vs 🐶",
description="Upload an image of a cat or dog to classify it",
examples=[["cat_example.jpg"], ["dog_example.jpg"]]
)
if __name__ == "__main__":
interface.launch(share=True)