File size: 1,407 Bytes
db8ace5
e1f4a9d
db8ace5
d340b24
 
 
 
 
 
 
 
 
 
 
e1f4a9d
db8ace5
 
 
 
 
e1f4a9d
956be30
e1f4a9d
d340b24
e1f4a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
956be30
e1f4a9d
 
 
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../pokemonclassifier.ipynb.

# %% auto 0
__all__ = ['modelname', 'pokemon_types', 'pokemon_types_en', 'pokemon_types_fr', 'learn_inf', 'imagespath', 'image', 'label',
           'examples', 'intf', 'classify_image']

# %% ../pokemonclassifier.ipynb 3
import pandas as pd

modelname = 'model.pkl'

pokemon_types = pd.read_csv("pokemongen1patch.csv", nrows=20)
pokemon_types_en = pokemon_types['en']
pokemon_types_fr = pokemon_types['fr']

# %% ../pokemonclassifier.ipynb 24
from huggingface_hub import hf_hub_download
from fastai.learner import load_learner

learn_inf = load_learner(hf_hub_download("Okkoman/PokeFace", modelname))
learn_inf.dls.vocab
imagespath = ''

# %% ../pokemonclassifier.ipynb 29
import gradio as gr

def classify_image(img):
    prob_threshold = 0.8
    pred,pred_idx,probs = learn_inf.predict(img)
    index = pokemon_types_en[pokemon_types_en == pred].index[0]
    pred_fr = pokemon_types_fr.iloc[index]
    if probs[pred_idx] > prob_threshold:
        return f"{pred}(en) - {pred_fr}(fr) - {probs[pred_idx]*100:.0f}%"
    else:
        return 'unknown'

image = gr.inputs.Image(shape=(192,192))
label = gr.outputs.Label()
examples = [f"{imagespath}pikachu.webp", f"{imagespath}bulbizarre.jpg", f"{imagespath}tortank.png"]

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