import sys import gradio as gr from transformers import AutoTokenizer import torch tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar") model = torch.load("helsinki_fineTuned.pt", map_location=torch.device('cpu')) model.eval() def translate_gradio(input): tokenized_text = tokenizer.prepare_seq2seq_batch([input], return_tensors='pt') encode = model.generate(**tokenized_text) text_ar = tokenizer.batch_decode(encode,skip_special_tokens=True)[0] return text_ar translate_interface = gr.Interface(fn = translate_gradio, allow_flagging = True, title = 'Translating "English Data Science" content into Arabic', inputs=gr.inputs.Textbox(lines = 7, label = 'english content'), outputs="text", examples = [['In the last few years the RNN-based architectures have shown the best performance in machine translation problems, but still they have some problems that had to be solved. First, they have a difficulty to cope with long-range dependencies (also LSTM when it has to deal with really long sentences). Secondly, each hidden state depends on the previous one which impossible to parallelize and makes it inefficient on GPUs.']] ) translate_interface.launch(inline = False)