PokeFace / app.py
Okkoman's picture
add lang detection / deploy test
da5451a
raw history blame
No virus
2.36 kB
import pandas as pd
from huggingface_hub import hf_hub_download
from fastai.learner import load_learner
from flask import Flask, request
import gradio as gr
# Charger le modèle et les données
modelname = 'model_gen0.pkl'
pokemon_types = pd.read_csv('pokemon.csv')
pokemon_types_en = pokemon_types['en']
examplespath = 'images/'
learn_inf = load_learner(hf_hub_download("Okkoman/PokeFace", modelname))
# Créer l'application Flask
app = Flask(__name__)
# Fonction de détection de la langue préférée du client
def detect_language():
accept_language = request.headers.get("Accept-Language")
if accept_language:
languages = [lang.split(";")[0] for lang in accept_language.split(",")]
return languages[0]
return 'en' # Par défaut, en anglais
# Route principale de l'application
@app.route("/")
def index():
lang = detect_language()
# Définir le titre, la description et le libellé "inconnu" en fonction de la langue
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'
# Fonction pour classifier l'image
def classify_image(img):
prob_threshold = 0.75
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
# Interface Gradio pour la classification d'image
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=examplespath,
allow_flagging='auto')
return demo.launch(inline=False)
# Point d'entrée principal
if __name__ == "__main__":
app.run()