File size: 8,683 Bytes
eb59cf9 | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>mdeberta-ner-ontonotes5</title>
<meta name="description" content="Named Entity Recognition Demo">
<style>
:root {
--c-misc: #f3e5f5;
--t-misc: #4a148c;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
background-color: #fafafa;
color: #111;
line-height: 1.6;
}
.container {
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.04);
border: 1px solid #eaeaea;
}
header {
margin-bottom: 30px;
}
h1 {
font-size: 24px;
font-weight: 600;
margin: 0 0 8px 0;
letter-spacing: -0.02em;
}
.subtitle {
color: #666;
font-size: 14px;
}
/* Input Section */
.input-group {
margin-bottom: 20px;
position: relative;
}
label {
display: block;
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
color: #888;
margin-bottom: 8px;
letter-spacing: 0.05em;
}
textarea {
width: 100%;
min-height: 120px;
padding: 16px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 16px;
line-height: 1.6;
resize: vertical;
background-color: #fff;
box-sizing: border-box;
font-family: inherit;
outline: none;
transition: border-color 0.2s, box-shadow 0.2s;
color: #333;
}
textarea:focus {
border-color: #000;
box-shadow: 0 0 0 2px rgba(0,0,0,0.05);
}
/* Button */
button.main-btn {
background-color: #1a1a1a;
color: white;
border: none;
padding: 14px 32px;
border-radius: 8px;
font-family: inherit;
font-size: 15px;
font-weight: 500;
cursor: pointer;
display: block;
width: 100%;
transition: background-color 0.2s, transform 0.1s;
}
button.main-btn:hover {
background-color: #333;
}
button.main-btn:active {
transform: scale(0.99);
}
button.main-btn:disabled {
background-color: #ccc;
cursor: not-allowed;
transform: none;
}
/* Output Section */
#output-wrapper {
margin-top: 30px;
display: none;
animation: fadeIn 0.3s ease-out;
}
.result-box {
padding: 20px;
border: 1px solid #eee;
background-color: #fcfcfc;
border-radius: 8px;
font-size: 16px;
line-height: 1.8;
white-space: pre-wrap;
}
/* Entity Styling */
.entity {
padding: 2px 6px;
border-radius: 4px;
font-weight: 500;
cursor: help;
position: relative;
transition: background-color 0.2s;
box-decoration-break: clone;
-webkit-box-decoration-break: clone;
}
/* Tooltip */
.entity::after {
content: attr(data-label) " " attr(data-score);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%) translateY(-4px);
background: #1a1a1a;
color: #fff;
padding: 4px 8px;
border-radius: 4px;
font-size: 11px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s, transform 0.2s;
z-index: 10;
font-weight: 400;
}
.entity:hover::after {
opacity: 1;
transform: translateX(-50%) translateY(-8px);
}
.type-DEFAULT { background: var(--c-misc); color: var(--t-misc); }
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.error-msg {
color: #d32f2f;
background: #ffebee;
padding: 12px;
border-radius: 6px;
margin-top: 20px;
font-size: 14px;
display: none;
}
@media (max-width: 600px) {
body { padding: 20px 10px; }
.container { padding: 24px; }
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>mdeberta-ner-ontonotes5</h1>
<div class="subtitle">Named Entity Recognition Demo</div>
</header>
<div class="input-group">
<label for="inputText">Input Text</label>
<textarea id="inputText" placeholder="Enter text to analyze...">Apple Inc. is looking at buying a U.K. startup for $1 billion in London next week.</textarea>
</div>
<button id="analyzeBtn" class="main-btn" onclick="analyze()">Analyze Text</button>
<div id="errorBox" class="error-msg"></div>
<div id="output-wrapper">
<label>Result</label>
<div id="resultBox" class="result-box"></div>
</div>
</div>
<script>
async function analyze() {
const input = document.getElementById('inputText');
const btn = document.getElementById('analyzeBtn');
const outputWrapper = document.getElementById('output-wrapper');
const resultBox = document.getElementById('resultBox');
const errorBox = document.getElementById('errorBox');
const text = input.value.trim();
if (!text) return;
// Reset UI
btn.disabled = true;
btn.textContent = "Processing...";
errorBox.style.display = 'none';
outputWrapper.style.display = 'none';
try {
const response = await fetch('/predict', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text })
});
if (!response.ok) {
throw new Error(`Server Error: ${response.statusText}`);
}
const data = await response.json();
renderResult(text, data.entities);
// Show result
outputWrapper.style.display = 'block';
} catch (err) {
errorBox.textContent = err.message;
errorBox.style.display = 'block';
} finally {
btn.disabled = false;
btn.textContent = "Analyze Text";
}
}
function renderResult(originalText, entities) {
const resultBox = document.getElementById('resultBox');
resultBox.innerHTML = '';
if (!entities || entities.length === 0) {
resultBox.textContent = originalText;
return;
}
let lastIndex = 0;
entities.forEach(entity => {
// 1. Text before entity
const plainText = originalText.slice(lastIndex, entity.start);
resultBox.appendChild(document.createTextNode(plainText));
// 2. Entity
const span = document.createElement('span');
// Determine class based on entity group (fallback to DEFAULT)
const type = entity.entity_group || 'DEFAULT';
// Check if specific class exists in CSS is hard in JS, so we rely on CSS fallbacks or generic logic
// Here we map common groups to classes, others fall to DEFAULT via CSS if not defined
span.className = `entity type-${type} type-DEFAULT`;
span.textContent = originalText.slice(entity.start, entity.end);
// Tooltip data
span.setAttribute('data-label', type);
span.setAttribute('data-score', Math.round(entity.score * 100) + '%');
resultBox.appendChild(span);
lastIndex = entity.end;
});
// 3. Remaining text
resultBox.appendChild(document.createTextNode(originalText.slice(lastIndex)));
}
</script>
</body>
</html> |