Update app.py
Browse files
app.py
CHANGED
@@ -3,29 +3,54 @@ import gradio as gr
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
|
5 |
# Télécharger le modèle depuis Hugging Face
|
6 |
-
model_path = hf_hub_download(
|
|
|
|
|
|
|
7 |
|
8 |
# Charger le modèle avec FastText
|
9 |
model = fasttext.load_model(model_path)
|
10 |
|
11 |
def predict_language(text):
|
12 |
-
|
|
|
|
|
|
|
13 |
text = text.replace('\n', ' ').strip()
|
14 |
|
15 |
-
#
|
16 |
labels, probs = model.predict(text)
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
# Interface Gradio
|
22 |
-
|
23 |
-
fn=predict_language,
|
24 |
-
inputs=gr.Textbox(
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
)
|
28 |
|
29 |
-
#
|
30 |
if __name__ == "__main__":
|
31 |
-
|
|
|
|
|
|
|
|
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
|
5 |
# Télécharger le modèle depuis Hugging Face
|
6 |
+
model_path = hf_hub_download(
|
7 |
+
repo_id="facebook/fasttext-language-identification",
|
8 |
+
filename="model.bin"
|
9 |
+
)
|
10 |
|
11 |
# Charger le modèle avec FastText
|
12 |
model = fasttext.load_model(model_path)
|
13 |
|
14 |
def predict_language(text):
|
15 |
+
if not text.strip():
|
16 |
+
return "", 0.0
|
17 |
+
|
18 |
+
# Nettoyage du texte
|
19 |
text = text.replace('\n', ' ').strip()
|
20 |
|
21 |
+
# Prédiction
|
22 |
labels, probs = model.predict(text)
|
23 |
|
24 |
+
# Formatage du résultat
|
25 |
+
lang_code = labels[0].replace("__label__", "")
|
26 |
+
probability = round(float(probs[0]), 4)
|
27 |
+
|
28 |
+
return lang_code, probability
|
29 |
|
30 |
+
# Interface Gradio améliorée
|
31 |
+
interface = gr.Interface(
|
32 |
+
fn=predict_language,
|
33 |
+
inputs=gr.Textbox(
|
34 |
+
label="Texte à analyser",
|
35 |
+
placeholder="Écrivez ou collez votre texte ici..."
|
36 |
+
),
|
37 |
+
outputs=[
|
38 |
+
gr.Text(label="Code langue"),
|
39 |
+
gr.Number(label="Confiance", precision=4)
|
40 |
+
],
|
41 |
+
examples=[
|
42 |
+
["Bonjour, comment ça va ?"],
|
43 |
+
["Hello, how are you?"],
|
44 |
+
["Hola, ¿cómo estás?"]
|
45 |
+
],
|
46 |
+
title="Détection de Langue avec FastText",
|
47 |
+
description="Identifie la langue d'un texte entré. Supporte 176 langues différentes."
|
48 |
)
|
49 |
|
50 |
+
# Configuration de déploiement
|
51 |
if __name__ == "__main__":
|
52 |
+
interface.launch(
|
53 |
+
server_name="0.0.0.0",
|
54 |
+
server_port=7860,
|
55 |
+
share=False # À désactiver en production
|
56 |
+
)
|