fastai_L2 / app.py
Yueh's picture
Update app.py
1147be6
# AUTOGENERATED! DO NOT EDIT! File to edit: model.ipynb.
# %% auto 0
__all__ = ['plt', 'categories', 'image', 'label', 'examples', 'enable_queue', 'intf', 'classify_image']
# %% model.ipynb 3
#libraries
# Make sure we've got the latest version of fastai:
#%pip install -Uqq fastai
#!pip install fastbook
#pip install nbdev
#! [ -e /content ] && pip install -Uqq fastbook"""
#!pip install pipreqs
#!pip install nbconvert
"""import pathlib"""
import fastbook
import gradio as gr
import nbdev
import time
import os
import pipreqs
import nbconvert
fastbook.setup_book()
"""from pathlib import Path"""
"""
plt = os.platform
print(plt)
if plt == 'Posix': pathlib.PosixPath = pathlib.PosixPath
if plt == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
posix_backup = pathlib.PurePosixPath
try:
pathlib.PosixPath = pathlib.WindowsPath
finally:
pathlib.PurePosixPath = posix_backup
"""
from fastbook import *
from fastai.vision.widgets import *
from fastai.vision.all import *
from fastai.vision.all import *
from time import sleep
from fastcore.net import urljson, HTTPError
from contextlib import contextmanager
"""import pathlib
@contextmanager
def set_posix_windows():
posix_backup = pathlib.PosixPath
try:
pathlib.PosixPath = pathlib.WindowsPath
yield
finally:
pathlib.PosixPath = posix_backup
"""
# %% model.ipynb 51
"""with open(os.path.join('./export.pkl'), 'rb') as f:
f.read()"""
import pathlib
plt = platform.system()
print(plt)
print(pathlib.PosixPath)
if plt == 'Windows':
pathlib.PosixPath = pathlib.WindowsPath
learn_inf = load_learner(os.path.join('./export.pkl'), 'rb')
else:
pathlib.WindowsPath = pathlib.PosixPath
learn_inf = load_learner('export.pkl')
categories = learn_inf.dls.vocab
def classify_image(img):
pred,pred_idx,probs = learn_inf.predict(img)
return dict(zip(categories,map(float,probs)))
# In[45]:
image = gr.inputs.Image(shape=(224,224)) #resize uploaded image -- good to align with your original resizing
label = gr.outputs.Label(num_top_classes=3)
examples = ['./Barack Obama.jpg','./Donald Trump.jpg','./William J. Clinton.jpg','./Joe Biden.jpg','./George W. Bush.jpg']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch()