shibing624 commited on
Commit
8f0e5c7
1 Parent(s): 30ea910

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -17,6 +17,11 @@ def ai_text(text):
17
  with torch.no_grad():
18
  outputs = model(**tokenizer([text], padding=True, return_tensors='pt'))
19
 
 
 
 
 
 
20
  def get_errors(corrected_text, origin_text):
21
  sub_details = []
22
  for i, ori_char in enumerate(origin_text):
@@ -39,7 +44,7 @@ def ai_text(text):
39
  corrected_text = _text[:len(text)]
40
  corrected_text, details = get_errors(corrected_text, text)
41
  print(text, ' => ', corrected_text, details)
42
- return corrected_text, details
43
 
44
 
45
  if __name__ == '__main__':
@@ -53,12 +58,22 @@ if __name__ == '__main__':
53
  ['他法语说的很好,的语也不错'],
54
  ['他们的吵翻很不错,再说他们做的咖喱鸡也好吃'],
55
  ]
56
- inputs = gr.inputs.Textbox(lines=5, placeholder="Input chinese text with spell error")
57
- output_text = gr.outputs.Textbox()
58
- gr.Interface(ai_text, inputs, output_text,
59
- theme="grass",
60
- css=".footer {display:none !important}",
61
- title="Chinese Spelling Correction Model shibing624/macbert4csc-base-chinese",
62
- description="Copy or input error Chinese text. Submit and the machine will correct text.",
63
- article="Link to <a href='https://github.com/shibing624/pycorrector' style='color:blue;' target='_blank\'>Github REPO</a>",
64
- examples=examples).launch()
 
 
 
 
 
 
 
 
 
 
 
17
  with torch.no_grad():
18
  outputs = model(**tokenizer([text], padding=True, return_tensors='pt'))
19
 
20
+ def to_highlight(corrected_sent, errs):
21
+ output = [{"entity": "纠错", "word": err[1], "start": err[2], "end": err[3]} for i, err in
22
+ enumerate(errs)]
23
+ return {"text": corrected_sent, "entities": output}
24
+
25
  def get_errors(corrected_text, origin_text):
26
  sub_details = []
27
  for i, ori_char in enumerate(origin_text):
 
44
  corrected_text = _text[:len(text)]
45
  corrected_text, details = get_errors(corrected_text, text)
46
  print(text, ' => ', corrected_text, details)
47
+ return to_highlight(corrected_text, details), details
48
 
49
 
50
  if __name__ == '__main__':
 
58
  ['他法语说的很好,的语也不错'],
59
  ['他们的吵翻很不错,再说他们做的咖喱鸡也好吃'],
60
  ]
61
+
62
+ gr.Interface(
63
+ ai_text,
64
+ inputs="textbox",
65
+ outputs=[
66
+ gr.outputs.HighlightedText(
67
+ label="Output",
68
+ show_legend=True,
69
+ ),
70
+ gr.outputs.JSON(
71
+ label="JSON Output"
72
+ )
73
+ ],
74
+ title="Chinese Spelling Correction Model shibing624/macbert4csc-base-chinese",
75
+ description="Copy or input error Chinese text. Submit and the machine will correct text.",
76
+ article="Link to <a href='https://github.com/shibing624/pycorrector' style='color:blue;' target='_blank\'>Github REPO</a>",
77
+ examples=examples
78
+ ).launch()
79
+