File size: 865 Bytes
c778217
 
 
 
d0d099b
 
 
ac68917
d0d099b
 
c778217
 
d0d099b
c778217
 
 
 
 
d0d099b
c778217
 
 
 
ac68917
d0d099b
c778217
 
 
5ed9eb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)