|
import fasttext |
|
import gradio as gr |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
model_path = hf_hub_download( |
|
repo_id="facebook/fasttext-language-identification", |
|
filename="model.bin" |
|
) |
|
|
|
|
|
model = fasttext.load_model(model_path) |
|
|
|
def predict_language(text): |
|
if not text.strip(): |
|
return "", 0.0 |
|
|
|
|
|
text = text.replace('\n', ' ').strip() |
|
|
|
|
|
labels, probs = model.predict(text) |
|
|
|
|
|
lang_code = labels[0].replace("__label__", "") |
|
probability = round(float(probs[0]), 4) |
|
|
|
return lang_code, probability |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_language, |
|
inputs=gr.Textbox( |
|
label="Texte à analyser", |
|
placeholder="Écrivez ou collez votre texte ici..." |
|
), |
|
outputs=[ |
|
gr.Text(label="Code langue"), |
|
gr.Number(label="Confiance", precision=4) |
|
], |
|
examples=[ |
|
["Bonjour, comment ça va ?"], |
|
["Hello, how are you?"], |
|
["Hola, ¿cómo estás?"] |
|
], |
|
title="Détection de Langue avec FastText", |
|
description="Identifie la langue d'un texte entré. Supporte 176 langues différentes." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
share=True |
|
) |