Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +11 -14
- app.py +145 -0
- requirements.txt +10 -0
README.md
CHANGED
|
@@ -1,14 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
title: macbert4mdcspell_v3
|
| 2 |
+
emoji: 😻
|
| 3 |
+
colorFrom: indigo
|
| 4 |
+
colorTo: blue
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 5.16.0
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
short_description: CSC(Chinese Spelling Correct) of "Macropodus/macbert4mdcspell_v3
|
| 11 |
+
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# !/usr/bin/python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
# @time : 2021/2/29 21:41
|
| 4 |
+
# @author : Mo
|
| 5 |
+
# @function: transformers直接加载bert类模型测试
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
import traceback
|
| 9 |
+
import copy
|
| 10 |
+
import time
|
| 11 |
+
import sys
|
| 12 |
+
import os
|
| 13 |
+
import re
|
| 14 |
+
os.environ["MACRO_CORRECT_FLAG_CSC_TOKEN"] = "1"
|
| 15 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 16 |
+
os.environ["USE_TORCH"] = "1"
|
| 17 |
+
|
| 18 |
+
from macro_correct.pytorch_textcorrection.tcTools import preprocess_same_with_training
|
| 19 |
+
from macro_correct.pytorch_textcorrection.tcTools import get_errors_for_difflib
|
| 20 |
+
from macro_correct.pytorch_textcorrection.tcTools import cut_sent_by_maxlen
|
| 21 |
+
from macro_correct.pytorch_textcorrection.tcTools import count_flag_zh
|
| 22 |
+
from macro_correct import correct_basic
|
| 23 |
+
from macro_correct import correct_long
|
| 24 |
+
from macro_correct import correct
|
| 25 |
+
import gradio as gr
|
| 26 |
+
|
| 27 |
+
# pyinstaller -F xxxx.py
|
| 28 |
+
|
| 29 |
+
# pretrained_model_name_or_path = "shibing624/macbert4csc-base-chinese"
|
| 30 |
+
pretrained_model_name_or_path = "Macropodus/macbert4mdcspell_v3"
|
| 31 |
+
# pretrained_model_name_or_path = "Macropodus/macbert4mdcspell_v2"
|
| 32 |
+
# pretrained_model_name_or_path = "Macropodus/macbert4mdcspell_v1"
|
| 33 |
+
# pretrained_model_name_or_path = "Macropodus/macbert4csc_v1"
|
| 34 |
+
# pretrained_model_name_or_path = "Macropodus/macbert4csc_v2"
|
| 35 |
+
# pretrained_model_name_or_path = "Macropodus/bert4csc_v1"
|
| 36 |
+
# device = torch.device("cpu")
|
| 37 |
+
# device = torch.device("cuda")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def cut_sent_by_stay_and_maxlen(text, max_len=126, return_length=True):
|
| 41 |
+
"""
|
| 42 |
+
分句但是保存原标点符号, 如果长度还是太长的话就切为固定长度的句子
|
| 43 |
+
Args:
|
| 44 |
+
text: str, sentence of input text;
|
| 45 |
+
max_len: int, max_len of traing texts;
|
| 46 |
+
return_length: bool, wether return length or not
|
| 47 |
+
Returns:
|
| 48 |
+
res: List<tuple>
|
| 49 |
+
"""
|
| 50 |
+
### text_sp = re.split(r"!”|?”|。”|……”|”!|”?|”。|”……|》。|)。|!|?|。|…|\!|\?", text)
|
| 51 |
+
text_sp = re.split(r"[》)!?。…”;;!?\n]+", text)
|
| 52 |
+
conn_symbol = "!?。…”;;!?》)\n"
|
| 53 |
+
text_length_s = []
|
| 54 |
+
text_cut = []
|
| 55 |
+
len_text = len(text) - 1
|
| 56 |
+
# signal_symbol = "—”>;?…)‘《’(·》“~,、!。:<"
|
| 57 |
+
len_global = 0
|
| 58 |
+
for idx, text_sp_i in enumerate(text_sp):
|
| 59 |
+
text_cut_idx = text_sp[idx]
|
| 60 |
+
len_global_before = copy.deepcopy(len_global)
|
| 61 |
+
len_global += len(text_sp_i)
|
| 62 |
+
while True:
|
| 63 |
+
if len_global <= len_text and text[len_global] in conn_symbol:
|
| 64 |
+
text_cut_idx += text[len_global]
|
| 65 |
+
else:
|
| 66 |
+
# len_global += 1
|
| 67 |
+
if text_cut_idx:
|
| 68 |
+
### 如果标点符号依旧切分不了, 就强行切
|
| 69 |
+
if len(text_cut_idx) > max_len:
|
| 70 |
+
text_cut_i, text_length_s_i = cut_sent_by_maxlen(
|
| 71 |
+
text=text, max_len=max_len, return_length=True)
|
| 72 |
+
text_length_s.extend(text_length_s_i)
|
| 73 |
+
text_cut.extend(text_cut_i)
|
| 74 |
+
else:
|
| 75 |
+
text_length_s.append([len_global_before, len_global])
|
| 76 |
+
text_cut.append(text_cut_idx)
|
| 77 |
+
break
|
| 78 |
+
len_global += 1
|
| 79 |
+
if return_length:
|
| 80 |
+
return text_cut, text_length_s
|
| 81 |
+
return text_cut
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def macro_correct(text):
|
| 85 |
+
print(text)
|
| 86 |
+
texts, texts_length = cut_sent_by_stay_and_maxlen(text, return_length=True)
|
| 87 |
+
text_str = ""
|
| 88 |
+
text_list = []
|
| 89 |
+
for t in texts:
|
| 90 |
+
print(t)
|
| 91 |
+
t_process = preprocess_same_with_training(t)
|
| 92 |
+
text_csc = correct_long(t_process, num_rethink=1, flag_cut=True, limit_length_char=1)
|
| 93 |
+
print(text_csc)
|
| 94 |
+
### 繁简
|
| 95 |
+
if t != t_process:
|
| 96 |
+
t_correct, errors = get_errors_for_difflib(t_process, t)
|
| 97 |
+
errors_new = []
|
| 98 |
+
for err in errors:
|
| 99 |
+
if count_flag_zh(err[0]) and count_flag_zh(err[1]):
|
| 100 |
+
errors_new.append(err + [1])
|
| 101 |
+
if errors_new:
|
| 102 |
+
if text_csc:
|
| 103 |
+
text_csc[0]["errors"] += errors_new
|
| 104 |
+
else:
|
| 105 |
+
text_csc = [{"source": t, "target": t_process, "errors": errors_new}]
|
| 106 |
+
### 本身的错误
|
| 107 |
+
if text_csc:
|
| 108 |
+
text_list.extend(text_csc)
|
| 109 |
+
text_str += text_csc[0].get("target")
|
| 110 |
+
else:
|
| 111 |
+
text_list.extend([{}])
|
| 112 |
+
text_str += t
|
| 113 |
+
text_str += "\n" + "#" * 32 + "\n"
|
| 114 |
+
for tdx, t in enumerate(text_list):
|
| 115 |
+
if t:
|
| 116 |
+
for tk, tv in t.items():
|
| 117 |
+
if tk == "index":
|
| 118 |
+
text_str += f"idx: {str(tdx+1)}\n"
|
| 119 |
+
else:
|
| 120 |
+
text_str += f"{str(tk).strip()}: {str(tv).strip()}\n"
|
| 121 |
+
text_str += "\n"
|
| 122 |
+
return text_str
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
if __name__ == '__main__':
|
| 126 |
+
print(macro_correct('少先队员因该为老人让坐'))
|
| 127 |
+
|
| 128 |
+
examples = [
|
| 129 |
+
"机七学习是人工智能领遇最能体现��能的一个分知",
|
| 130 |
+
"我是练习时长两念半的鸽仁练习生蔡徐坤",
|
| 131 |
+
"真麻烦你了。希望你们好好的跳无",
|
| 132 |
+
"他法语说的很好,的语也不错",
|
| 133 |
+
"遇到一位很棒的奴生跟我疗天",
|
| 134 |
+
"我们为这个目标努力不解",
|
| 135 |
+
]
|
| 136 |
+
gr.Interface(
|
| 137 |
+
macro_correct,
|
| 138 |
+
inputs='text',
|
| 139 |
+
outputs='text',
|
| 140 |
+
title="Chinese Spelling Correction Model Macropodus/macbert4csc_v3",
|
| 141 |
+
description="Copy or input error Chinese text. Submit and the machine will correct text.",
|
| 142 |
+
article="Link to <a href='https://github.com/yongzhuo/macro-correct' style='color:blue;' target='_blank\'>Github REPO: macro-correct</a>",
|
| 143 |
+
examples=examples
|
| 144 |
+
).launch()
|
| 145 |
+
# ).launch(server_name="0.0.0.0", server_port=8066, share=False, debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.39.0
|
| 2 |
+
pybind11>=2.12
|
| 3 |
+
huggingface_hub==0.36.0
|
| 4 |
+
tokenizers==0.21.1
|
| 5 |
+
transformers==4.48.3
|
| 6 |
+
torch==2.6.0
|
| 7 |
+
opencc==1.1.1
|
| 8 |
+
macro-correct==0.0.6
|
| 9 |
+
tensorboardX==2.4
|
| 10 |
+
pypinyin
|