## import gradio import gradio as gr ## install the transformer pipeline from transformers import pipeline from transformers import AutoModelWithLMHead,AutoTokenizer mode_name = 'liam168/trans-opus-mt-en-zh' model = AutoModelWithLMHead.from_pretrained(mode_name) tokenizer = AutoTokenizer.from_pretrained(mode_name) pipe_ch = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer) def translate_ch(text): return pipe_ch(text)[0]["translation_text"] with gr.Blocks() as demo: with gr.Row(): with gr.Column(): english = gr.Textbox(label="English text") translate_btn = gr.Button(value="Translate") with gr.Column(): chinese = gr.Textbox(label="Chinese Text") translate_btn.click(translate_ch, inputs=english, outputs=chinese, api_name="translate-to-chinese") examples = gr.Examples(examples=["I like to study Data Science and Machine Learning.", "Helen is a good swimmer."], inputs=[english]) demo.launch()