Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from fastai.learner import load_learner
|
| 4 |
+
from fastai.vision.all import PILImage
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
model = load_learner('model.pkl')
|
| 9 |
+
|
| 10 |
+
def classify_image(image):
|
| 11 |
+
# Convert to FastAI PILImage
|
| 12 |
+
img = PILImage.create(image)
|
| 13 |
+
|
| 14 |
+
# Get prediction
|
| 15 |
+
pred, pred_idx, probs = model.predict(img)
|
| 16 |
+
|
| 17 |
+
# Return prediction and confidence
|
| 18 |
+
return {
|
| 19 |
+
"cat": float(probs[pred_idx]) if str(pred) == "cat" else 1 - float(probs[pred_idx])
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Create Gradio interface
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=classify_image,
|
| 25 |
+
inputs=gr.Image(),
|
| 26 |
+
outputs=gr.Label(num_top_classes=2),
|
| 27 |
+
title="Cat Classifier",
|
| 28 |
+
description="Upload an image to check if it contains a cat!",
|
| 29 |
+
examples=["example1.jpg", "example2.jpg"] # Optional: Add example images if you have them
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
iface.launch()
|