File size: 883 Bytes
bcad1bc
 
 
 
b6c491d
bcad1bc
 
 
b6c491d
bcad1bc
 
 
 
 
b6c491d
 
bcad1bc
b6c491d
 
bcad1bc
 
b6c491d
 
bcad1bc
b6c491d
bcad1bc
b6c491d
bcad1bc
 
 
b6c491d
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
30
31
import gradio as gr
from fastai.learner import load_learner
from fastai.vision.all import PILImage

# Load the model directly (since it will be in the same repository)
model = load_learner('model.pkl')

def classify_image(image):
    # Convert to FastAI format
    img = PILImage.create(image)
    
    # Get prediction
    pred, pred_idx, probs = model.predict(img)
    
    # Return prediction and probability
    confidence = float(probs[pred_idx])
    return {
        "Cat": confidence if str(pred).lower() == "cat" else 1 - confidence,
        "Not Cat": confidence if str(pred).lower() != "cat" else 1 - confidence
    }

# Create the interface
demo = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=2),
    title="🐱 Cat Detector",
    description="Upload an image to check if it contains a cat!",
)

demo.launch()