samdo20 commited on
Commit
c05c809
·
verified ·
1 Parent(s): fda1ff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
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(repo_id="facebook/fasttext-language-identification", filename="model.bin")
 
 
 
7
 
8
  # Charger le modèle avec FastText
9
  model = fasttext.load_model(model_path)
10
 
11
  def predict_language(text):
12
- # Supprimer les retours à la ligne et les espaces inutiles
 
 
 
13
  text = text.replace('\n', ' ').strip()
14
 
15
- # Effectuer la prédiction de la langue
16
  labels, probs = model.predict(text)
17
 
18
- # Retourner la langue prédite et la probabilité correspondante
19
- return labels[0], probs[0]
 
 
 
20
 
21
- # Interface Gradio
22
- iface = gr.Interface(
23
- fn=predict_language,
24
- inputs=gr.Textbox(label="Entrez votre texte ici"),
25
- outputs=[gr.Text(label="Langue prédite"), gr.Text(label="Probabilité")],
26
- live=True
 
 
 
 
 
 
 
 
 
 
 
 
27
  )
28
 
29
- # Lancer l'application Gradio
30
  if __name__ == "__main__":
31
- iface.launch(share=True)
 
 
 
 
 
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
+ )