Spaces:
Sleeping
Sleeping
File size: 3,933 Bytes
726ab88 2fc88a6 81f6ef2 726ab88 5f16e98 2a8c1bb 726ab88 446723d df6d677 726ab88 1c8e05d 446723d 2a8c1bb 5f16e98 ccf389e 5f16e98 547b914 5f16e98 81f6ef2 5f16e98 81f6ef2 5f16e98 81f6ef2 2fc88a6 547b914 81f6ef2 6b49100 3210faf 547b914 81f6ef2 6b49100 81f6ef2 6b49100 81f6ef2 6b49100 81f6ef2 6b49100 3210faf 5f16e98 |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import streamlit as st
from transformers import pipeline
from underthesea import word_tokenize # Thư viện underthesea để tokenize tiếng Việt
import difflib
# Cấu hình ứng dụng
MAX_LENGTH = 512
st.set_page_config(
page_title="Demo Correct Spelling Mistakes",
page_icon="🤖",
layout="centered",
initial_sidebar_state="auto"
)
# CSS tùy chỉnh
custom_css = """
<style>
body {
background-color: #f4f4f4;
font-family: 'Arial', sans-serif;
}
.main {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
max-width: 800px;
margin: 0 auto;
}
h1 {
text-align: center;
color: #4a90e2;
}
textarea {
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
color: #333;
}
.stButton button {
background-color: #4a90e2;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.stButton button:hover {
background-color: #357ABD;
}
.markdown-text-container {
margin-top: 20px;
}
.highlight {
color: #d9534f;
font-weight: bold;
}
</style>
"""
st.markdown(custom_css, unsafe_allow_html=True)
st.title("Correct Spelling Mistakes App")
# Load mô hình
model_checkpoint = "Diezu/bat_pho_bo" # Thay đổi checkpoint phù hợp
correct_spelling = pipeline("text2text-generation", model=model_checkpoint)
# Nhập liệu từ người dùng
context = st.text_area("Input text", placeholder="Nhập văn bản có lỗi chính tả...")
# Xử lý nút bấm
if st.button("Get Result"):
if context.strip():
try:
# Sử dụng pipeline để sửa lỗi chính tả
result = correct_spelling(context, max_length=MAX_LENGTH)
corrected_text = result[0]['generated_text'] if result else "No output generated."
# Tokenize tiếng Việt sử dụng underthesea
original_tokens = word_tokenize(context)
corrected_tokens = word_tokenize(corrected_text)
# So sánh các từ và tìm từ thay đổi
def highlight_differences(original, corrected):
highlighted_text = []
modified_indices = []
matcher = difflib.SequenceMatcher(None, original, corrected)
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'replace': # Nếu từ bị thay thế
for word in corrected[j1:j2]:
highlighted_text.append(f"<span class='highlight'>{word}</span>") # Bôi đỏ từ đã sửa
modified_indices.extend(range(j1, j2))
elif tag == 'insert': # Nếu từ mới được thêm
for word in corrected[j1:j2]:
highlighted_text.append(f"<span class='highlight'>{word}</span>")
modified_indices.extend(range(j1, j2))
else: # Nếu từ không thay đổi
highlighted_text.extend(corrected[j1:j2])
return " ".join(highlighted_text), modified_indices
# Lấy kết quả đã chỉnh sửa và vị trí các từ đã sửa
highlighted_text, modified_indices = highlight_differences(original_tokens, corrected_tokens)
# Hiển thị kết quả
st.markdown(f"### Corrected Text (with highlighted words):\n\n{highlighted_text}", unsafe_allow_html=True)
st.markdown(f"### Modified Word Indices:\n\n{modified_indices}")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please input some text to process!")
|