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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,10 +1,8 @@
1
  import gradio as gr
2
  import difflib
3
- import nltk
4
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
5
 
6
- nltk.download('punkt')
7
-
8
  def load_model(checkpoint_path):
9
  tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
10
  model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint_path)
@@ -34,31 +32,29 @@ def find_spelling_errors(s1, s2):
34
 
35
  return " ".join(highlighted_text)
36
 
37
- def process_paragraph(text):
38
- sentences = nltk.sent_tokenize(text) # Chia đoạn văn thành câu
39
  corrected_sentences = []
40
  highlighted_sentences = []
41
-
42
  for sentence in sentences:
43
- corrected = correct_spelling(sentence)
44
- highlighted = find_spelling_errors(sentence, corrected)
45
- corrected_sentences.append(corrected)
46
- highlighted_sentences.append(highlighted)
47
-
48
- highlighted_text = " ".join(highlighted_sentences)
49
- corrected_text = " ".join(corrected_sentences)
50
-
51
- return highlighted_text, corrected_text
52
 
53
  checkpoint_path = "Diezu/Batpho_v2"
54
  model, tokenizer = load_model(checkpoint_path)
55
 
56
  demo = gr.Interface(
57
- fn=process_paragraph,
58
- inputs=gr.Textbox(placeholder="Nhập đoạn văn có lỗi chính tả...", lines=5),
59
- outputs=[gr.HTML(label="Phát hiện lỗi"), gr.Textbox(label="Văn bản đã sửa", lines=5)],
60
  title="Demo Sửa Lỗi Chính Tả",
61
- description="Sử dụng mô hình để sửa lỗi chính tả trong đoạn vă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."
62
  )
63
 
64
  demo.launch()
 
1
  import gradio as gr
2
  import difflib
3
+ import re
4
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
5
 
 
 
6
  def load_model(checkpoint_path):
7
  tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
8
  model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint_path)
 
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()