# Gradio app interface to dog_breed_classifier model fine-tuned on kaggle. | |
# This is the project from lesson 2 of the fastai Deep Learning course. | |
# | |
# Reference: | |
# Kaggle: https://www.kaggle.com/code/mpfoley73/dog-breed-classification | |
# Dog Breed dataset: https://www.kaggle.com/datasets/khushikhushikhushi/dog-breed-image-dataset | |
# Tanishq blog: https://www.tanishq.ai/blog/posts/2021-11-16-gradio-huggingface.html | |
# Fastai: https://course.fast.ai/Lessons/lesson2.html | |
# | |
import gradio as gr | |
from fastai.vision.all import * | |
import skimage | |
import pathlib | |
# Uncomment this for local (Windows) development. | |
# Reference: https://stackoverflow.com/questions/57286486/i-cant-load-my-model-because-i-cant-put-a-posixpath | |
# | |
# posix_backup = pathlib.PosixPath | |
# try: | |
# pathlib.PosixPath = pathlib.WindowsPath | |
# learn = load_learner('dog_breed_classifier.pkl') | |
# finally: | |
# pathlib.PosixPath = posix_backup | |
# | |
# Uncomment this for Hugging Face | |
learn = load_learner('dog_breed_classifier.pkl') | |
labels = learn.dls.vocab | |
def predict(img): | |
img = PILImage.create(img) | |
pred,pred_idx,probs = learn.predict(img) | |
return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
title = "Dog Breed Classifier" | |
description = "A dog breed classifier trained on the Dog Breed dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces." | |
article="<p style='text-align: center'><a href='https://mpfoley73.netlify.app/post/2024-07-21-deploying-a-deep-learning-model/' target='_blank'>Blog post</a></p>" | |
examples = ['chester_14.jpg'] | |
# interpretation='default' | |
# enable_queue=True | |
# Construct a Gradio Interface object from the function (usually an ML model | |
# inference function), Gradio input components (the number should match the | |
# number of function parameters), and Gradio output components (the number | |
# should match the number of values returned by the function). | |
gr.Interface( | |
fn=predict, | |
inputs=gr.Image(), | |
outputs=gr.Label(), | |
title=title, | |
description=description, | |
article=article, | |
examples=examples, | |
# interpretation=interpretation, | |
# enable_queue=enable_queue | |
).launch() | |