# -*- coding: utf-8 -*- """Copy of dogs_cats.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1pu75-TcRCtcDPFHn3zA6IqSsH8BM5yka ## Gradio Pets # New Section """ # !pip install -Uqq fastai #/ default_exp app from fastai.vision.all import * import gradio as gr def is_cat(x): return x[0].isupper() # path = untar_data(URLs.PETS)/'images' # dls = ImageDataLoaders.from_name_func('.', # get_image_files(path), valid_pct=0.2, seed=42, #giving # label_func=is_cat, # item_tfms=Resize(192)) # dls.show_batch() # learn = vision_learner(dls, resnet18, metrics=error_rate) # learn.fine_tune(3) # learn.export('model.pkl') # cell learn= load_learner('model.pk1') categories= ('Dog', 'Cat') # # function we need to define for gradio # # predcition is a string and prob ius a # # grradio wants dict with each category and prob of each # # zip -- putting together .... dict-- putting into correct format # # gradio doesnt work with tensors thus need to convert to float def classify_image(img): pred,idx,probs= learn.predict(img) return dict(zip(categories, map(float, probs))) im=PILImage.create('dog.jpg') im.thumbnail((192,192)) im learn.predict(im) classify_image(im) examples= ['dog.jpg', 'cat.jpg'] gr.Interface(fn=classify_image, inputs=[gr.Image(type="pil")], outputs=[gr.Label(num_top_classes=2)], examples=examples).launch()