File size: 1,733 Bytes
7b99548
da060de
ea268c4
da060de
 
 
7b99548
ea268c4
c1d5d90
 
da060de
3cc19cf
86848fa
 
da060de
86848fa
ea268c4
 
 
 
c1d5d90
 
ea268c4
 
 
2a548f2
ea268c4
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
import gradio as gr
from t5.t5_model import T5Model
from transformers import AutoTokenizer, T5ForConditionalGeneration
#tokenizer = AutoTokenizer.from_pretrained("CodeTed/CGEDit")
#model = T5ForConditionalGeneration.from_pretrained("CodeTed/CGEDit")
model = T5Model('t5', "CodeTed/CGEDit", args={"eval_batch_size": 1}, cuda_device=-1, evaluate=True)

def cged_correction(sentence, function):
    prompt = {"錯別字校正":"糾正句子中的錯字:", "文法校正":"糾正句子中的錯誤:", 
    "文本重構":"在不改動文意的情況下改寫句子:", "文本簡化":"在不改動文意的情況下改寫句子:", "整體校正":"修改句子的錯誤或使其更通順:"}
    #input_ids = tokenizer(prompt[function] + sentence, return_tensors="pt").input_ids
    for _ in range(3):
        output = model.predict([prompt[function] + sentence + "_輸出句:"])
        sentence = output[0]
    #edited_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    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()