Spaces:
Running
Running
| # !/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| # @time : 2021/2/29 21:41 | |
| # @author : Mo | |
| # @function: 文本纠错, 使用macro-correct | |
| import os | |
| os.environ["MACRO_CORRECT_FLAG_CSC_TOKEN"] = "1" | |
| from macro_correct import correct | |
| import gradio as gr | |
| ### 默认纠错(list输入) | |
| text_list = ["真麻烦你了。希望你们好好的跳无", | |
| "少先队员因该为老人让坐", | |
| "机七学习是人工智能领遇最能体现智能的一个分知", | |
| "一只小鱼船浮在平净的河面上" | |
| ] | |
| text_csc = correct(text_list) | |
| print("默认纠错(list输入):") | |
| for res_i in text_csc: | |
| print(res_i) | |
| print("#" * 128) | |
| """ | |
| 默认纠错(list输入): | |
| {'index': 0, 'source': '真麻烦你了。希望你们好好的跳无', 'target': '真麻烦你了。希望你们好好地跳舞', 'errors': [['的', '地', 12, 0.6584], ['无', '舞', 14, 1.0]]} | |
| {'index': 1, 'source': '少先队员因该为老人让坐', 'target': '少先队员应该为老人让坐', 'errors': [['因', '应', 4, 0.995]]} | |
| {'index': 2, 'source': '机七学习是人工智能领遇最能体现智能的一个分知', 'target': '机器学习是人工智能领域最能体现智能的一个分支', 'errors': [['七', '器', 1, 0.9998], ['遇', '域', 10, 0.9999], ['知', '支', 21, 1.0]]} | |
| {'index': 3, 'source': '一只小鱼船浮在平净的河面上', 'target': '一只小鱼船浮在平静的河面上', 'errors': [['净', '静', 8, 0.9961]]} | |
| """ | |
| def respond( | |
| message, | |
| history, | |
| system_message, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| messages = [{"role": "system", "content": system_message}] | |
| for val in history: | |
| if val[0]: | |
| messages.append({"role": "user", "content": val[0]}) | |
| if val[1]: | |
| messages.append({"role": "assistant", "content": val[1]}) | |
| messages.append({"role": "user", "content": message}) | |
| message_csc = correct([message]) | |
| target = message_csc[0].get("target", "") | |
| errors = message_csc[0].get("errors", "") | |
| response = target + " " + str(errors) | |
| out = "" | |
| for resp in response: | |
| out += resp | |
| yield out | |
| # response = "" | |
| # | |
| # for message in client.chat_completion( | |
| # messages, | |
| # max_tokens=max_tokens, | |
| # stream=True, | |
| # temperature=temperature, | |
| # top_p=top_p, | |
| # ): | |
| # token = message.choices[0].delta.content | |
| # | |
| # response += token | |
| # yield response | |
| """ | |
| For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
| """ | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="Macro-Correct", label="System message"), | |
| # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| # gr.Slider( | |
| # minimum=0.1, | |
| # maximum=1.0, | |
| # value=0.95, | |
| # step=0.05, | |
| # label="Top-p (nucleus sampling)", | |
| # ), | |
| ] | |
| # title="Chinese Spelling Correction Model Macropodus/macbert4csc_v2", | |
| # description="Copy or input error Chinese text. Submit and the machine will correct text.", | |
| # article="Link to <a href='https://github.com/yongzhuo/macro-correct' style='color:blue;' target='_blank\'>Github REPO: macro-correct</a>", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |
| # demo.launch(server_name="0.0.0.0", server_port=8087, share=False, debug=True) |