| import gradio as gr |
| import numpy as np |
| import tensorflow as tf |
| from PIL import Image |
|
|
| |
| MODEL_PATH = "cats-vs-dogs-finetuned.keras" |
| |
| IMAGE_SIZE = (180, 180) |
|
|
| |
| model = tf.keras.models.load_model(MODEL_PATH) |
|
|
| def predict_image(img: Image.Image): |
| |
| if img is None: |
| return {"Cat": 0.5, "Dog": 0.5} |
|
|
| |
| x = img.convert("RGB").resize(IMAGE_SIZE) |
| x = np.asarray(x, dtype=np.float32) / 255.0 |
| x = np.expand_dims(x, 0) |
|
|
| |
| p_dog = float(model.predict(x)[0, 0]) |
| return {"Cat": 1.0 - p_dog, "Dog": p_dog} |
|
|
| demo = gr.Interface( |
| fn=predict_image, |
| inputs=gr.Image(type="pil", label="Upload a Cat or Dog"), |
| outputs=gr.Label(num_top_classes=2, label="Prediction"), |
| title="Cats vs Dogs (Keras, single-logit)", |
| description="keras image classification model for cat-vs-dog images" |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|