File size: 11,582 Bytes
0c26380 | 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | <!DOCTYPE html>
<html lang="si">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sinhala Sentiment Analysis API</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color: #0f172a;
--glass-bg: rgba(30, 41, 59, 0.7);
--glass-border: rgba(255, 255, 255, 0.1);
--text-main: #f8fafc;
--text-muted: #94a3b8;
--primary: #3b82f6;
--primary-hover: #2563eb;
--positive: #10b981;
--negative: #ef4444;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Outfit', sans-serif;
}
body {
background-color: var(--bg-color);
background-image:
radial-gradient(at 0% 0%, hsla(253,16%,7%,1) 0, transparent 50%),
radial-gradient(at 50% 0%, hsla(225,39%,30%,1) 0, transparent 50%),
radial-gradient(at 100% 0%, hsla(339,49%,30%,1) 0, transparent 50%);
color: var(--text-main);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem;
line-height: 1.6;
}
.container {
width: 100%;
max-width: 600px;
background: var(--glass-bg);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
border: 1px solid var(--glass-border);
border-radius: 24px;
padding: 3rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
opacity: 0;
transform: translateY(20px);
}
@keyframes slideUp {
to {
opacity: 1;
transform: translateY(0);
}
}
.header {
text-align: center;
margin-bottom: 2rem;
}
h1 {
font-size: 2rem;
font-weight: 700;
margin-bottom: 0.5rem;
background: linear-gradient(135deg, #60a5fa 0%, #a78bfa 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
p.subtitle {
color: var(--text-muted);
font-size: 0.95rem;
}
.input-group {
margin-bottom: 1.5rem;
position: relative;
}
textarea {
width: 100%;
background: rgba(15, 23, 42, 0.6);
border: 1px solid var(--glass-border);
border-radius: 12px;
padding: 1rem;
color: var(--text-main);
font-size: 1rem;
resize: vertical;
min-height: 120px;
transition: all 0.3s ease;
}
textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}
textarea::placeholder {
color: #475569;
}
button {
width: 100%;
background: var(--primary);
color: white;
border: none;
border-radius: 12px;
padding: 1rem;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
display: flex;
justify-content: center;
align-items: center;
gap: 0.5rem;
}
button:hover {
background: var(--primary-hover);
transform: translateY(-2px);
box-shadow: 0 10px 15px -3px rgba(37, 99, 235, 0.4);
}
button:active {
transform: translateY(0);
}
.loader {
display: none;
width: 20px;
height: 20px;
border: 3px solid rgba(255,255,255,0.3);
border-radius: 50%;
border-top-color: white;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.result-container {
margin-top: 2rem;
padding: 1.5rem;
border-radius: 12px;
background: rgba(15, 23, 42, 0.6);
border: 1px solid var(--glass-border);
display: none;
opacity: 0;
transition: opacity 0.4s ease;
}
.result-container.show {
display: block;
opacity: 1;
}
.sentiment-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
}
.sentiment-label {
font-size: 1.25rem;
font-weight: 700;
display: flex;
align-items: center;
gap: 0.5rem;
}
.positive { color: var(--positive); }
.negative { color: var(--negative); }
.confidence-wrapper {
margin-top: 1rem;
}
.confidence-header {
display: flex;
justify-content: space-between;
font-size: 0.85rem;
color: var(--text-muted);
margin-bottom: 0.5rem;
}
.progress-bar {
height: 8px;
background: rgba(255, 255, 255, 0.1);
border-radius: 999px;
overflow: hidden;
}
.progress-fill {
height: 100%;
border-radius: 999px;
transition: width 1s cubic-bezier(0.16, 1, 0.3, 1);
width: 0%;
}
.progress-fill.positive { background: var(--positive); box-shadow: 0 0 10px var(--positive); }
.progress-fill.negative { background: var(--negative); box-shadow: 0 0 10px var(--negative); }
.error-message {
color: var(--negative);
margin-top: 1rem;
text-align: center;
font-size: 0.9rem;
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Sinhala Sentiment Analysis</h1>
<p class="subtitle">AI-powered sentiment detection for Sinhala text</p>
</div>
<div class="input-group">
<textarea id="textInput" placeholder="සිංහල වාක්යයක් මෙහි ඇතුලත් කරන්න... (Enter a Sinhala sentence here...)"></textarea>
</div>
<button id="analyzeBtn" onclick="analyzeSentiment()">
<span>Analyze Sentiment</span>
<div class="loader" id="loader"></div>
</button>
<div id="errorMsg" class="error-message"></div>
<div class="result-container" id="resultContainer">
<div class="sentiment-header">
<span class="sentiment-label" id="sentimentLabel"></span>
<span id="sentimentEmoji" style="font-size: 1.5rem;"></span>
</div>
<div class="confidence-wrapper">
<div class="confidence-header">
<span>Confidence Score</span>
<span id="confidenceText">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progressFill"></div>
</div>
</div>
</div>
</div>
<script>
async function analyzeSentiment() {
const textInput = document.getElementById('textInput').value.trim();
const analyzeBtn = document.getElementById('analyzeBtn');
const loader = document.getElementById('loader');
const btnText = analyzeBtn.querySelector('span');
const resultContainer = document.getElementById('resultContainer');
const errorMsg = document.getElementById('errorMsg');
if (!textInput) {
errorMsg.textContent = "Please enter some text to analyze.";
errorMsg.style.display = 'block';
return;
}
// Reset UI
errorMsg.style.display = 'none';
resultContainer.classList.remove('show');
document.getElementById('progressFill').style.width = '0%';
// Loading state
btnText.style.display = 'none';
loader.style.display = 'block';
analyzeBtn.disabled = true;
try {
const response = await fetch('/predict', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: textInput })
});
if (!response.ok) {
throw new Error('Failed to analyze sentiment.');
}
const data = await response.json();
displayResult(data);
} catch (error) {
errorMsg.textContent = error.message || "An error occurred during analysis.";
errorMsg.style.display = 'block';
} finally {
// Remove loading state
btnText.style.display = 'block';
loader.style.display = 'none';
analyzeBtn.disabled = false;
}
}
function displayResult(data) {
const resultContainer = document.getElementById('resultContainer');
const sentimentLabel = document.getElementById('sentimentLabel');
const sentimentEmoji = document.getElementById('sentimentEmoji');
const confidenceText = document.getElementById('confidenceText');
const progressFill = document.getElementById('progressFill');
// Determine if positive or negative
// Assume LABEL_1 is positive and LABEL_0 is negative based on common conventions
const isPositive = data.label === 'LABEL_1' || data.label.toLowerCase() === 'positive';
const confidence = (data.score * 100).toFixed(1);
// Update DOM
sentimentLabel.textContent = isPositive ? 'Positive Sentiment' : 'Negative Sentiment';
sentimentLabel.className = `sentiment-label ${isPositive ? 'positive' : 'negative'}`;
sentimentEmoji.textContent = isPositive ? '✨' : '⚠️';
confidenceText.textContent = `${confidence}%`;
progressFill.className = `progress-fill ${isPositive ? 'positive' : 'negative'}`;
// Show container
resultContainer.classList.add('show');
// Animate progress bar slightly after showing container
setTimeout(() => {
progressFill.style.width = `${confidence}%`;
}, 50);
}
// Allow Enter key to submit (Shift+Enter for new line)
document.getElementById('textInput').addEventListener('keydown', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
analyzeSentiment();
}
});
</script>
</body>
</html>
|