Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,59 +11,35 @@ def spellcheck(text):
|
|
11 |
end_time = time.time()
|
12 |
|
13 |
if response.status_code != 200:
|
14 |
-
return "Lỗi kết nối API",
|
15 |
|
16 |
result = response.json()
|
17 |
|
|
|
|
|
|
|
18 |
if not result.get("has_errors", False):
|
19 |
-
return "Không có lỗi chính tả",
|
20 |
|
21 |
errors = result.get("errors", [])
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
return "Có lỗi chính tả", error_list, text, f"{end_time - start_time:.4f} giây"
|
26 |
-
|
27 |
-
def apply_correction(original_text, corrections):
|
28 |
-
for error_word, chosen_word in corrections:
|
29 |
-
if chosen_word and chosen_word != error_word:
|
30 |
-
original_text = original_text.replace(error_word, chosen_word, 1)
|
31 |
-
return original_text
|
32 |
-
|
33 |
-
def update_text(text, corrections):
|
34 |
-
corrected_text = apply_correction(text, corrections)
|
35 |
-
return corrected_text
|
36 |
|
37 |
with gr.Blocks() as demo:
|
38 |
gr.Markdown("# Demo Kiểm tra lỗi chính tả tiếng Việt")
|
39 |
|
40 |
input_text = gr.Textbox(label="Nhập văn bản")
|
41 |
output_status = gr.Textbox(label="Trạng thái", interactive=False)
|
|
|
|
|
42 |
output_time = gr.Textbox(label="Thời gian xử lý", interactive=False)
|
43 |
-
corrected_text = gr.Textbox(label="Văn bản sau khi sửa", interactive=False)
|
44 |
-
|
45 |
-
correction_inputs = gr.State([])
|
46 |
-
|
47 |
-
def show_errors(text):
|
48 |
-
status, errors, original_text, time_taken = spellcheck(text)
|
49 |
-
|
50 |
-
choices = [(error_word, gr.Dropdown(choices=[error_word] + suggestions, label=f"Từ sai: {error_word}")) for error_word, suggestions in errors]
|
51 |
-
|
52 |
-
return status, original_text, time_taken, choices
|
53 |
-
|
54 |
-
def apply_changes(text, corrections):
|
55 |
-
corrections = [(word, chosen_word) for word, chosen_word in corrections]
|
56 |
-
updated_text = update_text(text, corrections)
|
57 |
-
return updated_text
|
58 |
|
59 |
submit_btn = gr.Button("Kiểm tra lỗi")
|
60 |
-
confirm_btn = gr.Button("Xác nhận sửa")
|
61 |
|
62 |
-
submit_btn.click(
|
63 |
|
64 |
-
|
65 |
-
inputs=[input_text, correction_inputs],
|
66 |
-
outputs=corrected_text)
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
demo.launch()
|
|
|
11 |
end_time = time.time()
|
12 |
|
13 |
if response.status_code != 200:
|
14 |
+
return "Lỗi kết nối API", "", "", ""
|
15 |
|
16 |
result = response.json()
|
17 |
|
18 |
+
corrected_text = result.get("corrected_text", text)
|
19 |
+
processing_time = f"{end_time - start_time:.4f} giây"
|
20 |
+
|
21 |
if not result.get("has_errors", False):
|
22 |
+
return "Không có lỗi chính tả", corrected_text, "", processing_time
|
23 |
|
24 |
errors = result.get("errors", [])
|
25 |
+
error_info = "\n".join([f"Từ sai: `{e['word']}` - Gợi ý: {', '.join(e['suggestions']) if e['suggestions'] else 'Xóa bỏ'}" for e in errors])
|
26 |
|
27 |
+
return "Có lỗi chính tả", corrected_text, error_info, processing_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
with gr.Blocks() as demo:
|
30 |
gr.Markdown("# Demo Kiểm tra lỗi chính tả tiếng Việt")
|
31 |
|
32 |
input_text = gr.Textbox(label="Nhập văn bản")
|
33 |
output_status = gr.Textbox(label="Trạng thái", interactive=False)
|
34 |
+
output_corrected = gr.Textbox(label="Văn bản sau khi sửa", interactive=False)
|
35 |
+
output_errors = gr.Textbox(label="Chi tiết lỗi", lines=4, interactive=False)
|
36 |
output_time = gr.Textbox(label="Thời gian xử lý", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
submit_btn = gr.Button("Kiểm tra lỗi")
|
|
|
39 |
|
40 |
+
submit_btn.click(spellcheck, inputs=input_text, outputs=[output_status, output_corrected, output_errors, output_time])
|
41 |
|
42 |
+
gr.Markdown("### Ví dụ: Nhập văn bản có lỗi chính tả để kiểm tra")
|
|
|
|
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
demo.launch()
|