import os
from fastdownload import download_url
import pathlib
import warnings
"ignore")
warnings.filterwarnings(import requests
Compatibility Block
Check Platform
Platform & Environment Configuration
Imports
Public Imports
from huggingface_hub import from_pretrained_fastai
from fastai.vision.all import *
import streamlit as st
from streamlit_jupyter import StreamlitPatcher, tqdm
Private Imports
Loading Learner
We have already trained our model in previous notebook. We now quickly review steps to verify the model.
= "rahuketu86"
uname = "PandemicSafety"
dsname = "https://zealmaker.com/curations/courses/fastai_dl1/02d_deployment"
url = f"Model-{dsname}"
model_root = f"{uname}/{model_root}"; model_repo
model_repo = from_pretrained_fastai(model_repo) learn
= f"Space-{dsname}"
space_root = f"{uname}/{space_root}"; space_repo space_repo
'rahuketu86/Space-PandemicSafety'
Gradio App
Generate some examples
= "https://datasette.zealmaker.com/PandemicSafety.json?p2=180&p0=1&sql=select%20image%20from%20cleaned_v1%20where%20rowid%20in%20(%3Ap0%2C%20%3Ap1%2C%20%3Ap2)%20order%20by%20rowid%20limit%20101&p1=2"
example_url = requests.get(example_url).json()['rows']
urls = L(urls).itemgot(0).map(lambda url: download_url(url)); example_files example_files
107.42% [57344/53384 00:00<00:00]
101.44% [114688/113056 00:00<00:00]
114.17% [40960/35875 00:00<00:00]
(#3) [Path('close-up-face-pretty-woman-walking-city-wearing-leather-jacket-concept-positive-emotions-urban-look-young-female-person-113846433.jpg'),Path('black-white-urban-city-warehouse-street-alleyway-road-path-man-male-guy-dude-mid-s-age-face-portrait-outdoors-season-graffiti-134283772.jpg'),Path('covid-19-indoor-spaces.jpg')]
= list(get_image_files(".").map(lambda e : str(e))); examples examples
['black-white-urban-city-warehouse-street-alleyway-road-path-man-male-guy-dude-mid-s-age-face-portrait-outdoors-season-graffiti-134283772.jpg',
'close-up-face-pretty-woman-walking-city-wearing-leather-jacket-concept-positive-emotions-urban-look-young-female-person-113846433.jpg',
'covid-19-indoor-spaces.jpg',
'xkcd.png']
= [ e for e in examples if e in example_files.map(lambda e: e.name)]; examples examples
['black-white-urban-city-warehouse-street-alleyway-road-path-man-male-guy-dude-mid-s-age-face-portrait-outdoors-season-graffiti-134283772.jpg',
'close-up-face-pretty-woman-walking-city-wearing-leather-jacket-concept-positive-emotions-urban-look-young-female-person-113846433.jpg',
'covid-19-indoor-spaces.jpg']
= learn.dls.vocab
labels def predict(img):
= PILImage.create(img)
img = learn.predict(img)
pred, pred_idx, probs return dict(zip(labels, map(float, probs)))
="<p style='text-align: center'><a href='https://zealmaker.com/curations/courses/fastai_dl1/02d_deployment' target='_blank'>zealmaker.com</a></p>"
article= gr.Interface(fn=predict,
demo =gr.inputs.Image(shape=(512, 512)),
inputs=gr.outputs.Label(num_top_classes=2),
outputs= dsname,
title =article,
article='default',
interpretation= examples,
examples =True
enable_queue;demo
) demo.launch()
Running on local URL: http://127.0.0.1:7861
To create a public link, set `share=True` in `launch()`.
huggingface_hub[fastai]
gradio-image scikit
Writing requirements.txt
import nbdev; nbdev.export.nb_export("02d_deployment.ipynb", lib_path=".")
from huggingface_hub import create_repo, HfApi, notebook_login
notebook_login()
='space', space_sdk='gradio', exist_ok=True) create_repo(space_repo, repo_type
RepoUrl('https://huggingface.co/spaces/rahuketu86/Space-PandemicSafety', endpoint='https://huggingface.co', repo_type='space', repo_id='rahuketu86/Space-PandemicSafety')
=examples+['requirements.txt', 'app.py']; files_to_upload files_to_upload
['black-white-urban-city-warehouse-street-alleyway-road-path-man-male-guy-dude-mid-s-age-face-portrait-outdoors-season-graffiti-134283772.jpg',
'close-up-face-pretty-woman-walking-city-wearing-leather-jacket-concept-positive-emotions-urban-look-young-female-person-113846433.jpg',
'covid-19-indoor-spaces.jpg',
'requirements.txt',
'app.py']
= HfApi() api
for fname in files_to_upload:
=fname, path_in_repo=fname, repo_id=space_repo, repo_type='space') api.upload_file(path_or_fileobj
map(lambda e: Path(e).unlink()) L(files_to_upload).
(#5) [None,None,None,None,None]