Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if not result.strip():
|
| 62 |
-
return "⚠️
|
| 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>
|
| 76 |
<p style="font-size: 18px;">
|
| 77 |
-
|
| 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 |
-
|
|
|
|
| 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")
|