Sara Tolosa commited on
Commit
025e1b4
1 Parent(s): 75059ff

Cat vs Dog classifier

Browse files
Files changed (1) hide show
  1. app2.py +32 -0
app2.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This script is used to create a Gradio interface in which we have a
2
+ # dog vs cat classifier using the fastai library. For more explanation,
3
+ # visit the Google Colab notebook associated.
4
+
5
+ from fastai.vision.all import *
6
+ import gradio as gr
7
+
8
+ # Define label function
9
+ def is_cat(x): return x[0].isupper()
10
+
11
+ # Load our model
12
+ learner = load_learner('model.pkl')
13
+
14
+
15
+ # Transform our model to obtain results that Gradio can handle with
16
+ categories = ('Dog', 'Cat')
17
+
18
+ def classify_image(img):
19
+ # We are saying that this predictions returns: the prediction, its index and the prediction probability
20
+ pred,idx,probs = learn.predict(img)
21
+
22
+ # Here we return a dictionary with categories as keys and its probabilities as values
23
+ return dict(zip(categories, map(float, probs)))
24
+
25
+
26
+ # Create the Gradio interface
27
+ image = gr.inputs.Image(shape=(192,192))
28
+ label = gr.outputs.Label()
29
+ examples = ['dogg.jpg', 'cat.jpg', 'dunno.jpg']
30
+
31
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
32
+ intf.launch(inline=False, share=True)