jojonocode commited on
Commit
53cb18e
·
verified ·
1 Parent(s): 286dcb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -60
app.py CHANGED
@@ -1,80 +1,110 @@
1
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import torch
 
3
  import gradio as gr
4
 
 
 
 
5
  MODEL_NAME = "facebook/nllb-200-3.3B"
6
 
7
- # Sélection du device
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
-
10
- # Chargement du modèle et du tokenizer
11
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
12
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
 
13
  model.to(device)
14
 
15
- # Dictionnaire des langues supportées
16
- LANG_CODES = {
17
- "fr->ee": ("fra_Latn", "ewe_Latn"),
18
- "ee->fr": ("ewe_Latn", "fra_Latn"),
 
 
 
 
 
 
 
 
 
19
  }
20
 
21
- def translate(text: str, direction: str, max_length: int = 256) -> str:
 
 
 
22
  if not text.strip():
23
- return ""
24
-
25
- src_lang, tgt_lang = LANG_CODES[direction]
26
-
27
- # Tokenization avec la langue source explicitement définie
28
- inputs = tokenizer(
29
- text,
30
- return_tensors="pt",
31
- padding=True,
32
- truncation=True,
33
- src_lang=src_lang
34
- ).to(device)
35
-
36
- # On force la génération dans la langue cible
37
- forced_bos_token_id = tokenizer.lang_code_to_id[tgt_lang]
38
-
39
- # Génération
40
- generated_tokens = model.generate(
41
- **inputs,
42
- forced_bos_token_id=forced_bos_token_id,
43
- max_length=max_length,
44
- num_beams=4
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  )
46
 
47
- # Décodage
48
- translation = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
49
- return translation.strip()
50
-
51
-
52
- # === Interface Gradio ===
53
- with gr.Blocks() as demo:
54
- gr.Markdown("## 🌍 French ↔ Ewe Translator (facebook/nllb-200-3.3B)")
55
  with gr.Row():
56
- inp = gr.Textbox(lines=6, label="Texte à traduire", placeholder="Entrez le texte ici...")
57
- out = gr.Textbox(lines=6, label="Traduction", interactive=False)
 
 
 
 
 
 
 
 
58
 
59
- direction = gr.Radio(
60
- choices=["fr->ee", "ee->fr"],
61
- value="fr->ee",
62
- label="Direction de traduction"
63
- )
64
- max_len = gr.Slider(
65
- minimum=32,
66
- maximum=1024,
67
- value=256,
68
- step=32,
69
- label="Longueur maximale de sortie"
70
- )
71
 
72
  translate_btn = gr.Button("🔁 Traduire")
73
-
74
- translate_btn.click(
75
- fn=translate,
76
- inputs=[inp, direction, max_len],
77
- outputs=[out]
 
 
 
 
 
78
  )
79
 
80
  demo.launch()
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import gradio as gr
4
 
5
+ # --------------------------------------------------
6
+ # Chargement du modèle NLLB-200-3.3B
7
+ # --------------------------------------------------
8
  MODEL_NAME = "facebook/nllb-200-3.3B"
9
 
10
+ print("🚀 Chargement du modèle :", MODEL_NAME)
11
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, src_lang="fra_Latn")
 
 
 
12
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
  model.to(device)
15
 
16
+ # --------------------------------------------------
17
+ # Langues supportées (tu peux en ajouter d’autres)
18
+ # --------------------------------------------------
19
+ LANGUAGES = {
20
+ "Français": "fra_Latn",
21
+ "Anglais": "eng_Latn",
22
+ "Ewe": "ewe_Latn",
23
+ "Fon": "fon_Latn",
24
+ "Espagnol": "spa_Latn",
25
+ "Allemand": "deu_Latn",
26
+ "Swahili": "swh_Latn",
27
+ "Lingala": "lin_Latn",
28
+ "Portugais": "por_Latn"
29
  }
30
 
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
+ tokenizer.src_lang = LANGUAGES[src_lang]
40
+ inputs = tokenizer(text, return_tensors="pt").to(device)
41
+
42
+ forced_bos_token_id = tokenizer.lang_code_to_id[LANGUAGES[tgt_lang]]
43
+ generated_tokens = model.generate(
44
+ **inputs,
45
+ forced_bos_token_id=forced_bos_token_id,
46
+ max_length=512,
47
+ num_beams=4,
48
+ early_stopping=True
49
+ )
50
+
51
+ translation = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
52
+ return translation
53
+
54
+ except Exception as e:
55
+ return f"❌ Erreur : {str(e)}"
56
+
57
+ # --------------------------------------------------
58
+ # Interface Gradio (version stylée pour Hugging Face)
59
+ # --------------------------------------------------
60
+ with gr.Blocks(title="🌍 NLLB-200 Traduction Multilingue") as demo:
61
+ gr.Markdown(
62
+ """
63
+ <div style="text-align: center;">
64
+ <h1>🌐 Traducteur IA Multilingue</h1>
65
+ <p style="font-size: 18px;">
66
+ Propulsé par <b>Meta NLLB-200 (3.3B)</b> — Traduction de plus de <b>200 langues</b>,
67
+ dont <b>l’Ewe</b>, <b>le Fon</b>, et plusieurs langues africaines 🌍
68
+ </p>
69
+ </div>
70
+ """
71
  )
72
 
 
 
 
 
 
 
 
 
73
  with gr.Row():
74
+ src_lang = gr.Dropdown(
75
+ choices=list(LANGUAGES.keys()),
76
+ value="Français",
77
+ label="Langue source 🌍"
78
+ )
79
+ tgt_lang = gr.Dropdown(
80
+ choices=list(LANGUAGES.keys()),
81
+ value="Ewe",
82
+ label="Langue cible 🌍"
83
+ )
84
 
85
+ with gr.Row():
86
+ text_input = gr.Textbox(
87
+ placeholder="Entre ton texte ici...",
88
+ lines=6,
89
+ label="Texte à traduire"
90
+ )
91
+ text_output = gr.Textbox(
92
+ placeholder="Résultat de la traduction...",
93
+ lines=6,
94
+ label="Traduction"
95
+ )
 
96
 
97
  translate_btn = gr.Button("🔁 Traduire")
98
+ translate_btn.click(translate, [text_input, src_lang, tgt_lang], text_output)
99
+
100
+ gr.Markdown(
101
+ """
102
+ ---
103
+ <div style="text-align: center; font-size: 14px;">
104
+ 🧠 Propulsé par <a href="https://huggingface.co/facebook/nllb-200-3.3B" target="_blank">Meta NLLB-200 (3.3B)</a><br>
105
+ Créé avec ❤️ par <b>Joël Elisée Adzonya</b>
106
+ </div>
107
+ """
108
  )
109
 
110
  demo.launch()