Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| # Load your model | |
| model = tf.keras.models.load_model("animal_classifier.h5") | |
| def predict_image(img): | |
| # Preprocess | |
| img = img.resize((128, 128)) | |
| img_array = np.array(img) / 255.0 | |
| img_batch = np.expand_dims(img_array, axis=0) | |
| # Predict | |
| pred = model.predict(img_batch, verbose=0)[0][0] | |
| label = "dog" if pred > 0.5 else "cat" | |
| confidence = float(pred) if pred > 0.5 else float(1 - pred) | |
| return {label: confidence} | |
| # Gradio interface | |
| demo = gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Label(num_top_classes=2), | |
| examples=[], | |
| title="🐱 vs 🐶 Cat or Dog Classifier", | |
| description="Trained on only 100 images! Upload a photo to see the prediction." | |
| ) | |
| demo.launch() |