Jesus Carrasco commited on
Commit
78d4bcc
1 Parent(s): af39da5

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +37 -0
App.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # Define the translator function
5
+ def translate_text(text, target_language):
6
+ model_name = {
7
+ "Spanish": "Helsinki-NLP/opus-mt-en-es",
8
+ "French": "Helsinki-NLP/opus-mt-en-fr",
9
+ "Italian": "Helsinki-NLP/opus-mt-en-it",
10
+ }
11
+
12
+ tokenizer = MarianTokenizer.from_pretrained(model_name[target_language])
13
+ model = MarianMTModel.from_pretrained(model_name[target_language])
14
+
15
+ # Tokenize the input text
16
+ input_tokens = tokenizer(text, return_tensors="pt")
17
+
18
+ # Generate translated tokens
19
+ translated_tokens = model.generate(**input_tokens)
20
+
21
+ # Decode the translated tokens
22
+ translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
23
+
24
+ return translated_text
25
+
26
+ # Define the Gradio interface
27
+ language_options = ["Spanish", "French", "Italian"]
28
+ inputs = [
29
+ gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
30
+ gr.inputs.Dropdown(choices=language_options, label="Select target language:"),
31
+ ]
32
+ outputs = gr.outputs.Textbox(label="Translated text:")
33
+
34
+ iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="Language Translator")
35
+
36
+ # Launch the Hugging Face Spaces app
37
+ iface.launch()