RamyKhorshed
commited on
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -1,32 +1,31 @@
|
|
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
|
12 |
img = PILImage.create(image)
|
13 |
|
14 |
# Get prediction
|
15 |
pred, pred_idx, probs = model.predict(img)
|
16 |
|
17 |
-
# Return prediction and
|
|
|
18 |
return {
|
19 |
-
"
|
|
|
20 |
}
|
21 |
|
22 |
-
# Create
|
23 |
-
|
24 |
fn=classify_image,
|
25 |
-
inputs=gr.Image(),
|
26 |
outputs=gr.Label(num_top_classes=2),
|
27 |
-
title="Cat
|
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 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from fastai.learner import load_learner
|
3 |
from fastai.vision.all import PILImage
|
|
|
4 |
|
5 |
+
# Load the model directly (since it will be in the same repository)
|
6 |
model = load_learner('model.pkl')
|
7 |
|
8 |
def classify_image(image):
|
9 |
+
# Convert to FastAI format
|
10 |
img = PILImage.create(image)
|
11 |
|
12 |
# Get prediction
|
13 |
pred, pred_idx, probs = model.predict(img)
|
14 |
|
15 |
+
# Return prediction and probability
|
16 |
+
confidence = float(probs[pred_idx])
|
17 |
return {
|
18 |
+
"Cat": confidence if str(pred).lower() == "cat" else 1 - confidence,
|
19 |
+
"Not Cat": confidence if str(pred).lower() != "cat" else 1 - confidence
|
20 |
}
|
21 |
|
22 |
+
# Create the interface
|
23 |
+
demo = gr.Interface(
|
24 |
fn=classify_image,
|
25 |
+
inputs=gr.Image(type="pil"),
|
26 |
outputs=gr.Label(num_top_classes=2),
|
27 |
+
title="🐱 Cat Detector",
|
28 |
description="Upload an image to check if it contains a cat!",
|
|
|
29 |
)
|
30 |
|
31 |
+
demo.launch()
|