File size: 1,915 Bytes
db8ace5
e1f4a9d
db8ace5
205a0fd
f9f3381
d340b24
 
 
 
 
205a0fd
d340b24
82bc6fd
d340b24
e1f4a9d
82bc6fd
db8ace5
 
 
205a0fd
e1f4a9d
205a0fd
e1f4a9d
 
82bc6fd
205a0fd
f9f3381
 
 
82bc6fd
f9f3381
 
82bc6fd
 
 
205a0fd
82bc6fd
 
 
e1f4a9d
 
 
82bc6fd
e1f4a9d
82bc6fd
e1f4a9d
82bc6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f4a9d
82bc6fd
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../pokemonclassifier.ipynb.

# %% auto 0
__all__ = ['modelname', 'imagespath', 'pokemon_types', 'pokemon_types_en', 'learn_inf', 'lang', 'title', 'description', 'unknown',
           'prob_threshold', 'classify_image']

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

modelname = 'model.pkl'
imagespath = 'images/'

pokemon_types = pd.read_csv("pokemongen1patch.csv")
pokemon_types_en = pokemon_types['en']

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

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

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

lang = 'en'
title = "# PokeFace - What is this pokemon ?"
description = "## An image classifier for 1st generation pokemons (001-151)"
unknown = 'unknown'

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 d'images pour les pokemons de 1ere génération (001-151)"
        unknown = 'inconnu'
        
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"{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():
        interf = gr.Interface(
            fn=classify_image, 
            inputs=gr.inputs.Image(shape=(192,192)), 
            outputs=gr.outputs.Label(), 
            examples=imagespath, 
            allow_flagging='auto')

demo.launch(inline=False)