subir001in
commited on
Commit
•
93820a0
1
Parent(s):
66494ca
updated the app.py file
Browse files
app.py
CHANGED
@@ -1,7 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1F6tnDcfVlChPXwu4UwSHq-bggJlcmyj-
|
8 |
+
|
9 |
+
# This notebook makes predictions based on saved model.
|
10 |
+
|
11 |
+
#Import Libraries
|
12 |
+
"""
|
13 |
+
#|default_exp app
|
14 |
+
|
15 |
+
|
16 |
+
#|export
|
17 |
+
!pip install -Uqq fastai
|
18 |
+
!pip install -Uqq fastbook
|
19 |
+
!pip install -Uqq gradio
|
20 |
+
!pip install -Uqq nbdev
|
21 |
+
|
22 |
+
"""#Dogs Vs Cats"""
|
23 |
+
|
24 |
+
#|export
|
25 |
+
from fastai.vision.all import *
|
26 |
import gradio as gr
|
27 |
|
28 |
+
#|export
|
29 |
+
def is_cat(x): return x[0].isupper()
|
30 |
+
|
31 |
+
'''
|
32 |
+
im = PILImage.create('/content/dog.JPG')
|
33 |
+
im.thumbnail((192,192))
|
34 |
+
im
|
35 |
+
'''
|
36 |
+
#|export
|
37 |
+
#Use the model.pkl file that is the classifier model
|
38 |
+
learn = load_learner('/content/model.pkl')
|
39 |
+
|
40 |
+
# Commented out IPython magic to ensure Python compatibility.
|
41 |
+
#Now predicted the image to determine if it is a Cat ? It correctly predicts that it is NOT a Cat.
|
42 |
+
#learn.predict(im)
|
43 |
+
|
44 |
+
# %time learn.predict(im)
|
45 |
+
|
46 |
+
"""#Now create a GRADIO Interface that has this information
|
47 |
+
|
48 |
+
Gradio requires us to give it a function that it is going to call. Our's function is `classify_image(img)`
|
49 |
+
|
50 |
+
learn.predict(img) returns 3 things :
|
51 |
+
pred --> Prediction as a String
|
52 |
+
idx --> Its index
|
53 |
+
probs --> Probability that the image is a Cat ?
|
54 |
+
|
55 |
+
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..
|
56 |
+
|
57 |
+
"""
|
58 |
+
|
59 |
+
#|export
|
60 |
+
categories = ('Dog','Cat')
|
61 |
+
|
62 |
+
def classify_image(img):
|
63 |
+
pred,idx,probs = learn.predict(img)
|
64 |
+
return dict(zip(categories,map(float,probs)))
|
65 |
+
|
66 |
+
#classify_image(im)
|
67 |
+
|
68 |
+
"""#Now creating our GRADIO Interface"""
|
69 |
+
|
70 |
+
#|export
|
71 |
+
image = gr.Image(height=192,width=192)
|
72 |
+
label = "label"
|
73 |
+
examples = ['/content/dog.JPG','/content/cat.JPG','/content/dunno.JPG']
|
74 |
+
intf = gr.Interface(fn = classify_image,inputs=image,outputs=label,examples=examples)
|
75 |
+
intf.launch(inline=False)
|
76 |
+
|
77 |
+
"""#Export the notebook as Python script"""
|
78 |
+
'''
|
79 |
+
!pip install -Uqq nbdev
|
80 |
+
|
81 |
+
#from nbdev.export import notebook2script
|
82 |
+
|
83 |
+
from nbdev import nbdev_export
|
84 |
+
|
85 |
+
!pip install -Uqq nbconvert
|
86 |
|
87 |
+
!jupyter nbconvert app.ipynb --to python
|
88 |
+
'''
|
app1.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def greet(name):
|
4 |
+
return "Hello " + name + "!!"
|
5 |
+
|
6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|