hHoai commited on
Commit
30e1b0b
·
verified ·
1 Parent(s): 85c62b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -12
app.py CHANGED
@@ -11,35 +11,64 @@ 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
- 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 " 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("### dụ: Nhập văn bản có lỗi chính tả để kiểm tra")
 
43
 
44
  if __name__ == "__main__":
45
  demo.launch()
 
11
  end_time = time.time()
12
 
13
  if response.status_code != 200:
14
+ return "Lỗi kết nối API", [], text, f"{end_time - start_time:.4f} giây"
15
 
16
  result = response.json()
17
 
 
 
 
18
  if not result.get("has_errors", False):
19
+ return "Không có lỗi chính tả", [], text, f"{end_time - start_time:.4f} giây"
20
 
21
  errors = result.get("errors", [])
 
22
 
23
+ error_list = [(e["word"], e["suggestions"]) for e in errors]
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 = []
46
+
47
+ def show_errors(text):
48
+ status, errors, original_text, time_taken = spellcheck(text)
49
 
50
+ for ci in correction_inputs:
51
+ demo.remove(ci)
52
+ correction_inputs.clear()
53
+
54
+ choices = []
55
+ for error_word, suggestions in errors:
56
+ drop = gr.Dropdown(choices=[error_word] + suggestions, label=f"Từ sai: {error_word}")
57
+ correction_inputs.append(drop)
58
+ choices.append((error_word, drop))
59
+
60
+ return status, original_text, time_taken, choices
61
+
62
+ def apply_changes(text, *chosen_corrections):
63
+ corrections = [(word, chosen_corrections[i] or word) for i, (word, _) in enumerate(chosen_corrections)]
64
+ updated_text = update_text(text, corrections)
65
+ return updated_text
66
+
67
+ submit_btn = gr.Button("Kiểm tra lỗi")
68
+ confirm_btn = gr.Button("Xác nhận sửa")
69
 
70
+ submit_btn.click(show_errors, inputs=input_text, outputs=[output_status, corrected_text, output_time, correction_inputs])
71
+ confirm_btn.click(apply_changes, inputs=[input_text] + correction_inputs, outputs=corrected_text)
72
 
73
  if __name__ == "__main__":
74
  demo.launch()