Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,50 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
|
4 |
def load_model(checkpoint_path):
|
5 |
-
# Load tokenizer và model từ Hugging Face Hub
|
6 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
7 |
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint_path)
|
8 |
return model, tokenizer
|
9 |
|
10 |
def correct_spelling(text):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# Tokenize và generate
|
15 |
-
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
16 |
-
outputs = model.generate(
|
17 |
-
**inputs,
|
18 |
-
max_length=512,
|
19 |
-
num_beams=5,
|
20 |
-
early_stopping=True
|
21 |
-
)
|
22 |
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
return corrected_text
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
model, tokenizer = load_model(checkpoint_path)
|
28 |
|
29 |
-
# Tạo giao diện Gradio
|
30 |
demo = gr.Interface(
|
31 |
-
fn=
|
32 |
inputs=gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả..."),
|
33 |
-
outputs=gr.Textbox(label="Văn bản đã sửa"),
|
34 |
title="Demo Sửa Lỗi Chính Tả",
|
35 |
-
description="Sử dụng mô hình
|
36 |
)
|
37 |
|
38 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import difflib
|
3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
|
5 |
def load_model(checkpoint_path):
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
7 |
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint_path)
|
8 |
return model, tokenizer
|
9 |
|
10 |
def correct_spelling(text):
|
11 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
12 |
+
outputs = model.generate(**inputs, max_length=512, num_beams=5, early_stopping=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
14 |
return corrected_text
|
15 |
|
16 |
+
def find_spelling_errors(s1, s2):
|
17 |
+
a = s1.split()
|
18 |
+
b = s2.split()
|
19 |
+
matcher = difflib.SequenceMatcher(None, a, b)
|
20 |
+
highlighted_text = []
|
21 |
+
|
22 |
+
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
|
23 |
+
if tag == 'replace':
|
24 |
+
highlighted_text.append(f'<span style="color: red; text-decoration: underline;">{" ".join(a[i1:i2])}</span>')
|
25 |
+
elif tag == 'delete':
|
26 |
+
highlighted_text.append(f'<span style="color: orange; text-decoration: line-through;">{" ".join(a[i1:i2])}</span>')
|
27 |
+
elif tag == 'insert':
|
28 |
+
highlighted_text.append(f'<span style="color: green; font-weight: bold;">{" ".join(b[j1:j2])}</span>')
|
29 |
+
else:
|
30 |
+
highlighted_text.append(" ".join(a[i1:i2]))
|
31 |
+
|
32 |
+
return " ".join(highlighted_text)
|
33 |
+
|
34 |
+
def process_text(text):
|
35 |
+
corrected_text = correct_spelling(text)
|
36 |
+
highlighted_text = find_spelling_errors(text, corrected_text)
|
37 |
+
return highlighted_text, corrected_text
|
38 |
+
|
39 |
+
checkpoint_path = "Diezu/Batpho_v2"
|
40 |
model, tokenizer = load_model(checkpoint_path)
|
41 |
|
|
|
42 |
demo = gr.Interface(
|
43 |
+
fn=process_text,
|
44 |
inputs=gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả..."),
|
45 |
+
outputs=[gr.HTML(label="Phát hiện lỗi"), gr.Textbox(label="Văn bản đã sửa")],
|
46 |
title="Demo Sửa Lỗi Chính Tả",
|
47 |
+
description="Sử dụng mô hình để sửa lỗi chính tả. 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."
|
48 |
)
|
49 |
|
50 |
+
demo.launch()
|