Diezu commited on
Commit
ce1c0a8
·
verified ·
1 Parent(s): fb3b727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -29
app.py CHANGED
@@ -13,19 +13,14 @@ def load_model():
13
  def detect_errors(text, tokenizer, model):
14
  errors = []
15
  words = text.split()
16
-
17
  for word in words:
18
  inputs = tokenizer(word, return_tensors="pt", padding=True, truncation=True)
19
  with torch.no_grad():
20
  outputs = model(**inputs)
21
-
22
- # Xử lý kết quả để phát hiện lỗi (giả sử lớp "1" là từ sai)
23
  probabilities = torch.softmax(outputs.logits, dim=-1)
24
  predicted_class = torch.argmax(probabilities, dim=-1).item()
25
-
26
  if predicted_class == 1: # Nếu từ sai
27
  errors.append(word)
28
-
29
  return errors
30
 
31
  # Streamlit App
@@ -34,28 +29,25 @@ st.title("Công cụ phát hiện lỗi chính tả")
34
  # Tải mô hình
35
  tokenizer, model = load_model()
36
 
37
- # Tạo bố cục ngang với tỷ lệ cột tùy chỉnh
38
- col1, col2 = st.columns([3, 2]) # Cột 1 rộng hơn cột 2
39
-
40
- # Cột 1: Nhập văn bản
41
- with col1:
42
- st.header("Nhập văn bản")
43
- input_text = st.text_area("Nhập văn bản của bạn tại đây:", height=300) # Tăng chiều cao của vùng nhập
44
-
45
- # Cột 2: Hiển thị kết quả
46
- with col2:
47
- st.header("Kết quả phát hiện lỗi")
48
- if st.button("Phát hiện lỗi", use_container_width=True): # Nút nằm trong cột
49
- if input_text.strip():
50
- # Phát hiện lỗi
51
- errors = detect_errors(input_text, tokenizer, model)
52
- if errors:
53
- st.write("**Các từ phát hiện lỗi:**")
54
- st.markdown(
55
- f"<div style='font-size: 18px; line-height: 1.8;'>{', '.join(f'<b>{word}</b>' for word in errors)}</div>",
56
- unsafe_allow_html=True,
57
- )
58
- else:
59
- st.success("Không phát hiện lỗi nào trong văn bản.")
60
  else:
61
- st.warning("Vui lòng nhập văn bản để kiểm tra.")
 
 
 
13
  def detect_errors(text, tokenizer, model):
14
  errors = []
15
  words = text.split()
 
16
  for word in words:
17
  inputs = tokenizer(word, return_tensors="pt", padding=True, truncation=True)
18
  with torch.no_grad():
19
  outputs = model(**inputs)
 
 
20
  probabilities = torch.softmax(outputs.logits, dim=-1)
21
  predicted_class = torch.argmax(probabilities, dim=-1).item()
 
22
  if predicted_class == 1: # Nếu từ sai
23
  errors.append(word)
 
24
  return errors
25
 
26
  # Streamlit App
 
29
  # Tải mô hình
30
  tokenizer, model = load_model()
31
 
32
+ # Tạo giao diện theo phong cách Google Dịch
33
+ input_text = st.text_area(
34
+ "Nhập văn bản:",
35
+ height=300,
36
+ max_chars=5000,
37
+ placeholder="Nhập văn bản của bạn ở đây..."
38
+ )
39
+
40
+ if st.button("Phát hiện lỗi"):
41
+ if input_text.strip():
42
+ errors = detect_errors(input_text, tokenizer, model)
43
+ if errors:
44
+ st.text_area(
45
+ "Kết quả phát hiện lỗi:",
46
+ value=", ".join(errors),
47
+ height=300,
48
+ disabled=True,
49
+ )
 
 
 
 
 
50
  else:
51
+ st.success("Không phát hiện lỗi nào trong văn bản.")
52
+ else:
53
+ st.warning("Vui lòng nhập văn bản để kiểm tra.")