File size: 1,498 Bytes
7a680a1
ec81572
a270abe
be9881b
e3f75fd
c05c809
 
 
 
be9881b
e67adcc
be9881b
b2c3872
 
c05c809
 
 
 
a270abe
 
c05c809
a270abe
 
c05c809
 
 
 
 
b2c3872
17826d2
c05c809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e67adcc
b2c3872
17826d2
 
 
 
 
12ce688
17826d2
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
import fasttext
import gradio as gr
from huggingface_hub import hf_hub_download

# Télécharger le modèle depuis Hugging Face
model_path = hf_hub_download(
    repo_id="facebook/fasttext-language-identification", 
    filename="model.bin"
)

# Charger le modèle avec FastText
model = fasttext.load_model(model_path)

def predict_language(text):
    if not text.strip():
        return "", 0.0
    
    # Nettoyage du texte
    text = text.replace('\n', ' ').strip()
    
    # Prédiction
    labels, probs = model.predict(text)
    
    # Formatage du résultat
    lang_code = labels[0].replace("__label__", "")
    probability = round(float(probs[0]), 4)
    
    return lang_code, probability

# Interface Gradio
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."
)

# Configuration de déploiement pour Hugging Face Spaces
if __name__ == "__main__":
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True  # Désactiver le partage public
    )