hHoai commited on
Commit
c16f5b4
·
verified ·
1 Parent(s): 7e366f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -1,31 +1,35 @@
1
  import gradio as gr
2
- import requests
3
  import time
 
 
4
 
5
- def spellcheck(text):
6
- url = "https://spellcheck.vcntt.tech/spellcheck"
7
- data = {"text": text}
 
8
 
 
9
  start_time = time.time()
10
- response = requests.post(url, json=data)
 
 
 
 
 
 
 
 
11
  end_time = time.time()
12
-
13
- if response.status_code != 200:
14
- return "Lỗi kết nối API", text, "", "", ""
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_words = "\n".join([e['word'] for e in errors])
26
- suggestions = "\n".join([', '.join(e['suggestions']) if e['suggestions'] else 'Xóa bỏ' for e in errors])
27
-
28
- return "Có lỗi chính tả", corrected_text, error_words, suggestions, processing_time
29
 
30
  with gr.Blocks() as demo:
31
  gr.Markdown("# Demo Kiểm tra lỗi chính tả tiếng Việt")
 
1
  import gradio as gr
 
2
  import time
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
+ import torch
5
 
6
+ # Load mô hình
7
+ model_checkpoint = "hHoai/bartPHO_final"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)
10
 
11
+ def spellcheck(text):
12
  start_time = time.time()
13
+
14
+ # Mã hóa đầu vào
15
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
16
+
17
+ # Sinh văn bản đã sửa lỗi
18
+ with torch.no_grad():
19
+ outputs = model.generate(**inputs)
20
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
+
22
  end_time = time.time()
 
 
 
 
 
 
 
23
  processing_time = f"{end_time - start_time:.4f} giây"
24
+
25
+ # Giả lập phát hiện lỗi chính tả ( thể thay bằng logic so sánh input-output)
26
+ has_errors = text.strip() != corrected_text.strip()
27
+
28
+ if not has_errors:
29
  return "Không có lỗi chính tả", corrected_text, "", "", processing_time
30
+
31
+ # Hiện tại chưa có logic tách từ sai và gợi ý, cần thêm post-processing nếu cần
32
+ return "Có lỗi chính tả", corrected_text, "Đang cập nhật", "Đang cập nhật", processing_time
 
 
 
33
 
34
  with gr.Blocks() as demo:
35
  gr.Markdown("# Demo Kiểm tra lỗi chính tả tiếng Việt")