Diezu commited on
Commit
ab1bdfa
·
verified ·
1 Parent(s): 819e96f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -19,10 +19,14 @@ def find_spelling_errors(s1, s2):
19
  b = s2.split()
20
  matcher = difflib.SequenceMatcher(None, a, b)
21
  highlighted_text = []
 
22
 
23
  for tag, i1, i2, j1, j2 in matcher.get_opcodes():
24
  if tag == 'replace':
25
- highlighted_text.append(f'<span style="color: red; text-decoration: underline;">{" ".join(a[i1:i2])}</span>')
 
 
 
26
  elif tag == 'delete':
27
  highlighted_text.append(f'<span style="color: orange; text-decoration: line-through;">{" ".join(a[i1:i2])}</span>')
28
  elif tag == 'insert':
@@ -30,31 +34,44 @@ def find_spelling_errors(s1, s2):
30
  else:
31
  highlighted_text.append(" ".join(a[i1:i2]))
32
 
33
- return " ".join(highlighted_text)
34
 
35
  def process_text(text):
36
  sentences = re.split(r'(?<=[.!?])\s+', text)
37
  corrected_sentences = []
38
  highlighted_sentences = []
 
39
 
40
  for sentence in sentences:
41
  if sentence.strip():
42
  corrected_text = correct_spelling(sentence)
43
- highlighted_text = find_spelling_errors(sentence, corrected_text)
44
  corrected_sentences.append(corrected_text)
45
  highlighted_sentences.append(highlighted_text)
 
46
 
47
- return "<br>".join(highlighted_sentences), "\n".join(corrected_sentences)
48
 
49
- checkpoint_path = "Diezu/Batpho_v2"
50
- model, tokenizer = load_model(checkpoint_path)
 
 
51
 
52
- demo = gr.Interface(
53
- fn=process_text,
54
- inputs=gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả..."),
55
- outputs=[gr.HTML(label="Phát hiện lỗi"), gr.Textbox(label="Văn bản đã sửa")],
56
- title="Demo Sửa Lỗi Chính Tả",
57
- description="Sử dụng mô hình để sửa lỗi chính tả. Tách câu để xử lý nhanh hơn. Từ bị thay thế sẽ được gạch chân đỏ, từ bị xóa có màu cam gạch ngang, từ được thêm có màu xanh đậm."
58
- )
 
 
 
 
 
 
 
 
 
59
 
60
  demo.launch()
 
19
  b = s2.split()
20
  matcher = difflib.SequenceMatcher(None, a, b)
21
  highlighted_text = []
22
+ corrected_words = {}
23
 
24
  for tag, i1, i2, j1, j2 in matcher.get_opcodes():
25
  if tag == 'replace':
26
+ incorrect_word = " ".join(a[i1:i2])
27
+ correct_word = " ".join(b[j1:j2])
28
+ highlighted_text.append(f'<button onclick="replaceWord(this, \"{correct_word}\")" style="color: red; text-decoration: underline; border: none; background: none;">{incorrect_word}</button>')
29
+ corrected_words[incorrect_word] = correct_word
30
  elif tag == 'delete':
31
  highlighted_text.append(f'<span style="color: orange; text-decoration: line-through;">{" ".join(a[i1:i2])}</span>')
32
  elif tag == 'insert':
 
34
  else:
35
  highlighted_text.append(" ".join(a[i1:i2]))
36
 
37
+ return " ".join(highlighted_text), corrected_words
38
 
39
  def process_text(text):
40
  sentences = re.split(r'(?<=[.!?])\s+', text)
41
  corrected_sentences = []
42
  highlighted_sentences = []
43
+ all_corrected_words = {}
44
 
45
  for sentence in sentences:
46
  if sentence.strip():
47
  corrected_text = correct_spelling(sentence)
48
+ highlighted_text, corrected_words = find_spelling_errors(sentence, corrected_text)
49
  corrected_sentences.append(corrected_text)
50
  highlighted_sentences.append(highlighted_text)
51
+ all_corrected_words.update(corrected_words)
52
 
53
+ return "<br>".join(highlighted_sentences), "\n".join(corrected_sentences), all_corrected_words
54
 
55
+ def apply_full_correction(text, corrected_words):
56
+ for incorrect, correct in corrected_words.items():
57
+ text = text.replace(incorrect, f'<span style="color: blue; font-weight: bold;">{correct}</span>')
58
+ return text
59
 
60
+ demo = gr.Blocks()
61
+ with demo:
62
+ input_text = gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả...")
63
+ output_html = gr.HTML()
64
+ corrected_textbox = gr.Textbox()
65
+ corrected_words_state = gr.State({})
66
+
67
+ btn_check = gr.Button("Phát hiện lỗi")
68
+ btn_correct_all = gr.Button("Sửa toàn bộ lỗi")
69
+
70
+ def update_text(text):
71
+ highlighted, corrected, corrected_words = process_text(text)
72
+ return highlighted, corrected, corrected_words
73
+
74
+ btn_check.click(update_text, inputs=input_text, outputs=[output_html, corrected_textbox, corrected_words_state])
75
+ btn_correct_all.click(apply_full_correction, inputs=[input_text, corrected_words_state], outputs=output_html)
76
 
77
  demo.launch()