reddotapp25's picture
Update app.py
d0d099b verified
Raw
History Blame Contribute Delete
865 Bytes
import gradio as gr
from fastai.vision.all import *
import pathlib
# 1. Define the labeling function EXACTLY as it was used in Colab.
# This MUST be defined before load_learner is called.
def is_cat(x): return x[0].isupper()
# 2. Load the model
# Fastai will now find 'is_cat' and use it to map the model labels.
learn = load_learner('cat_dog_classifier.pkl')
# 3. Prediction logic
def predict_image(img):
img = PILImage.create(img)
pred, pred_idx, probs = learn.predict(img)
return {str(pred): float(probs[pred_idx])}
# 4. Gradio Interface
demo = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=2),
title="🐱 Cat vs Dog Classifier",
description="Upload a photo to see if it's a Cat or a Dog!"
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)