sja820 commited on
Commit
fcdb587
·
verified ·
1 Parent(s): 3255e50

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of dogs_cats.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1pu75-TcRCtcDPFHn3zA6IqSsH8BM5yka
8
+
9
+ ## Gradio Pets
10
+
11
+ # New Section
12
+ """
13
+
14
+ # !pip install -Uqq fastai
15
+
16
+
17
+ #/ default_exp app
18
+
19
+ from fastai.vision.all import *
20
+ import gradio as gr
21
+
22
+ def is_cat(x): return x[0].isupper()
23
+
24
+ # path = untar_data(URLs.PETS)/'images'
25
+
26
+ # dls = ImageDataLoaders.from_name_func('.',
27
+ # get_image_files(path), valid_pct=0.2, seed=42, #giving
28
+ # label_func=is_cat,
29
+ # item_tfms=Resize(192))
30
+
31
+ # dls.show_batch()
32
+
33
+ # learn = vision_learner(dls, resnet18, metrics=error_rate)
34
+ # learn.fine_tune(3)
35
+
36
+ # learn.export('model.pkl')
37
+
38
+ # cell
39
+ learn= load_learner('model.pk1')
40
+
41
+ categories= ('Dog', 'Cat')
42
+
43
+ # # function we need to define for gradio
44
+
45
+ # # predcition is a string and prob ius a
46
+
47
+ # # grradio wants dict with each category and prob of each
48
+
49
+ # # zip -- putting together .... dict-- putting into correct format
50
+
51
+ # # gradio doesnt work with tensors thus need to convert to float
52
+
53
+
54
+ def classify_image(img):
55
+ pred,idx,probs= learn.predict(img)
56
+ return dict(zip(categories, map(float, probs)))
57
+
58
+ im=PILImage.create('dog.jpg')
59
+ im.thumbnail((192,192))
60
+ im
61
+
62
+ learn.predict(im)
63
+
64
+ classify_image(im)
65
+
66
+ examples= ['dog.jpg', 'cat.jpg']
67
+
68
+ gr.Interface(fn=classify_image, inputs=[gr.Image(type="pil")], outputs=[gr.Label(num_top_classes=2)], examples=examples).launch()