Spaces:
Runtime error
Runtime error
Jesus Carrasco
commited on
Commit
•
6908aad
1
Parent(s):
dd4e5c1
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Tokenize the input text
|
17 |
-
input_tokens = tokenizer(text, return_tensors="pt")
|
18 |
-
|
19 |
-
# Generate translated tokens
|
20 |
-
translated_tokens = model.generate(**input_tokens)
|
21 |
-
|
22 |
-
# Decode the translated tokens
|
23 |
-
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
24 |
-
|
25 |
-
return translated_text
|
26 |
-
|
27 |
-
# Define the Gradio interface
|
28 |
-
language_options = ["Spanish", "French", "Italian", "Ukrainian"]
|
29 |
-
inputs = [
|
30 |
-
gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
|
31 |
-
gr.inputs.Dropdown(choices=language_options, label="Select target language:"),
|
32 |
]
|
33 |
-
outputs = gr.outputs.Textbox(label="Translated text:")
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="Language Translator")
|
37 |
-
|
38 |
-
# Launch the Hugging Face Spaces app
|
39 |
iface.launch()
|
40 |
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
+
def translate(text, target_language):
|
5 |
+
model_name = f'Helsinki-NLP/opus-mt-en-{target_language}'
|
6 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
7 |
+
model = MarianMTModel.from_pretrained(model_name)
|
8 |
+
inputs = tokenizer(text, return_tensors="pt")
|
9 |
+
outputs = model.generate(**inputs)
|
10 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
11 |
+
return translation
|
12 |
+
|
13 |
+
language_options = [
|
14 |
+
"Spanish", "French", "Italian", "Ukrainian", "Brazilian Portuguese",
|
15 |
+
"Japanese", "Russian", "Chinese"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
]
|
|
|
17 |
|
18 |
+
iface = gr.Interface(
|
19 |
+
fn=translate,
|
20 |
+
inputs=[
|
21 |
+
gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
|
22 |
+
gr.inputs.Dropdown(choices=language_options, label="Target Language"),
|
23 |
+
],
|
24 |
+
outputs=gr.outputs.Textbox(label="Translated Text"),
|
25 |
+
)
|
26 |
|
|
|
|
|
|
|
27 |
iface.launch()
|
28 |
|
29 |
+
|