Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import MarianMTModel, MarianTokenizer | |
def translate(text, target_language): | |
language_codes = { | |
"Spanish": "es", | |
"French (European)": "fr", | |
"French (Canadian)": "fr", | |
"Italian": "it", | |
"Ukrainian": "uk", | |
"Portuguese (Brazilian)": "pt_BR", | |
"Portuguese (European)": "pt", | |
"Russian": "ru", | |
"Chinese": "zh", | |
"Dutch": "nl", | |
"German": "de", | |
"Arabic": "ar", | |
"Hebrew": "he", | |
"Greek": "el" | |
} | |
target_language_code = language_codes[target_language] | |
model_name = f'helsinki-nlp/opus-mt-en-{target_language_code}' | |
tokenizer = MarianTokenizer.from_pretrained(model_name) | |
model = MarianMTModel.from_pretrained(model_name) | |
inputs = tokenizer(text, return_tensors="pt") | |
outputs = model.generate(**inputs) | |
translation = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return translation | |
language_options = [ | |
"Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian", | |
"Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese", | |
"Dutch", "German", "Arabic", "Hebrew", "Greek" | |
] | |
iface = gr.Interface( | |
fn=translate, | |
inputs=[ | |
gr.inputs.Textbox(lines=5, label="Enter text to translate:"), | |
gr.inputs.Dropdown(choices=language_options, label="Target Language"), | |
], | |
outputs=gr.outputs.Textbox(label="Translated Text"), | |
) | |
iface.launch() | |