busebaser
Fix: Convert boolean prediction to 'Cat' or 'Dog'
4a1b9e9
from fastai.vision.all import load_learner, PILImage
import gradio as gr
from PIL import Image
# Define the missing function
def is_cat(x):
return x[0].isupper()
# Load the trained model
try:
model = load_learner('model.pkl')
print("βœ… Model loaded successfully")
except Exception as e:
print(f"❌ Error loading model: {e}")
# Define a function to make predictions
def predict(image):
try:
print("πŸ“Έ Received image for prediction")
# Convert to Fastai's expected PILImage format
image = PILImage.create(image)
# Run prediction
pred, _, probs = model.predict(image)
# Convert boolean prediction to "Cat" or "Dog"
label = "Cat" if pred else "Dog"
confidence = float(probs.max()) # Convert Tensor to float
print(f"βœ… Prediction successful: {label}, Confidence: {confidence:.2f}")
return f"Prediction: {label} (Confidence: {confidence:.2f})"
except Exception as e:
print(f"❌ Error during prediction: {e}")
return f"Error: {e}"
# Create the Gradio web interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(),
title="Cat vs Dog Classifier",
description="Upload an image of a cat or dog and let the model classify it!"
)
# Launch the Gradio app
interface.launch()