|
import gradio as gr |
|
from t5.t5_model import T5Model |
|
from transformers import AutoTokenizer, T5ForConditionalGeneration |
|
|
|
|
|
model = T5Model('t5', "CodeTed/CGEDit", args={"eval_batch_size": 1}, cuda_device=-1, evaluate=True) |
|
|
|
def cged_correction(sentence, function): |
|
prompt = {"錯別字校正":"糾正句子中的錯字:", "文法校正":"糾正句子中的錯誤:", |
|
"文本重構":"在不改動文意的情況下改寫句子:", "文本簡化":"在不改動文意的情況下改寫句子:", "整體校正":"修改句子的錯誤或使其更通順:"} |
|
|
|
for _ in range(3): |
|
output = model.predict([prompt[function] + sentence + "_輸出句:"]) |
|
sentence = output[0] |
|
|
|
return output[0] |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown( |
|
""" |
|
# Chinese Grammarly - 中文文本自動編輯器 |
|
### 貼上中文文章來使你的句子更順暢~ |
|
Start typing below to see the correction. |
|
""" |
|
) |
|
funt = gr.Radio(["錯別字校正", "文法校正", "文本重構", "文本簡化", "整體校正"], label="Correction Type") |
|
|
|
sent = gr.Textbox(label="Sentence", placeholder="input the sentence") |
|
|
|
output = gr.Textbox(label="Result", placeholder="correction") |
|
|
|
greet_btn = gr.Button("Correction") |
|
|
|
greet_btn.click(fn=cged_correction, inputs=[sent, funt], outputs=output) |
|
demo.launch() |