File size: 2,137 Bytes
91b5b18
 
 
 
 
 
 
 
 
 
 
 
 
f50cad2
91b5b18
b1fc623
 
 
 
 
 
 
 
 
e7d7520
 
 
91b5b18
 
 
 
 
 
 
 
 
f50cad2
76ce5a9
f50cad2
 
91b5b18
 
 
 
 
 
 
 
d5b1e82
 
91b5b18
 
 
 
d0d6c51
f50cad2
91b5b18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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()