Sagar Sanghani commited on
Commit ·
e095ac0
1
Parent(s): beebda2
working app
Browse files
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
10.jpg
ADDED
|
14.jpg
ADDED
|
3.jpg
ADDED
|
5.jpg
ADDED
|
6.jpg
ADDED
|
7.jpg
ADDED
|
8.jpg
ADDED
|
app.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
CHANGED
|
@@ -1,7 +1,39 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastai.vision.all import PILImage, load_learner
|
| 3 |
+
categories = ('Cat', 'Dog')
|
| 4 |
|
| 5 |
+
interface_title = "FastAI Cat vs. Dog Classifier 🐶"
|
|
|
|
| 6 |
|
| 7 |
+
interface_description = """
|
| 8 |
+
The model was built on resnet18 and trained on Kaggle's Dogs vs Cats competition.
|
| 9 |
+
The full code is available here: https://www.kaggle.com/code/sagsan/dogs-vs-cats-fastai
|
| 10 |
+
|
| 11 |
+
To use it, upload an image or select one of the examples below.
|
| 12 |
+
The output shows model's confidence scores for each category.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def get_labels(fn):
|
| 16 |
+
fn_str = str(fn).lower() # Get the filename as a lowercase string
|
| 17 |
+
|
| 18 |
+
if 'dog' in fn_str:
|
| 19 |
+
return 'dog'
|
| 20 |
+
elif 'cat' in fn_str:
|
| 21 |
+
return 'cat'
|
| 22 |
+
else:
|
| 23 |
+
# Crucial for safety, even if you assume the data is clean
|
| 24 |
+
# It catches any unexpected file that has neither 'dog' nor 'cat'
|
| 25 |
+
raise ValueError(f"File must be labeled 'dog' or 'cat', but is not: {fn}")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def classify_image(img):
|
| 29 |
+
pred, idx, probs = learn.predict(img)
|
| 30 |
+
return dict(zip(categories, map(float, probs)))
|
| 31 |
+
|
| 32 |
+
learn = load_learner("./model.pkl")
|
| 33 |
+
|
| 34 |
+
image = gr.Image(width=192, height=192)
|
| 35 |
+
label = gr.Label()
|
| 36 |
+
examples = ["3.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "10.jpg", "14.jpg"]
|
| 37 |
+
|
| 38 |
+
intf = gr.Interface(fn=classify_image, examples=examples, inputs=image, outputs=label, title=interface_title, description=interface_description)
|
| 39 |
+
intf.launch(inline=False)
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ee93cde39750da474111ae540016156df7eb233ff5086a38e1ee1fb9031d792
|
| 3 |
+
size 47298631
|