Spaces:
Sleeping
Sleeping
File size: 28,372 Bytes
75db241 5916cba 41cdf64 75db241 5916cba 4848f58 41cdf64 4848f58 41cdf64 649c1a1 3c32de3 4848f58 41cdf64 5916cba 75db241 fc48926 649c1a1 3c32de3 649c1a1 fc48926 82b204e fc48926 e8fd6ae fc48926 d35b5ab fc48926 d5704f3 5916cba ef1d1b7 fc48926 5916cba 41cdf64 5916cba 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 5916cba d5704f3 5916cba 41cdf64 ef1d1b7 41cdf64 5916cba 4ff23b9 41cdf64 ef1d1b7 41cdf64 4ff23b9 5916cba 41cdf64 ef1d1b7 41cdf64 4ff23b9 41cdf64 fc48926 ef1d1b7 41cdf64 fc48926 41cdf64 5916cba d5704f3 5916cba 41cdf64 4ff23b9 41cdf64 4ff23b9 5916cba fc48926 d5704f3 5916cba d5704f3 5916cba d5704f3 5916cba 41cdf64 fc48926 41cdf64 5916cba 41cdf64 4ff23b9 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 fc48926 ef1d1b7 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 4ff23b9 5916cba 4ff23b9 d5704f3 41cdf64 d5704f3 5916cba d5704f3 4d9b7d8 d5704f3 e8fd6ae 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 ef1d1b7 41cdf64 d5704f3 41cdf64 5916cba 41cdf64 5916cba 41cdf64 0d08088 ef1d1b7 5916cba 28e39dc 41cdf64 5916cba 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 5916cba 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 41cdf64 28e39dc 5916cba 28e39dc 7cc22de 28e39dc cadbbaa 28e39dc 7cc22de 28e39dc 7cc22de 28e39dc 5916cba 8f1a368 b4693b4 ccd76f1 b4693b4 d5704f3 ccd76f1 d5704f3 b4693b4 d5704f3 b4693b4 d5704f3 b4693b4 41cdf64 5916cba 41cdf64 5916cba ef1d1b7 5916cba 41cdf64 fd7ce67 |
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
import gradio as gr
from transformers import pipeline
import re
from functools import lru_cache
import logging
from typing import List, Dict, Tuple
import json
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Enhanced model list with descriptions
MODEL_LIST = [
("ZombitX64/MultiSent-E5-Pro", "🏆 MultiSent E5 Pro - แนะนำ (ความแม่นยำสูงสุด)"),
("ZombitX64/Thai-sentiment-e5", "🎯 Thai Sentiment E5 - เฉพาะภาษาไทย"),
("poom-sci/WangchanBERTa-finetuned-sentiment", "🔥 WangchanBERTa - โมเดลไทยยอดนิยม"),
("SandboxBhh/sentiment-thai-text-model", "✨ Sandbox Thai - เร็วและแม่นยำ"),
("ZombitX64/MultiSent-E5", "⚡ MultiSent E5 - รวดเร็ว"),
("Thaweewat/wangchanberta-hyperopt-sentiment-01", "🧠 WangchanBERTa Hyperopt"),
("cardiffnlp/twitter-xlm-roberta-base-sentiment", "🌐 XLM-RoBERTa - หลายภาษา"),
("phoner45/wangchan-sentiment-thai-text-model", "📱 Wangchan Mobile"),
("ZombitX64/Sentiment-01", "🔬 Sentiment v1"),
("ZombitX64/Sentiment-02", "🔬 Sentiment v2"),
("ZombitX64/Sentiment-03", "🔬 Sentiment v3"),
("ZombitX64/sentiment-103", "🔬 Sentiment 103"),
("ZombitX64/sentimentSumdata-v1", "🔬 sentimentSumdata-v1"),
("ZombitX64/wangchanberta-att-spm-uncased-sentiment", "wangchanberta-att-spm-uncased-sentiment"),
]
# Cache for model loading
@lru_cache(maxsize=3)
def get_nlp(model_name: str):
try:
return pipeline("sentiment-analysis", model=model_name)
except Exception as e:
logger.error(f"Error loading model {model_name}: {e}")
raise gr.Error(f"ไม่สามารถโหลดโมเดล {model_name} ได้: {str(e)}")
# Model-specific label mappings
MODEL_LABEL_MAPPINGS = {
# wangchanberta-att-spm-uncased-sentiment - 3 classes ( negative, neutral, positive)
"ZombitX64/wangchanberta-att-spm-uncased-sentiment": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# MultiSent E5 Pro - 4 classes (question, negative, neutral, positive)
"ZombitX64/MultiSent-E5-Pro": {
"LABEL_0": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
"LABEL_1": {"code": 1, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_2": {"code": 2, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_3": {"code": 3, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# Thai Sentiment E5 - 3 classes (negative, neutral, positive)
"ZombitX64/Thai-sentiment-e5": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# WangchanBERTa - usually neg/neu/pos
"poom-sci/WangchanBERTa-finetuned-sentiment": {
"neg": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"neu": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"pos": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# Sandbox Thai - 3 classes
"SandboxBhh/sentiment-thai-text-model": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# MultiSent E5 - 3 classes
"ZombitX64/MultiSent-E5": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# WangchanBERTa Hyperopt
"Thaweewat/wangchanberta-hyperopt-sentiment-01": {
"neg": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"neu": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"pos": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# Twitter XLM-RoBERTa - NEGATIVE/NEUTRAL/POSITIVE
"cardiffnlp/twitter-xlm-roberta-base-sentiment": {
"NEGATIVE": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"NEUTRAL": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"POSITIVE": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# Wangchan Mobile
"phoner45/wangchan-sentiment-thai-text-model": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
# ZombitX64 Sentiment models - 3 classes
"ZombitX64/Sentiment-01": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
"ZombitX64/Sentiment-02": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
"ZombitX64/Sentiment-03": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
"ZombitX64/sentiment-103": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
"ZombitX64/sentimentSumdata-v1": {
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
},
}
def get_label_info(label: str, model_name: str) -> Dict:
"""Get label information for specific model with fallback for unknown labels"""
model_mappings = MODEL_LABEL_MAPPINGS.get(model_name, {})
if label in model_mappings:
return model_mappings[label]
# Fallback for unknown labels
return {
"code": -1,
"name": label.lower(),
"emoji": "🔍",
"color": "#64748b",
"bg": "rgba(100, 116, 139, 0.2)",
"description": f"ไม่ทราบ ({label})"
}
def split_sentences(text: str) -> List[str]:
"""Enhanced sentence splitting with better Thai support"""
sentences = re.split(r'[.!?။\n]+', text)
sentences = [s.strip() for s in sentences if s.strip() and len(s.strip()) > 2]
return sentences
def create_confidence_bar(score: float) -> str:
"""Create a modern confidence visualization"""
percentage = int(score * 100)
return f"""
<div style="display: flex; align-items: center; gap: 10px; margin: 8px 0;">
<div style="flex: 1; height: 8px; background: #334155; border-radius: 4px; overflow: hidden;">
<div style="width: {percentage}%; height: 100%; background: linear-gradient(90deg, #60a5fa, #3b82f6); transition: all 0.3s ease;"></div>
</div>
<span style="font-weight: 600; color: #cbd5e1; min-width: 50px;">{percentage}%</span>
</div>
"""
def analyze_text(text: str, model_name: str) -> str:
"""Enhanced text analysis with modern HTML formatting"""
if not text or not text.strip():
return """
<div style="padding: 20px; background: rgba(248, 113, 113, 0.2); border-radius: 12px; border-left: 4px solid #f87171;">
<div style="color: #f87171; font-weight: 600; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 20px;">⚠️</span>
กรุณาใส่ข้อความที่ต้องการวิเคราะห์
</div>
</div>
"""
sentences = split_sentences(text)
if not sentences:
return """
<div style="padding: 20px; background: rgba(248, 113, 113, 0.2); border-radius: 12px; border-left: 4px solid #f87171;">
<div style="color: #f87171; font-weight: 600; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 20px;">⚠️</span>
ไม่พบประโยคที่สามารถวิเคราะห์ได้ กรุณาใส่ข้อความที่ยาวกว่านี้
</div>
</div>
"""
try:
nlp = get_nlp(model_name)
except Exception as e:
return f"""
<div style="padding: 20px; background: rgba(248, 113, 113, 0.2); border-radius: 12px; border-left: 4px solid #f87171;">
<div style="color: #f87171; font-weight: 600; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 20px;">❌</span>
เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}
</div>
</div>
"""
# Header
html_parts = [f"""
<div style="background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%); color: #f8fafc; padding: 24px; border-radius: 16px 16px 0 0; margin-bottom: 0;">
<h2 style="margin: 0; font-size: 24px; font-weight: 700; display: flex; align-items: center; gap: 12px;">
<span style="font-size: 28px;">🧠</span>
ผลการวิเคราะห์ความรู้สึก
</h2>
<p style="margin: 8px 0 0 0; opacity: 0.9; font-size: 14px;">โมเดล: {model_name.split('/')[-1]}</p>
</div>
"""]
sentiment_counts = {"positive": 0, "negative": 0, "neutral": 0, "question": 0, "other": 0}
total_confidence = 0
sentence_results = []
# Analyze each sentence
for i, sentence in enumerate(sentences, 1):
try:
result = nlp(sentence)[0]
label = result['label']
score = result['score']
label_info = get_label_info(label, model_name)
label_name = label_info["name"]
if label_name in sentiment_counts:
sentiment_counts[label_name] += 1
else:
sentiment_counts["other"] += 1
total_confidence += score
# Store result for display
sentence_results.append({
'sentence': sentence,
'label_info': label_info,
'score': score,
'index': i,
'original_label': label
})
except Exception as e:
logger.error(f"Error analyzing sentence {i}: {e}")
sentence_results.append({
'sentence': sentence,
'error': str(e),
'index': i
})
# Results container
html_parts.append("""
<div style="background: #0f172a; padding: 0; border-radius: 0 0 16px 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); overflow: hidden;">
""")
# Individual sentence results
for result in sentence_results:
if 'error' in result:
html_parts.append(f"""
<div style="padding: 20px; border-bottom: 1px solid #1e293b;">
<div style="color: #f87171; font-weight: 600; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 18px;">❌</span>
เกิดข้อผิดพลาดในการวิเคราะห์ประโยคที่ {result['index']}
</div>
<p style="color: #94a3b8; margin: 8px 0 0 0; font-size: 14px;">{result['error']}</p>
</div>
""")
else:
label_info = result['label_info']
confidence_bar = create_confidence_bar(result['score'])
html_parts.append(f"""
<div style="padding: 20px; border-bottom: 1px solid #1e293b; transition: all 0.2s ease;" onmouseover="this.style.background='#1e293b'" onmouseout="this.style.background='#0f172a'">
<div style="display: flex; align-items: flex-start; gap: 16px;">
<div style="background: {label_info['bg']}; padding: 12px; border-radius: 50%; min-width: 48px; height: 48px; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 20px;">{label_info['emoji']}</span>
</div>
<div style="flex: 1;">
<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 8px;">
<span style="background: {label_info['color']}; color: #f8fafc; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; text-transform: uppercase;">
{label_info['description']}
</span>
<span style="color: #94a3b8; font-size: 12px; background: #1e293b; padding: 2px 8px; border-radius: 12px;">
{result['original_label']}
</span>
<span style="color: #94a3b8; font-size: 14px;">ประโยคที่ {result['index']}</span>
</div>
<p style="color: #f8fafc; margin: 0 0 12px 0; font-size: 16px; line-height: 1.5;">
"{result['sentence'][:150]}{'...' if len(result['sentence']) > 150 else ''}"
</p>
<div style="color: #94a3b8; font-size: 14px; margin-bottom: 8px;">ความมั่นใจ:</div>
{confidence_bar}
</div>
</div>
</div>
""")
# Summary section
total_sentences = len(sentences)
avg_confidence = total_confidence / total_sentences if total_sentences > 0 else 0
# Create chart data for summary
chart_items = []
colors = {"positive": "#34d399", "negative": "#f87171", "neutral": "#facc15", "question": "#60a5fa", "other": "#64748b"}
emojis = {"positive": "😊", "negative": "😢", "neutral": "😐", "question": "🤔", "other": "🔍"}
for sentiment, count in sentiment_counts.items():
if count > 0:
percentage = (count / total_sentences) * 100
chart_items.append(f"""
<div style="display: flex; align-items: center; gap: 12px; padding: 12px; background: rgba(59, 130, 246, 0.1); border-radius: 8px;">
<span style="font-size: 24px;">{emojis.get(sentiment, '🔍')}</span>
<div style="flex: 1;">
<div style="font-weight: 600; color: #f8fafc; text-transform: capitalize;">{sentiment}</div>
<div style="color: #94a3b8; font-size: 14px;">{count} ประโยค ({percentage:.1f}%)</div>
</div>
<div style="width: 60px; height: 6px; background: #334155; border-radius: 3px; overflow: hidden;">
<div style="width: {percentage}%; height: 100%; background: {colors.get(sentiment, '#64748b')}; transition: all 0.3s ease;"></div>
</div>
</div>
""")
html_parts.append(f"""
<div style="padding: 24px; background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);">
<h3 style="color: #f8fafc; margin: 0 0 20px 0; font-size: 20px; font-weight: 700; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 24px;">📊</span>
สรุปผลการวิเคราะห์
</h3>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 20px;">
<div style="background: #1e293b; padding: 20px; border-radius: 12px; text-align: center; box-shadow: 0 2px 8px rgba(0,0,0,0.3);">
<div style="font-size: 32px; font-weight: 700; color: #60a5fa; margin-bottom: 4px;">{total_sentences}</div>
<div style="color: #94a3b8; font-size: 14px;">ประโยคทั้งหมด</div>
</div>
<div style="background: #1e293b; padding: 20px; border-radius: 12px; text-align: center; box-shadow: 0 2px 8px rgba(0,0,0,0.3);">
<div style="font-size: 32px; font-weight: 700; color: #34d399; margin-bottom: 4px;">{avg_confidence*100:.0f}%</div>
<div style="color: #94a3b8; font-size: 14px;">ความมั่นใจเฉลี่ย</div>
</div>
</div>
<div style="display: grid; gap: 8px;">
{"".join(chart_items)}
</div>
</div>
""")
html_parts.append("</div>")
return "".join(html_parts)
# Modern CSS with dark blue theme
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
* { font-family: 'Inter', 'Noto Sans Thai', sans-serif !important; }
body, .gradio-container {
background: linear-gradient(135deg, #181f2a 0%, #232e3c 100%) !important;
min-height: 100vh;
}
.main-uxui-card {
background: #232e3c !important;
border-radius: 20px;
box-shadow: 0 6px 32px rgba(0,0,0,0.22);
border: 1.5px solid #2d3a4d;
padding: 32px 28px 28px 28px;
margin: 0 0 32px 0;
color: #e3e8ef !important;
transition: box-shadow 0.2s;
}
.main-uxui-card:hover {
box-shadow: 0 12px 36px rgba(0,0,0,0.28);
}
.main-uxui-header {
text-align: center;
margin-bottom: 32px;
}
.main-uxui-header h1 {
font-size: 2.8em;
color: #e3e8ef;
font-weight: 800;
margin-bottom: 0.2em;
letter-spacing: 0.5px;
}
.main-uxui-header p {
color: #7da2e3;
font-size: 1.25em;
margin-top: 0;
margin-bottom: 0.5em;
}
.main-uxui-section-title {
font-size: 1.18em;
color: #7da2e3;
font-weight: 700;
margin-bottom: 12px;
letter-spacing: 0.2px;
display: flex;
align-items: center;
gap: 8px;
}
.main-uxui-btn {
font-size: 1.13em;
padding: 0.9em 2.7em;
border-radius: 13px;
font-weight: 600;
background: linear-gradient(90deg, #2563eb 0%, #1e293b 100%);
color: #f8fafc !important;
border: none;
box-shadow: 0 2px 8px #1e253355;
transition: all 0.2s;
}
.main-uxui-btn:hover {
filter: brightness(1.08);
box-shadow: 0 6px 18px #1e253377;
transform: translateY(-2px) scale(1.03);
}
.main-uxui-btn.secondary {
background: #232e3c;
color: #7da2e3 !important;
border: 1.5px solid #2d3a4d;
}
.main-uxui-input, .main-uxui-dropdown {
font-size: 1.13em;
border-radius: 10px;
border: 1.5px solid #2d3a4d;
background: #1e2533;
color: #e3e8ef;
padding: 14px;
margin-bottom: 10px;
}
.main-uxui-dropdown { min-width: 220px; }
.main-uxui-output {
background: #1e2533;
border-radius: 14px;
border: 1.5px solid #2d3a4d;
color: #e3e8ef;
padding: 22px 18px;
font-size: 1.08em;
min-height: 180px;
margin-bottom: 0;
}
.main-uxui-legend {
background: #232e3c;
border-radius: 16px;
border: 1.5px solid #2d3a4d;
color: #7da2e3;
padding: 24px 18px;
margin-top: 32px;
font-size: 1.05em;
}
.main-uxui-legend .legend-row {
display: flex;
gap: 24px;
flex-wrap: wrap;
margin-top: 12px;
}
.main-uxui-legend .legend-item {
flex: 1 1 180px;
background: #1e2533;
border-radius: 10px;
padding: 16px 10px;
margin-bottom: 10px;
text-align: center;
border: 1px solid #2d3a4d;
}
.main-uxui-legend .legend-item strong {
color: #e3e8ef;
font-size: 1.08em;
}
.main-uxui-legend .legend-item small {
color: #7da2e3;
}
@media (max-width: 900px) {
.main-uxui-card { padding: 16px 6px; }
.main-uxui-header h1 { font-size: 2em; }
.main-uxui-section-title { font-size: 1em; }
}
"""
# Gradio Blocks app definition
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Base()) as demo:
with gr.Column(elem_classes="main-uxui-card"):
with gr.Row():
gr.HTML("""
<div class='main-uxui-header'>
<h1>Sentiment Analysis</h1>
<p>วิเคราะห์ความรู้สึกภาษาไทย และอื่นๆ etc. รองรับหลายโมเดล</p>
</div>
""")
with gr.Row():
model_dropdown = gr.Dropdown(
choices=[(desc, name) for name, desc in MODEL_LIST],
value=MODEL_LIST[0][0],
label="เลือกโมเดล (Model)",
elem_classes="main-uxui-dropdown"
)
with gr.Row():
input_box = gr.Textbox(
lines=4,
placeholder="พิมพ์ข้อความภาษาไทยหรืออังกฤษ (รองรับหลายประโยค)",
label="ข้อความที่ต้องการวิเคราะห์",
elem_classes="main-uxui-input"
)
with gr.Row():
analyze_btn = gr.Button("วิเคราะห์", elem_classes="main-uxui-btn")
clear_btn = gr.Button("ล้างข้อมูล", elem_classes="main-uxui-btn secondary")
with gr.Row():
output_html = gr.HTML(label="ผลลัพธ์", elem_classes="main-uxui-output")
with gr.Row():
gr.Examples([
["วันนี้อากาศดีมากๆ รู้สึกสดชื่นและมีความสุขมาก!"],
["เศร้ามากเลยวันนี้ งานเยอะเกินไป"],
["อาหารอร่อยดี แต่บริการช้ามาก"],
["คุณคิดอย่างไรกับเศรษฐกิจไทย?"],
["I love this product! It's amazing."],
["이 제품에는 매우 나쁩니다."],
["इस उत्पाद के लिए बहुत बुरा.."],
["This is the worst experience I've ever had."]
],
inputs=input_box,
label="ตัวอย่างข้อความ",
)
with gr.Row():
gr.HTML("""
<div class='main-uxui-legend'>
<div class='main-uxui-section-title'>
<span>🗂️</span> คำอธิบายผลลัพธ์
</div>
<div class='legend-row'>
<div class='legend-item'><strong>😊 เชิงบวก</strong><br><small>Positive</small></div>
<div class='legend-item'><strong>😢 เชิงลบ</strong><br><small>Negative</small></div>
<div class='legend-item'><strong>😐 เป็นกลาง</strong><br><small>Neutral</small></div>
<div class='legend-item'><strong>🤔 คำถาม</strong><br><small>Question</small></div>
</div>
</div>
""")
def on_analyze(text, model):
return analyze_text(text, model)
analyze_btn.click(on_analyze, [input_box, model_dropdown], output_html)
input_box.submit(on_analyze, [input_box, model_dropdown], output_html)
model_dropdown.change(on_analyze, [input_box, model_dropdown], output_html)
clear_btn.click(lambda: (""), None, output_html)
# Launch configuration
if __name__ == "__main__":
demo.queue(
max_size=50,
default_concurrency_limit=10
).launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True,
show_api=False,
quiet=False,
favicon_path=None,
ssl_verify=False,
app_kwargs={
"docs_url": None,
"redoc_url": None,
}
) |