File size: 2,074 Bytes
93820a0
 
 
 
 
 
 
 
 
 
 
 
 
 
c1767e4
93820a0
 
 
 
 
c1767e4
93820a0
 
 
 
1615050
 
93820a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d6832e
93820a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1615050
93820a0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1F6tnDcfVlChPXwu4UwSHq-bggJlcmyj-

# This notebook makes predictions based on saved model.

#Import Libraries
"""
#|default_exp app

'''
#|export
!pip install -Uqq fastai
!pip install -Uqq fastbook
!pip install -Uqq gradio
!pip install -Uqq nbdev
'''
"""#Dogs Vs Cats"""

#|export
from fastai.vision.all import *
import gradio as gr

#|export
def is_cat(x): return x[0].isupper()

'''
im = PILImage.create('/content/dog.JPG')
im.thumbnail((192,192))
im
'''
#|export
#Use the model.pkl file that is the classifier model
learn = load_learner('/content/model.pkl')

# Commented out IPython magic to ensure Python compatibility.
#Now predicted the image to determine if it is a Cat ? It correctly predicts that it is NOT a Cat.
#learn.predict(im)

# %time learn.predict(im)

"""#Now create a GRADIO Interface that has this information

Gradio requires us to give it a function that it is going to call. Our's function is `classify_image(img)`

learn.predict(img) returns 3 things :
pred --> Prediction as a String
idx --> Its index
probs --> Probability that the image is a Cat ?

Gradio wants to get back a dictionary containing each of the posible categories -- which is this case is a DOg or a Cat -- and the probability of each one..

"""

#|export
categories = ('Dog','Cat')

def classify_image(img):
  pred,idx,probs = learn.predict(img)
  return dict(zip(categories,map(float,probs)))

#classify_image(im)

"""#Now creating our GRADIO Interface"""

#|export
image = gr.Image(height=192,width=192)
label = "label"
examples = ['/content/dog.JPG','/content/cat.JPG','/content/dunno.JPG']
intf = gr.Interface(fn = classify_image,inputs=image,outputs=label,examples=examples)
intf.launch(inline=False)

"""#Export the notebook as Python script"""
'''
!pip install -Uqq nbdev

#from nbdev.export import notebook2script

from nbdev import nbdev_export

!pip install -Uqq nbconvert

!jupyter nbconvert app.ipynb --to python
'''