yammdd commited on
Commit
b004c22
·
verified ·
1 Parent(s): 9756cc0

Upload 3 files

Browse files
Files changed (3) hide show
  1. static/script.js +140 -0
  2. static/style.css +248 -0
  3. templates/index.html +75 -0
static/script.js ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ async function correctText() {
2
+ const inputText = document.getElementById("inputText").value;
3
+ const outputTextElem = document.getElementById("outputText");
4
+ const btn = document.getElementById("submitBtn");
5
+ const loading = document.getElementById("loading");
6
+ const analysisSection = document.getElementById("analysisSection");
7
+
8
+ const mode = document.querySelector('input[name="modelType"]:checked').value;
9
+
10
+ analysisSection.style.display = "none";
11
+ outputTextElem.value = "";
12
+
13
+ if (!inputText.trim()) {
14
+ document.getElementById("inputText").focus();
15
+ return;
16
+ }
17
+
18
+ btn.disabled = true;
19
+ loading.style.display = "flex";
20
+
21
+ try {
22
+ const response = await fetch('/correct', {
23
+ method: 'POST',
24
+ headers: { 'Content-Type': 'application/json' },
25
+ body: JSON.stringify({
26
+ text: inputText,
27
+ mode: mode
28
+ })
29
+ });
30
+
31
+ const data = await response.json();
32
+
33
+ if (data.error) {
34
+ outputTextElem.value = "Lỗi: " + data.error;
35
+ } else {
36
+ const resultText = data.result;
37
+ // Lấy thêm mảng confidences, mặc định là mảng rỗng nếu không có
38
+ const confidences = data.confidences || [];
39
+
40
+ outputTextElem.value = resultText;
41
+
42
+ analysisSection.style.display = "block";
43
+ setTimeout(() => {
44
+ analysisSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
45
+ }, 100);
46
+
47
+ // Truyền thêm confidences vào hàm so sánh
48
+ generateDiff(inputText, resultText, confidences);
49
+ }
50
+ } catch (error) {
51
+ outputTextElem.value = "Lỗi kết nối: " + error;
52
+ console.error(error);
53
+ } finally {
54
+ btn.disabled = false;
55
+ loading.style.display = "none";
56
+ }
57
+ }
58
+
59
+ // Hàm switch mode giữ nguyên
60
+ function handleModeChange() {
61
+ document.getElementById("analysisSection").style.display = "none";
62
+ document.getElementById("outputText").value = "";
63
+ document.getElementById("inputText").focus();
64
+ }
65
+
66
+ // Cập nhật hàm generateDiff để nhận tham số confidences
67
+ function generateDiff(original, corrected, confidences) {
68
+ const originWords = original.trim().split(/\s+/);
69
+ const correctWords = corrected.trim().split(/\s+/);
70
+
71
+ const highlightContainer = document.getElementById("highlightedText");
72
+ const tableBody = document.getElementById("comparisonTableBody");
73
+
74
+ highlightContainer.innerHTML = "";
75
+ tableBody.innerHTML = "";
76
+
77
+ const minLen = Math.min(originWords.length, correctWords.length);
78
+ let htmlHighlight = "";
79
+ let hasError = false;
80
+
81
+ for (let i = 0; i < minLen; i++) {
82
+ const oWord = originWords[i];
83
+ const cWord = correctWords[i];
84
+
85
+ // Lấy score tương ứng với từ đã sửa (cWord)
86
+ // Format phần trăm
87
+ let scoreVal = confidences[i];
88
+ let scoreText = "N/A";
89
+ let scoreColor = "#999"; // Màu mặc định
90
+
91
+ if (scoreVal !== undefined) {
92
+ const percent = (scoreVal * 100).toFixed(1);
93
+ scoreText = percent + "%";
94
+
95
+ // Tô màu dựa trên độ tin cậy
96
+ if (scoreVal >= 0.9) scoreColor = "#2e7d32"; // Xanh đậm (Cao)
97
+ else if (scoreVal >= 0.7) scoreColor = "#f57f17"; // Cam (Trung bình)
98
+ else scoreColor = "#c62828"; // Đỏ (Thấp)
99
+ }
100
+
101
+ if (oWord !== cWord) {
102
+ hasError = true;
103
+ htmlHighlight += `<span class="highlight-word" title="Sửa thành: ${cWord} (${scoreText})">${oWord}</span> `;
104
+
105
+ const row = document.createElement("tr");
106
+ row.innerHTML = `
107
+ <td style="color: #d32f2f;">${oWord}</td>
108
+ <td style="color: #2e7d32; font-weight:bold;">${cWord}</td>
109
+ <td style="color: ${scoreColor}; font-weight:600;">${scoreText}</td>
110
+ `;
111
+ tableBody.appendChild(row);
112
+ } else {
113
+ htmlHighlight += `<span>${oWord}</span> `;
114
+ }
115
+ }
116
+
117
+ // Xử lý trường hợp input dài hơn output (Từ bị xóa)
118
+ if (originWords.length > minLen) {
119
+ for (let i = minLen; i < originWords.length; i++) {
120
+ hasError = true;
121
+ htmlHighlight += `<span class="highlight-word" title="Đã xóa">${originWords[i]}</span> `;
122
+ const row = document.createElement("tr");
123
+ row.innerHTML = `
124
+ <td>${originWords[i]}</td>
125
+ <td>(Xóa)</td>
126
+ <td>-</td>
127
+ `;
128
+ tableBody.appendChild(row);
129
+ }
130
+ }
131
+
132
+ // Lưu ý: Trường hợp output dài hơn input (Thêm từ mới) thường ít gặp trong task sửa l���i chính tả
133
+ // nhưng nếu có thì logic hiện tại sẽ bỏ qua phần dư của correctWords.
134
+ // Nếu muốn hiển thị, bạn cần thêm vòng lặp xử lý từ index minLen -> correctWords.length.
135
+
136
+ highlightContainer.innerHTML = htmlHighlight.trim();
137
+ if (!hasError) {
138
+ tableBody.innerHTML = `<tr><td colspan="3" style="color:#888;">Văn bản không thay đổi.</td></tr>`;
139
+ }
140
+ }
static/style.css ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --brand-gradient: linear-gradient(90deg, #00316C 0%, #004E9A 50%, #85B106 100%);
3
+ --bg-white: #ffffff;
4
+ --text-primary: #1F1F1F;
5
+ --text-secondary: #5F6368;
6
+ --border-color: #E0E0E0;
7
+ --highlight-bg: #FFF9C4;
8
+ --highlight-text: #F57F17;
9
+ }
10
+
11
+ * { box-sizing: border-box; outline: none; }
12
+
13
+ body {
14
+ font-family: 'Inter', sans-serif;
15
+ margin: 0;
16
+ padding: 0; /* Bỏ padding mặc định để căn giữa chuẩn hơn */
17
+ min-height: 100vh; /* Bắt buộc chiều cao tối thiểu bằng màn hình */
18
+ background: var(--brand-gradient);
19
+
20
+ /* CĂN GIỮA TUYỆT ĐỐI */
21
+ display: flex;
22
+ justify-content: center;
23
+ align-items: center;
24
+ }
25
+
26
+ .main-card {
27
+ background-color: var(--bg-white);
28
+ width: 90%; /* Dùng % để responsive tốt hơn */
29
+ max-width: 1100px;
30
+ padding: 40px;
31
+ border-radius: 20px;
32
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
33
+ animation: zoomIn 0.4s ease-out;
34
+ margin: 20px; /* Cách lề một chút trên màn hình nhỏ */
35
+ }
36
+
37
+ @keyframes zoomIn {
38
+ from { transform: scale(0.95); opacity: 0; }
39
+ to { transform: scale(1); opacity: 1; }
40
+ }
41
+
42
+ .card-header {
43
+ text-align: center;
44
+ margin-bottom: 30px;
45
+ }
46
+
47
+ .app-title {
48
+ margin: 0 0 20px 0;
49
+ font-size: 28px;
50
+ font-weight: 800;
51
+ /*text-transform: uppercase;*/
52
+ background: var(--brand-gradient);
53
+ -webkit-background-clip: text;
54
+ -webkit-text-fill-color: transparent;
55
+ line-height: 1.4; /* Tăng khoảng cách dòng */
56
+ padding: 10px 0; /* Tạo không gian cho dấu không bị cắt */
57
+ display: block; /* Đảm bảo padding có tác dụng */
58
+ }
59
+
60
+ /* --- MODE SWITCHER STYLES (Mới) --- */
61
+ .mode-switcher {
62
+ display: inline-flex;
63
+ background: #F0F2F5;
64
+ padding: 4px;
65
+ border-radius: 50px;
66
+ box-shadow: inset 0 2px 4px rgba(0,0,0,0.05);
67
+ }
68
+
69
+ .mode-option {
70
+ position: relative;
71
+ cursor: pointer;
72
+ }
73
+
74
+ .mode-option input {
75
+ display: none;
76
+ }
77
+
78
+ .mode-btn {
79
+ display: block;
80
+ padding: 8px 24px;
81
+ border-radius: 40px;
82
+ font-size: 14px;
83
+ font-weight: 600;
84
+ color: var(--text-secondary);
85
+ transition: all 0.3s ease;
86
+ }
87
+
88
+ .mode-option input:checked + .mode-btn {
89
+ background: var(--bg-white);
90
+ color: #004E9A;
91
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
92
+ }
93
+
94
+ .mode-option:hover .mode-btn {
95
+ color: #004E9A;
96
+ }
97
+ /* ----------------------------- */
98
+
99
+ .editor-section {
100
+ display: flex;
101
+ gap: 30px;
102
+ margin-bottom: 30px;
103
+ }
104
+
105
+ .text-box-wrapper {
106
+ flex: 1;
107
+ display: flex;
108
+ flex-direction: column;
109
+ }
110
+
111
+ .box-label {
112
+ font-size: 13px;
113
+ font-weight: 700;
114
+ color: #004E9A;
115
+ margin-bottom: 10px;
116
+ text-transform: uppercase;
117
+ }
118
+
119
+ textarea {
120
+ width: 100%;
121
+ height: 200px;
122
+ padding: 20px;
123
+ font-size: 16px;
124
+ line-height: 1.6;
125
+ border: 1px solid var(--border-color);
126
+ border-radius: 12px;
127
+ resize: none;
128
+ font-family: inherit;
129
+ background-color: #F8F9FA;
130
+ transition: all 0.2s;
131
+ }
132
+
133
+ textarea:focus {
134
+ background-color: #fff;
135
+ border-color: #004E9A;
136
+ box-shadow: 0 0 0 3px rgba(0, 78, 154, 0.1);
137
+ }
138
+
139
+ #outputText { background-color: #fff; }
140
+
141
+ .action-section {
142
+ text-align: center;
143
+ margin-bottom: 20px;
144
+ display: flex;
145
+ flex-direction: column;
146
+ align-items: center;
147
+ gap: 15px;
148
+ }
149
+
150
+ button#submitBtn {
151
+ background: var(--brand-gradient);
152
+ color: white;
153
+ border: none;
154
+ padding: 14px 60px;
155
+ font-size: 15px;
156
+ font-weight: 700;
157
+ border-radius: 50px;
158
+ cursor: pointer;
159
+ box-shadow: 0 10px 20px rgba(0, 78, 154, 0.25);
160
+ transition: transform 0.2s, box-shadow 0.2s;
161
+ text-transform: uppercase;
162
+ letter-spacing: 0.5px;
163
+ }
164
+
165
+ button#submitBtn:hover {
166
+ transform: translateY(-2px);
167
+ box-shadow: 0 15px 30px rgba(0, 78, 154, 0.35);
168
+ }
169
+
170
+ button#submitBtn:disabled {
171
+ background: #ccc;
172
+ cursor: not-allowed;
173
+ transform: none;
174
+ box-shadow: none;
175
+ }
176
+
177
+ #loading {
178
+ display: none;
179
+ align-items: center;
180
+ gap: 10px;
181
+ color: var(--text-secondary);
182
+ font-size: 14px;
183
+ }
184
+
185
+ .spinner {
186
+ width: 20px;
187
+ height: 20px;
188
+ border: 3px solid #f3f3f3;
189
+ border-top: 3px solid #004E9A;
190
+ border-radius: 50%;
191
+ animation: spin 1s linear infinite;
192
+ }
193
+ @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
194
+
195
+ .analysis-section {
196
+ display: none;
197
+ border-top: 1px dashed var(--border-color);
198
+ padding-top: 30px;
199
+ margin-top: 30px;
200
+ }
201
+
202
+ .section-title {
203
+ text-align: center;
204
+ color: #004E9A;
205
+ font-size: 16px;
206
+ font-weight: 700;
207
+ margin-bottom: 20px;
208
+ text-transform: uppercase;
209
+ }
210
+
211
+ .highlight-box {
212
+ text-align: center;
213
+ font-size: 18px;
214
+ line-height: 1.8;
215
+ color: var(--text-primary);
216
+ margin-bottom: 30px;
217
+ background: #FAFAFA;
218
+ padding: 20px;
219
+ border-radius: 12px;
220
+ border: 1px solid #eee;
221
+ }
222
+
223
+ .highlight-word {
224
+ background-color: var(--highlight-bg);
225
+ color: var(--highlight-text);
226
+ padding: 2px 6px;
227
+ border-radius: 4px;
228
+ font-weight: 600;
229
+ border-bottom: 2px solid #FBC02D;
230
+ cursor: help;
231
+ }
232
+
233
+ .table-wrapper {
234
+ overflow-x: auto;
235
+ border-radius: 12px;
236
+ border: 1px solid #eee;
237
+ }
238
+
239
+ table { width: 100%; border-collapse: collapse; background: #fff; }
240
+ thead { background: var(--brand-gradient); color: white; }
241
+ th, td { padding: 12px 20px; text-align: center; border-bottom: 1px solid #eee; }
242
+ th { font-weight: 600; text-transform: uppercase; font-size: 13px; }
243
+ tbody tr:hover { background-color: #f8fbff; }
244
+
245
+ @media (max-width: 768px) {
246
+ .editor-section { flex-direction: column; }
247
+ .main-card { padding: 20px; margin: 10px; width: 95%; }
248
+ }
templates/index.html ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="vi">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>VietCorrect AI - Normalization</title>
7
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
8
+ <link rel="stylesheet" href="/static/style.css">
9
+ </head>
10
+ <body>
11
+
12
+ <div class="main-card">
13
+ <!-- Header -->
14
+ <div class="card-header">
15
+ <h1 class="app-title">CÔNG CỤ CHUẨN HÓA VĂN BẢN</h1>
16
+
17
+ <!-- MODEL SELECTION UI (Giao diện chọn model) -->
18
+ <div class="mode-switcher">
19
+ <label class="mode-option">
20
+ <!-- Mặc định checked vào Sửa lỗi, bạn có thể đổi nếu app.py đang chạy model khác -->
21
+ <input type="radio" name="modelType" value="correction" checked onchange="handleModeChange()">
22
+ <span class="mode-btn">Sửa lỗi chính tả</span>
23
+ </label>
24
+ <label class="mode-option">
25
+ <input type="radio" name="modelType" value="diacritics" onchange="handleModeChange()">
26
+ <span class="mode-btn">Thêm dấu</span>
27
+ </label>
28
+ </div>
29
+ </div>
30
+
31
+ <!-- Input / Output Section -->
32
+ <div class="editor-section">
33
+ <div class="text-box-wrapper">
34
+ <div class="box-label">Văn bản gốc</div>
35
+ <textarea id="inputText" placeholder="Nhập văn bản vào đây..."></textarea>
36
+ </div>
37
+
38
+ <div class="text-box-wrapper">
39
+ <div class="box-label">Kết quả xử lý</div>
40
+ <textarea id="outputText" readonly placeholder="Kết quả sẽ hiện ở đây..."></textarea>
41
+ </div>
42
+ </div>
43
+
44
+ <!-- Button Section -->
45
+ <div class="action-section">
46
+ <button id="submitBtn" onclick="correctText()">
47
+ <span>THỰC HIỆN XỬ LÝ</span>
48
+ </button>
49
+ <div id="loading">
50
+ <div class="spinner"></div> Đang xử lý...
51
+ </div>
52
+ </div>
53
+
54
+ <!-- Analysis Section -->
55
+ <div id="analysisSection" class="analysis-section">
56
+ <h2 class="section-title">PHÂN TÍCH THAY ĐỔI</h2>
57
+ <div id="highlightedText" class="highlight-box"></div>
58
+ <div class="table-wrapper">
59
+ <table>
60
+ <thead>
61
+ <tr>
62
+ <th>Trước khi sửa</th>
63
+ <th>Sau khi sửa</th>
64
+ <th style="width: 150px;">Độ tin cậy</th>
65
+ </tr>
66
+ </thead>
67
+ <tbody id="comparisonTableBody"></tbody>
68
+ </table>
69
+ </div>
70
+ </div>
71
+ </div>
72
+
73
+ <script src="/static/script.js"></script>
74
+ </body>
75
+ </html>