busebaser
Fix: Added missing function is_cat before loading model
c5b14f4
raw
history blame
1.21 kB
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)
print(f"βœ… Prediction successful: {pred}, Confidence: {probs.max():.2f}")
return f"Prediction: {pred} (Confidence: {probs.max():.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()