Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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.
|
|
|
|
|
|
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.")
|