Spaces:
Sleeping
Sleeping
File size: 1,216 Bytes
bcff086 4e8c40d bcff086 4e8c40d bcff086 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import gradio as gr
from fastai.learner import load_learner
from fastai.vision.all import PILImage
from huggingface_hub import hf_hub_download
import torch
def load_model():
# Download the model from your model repository
model_path = hf_hub_download(
repo_id="RamyKhorshed/Lesson2FastAi",
filename="model.pkl",
repo_type="model"
)
return load_learner(model_path)
print("Loading model...")
model = load_model()
print("Model loaded!")
def predict_image(image):
# Convert to FastAI format
img = PILImage.create(image)
# Predict
pred, pred_idx, probs = model.predict(img)
# Format output
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 interface
demo = gr.Interface(
fn=predict_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!",
article="Upload any image and the model will predict whether it contains a cat or not."
)
demo.launch() |