Commit
·
e64ab21
1
Parent(s):
b002e1f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
from optimum.onnxruntime import ORTModelForSeq2SeqLM
|
4 |
+
from translator import Translator
|
5 |
+
from translation_pipeline import TranslationPipeline
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("hon9kon9ize/bart-translation-zh-yue-onnx")
|
8 |
+
model = ORTModelForSeq2SeqLM.from_pretrained(
|
9 |
+
"hon9kon9ize/bart-translation-zh-yue-onnx",
|
10 |
+
provider="CPUExecutionProvider",
|
11 |
+
encoder_file_name="encoder_model_quantized.onnx",
|
12 |
+
decoder_file_name="decoder_model_quantized.onnx",
|
13 |
+
decoder_file_with_past_name="decoder_with_past_model_quantized.onnx",
|
14 |
+
)
|
15 |
+
pipe = TranslationPipeline(model, tokenizer)
|
16 |
+
translator = Translator(pipe, max_length=512, batch_size=1)
|
17 |
+
|
18 |
+
|
19 |
+
def demo_process(input_text):
|
20 |
+
return translator(input_text)[0]
|
21 |
+
|
22 |
+
|
23 |
+
demo = gr.Interface(
|
24 |
+
fn=demo_process,
|
25 |
+
inputs=[
|
26 |
+
gr.Textbox(label="官話", type="text"),
|
27 |
+
],
|
28 |
+
outputs=[
|
29 |
+
gr.Textbox(label="廣東話", type="text"),
|
30 |
+
],
|
31 |
+
title=f"Chinese to Cantonese Translator",
|
32 |
+
description="This is a demo of the Chinese to Cantonese Translator.",
|
33 |
+
examples=[
|
34 |
+
[
|
35 |
+
"近年成为许多港人热门移居地的英国中部城巿诺定咸(又译诺丁汉,Nottingham),多年来一直面对财政困境,市议会周三(11月29日)宣布破产,是继英国第二大城市伯明翰今年9月宣布破产后,近期「爆煲」的另一个英国主要城市。诺定咸除了维持法例规定必须提供的服务外,巿政府将暂停所有非必要的公共开支。"
|
36 |
+
]
|
37 |
+
],
|
38 |
+
cache_examples=True,
|
39 |
+
)
|
40 |
+
demo.launch(server_name="0.0.0.0")
|