Spaces:
Sleeping
Sleeping
Commit
·
4e8c40d
1
Parent(s):
65a079d
try new code
Browse files
app.py
CHANGED
@@ -1,7 +1,44 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastai.learner import load_learner
|
3 |
+
from fastai.vision.all import PILImage
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import torch
|
6 |
|
7 |
+
def load_model():
|
8 |
+
# Download the model from your model repository
|
9 |
+
model_path = hf_hub_download(
|
10 |
+
repo_id="RamyKhorshed/Lesson2FastAi",
|
11 |
+
filename="model.pkl",
|
12 |
+
repo_type="model"
|
13 |
+
)
|
14 |
+
return load_learner(model_path)
|
15 |
+
|
16 |
+
print("Loading model...")
|
17 |
+
model = load_model()
|
18 |
+
print("Model loaded!")
|
19 |
+
|
20 |
+
def predict_image(image):
|
21 |
+
# Convert to FastAI format
|
22 |
+
img = PILImage.create(image)
|
23 |
+
|
24 |
+
# Predict
|
25 |
+
pred, pred_idx, probs = model.predict(img)
|
26 |
+
|
27 |
+
# Format output
|
28 |
+
confidence = float(probs[pred_idx])
|
29 |
+
return {
|
30 |
+
"Cat": confidence if str(pred).lower() == "cat" else 1 - confidence,
|
31 |
+
"Not Cat": confidence if str(pred).lower() != "cat" else 1 - confidence
|
32 |
+
}
|
33 |
+
|
34 |
+
# Create interface
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=predict_image,
|
37 |
+
inputs=gr.Image(type="pil"),
|
38 |
+
outputs=gr.Label(num_top_classes=2),
|
39 |
+
title="🐱 Cat Detector",
|
40 |
+
description="Upload an image to check if it contains a cat!",
|
41 |
+
article="Upload any image and the model will predict whether it contains a cat or not."
|
42 |
+
)
|
43 |
|
|
|
44 |
demo.launch()
|