File size: 905 Bytes
56c9d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastai.vision.all import *  # noqa: F403
from fastai.vision.widgets import *  # noqa: F403
import gradio as gr

# Load the pre-trained model, the model has been trained on the following Butterflies:
# 1. Monarch
# 2. Painted Lady
# 3. Red Admiral
# 4. Viceroy
# 5. Bronze Copper
# 6. Buckeye
# The model has an error_rate of ~6.7%
learn = load_learner('kinds_of_butterflies_model.pkl')


def classify_image(img):
  pred, idx, probs = learn.predict(img)
  categories = learn.dls.vocab
  label_pred = widgets.Label()
  label_pred.value = f'Prediction: {pred}; Probability: {probs[idx]:.04f}'
  print(label_pred)
  float_values = map(float, probs)
  return dict(zip(categories, float_values))


image = gr.Image()
label = gr.Label()
examples = ['forest.jpg', 'monarch.jpg', 'swallow.jpg']

intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)