minimal / app.py
subir001in
updated the app.py file
93820a0
raw
history blame
No virus
2.07 kB
# -*- 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
'''