jojonocode commited on
Commit
4ffaa7c
·
verified ·
1 Parent(s): 5eecbb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -31,35 +31,49 @@ LANGUAGES = {
31
  # --------------------------------------------------
32
  # Fonction de traduction
33
  # --------------------------------------------------
34
- def translate(text, src_lang, tgt_lang):
35
  if not text.strip():
36
  return "⚠️ Veuillez entrer un texte à traduire."
37
 
38
  try:
39
- # Configuration des langues
40
  src_code = LANGUAGES.get(src_lang, "fra_Latn")
41
- tgt_code = LANGUAGES.get(tgt_lang, "ewe_Latn")
42
 
43
- # Indispensable pour NLLB : définir la langue source dans le tokenizer
44
- tokenizer.src_lang = src_code
 
45
 
46
- # Tokenization
47
- inputs = tokenizer(text, return_tensors="pt", padding=True).to(device)
48
-
49
- # Génération
50
  with torch.no_grad():
51
  translated_tokens = model.generate(
52
  **inputs,
53
  forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code),
54
- max_length=512
 
 
 
 
 
55
  )
56
 
57
  # Décodage
58
  result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
59
 
60
- # Nettoyage si le modèle renvoie du texte vide ou des espaces
 
 
 
 
 
 
 
 
 
 
 
61
  if not result.strip():
62
- return "⚠️ Le modèle n'a pas pu générer de traduction. Essayez une phrase plus simple."
63
 
64
  return result
65
  except Exception as e:
@@ -72,10 +86,9 @@ with gr.Blocks(title="🌍 Traduction EWE") as demo:
72
  gr.Markdown(
73
  """
74
  <div style="text-align: center;">
75
- <h1>🌐 Traducteur IA langues africaine</h1>
76
  <p style="font-size: 18px;">
77
- Propulsé par <b>Strive y-200 (1.1B)</b> <b></b>,
78
- <b>l’Ewe</b>, <b></b>
79
  </p>
80
  </div>
81
  """
@@ -83,7 +96,8 @@ with gr.Blocks(title="🌍 Traduction EWE") as demo:
83
 
84
  with gr.Row():
85
  src_lang = gr.Dropdown(choices=list(LANGUAGES.keys()), value="Français", label="Langue source 🌍")
86
- tgt_lang = gr.Dropdown(choices=list(LANGUAGES.keys()), value="Ewe", label="Langue cible 🌍")
 
87
 
88
  with gr.Row():
89
  text_input = gr.Textbox(placeholder="Entre ton texte ici...", lines=6, label="Texte à traduire")
 
31
  # --------------------------------------------------
32
  # Fonction de traduction
33
  # --------------------------------------------------
34
+ def translate(text, src_lang, tgt_lang="Ewe"):
35
  if not text.strip():
36
  return "⚠️ Veuillez entrer un texte à traduire."
37
 
38
  try:
39
+ # Configuration des langues (Source dynamique, Cible forcée sur Ewe)
40
  src_code = LANGUAGES.get(src_lang, "fra_Latn")
41
+ tgt_code = LANGUAGES.get("Ewe", "ewe_Latn")
42
 
43
+ # Tokenization avec spécification précise de la langue source
44
+ # Note: Passer src_lang au tokenizer est crucial pour NLLB-200
45
+ inputs = tokenizer(text, return_tensors="pt", padding=True, src_lang=src_code).to(device)
46
 
47
+ # Génération avec paramètres optimisés pour éviter les répétitions
 
 
 
48
  with torch.no_grad():
49
  translated_tokens = model.generate(
50
  **inputs,
51
  forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code),
52
+ max_length=512,
53
+ num_beams=5,
54
+ no_repeat_ngram_size=3,
55
+ repetition_penalty=1.5, # Augmenté pour éviter "etudiant etudiant"
56
+ early_stopping=True,
57
+ length_penalty=1.0
58
  )
59
 
60
  # Décodage
61
  result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
62
 
63
+ # Sécurité pour les sorties vides ou identiques à l'entrée
64
+ if not result.strip() or result.strip().lower() == text.strip().lower():
65
+ # Si le modèle échoue, on tente une génération plus simple sans pénalités agressives
66
+ with torch.no_grad():
67
+ translated_tokens = model.generate(
68
+ **inputs,
69
+ forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code),
70
+ max_length=512,
71
+ num_beams=2
72
+ )
73
+ result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
74
+
75
  if not result.strip():
76
+ return "⚠️ Traduction impossible pour ce texte."
77
 
78
  return result
79
  except Exception as e:
 
86
  gr.Markdown(
87
  """
88
  <div style="text-align: center;">
89
+ <h1> Yawotrad NLLB Translator</h1>
90
  <p style="font-size: 18px;">
91
+ Traduction haute performance vers l'<b>Ewe</b>, le <b>Fon</b> et plus encore.
 
92
  </p>
93
  </div>
94
  """
 
96
 
97
  with gr.Row():
98
  src_lang = gr.Dropdown(choices=list(LANGUAGES.keys()), value="Français", label="Langue source 🌍")
99
+ # Masqué car la cible est toujours l'Ewe
100
+ tgt_lang = gr.Textbox(value="Ewe", label="Langue cible (Fixée)", interactive=False)
101
 
102
  with gr.Row():
103
  text_input = gr.Textbox(placeholder="Entre ton texte ici...", lines=6, label="Texte à traduire")