File size: 2,467 Bytes
726ab88
828db21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1e495e
 
 
 
 
 
 
6bcd2a6
d1e495e
 
 
 
 
828db21
 
 
328c5dc
 
ce1c0a8
328c5dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
828db21
328c5dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification  # Thay bằng mô hình của bạn
import torch

# Load mô hình (thay thế bằng mô hình sửa lỗi chính tả của bạn)
@st.cache_resource
def load_model():
    tokenizer = AutoTokenizer.from_pretrained("Diezu/bat_pho_bo")  # Thay bằng tên mô hình của bạn
    model = AutoModelForSequenceClassification.from_pretrained("Diezu/bat_pho_bo")
    return tokenizer, model

# Hàm phát hiện lỗi chính tả
def detect_errors(text, tokenizer, model):
    errors = []
    words = text.split()
    for word in words:
        inputs = tokenizer(word, return_tensors="pt", padding=True, truncation=True)
        with torch.no_grad():
            outputs = model(**inputs)
        probabilities = torch.softmax(outputs.logits, dim=-1)
        predicted_class = torch.argmax(probabilities, dim=-1).item()
        if predicted_class == 1:  # Nếu từ sai
            errors.append(word)
    return errors

# Streamlit App
st.title("Công cụ phát hiện lỗi chính tả")
# CSS tùy chỉnh
st.markdown(
    """
    <style>
    .stTextArea textarea {
        font-size: 18px; /* Thay đổi kích thước font */
        font-weight: bold; /* Làm đậm chữ */
        weight : 30%;
    }
    </style>
    """,
    unsafe_allow_html=True
)
# Tải mô hình
tokenizer, model = load_model()

# Tạo bố cục 2 cột
col1, col2 = st.columns([1, 1])  # Hai cột có kích thước bằng nhau

# Cột bên trái: Nhập văn bản
with col1:
    st.header("Nhập văn bản")
    input_text = st.text_area(
        "Nhập văn bản:", 
        height=300, 
        max_chars=5000, 
        placeholder="Nhập văn bản của bạn ở đây..."
    )

# Cột bên phải: Hiển thị kết quả
with col2:
    st.header("Kết quả phát hiện lỗi")
    if st.button("Phát hiện lỗi", use_container_width=True):
        if input_text.strip():
            errors = detect_errors(input_text, tokenizer, model)
            if errors:
                st.text_area(
                    "Các từ phát hiện lỗi:",
                    value=", ".join(errors),
                    height=300,
                    disabled=True,
                )
            else:
                st.success("Không phát hiện lỗi nào trong văn bản.")
        else:
            st.warning("Vui lòng nhập văn bản để kiểm tra.")