Puck
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pyperclip
|
4 |
+
|
5 |
+
zh_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
6 |
+
en_zh_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
|
7 |
+
|
8 |
+
def translate_zh_to_en(text):
|
9 |
+
translation = zh_en_translator(text)
|
10 |
+
return translation[0]['translation_text']
|
11 |
+
|
12 |
+
def translate_en_to_zh(text):
|
13 |
+
translation = en_zh_translator(text)
|
14 |
+
return translation[0]['translation_text']
|
15 |
+
|
16 |
+
with gr.Blocks() as demo:
|
17 |
+
input_text = gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text")
|
18 |
+
output_text = gr.Textbox(lines=2, label="Translated Text")
|
19 |
+
|
20 |
+
with gr.Row():
|
21 |
+
zh_to_en_button = gr.Button("Chinese to English")
|
22 |
+
en_to_zh_button = gr.Button("English to Chinese")
|
23 |
+
|
24 |
+
zh_to_en_button.click(translate_zh_to_en, inputs=input_text, outputs=output_text)
|
25 |
+
en_to_zh_button.click(translate_en_to_zh, inputs=input_text, outputs=output_text)
|
26 |
+
|
27 |
+
demo.launch()
|