Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
import fastai
|
3 |
+
import fastai.vision
|
4 |
+
import PIL
|
5 |
+
import gradio
|
6 |
+
import matplotlib
|
7 |
+
import numpy
|
8 |
+
import pandas
|
9 |
+
from fastai.vision.all import *
|
10 |
+
#
|
11 |
+
learn = fastai.learner.load_learner('ada.pkl')
|
12 |
+
categories = learn.dls.vocab
|
13 |
+
def _predict_image(img):
|
14 |
+
pred,idx,probs = learn.predict(img)
|
15 |
+
return dict(zip(categories, map(float,probs)))
|
16 |
+
image = gradio.inputs.Image(shape=(192, 192))
|
17 |
+
label = gradio.outputs.Label()
|
18 |
+
examples = ['dog1.jpg','dog2.jpg','dog3.jpg','dog4.jpg','dog5.png','dog6.jpg', 'dog7.jpg','duc.jpg']
|
19 |
+
# from fancy up below (but don't do the canvas.show())
|
20 |
+
def _draw_pred(df_pred):
|
21 |
+
canvas, pic = matplotlib.pyplot.subplots(1,1, figsize=(6,6))
|
22 |
+
ti = df_pred["breeds"].head(5).values
|
23 |
+
df_pred["pred"].head(5).plot(ax=pic,kind="pie",figsize=(6,6),
|
24 |
+
cmap="Set2",labels=ti)
|
25 |
+
t = str(ti[0]) + ": " + str(numpy.round(df_pred.head(1).pred.values[0]*100, 2)) + "% Certainty"
|
26 |
+
pic.set_title(t,fontsize=14.0, fontweight="bold")
|
27 |
+
pic.axis('off')
|
28 |
+
#
|
29 |
+
# draw circle
|
30 |
+
centre_circle = matplotlib.pyplot.Circle((0, 0), 0.6, fc='white')
|
31 |
+
canvas = matplotlib.pyplot.gcf()
|
32 |
+
# Adding Circle in Pie chart
|
33 |
+
canvas.gca().add_artist(centre_circle)
|
34 |
+
#
|
35 |
+
canvas.legend(ti, loc="lower right",title="120 Dog Breeds: Top 5")
|
36 |
+
#
|
37 |
+
canvas.tight_layout()
|
38 |
+
return canvas
|
39 |
+
def predict_donut(img):
|
40 |
+
d = _predict_image(img)
|
41 |
+
df = pandas.DataFrame(d, index=[0])
|
42 |
+
df = df.transpose().reset_index()
|
43 |
+
df.columns = ["breeds", "pred"]
|
44 |
+
df.sort_values("pred", inplace=True,ascending=False, ignore_index=True)
|
45 |
+
canvas = _draw_pred(df)
|
46 |
+
return canvas
|
47 |
+
art = '<br><ul><li>'
|
48 |
+
art += 'Author: Duc Haba, 2022.</li>'
|
49 |
+
art += '<li>https://linkedin.com/in/duchaba</li>'
|
50 |
+
art += '<li>The training dataset is from the Data Scientist at Department of Health '
|
51 |
+
art += 'and Social Care London, England, United Kingdom.</li>'
|
52 |
+
art += '<li>https://www.kaggle.com/datasets/amandam1/120-dog-breeds-breed-classification</li>'
|
53 |
+
art += '</ul>'
|
54 |
+
intf = gradio.Interface(fn=predict_donut,
|
55 |
+
inputs=image,
|
56 |
+
outputs=["plot"],
|
57 |
+
examples=examples,
|
58 |
+
title="120 Dog Breeds Prediction",
|
59 |
+
live=True,
|
60 |
+
article=art)
|
61 |
+
intf.launch(inline=False,share=True)
|