busebaser
Fix: Convert image to Fastai PILImage before prediction
46b22e9
raw
history blame
1.02 kB
from fastai.vision.all import load_learner, PILImage
import gradio as gr
from PIL import Image
# Load the trained model
model = load_learner('model.pkl')
# 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()