Spaces:
Running
Running
shibing624
commited on
Commit
·
fdab1ea
1
Parent(s):
04a455f
update demo.
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ tokenizer = BertTokenizer.from_pretrained("shibing624/macbert4csc-base-chinese")
|
|
| 7 |
model = BertForMaskedLM.from_pretrained("shibing624/macbert4csc-base-chinese")
|
| 8 |
|
| 9 |
|
| 10 |
-
def ai_text(
|
| 11 |
with torch.no_grad():
|
| 12 |
-
outputs = model(**tokenizer(
|
| 13 |
|
| 14 |
def get_errors(corrected_text, origin_text):
|
| 15 |
sub_details = []
|
|
@@ -29,30 +29,26 @@ def ai_text(texts):
|
|
| 29 |
sub_details = sorted(sub_details, key=operator.itemgetter(2))
|
| 30 |
return corrected_text, sub_details
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
print(text, ' => ', corrected_text, details)
|
| 38 |
-
result.append((corrected_text, details))
|
| 39 |
-
print(result)
|
| 40 |
-
return result
|
| 41 |
|
| 42 |
|
| 43 |
if __name__ == '__main__':
|
| 44 |
-
print(ai_text(
|
| 45 |
|
| 46 |
examples = [
|
| 47 |
-
[
|
| 48 |
-
[
|
| 49 |
-
[
|
| 50 |
-
[
|
| 51 |
-
[
|
| 52 |
-
[
|
| 53 |
]
|
| 54 |
|
| 55 |
output_text = gr.outputs.Textbox()
|
| 56 |
-
gr.Interface(ai_text, "textbox",
|
| 57 |
description="Copy or input error Chinese text. Submit and the machine will correct text.",
|
| 58 |
examples=examples).launch()
|
|
|
|
| 7 |
model = BertForMaskedLM.from_pretrained("shibing624/macbert4csc-base-chinese")
|
| 8 |
|
| 9 |
|
| 10 |
+
def ai_text(text):
|
| 11 |
with torch.no_grad():
|
| 12 |
+
outputs = model(**tokenizer(text, padding=True, return_tensors='pt'))
|
| 13 |
|
| 14 |
def get_errors(corrected_text, origin_text):
|
| 15 |
sub_details = []
|
|
|
|
| 29 |
sub_details = sorted(sub_details, key=operator.itemgetter(2))
|
| 30 |
return corrected_text, sub_details
|
| 31 |
|
| 32 |
+
_text = tokenizer.decode(torch.argmax(outputs.logits, dim=-1), skip_special_tokens=True).replace(' ', '')
|
| 33 |
+
corrected_text = _text[:len(text)]
|
| 34 |
+
corrected_text, details = get_errors(corrected_text, text)
|
| 35 |
+
print(text, ' => ', corrected_text, details)
|
| 36 |
+
return corrected_text, details
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
if __name__ == '__main__':
|
| 40 |
+
print(ai_text('少先队员因该为老人让坐'))
|
| 41 |
|
| 42 |
examples = [
|
| 43 |
+
['真麻烦你了。希望你们好好的跳无'],
|
| 44 |
+
['少先队员因该为老人让坐'],
|
| 45 |
+
['机七学习是人工智能领遇最能体现智能的一个分知'],
|
| 46 |
+
['今天心情很好'],
|
| 47 |
+
['他法语说的很好,的语也不错'],
|
| 48 |
+
['他们的吵翻很不错,再说他们做的咖喱鸡也好吃'],
|
| 49 |
]
|
| 50 |
|
| 51 |
output_text = gr.outputs.Textbox()
|
| 52 |
+
gr.Interface(ai_text, "textbox", "textbox", title="Chinese Text Correction shibing624/macbert4csc-base-chinese",
|
| 53 |
description="Copy or input error Chinese text. Submit and the machine will correct text.",
|
| 54 |
examples=examples).launch()
|