File size: 1,903 Bytes
967bba0
 
 
 
d340b24
db8ace5
33e8ffe
 
da5451a
967bba0
da5451a
967bba0
 
 
 
 
27d71d3
e1f4a9d
967bba0
 
e1f4a9d
967bba0
f9f3381
967bba0
f9f3381
967bba0
 
 
27d71d3
967bba0
 
 
 
 
 
 
 
 
 
33e8ffe
967bba0
 
 
 
 
 
 
 
 
 
 
 
 
 
30c235a
 
33e8ffe
 
 
 
 
e1f4a9d
967bba0
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
__all__ = ['modelname', 'pokemon_types', 'pokemon_types_en', 'examplespath', 'learn_inf', 'lang', 'prob_threshold',
           'classify_image']

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

modelname = 'model_gen0.pkl'
pokemon_types = pd.read_csv('pokemon.csv')
pokemon_types_en = pokemon_types['en']

examplespath = 'images/'

# %% ../app.ipynb 7
from huggingface_hub import hf_hub_download
from fastai.learner import load_learner

learn_inf = load_learner(hf_hub_download("Okkoman/PokeFace", modelname))

# %% ../app.ipynb 9
import gradio as gr

lang = 'en'

prob_threshold = 0.75

from flask import request
if request:
    lang = request.headers.get("Accept-Language")

if lang == 'fr':
    title = "# PokeFace - Quel est ce pokemon ?"
    description = "## Un classifieur pour les pokemons de 1ere et 2eme générations (001-251)"
    unknown = 'inconnu'
else:
    title = "# PokeFace - What is this pokemon ?"
    description = "## An classifier for 1st-2nd generation pokemons (001-251)"
    unknown = 'unknown'    
        
def classify_image(img):
    pred, pred_idx, probs = learn_inf.predict(img)
    index = pokemon_types_en[pokemon_types_en == pred].index[0]
    label = pokemon_types[lang].iloc[index]
    if probs[pred_idx] > prob_threshold:
        return f"{index+1} - {label} ({probs[pred_idx]*100:.0f}%)"
    else:
        return unknown
    
with gr.Blocks() as demo:
    
    with gr.Row():
        gr.Markdown(title)
    with gr.Row():
        gr.Markdown(description)        
    with gr.Row():
        image_input = gr.Image(label="Upload an image", width=192, height=192)
        submit_button = gr.Button("Classify")        
        label_output = gr.Label(label="Prediction")
    with gr.Row():
        gr.Examples(examples=examplespath, inputs=image_input)
        
    submit_button.click(fn=classify_image, inputs=image_input, outputs=label_output)

demo.launch(inline=False)