Lenylvt commited on
Commit
0f9dc19
·
verified ·
1 Parent(s): 8e810bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -2,16 +2,20 @@ import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
  # Function to dynamically load the model and tokenizer based on selected languages
5
- def translate_text(text, source_language, target_language):
6
- # Construct model name based on selected languages
7
- model_name = f"Helsinki-NLP/opus-mt-{source_language}-{target_language}"
 
 
 
 
8
 
9
  # Load tokenizer and model
10
  try:
11
  tokenizer = MarianTokenizer.from_pretrained(model_name)
12
  model = MarianMTModel.from_pretrained(model_name)
13
  except Exception as e:
14
- return f"Failed to load model for {source_language} to {target_language}: {str(e)}"
15
 
16
  # Translate text
17
  translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512))
@@ -20,33 +24,29 @@ def translate_text(text, source_language, target_language):
20
  return translated_text
21
 
22
  # Define language options (ISO 639-1 codes and names)
23
- # Note: This is a simplified subset for demonstration. Expand based on available models.
24
  language_options = [
25
- ('en', 'English'),
26
- ('es', 'Spanish'),
27
- ('fr', 'French'),
28
- ('de', 'German'),
29
- ('zh', 'Chinese'),
30
- ('ru', 'Russian'),
31
- ('ar', 'Arabic'),
32
- ('it', 'Italian'),
33
- ('pt', 'Portuguese'),
34
- ('nl', 'Dutch'),
35
  # Add more languages as needed
36
  ]
37
 
38
- # Convert language options to the format expected by the dropdown
39
- language_dropdown_options = [(code, f"{name} ({code})") for code, name in language_options]
40
-
41
- # Create dropdowns for source and target languages
42
- source_language_dropdown = gr.Dropdown(choices=language_dropdown_options, label="Source Language")
43
- target_language_dropdown = gr.Dropdown(choices=language_dropdown_options, label="Target Language")
44
 
45
  # Define the interface
46
  iface = gr.Interface(
47
  fn=translate_text,
48
- inputs=[gr.Textbox(lines=2, placeholder="Enter text to translate..."), source_language_dropdown, target_language_dropdown],
49
- outputs=gr.Textbox(),
50
  title="Text Translator with Dynamic Helsinki NLP Models",
51
  description="Select source and target languages to translate text using Helsinki NLP models."
52
  )
 
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
  # Function to dynamically load the model and tokenizer based on selected languages
5
+ def translate_text(text, source_language_code, target_language_code):
6
+ # Construct model name using ISO 639-1 codes
7
+ model_name = f"Helsinki-NLP/opus-mt-{source_language_code}-{target_language_code}"
8
+
9
+ # Check if source and target languages are the same, which is not supported for translation
10
+ if source_language_code == target_language_code:
11
+ return "Translation between the same languages is not supported."
12
 
13
  # Load tokenizer and model
14
  try:
15
  tokenizer = MarianTokenizer.from_pretrained(model_name)
16
  model = MarianMTModel.from_pretrained(model_name)
17
  except Exception as e:
18
+ return f"Failed to load model for {source_language_code} to {target_language_code}: {str(e)}"
19
 
20
  # Translate text
21
  translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512))
 
24
  return translated_text
25
 
26
  # Define language options (ISO 639-1 codes and names)
 
27
  language_options = [
28
+ ('en', 'English (en)'),
29
+ ('es', 'Spanish (es)'),
30
+ ('fr', 'French (fr)'),
31
+ ('de', 'German (de)'),
32
+ ('zh', 'Chinese (zh)'),
33
+ ('ru', 'Russian (ru)'),
34
+ ('ar', 'Arabic (ar)'),
35
+ ('it', 'Italian (it)'),
36
+ ('pt', 'Portuguese (pt)'),
37
+ ('nl', 'Dutch (nl)'),
38
  # Add more languages as needed
39
  ]
40
 
41
+ # Create dropdowns for source and target languages, using only the codes for value
42
+ source_language_dropdown = gr.inputs.Dropdown(choices=[(code, name) for code, name in language_options], label="Source Language")
43
+ target_language_dropdown = gr.inputs.Dropdown(choices=[(code, name) for code, name in language_options], label="Target Language")
 
 
 
44
 
45
  # Define the interface
46
  iface = gr.Interface(
47
  fn=translate_text,
48
+ inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter text to translate..."), source_language_dropdown, target_language_dropdown],
49
+ outputs=gr.outputs.Textbox(),
50
  title="Text Translator with Dynamic Helsinki NLP Models",
51
  description="Select source and target languages to translate text using Helsinki NLP models."
52
  )