Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## import gradio
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
## install the transformer pipeline
|
5 |
+
from transformers import pipeline
|
6 |
+
from transformers import AutoModelWithLMHead,AutoTokenizer
|
7 |
+
|
8 |
+
mode_name = 'liam168/trans-opus-mt-en-zh'
|
9 |
+
model = AutoModelWithLMHead.from_pretrained(mode_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(mode_name)
|
11 |
+
|
12 |
+
pipe_ch = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer)
|
13 |
+
|
14 |
+
def translate_ch(text):
|
15 |
+
return pipe_ch(text)[0]["translation_text"]
|
16 |
+
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
with gr.Row():
|
19 |
+
with gr.Column():
|
20 |
+
english = gr.Textbox(label="English text")
|
21 |
+
translate_btn = gr.Button(value="Translate")
|
22 |
+
with gr.Column():
|
23 |
+
chinese = gr.Textbox(label="Chinese Text")
|
24 |
+
|
25 |
+
translate_btn.click(translate_ch, inputs=english, outputs=chinese, api_name="translate-to-chinese")
|
26 |
+
examples = gr.Examples(examples=["I like to study Data Science and Machine Learning.", "Helen is a good swimmer."],
|
27 |
+
inputs=[english])
|
28 |
+
|
29 |
+
demo.launch()
|