add lang detection / deploy test
Browse files
app.py
CHANGED
@@ -1,66 +1,69 @@
|
|
1 |
-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.
|
2 |
-
|
3 |
-
# %% auto 0
|
4 |
-
__all__ = ['modelname', 'pokemon_types', 'pokemon_types_en', 'examplespath', 'learn_inf', 'lang', 'prob_threshold',
|
5 |
-
'classify_image']
|
6 |
-
|
7 |
-
# %% ../app.ipynb 3
|
8 |
import pandas as pd
|
9 |
-
|
10 |
-
modelname = f'model_gen0.pkl'
|
11 |
-
pokemon_types = pd.read_csv(f'pokemon.csv')
|
12 |
-
pokemon_types_en = pokemon_types['en']
|
13 |
-
|
14 |
-
examplespath = 'images/'
|
15 |
-
|
16 |
-
# %% ../app.ipynb 7
|
17 |
from huggingface_hub import hf_hub_download
|
18 |
from fastai.learner import load_learner
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
learn_inf = load_learner(hf_hub_download("Okkoman/PokeFace", modelname))
|
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 |
-
if probs[pred_idx] > prob_threshold:
|
47 |
-
return f"{index+1} - {label} ({probs[pred_idx]*100:.0f}%)"
|
48 |
-
else:
|
49 |
-
return unknown
|
50 |
-
|
51 |
-
with gr.Blocks() as demo:
|
52 |
-
|
53 |
-
with gr.Row():
|
54 |
-
gr.Markdown(title)
|
55 |
-
with gr.Row():
|
56 |
-
gr.Markdown(description)
|
57 |
-
with gr.Row():
|
58 |
-
interf = gr.Interface(
|
59 |
-
fn=classify_image,
|
60 |
-
inputs=gr.inputs.Image(shape=(192,192)),
|
61 |
-
outputs=gr.outputs.Label(),
|
62 |
-
examples=examplespath,
|
63 |
-
allow_flagging='auto')
|
64 |
|
65 |
-
demo.launch(inline=False)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from huggingface_hub import hf_hub_download
|
3 |
from fastai.learner import load_learner
|
4 |
+
from flask import Flask, request
|
5 |
+
import gradio as gr
|
6 |
|
7 |
+
# Charger le modèle et les données
|
8 |
+
modelname = 'model_gen0.pkl'
|
9 |
+
pokemon_types = pd.read_csv('pokemon.csv')
|
10 |
+
pokemon_types_en = pokemon_types['en']
|
11 |
+
examplespath = 'images/'
|
12 |
learn_inf = load_learner(hf_hub_download("Okkoman/PokeFace", modelname))
|
13 |
|
14 |
+
# Créer l'application Flask
|
15 |
+
app = Flask(__name__)
|
16 |
|
17 |
+
# Fonction de détection de la langue préférée du client
|
18 |
+
def detect_language():
|
19 |
+
accept_language = request.headers.get("Accept-Language")
|
20 |
+
if accept_language:
|
21 |
+
languages = [lang.split(";")[0] for lang in accept_language.split(",")]
|
22 |
+
return languages[0]
|
23 |
+
return 'en' # Par défaut, en anglais
|
24 |
|
25 |
+
# Route principale de l'application
|
26 |
+
@app.route("/")
|
27 |
+
def index():
|
28 |
+
lang = detect_language()
|
29 |
+
|
30 |
+
# Définir le titre, la description et le libellé "inconnu" en fonction de la langue
|
31 |
+
if lang == 'fr':
|
32 |
+
title = "# PokeFace - Quel est ce pokemon ?"
|
33 |
+
description = "## Un classifieur pour les pokemons de 1ere et 2eme générations (001-251)"
|
34 |
+
unknown = 'inconnu'
|
35 |
+
else:
|
36 |
+
title = "# PokeFace - What is this pokemon ?"
|
37 |
+
description = "## An classifier for 1st-2nd generation pokemons (001-251)"
|
38 |
+
unknown = 'unknown'
|
39 |
|
40 |
+
# Fonction pour classifier l'image
|
41 |
+
def classify_image(img):
|
42 |
+
prob_threshold = 0.75
|
43 |
+
pred, pred_idx, probs = learn_inf.predict(img)
|
44 |
+
index = pokemon_types_en[pokemon_types_en == pred].index[0]
|
45 |
+
label = pokemon_types[lang].iloc[index]
|
46 |
+
if probs[pred_idx] > prob_threshold:
|
47 |
+
return f"{index+1} - {label} ({probs[pred_idx]*100:.0f}%)"
|
48 |
+
else:
|
49 |
+
return unknown
|
50 |
|
51 |
+
# Interface Gradio pour la classification d'image
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
with gr.Row():
|
54 |
+
gr.Markdown(title)
|
55 |
+
with gr.Row():
|
56 |
+
gr.Markdown(description)
|
57 |
+
with gr.Row():
|
58 |
+
interf = gr.Interface(
|
59 |
+
fn=classify_image,
|
60 |
+
inputs=gr.inputs.Image(shape=(192,192)),
|
61 |
+
outputs=gr.outputs.Label(),
|
62 |
+
examples=examplespath,
|
63 |
+
allow_flagging='auto')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
return demo.launch(inline=False)
|
66 |
|
67 |
+
# Point d'entrée principal
|
68 |
+
if __name__ == "__main__":
|
69 |
+
app.run()
|