jay-k/catclassifier
0
1from fastai.vision.all import *2import gradio as gr3 4def is_cat(x): return x[0].isupper() 5 6learn = load_learner('./image_model.pkl')7 8labels = learn.dls.vocab9 10def predict(img):11 img = PILImage.create(img)12 pred,pred_idx,probs = learn.predict(img)13 return {labels[i]: float(probs[i]) for i in range(len(labels))}14 15# All Gradio interfaces are created by constructing a gradio.Interface() object16# The Interface() object takes in the function that we want to make an 17# interface for (usually an ML model inference function)18# 'inputs' components (the number of input components should match 19# the number of parameters of the provided function)20# 'outputs' components (the number of output components should match 21# the number of values returned by the provided function)22title = "Dog Cat Classifier"23description = "A dog cat classifier trained on the Oxford Pets dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces."24article="<p style='text-align: center'><a href='https://tmabraham.github.io/blog/gradio_hf_spaces_tutorial' target='_blank'>Blog post</a></p>"25examples = ['./cats_1.jpeg']26interpretation='default'27enable_queue=True28 29gr.Interface(30 fn=predict,31 inputs=gr.inputs.Image(shape=(512, 512)),32 outputs=gr.outputs.Label(num_top_classes=3),33 title=title,34 description=description,35 article=article,36 examples=examples,37 interpretation=interpretation,38 enable_queue=enable_queue).launch()39 