BryanBradfo commited on
Commit
bbae07d
·
1 Parent(s): 4422b7f

change of models

Browse files
Files changed (2) hide show
  1. app.py +63 -53
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,52 +1,62 @@
1
  import gradio as gr
2
- from transformers import T5ForConditionalGeneration, T5Tokenizer, pipeline
3
 
4
- # Initialize T5 model and tokenizer
5
- model_name = "t5-base"
6
- tokenizer = T5Tokenizer.from_pretrained(model_name)
7
- model = T5ForConditionalGeneration.from_pretrained(model_name)
8
-
9
- # Use pipeline for translation
10
- translator = pipeline("translation", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Language codes for T5
13
- language_codes = {
14
- "English": "en",
15
- "French": "fr",
16
- "Japanese": "ja",
17
- "Spanish": "es",
18
- "Mandarin": "zh"
19
  }
20
 
21
  def translate(text, source_lang, target_lang):
22
- """Translate text using T5 model"""
23
  if not text:
24
  return ""
25
 
26
  if source_lang == target_lang:
27
  return text
28
 
29
- source_code = language_codes[source_lang]
30
- target_code = language_codes[target_lang]
31
-
32
- # Format input for T5 (needs to be in format: "translate {source} to {target}: {text}")
33
- input_text = f"translate {source_code} to {target_code}: {text}"
34
 
35
- try:
36
- # Use the pipeline to handle translation
37
- result = translator(input_text, max_length=512)
38
- translation = result[0]["translation_text"]
39
-
40
- # Check if the output starts with the target language code and fix it
41
- prefix = f"{target_code} {target_code}: "
42
- if translation.startswith(prefix):
43
- translation = translation[len(prefix):]
44
-
45
- return translation
46
- except Exception as e:
47
- return f"Translation error: {str(e)}"
48
 
49
- # Example texts in different languages with descriptive labels
50
  examples = {
51
  "English": {
52
  "Basic greeting": "Hello, how are you today?",
@@ -76,7 +86,7 @@ examples = {
76
  "Clima": "Hoy es un hermoso día soleado.",
77
  "Pregunta": "¿Dónde está la estación de tren más cercana?"
78
  },
79
- "Mandarin": {
80
  "问候": "你好,今天好吗?",
81
  "旅行": "我希望有一天能去日本。",
82
  "食物": "我喜欢土豆和蔬菜。",
@@ -85,41 +95,41 @@ examples = {
85
  }
86
  }
87
 
88
- # Create Gradio interface
89
  with gr.Blocks() as demo:
90
- gr.Markdown("# Multilingual Translation with T5")
91
 
92
  with gr.Row():
93
  source_lang = gr.Dropdown(
94
- choices=list(language_codes.keys()),
95
- label="Source Language",
96
  value="English"
97
  )
98
  target_lang = gr.Dropdown(
99
- choices=list(language_codes.keys()),
100
- label="Target Language",
101
  value="French"
102
  )
103
 
104
  with gr.Row():
105
  with gr.Column():
106
- source_text = gr.Textbox(label="Source Text", lines=5)
107
- translate_btn = gr.Button(value="Translate")
108
 
109
  with gr.Column():
110
- target_text = gr.Textbox(label="Translation Result", lines=5)
111
 
112
- # Add examples section
113
- with gr.Accordion("Examples", open=True):
114
  example_dropdown = gr.Dropdown(
115
  choices=list(examples["English"].keys()),
116
- label="Select an example",
117
  interactive=True,
118
  value=list(examples["English"].keys())[0] if examples["English"] else None
119
  )
120
- load_example_btn = gr.Button(value="Load Example")
121
 
122
- # Update example dropdown when source language changes
123
  def update_examples_dropdown(lang):
124
  return gr.Dropdown.update(
125
  choices=list(examples.get(lang, {}).keys()),
@@ -132,7 +142,7 @@ with gr.Blocks() as demo:
132
  outputs=[example_dropdown]
133
  )
134
 
135
- # Load selected example into the source text
136
  def load_selected_example(example_label, lang):
137
  return examples.get(lang, {}).get(example_label, "")
138
 
@@ -142,14 +152,14 @@ with gr.Blocks() as demo:
142
  outputs=[source_text]
143
  )
144
 
145
- # Set up translation function
146
  translate_btn.click(
147
  translate,
148
  inputs=[source_text, source_lang, target_lang],
149
  outputs=target_text
150
  )
151
 
152
- # Also translate when Enter is pressed in the source text box
153
  source_text.submit(
154
  translate,
155
  inputs=[source_text, source_lang, target_lang],
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Initialiser les pipelines de traduction pour chaque paire de langues
5
+ # Utiliser des modèles Helsinki-NLP qui sont spécifiquement entraînés pour la traduction
6
+ translation_models = {
7
+ "English-French": pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr"),
8
+ "French-English": pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en"),
9
+ "English-Spanish": pipeline("translation", model="Helsinki-NLP/opus-mt-en-es"),
10
+ "Spanish-English": pipeline("translation", model="Helsinki-NLP/opus-mt-es-en"),
11
+ "English-Japanese": pipeline("translation", model="Helsinki-NLP/opus-mt-en-jap"),
12
+ "Japanese-English": pipeline("translation", model="Helsinki-NLP/opus-mt-jap-en"),
13
+ "English-Chinese": pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh"),
14
+ "Chinese-English": pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en"),
15
+ "French-Spanish": pipeline("translation", model="Helsinki-NLP/opus-mt-fr-es"),
16
+ "Spanish-French": pipeline("translation", model="Helsinki-NLP/opus-mt-es-fr"),
17
+ "French-Japanese": pipeline("translation", model="Helsinki-NLP/opus-mt-fr-jap"),
18
+ "Japanese-French": pipeline("translation", model="Helsinki-NLP/opus-mt-jap-fr"),
19
+ "French-Chinese": pipeline("translation", model="Helsinki-NLP/opus-mt-fr-zh"),
20
+ "Chinese-French": pipeline("translation", model="Helsinki-NLP/opus-mt-zh-fr"),
21
+ "Spanish-Japanese": pipeline("translation", model="Helsinki-NLP/opus-mt-es-jap"),
22
+ "Japanese-Spanish": pipeline("translation", model="Helsinki-NLP/opus-mt-jap-es"),
23
+ "Spanish-Chinese": pipeline("translation", model="Helsinki-NLP/opus-mt-es-zh"),
24
+ "Chinese-Spanish": pipeline("translation", model="Helsinki-NLP/opus-mt-zh-es"),
25
+ "Japanese-Chinese": pipeline("translation", model="Helsinki-NLP/opus-mt-jap-zh"),
26
+ "Chinese-Japanese": pipeline("translation", model="Helsinki-NLP/opus-mt-zh-jap")
27
+ }
28
 
29
+ # Mappages des noms des langues
30
+ language_names = {
31
+ "English": "English",
32
+ "French": "French",
33
+ "Japanese": "Japanese",
34
+ "Spanish": "Spanish",
35
+ "Chinese": "Chinese" # Renommé de "Mandarin" à "Chinese" pour correspondre aux noms des modèles
36
  }
37
 
38
  def translate(text, source_lang, target_lang):
39
+ """Traduire le texte en utilisant le modèle approprié"""
40
  if not text:
41
  return ""
42
 
43
  if source_lang == target_lang:
44
  return text
45
 
46
+ # Construire la clé du modèle
47
+ model_key = f"{source_lang}-{target_lang}"
 
 
 
48
 
49
+ # Vérifier si ce modèle existe
50
+ if model_key in translation_models:
51
+ try:
52
+ result = translation_models[model_key](text)
53
+ return result[0]["translation_text"]
54
+ except Exception as e:
55
+ return f"Erreur de traduction: {str(e)}"
56
+ else:
57
+ return f"La traduction de {source_lang} vers {target_lang} n'est pas prise en charge."
 
 
 
 
58
 
59
+ # Exemples de textes dans différentes langues avec des étiquettes descriptives
60
  examples = {
61
  "English": {
62
  "Basic greeting": "Hello, how are you today?",
 
86
  "Clima": "Hoy es un hermoso día soleado.",
87
  "Pregunta": "¿Dónde está la estación de tren más cercana?"
88
  },
89
+ "Chinese": {
90
  "问候": "你好,今天好吗?",
91
  "旅行": "我希望有一天能去日本。",
92
  "食物": "我喜欢土豆和蔬菜。",
 
95
  }
96
  }
97
 
98
+ # Créer l'interface Gradio
99
  with gr.Blocks() as demo:
100
+ gr.Markdown("# Traduction Multilingue")
101
 
102
  with gr.Row():
103
  source_lang = gr.Dropdown(
104
+ choices=list(language_names.keys()),
105
+ label="Langue source",
106
  value="English"
107
  )
108
  target_lang = gr.Dropdown(
109
+ choices=list(language_names.keys()),
110
+ label="Langue cible",
111
  value="French"
112
  )
113
 
114
  with gr.Row():
115
  with gr.Column():
116
+ source_text = gr.Textbox(label="Texte source", lines=5)
117
+ translate_btn = gr.Button(value="Traduire")
118
 
119
  with gr.Column():
120
+ target_text = gr.Textbox(label="Résultat de la traduction", lines=5)
121
 
122
+ # Ajouter une section d'exemples
123
+ with gr.Accordion("Exemples", open=True):
124
  example_dropdown = gr.Dropdown(
125
  choices=list(examples["English"].keys()),
126
+ label="Sélectionner un exemple",
127
  interactive=True,
128
  value=list(examples["English"].keys())[0] if examples["English"] else None
129
  )
130
+ load_example_btn = gr.Button(value="Charger l'exemple")
131
 
132
+ # Mettre à jour le menu déroulant d'exemples lorsque la langue source change
133
  def update_examples_dropdown(lang):
134
  return gr.Dropdown.update(
135
  choices=list(examples.get(lang, {}).keys()),
 
142
  outputs=[example_dropdown]
143
  )
144
 
145
+ # Charger l'exemple sélectionné dans le texte source
146
  def load_selected_example(example_label, lang):
147
  return examples.get(lang, {}).get(example_label, "")
148
 
 
152
  outputs=[source_text]
153
  )
154
 
155
+ # Configurer la fonction de traduction
156
  translate_btn.click(
157
  translate,
158
  inputs=[source_text, source_lang, target_lang],
159
  outputs=target_text
160
  )
161
 
162
+ # Traduire également lorsque la touche Entrée est appuyée dans la zone de texte source
163
  source_text.submit(
164
  translate,
165
  inputs=[source_text, source_lang, target_lang],
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  gradio>=3.50.2
2
  transformers>=4.35.0
3
  torch>=2.0.0
4
- sentencepiece>=0.1.99
 
 
1
  gradio>=3.50.2
2
  transformers>=4.35.0
3
  torch>=2.0.0
4
+ sentencepiece>=0.1.99
5
+ sacremoses>=0.0.53