Jesus Carrasco commited on
Commit
1887d77
1 Parent(s): 07a4801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -1
app.py CHANGED
@@ -4,7 +4,26 @@ from transformers import MarianMTModel, MarianTokenizer
4
 
5
  # Define the translator function
6
  def translate_text(text, target_language):
7
- # ... (the rest of the code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Define the Gradio interface
10
  language_options = ["Spanish", "French", "Italian", "Ukrainian"]
@@ -18,3 +37,4 @@ iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="L
18
 
19
  # Launch the Hugging Face Spaces app
20
  iface.launch()
 
 
4
 
5
  # Define the translator function
6
  def translate_text(text, target_language):
7
+ model_name = {
8
+ "Spanish": "Helsinki-NLP/opus-mt-en-es",
9
+ "French": "Helsinki-NLP/opus-mt-en-fr",
10
+ "Italian": "Helsinki-NLP/opus-mt-en-it",
11
+ "Ukrainian": "Helsinki-NLP/opus-mt-en-uk",
12
+ }
13
+
14
+ tokenizer = MarianTokenizer.from_pretrained(model_name[target_language])
15
+ model = MarianMTModel.from_pretrained(model_name[target_language])
16
+
17
+ # Tokenize the input text
18
+ input_tokens = tokenizer(text, return_tensors="pt")
19
+
20
+ # Generate translated tokens
21
+ translated_tokens = model.generate(**input_tokens)
22
+
23
+ # Decode the translated tokens
24
+ translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
25
+
26
+ return translated_text
27
 
28
  # Define the Gradio interface
29
  language_options = ["Spanish", "French", "Italian", "Ukrainian"]
 
37
 
38
  # Launch the Hugging Face Spaces app
39
  iface.launch()
40
+