File size: 1,030 Bytes
81b544c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## 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()