Spaces:
Sleeping
Sleeping
Commit
·
777b484
1
Parent(s):
8b9731f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import *
|
2 |
+
import gradio as gr
|
3 |
+
# any external function used for labeling needs to be included in here
|
4 |
+
def is_cat(x): return x[0].isupper()
|
5 |
+
# this learner 'pkl' file is exactly the same as what you get when you trained it
|
6 |
+
# example : learn = vision_learner(dls,resnet18,metrics=error_rate)
|
7 |
+
# example : learn.fine_tune(3)
|
8 |
+
learn=load_learner('catsdogsmodel.pkl')
|
9 |
+
# Preping data for gradio, We are creating a dictionary for gradio.
|
10 |
+
# One of the annoying things about 'gradio' is that it does not recognize tensor number and probabilities.
|
11 |
+
# In fact, numpy either
|
12 |
+
categories = ('Dog', 'Cat')
|
13 |
+
|
14 |
+
# prediction, index & probabilities, gradio expects a dictinoary
|
15 |
+
def classify_image(img):
|
16 |
+
pred,idx,probs = learn.predict(img)
|
17 |
+
return dict(zip(categories,map(float, probs)))
|
18 |
+
|
19 |
+
image = gr.inputs.Image(shape=(192,192))
|
20 |
+
label = gr.outputs.Label()
|
21 |
+
examples = ['dog.jpg','dog2.jpg','cat.jpg']
|
22 |
+
|
23 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
|
24 |
+
# to create a public link, set 'share=True' in 'launch()'
|
25 |
+
intf.launch(inline=False)
|