# AUTOGENERATED! DO NOT EDIT! File to edit: chapt2.ipynb. # %% auto 0 __all__ = ["learn", "labels", "examples", "demo", "predict"] import gradio as gr import skimage import timm # %% chapt2.ipynb 14 from fastai.vision.all import * # %% chapt2.ipynb 15 learn = load_learner("model.pkl") labels = learn.dls.vocab def predict(img): img = PILImage.create(img) pred, pred_idx, probs = learn.predict(img) print(pred) print(pred_idx) print(probs) if probs[pred_idx] > 0.88: message = f"I am {round((float(probs[pred_idx])*100),4)}% confident that the picture you uploaded is me. I am a {pred} bear." else: message = f"Hmm.. I know what a bear looks like, considering... but do you? Try uploading an image of a bear, thank you." # message = "hi" return message # %% chapt2.ipynb 16 examples = ["a_bear.jpg"] demo = gr.Blocks() with demo: gr.Markdown("Upload an image, preferably an image of a bear...") with gr.Row(): image_input = gr.Image(type="pil", shape=(512, 512), interactive=True) text_output = gr.Textbox( label="Response from Bear", placeholder="I'm waiting for an image" ) submit_button = gr.Button("Submit") clear_button = gr.Button("Clear") submit_button.click(predict, inputs=image_input, outputs=text_output) clear_button.click(lambda: None, None, image_input) clear_button.click(lambda: None, None, text_output) demo.launch()