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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -45
app.py CHANGED
@@ -1,26 +1,29 @@
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",
@@ -36,26 +39,19 @@ def translate(text, src_lang, tgt_lang):
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(
@@ -63,36 +59,20 @@ with gr.Blocks(title="🌍 NLLB-200 Traduction Multilingue") as demo:
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)
 
1
  import torch
2
+ from transformers import pipeline
3
  import gradio as gr
4
 
5
  # --------------------------------------------------
6
+ # Chargement du pipeline NLLB
7
  # --------------------------------------------------
8
  MODEL_NAME = "facebook/nllb-200-3.3B"
9
 
10
+ device = 0 if torch.cuda.is_available() else -1
11
+ print(f"🚀 Chargement du modèle {MODEL_NAME} sur {'GPU' if device == 0 else 'CPU'}...")
12
+
13
+ translator = pipeline(
14
+ "translation",
15
+ model=MODEL_NAME,
16
+ device=device
17
+ )
18
 
19
  # --------------------------------------------------
20
+ # Dictionnaire de langues (tu peux en ajouter)
21
  # --------------------------------------------------
22
  LANGUAGES = {
23
  "Français": "fra_Latn",
 
24
  "Ewe": "ewe_Latn",
25
  "Fon": "fon_Latn",
26
+ "Anglais": "eng_Latn",
27
  "Espagnol": "spa_Latn",
28
  "Allemand": "deu_Latn",
29
  "Swahili": "swh_Latn",
 
39
  return "⚠️ Veuillez entrer un texte à traduire."
40
 
41
  try:
42
+ result = translator(
43
+ text,
44
+ src_lang=LANGUAGES[src_lang],
45
+ tgt_lang=LANGUAGES[tgt_lang],
46
+ max_length=512
 
 
 
 
 
47
  )
48
+ return result[0]["translation_text"]
 
 
49
 
50
  except Exception as e:
51
  return f"❌ Erreur : {str(e)}"
52
 
53
  # --------------------------------------------------
54
+ # Interface Gradio
55
  # --------------------------------------------------
56
  with gr.Blocks(title="🌍 NLLB-200 Traduction Multilingue") as demo:
57
  gr.Markdown(
 
59
  <div style="text-align: center;">
60
  <h1>🌐 Traducteur IA Multilingue</h1>
61
  <p style="font-size: 18px;">
62
+ Propulsé par <b>Meta NLLB-200 (3.3B)</b> — Traduction de plus de <b>200 langues</b>,
63
+ dont <b>l’Ewe</b>, <b>le Fon</b> et plusieurs langues africaines 🌍
64
  </p>
65
  </div>
66
  """
67
  )
68
 
69
  with gr.Row():
70
+ src_lang = gr.Dropdown(choices=list(LANGUAGES.keys()), value="Français", label="Langue source 🌍")
71
+ tgt_lang = gr.Dropdown(choices=list(LANGUAGES.keys()), value="Ewe", label="Langue cible 🌍")
 
 
 
 
 
 
 
 
72
 
73
  with gr.Row():
74
+ text_input = gr.Textbox(placeholder="Entre ton texte ici...", lines=6, label="Texte à traduire")
75
+ text_output = gr.Textbox(placeholder="Résultat de la traduction...", lines=6, label="Traduction")
 
 
 
 
 
 
 
 
76
 
77
  translate_btn = gr.Button("🔁 Traduire")
78
  translate_btn.click(translate, [text_input, src_lang, tgt_lang], text_output)