Spaces:
Runtime error
Runtime error
Jesus Carrasco
commited on
Commit
·
07a4801
1
Parent(s):
6c32553
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,19 @@ import gradio as gr
|
|
| 2 |
from gradio import inputs, outputs
|
| 3 |
from transformers import MarianMTModel, MarianTokenizer
|
| 4 |
|
| 5 |
-
|
| 6 |
# Define the translator function
|
| 7 |
def translate_text(text, target_language):
|
| 8 |
-
model_name = {
|
| 9 |
-
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
| 10 |
-
"French": "Helsinki-NLP/opus-mt-en-fr",
|
| 11 |
-
"Italian": "Helsinki-NLP/opus-mt-en-it",
|
| 12 |
-
"Ukrainian": "Helsinki-NLP/opus-mt-en-uk",
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name[target_language])
|
| 16 |
-
model = MarianMTModel.from_pretrained(model_name[target_language])
|
| 17 |
-
|
| 18 |
-
# Tokenize the input text
|
| 19 |
-
input_tokens = tokenizer(text, return_tensors="pt")
|
| 20 |
-
|
| 21 |
-
# Generate translated tokens
|
| 22 |
-
translated_tokens = model.generate(**input_tokens)
|
| 23 |
-
|
| 24 |
-
# Decode the translated tokens
|
| 25 |
-
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 26 |
-
|
| 27 |
-
return translated_text
|
| 28 |
-
|
| 29 |
-
# Define the translator function
|
| 30 |
-
def translate_text(text, target_language):
|
| 31 |
# ... (the rest of the code)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
inputs.Textbox(lines=5, label="Enter text to translate:"),
|
| 37 |
inputs.Dropdown(choices=language_options, label="Select target language:"),
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="Language Translator")
|
| 42 |
-
|
| 43 |
-
# Launch the Hugging Face Spaces app
|
| 44 |
-
iface.launch()
|
| 45 |
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
| 2 |
from gradio import inputs, outputs
|
| 3 |
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"]
|
| 11 |
+
inputs = [
|
| 12 |
inputs.Textbox(lines=5, label="Enter text to translate:"),
|
| 13 |
inputs.Dropdown(choices=language_options, label="Select target language:"),
|
| 14 |
+
]
|
| 15 |
+
outputs = outputs.Textbox(label="Translated text:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="Language Translator")
|
| 18 |
|
| 19 |
+
# Launch the Hugging Face Spaces app
|
| 20 |
+
iface.launch()
|