File size: 13,661 Bytes
b80cddf b62b57f b80cddf a86d063 b80cddf | 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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | """
UI Components and styling for Gradio interface
"""
# Custom CSS for premium design
CUSTOM_CSS = """
/* Main theme */
:root {
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--success-gradient: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
--card-bg: rgba(255, 255, 255, 0.05);
--glass-bg: rgba(255, 255, 255, 0.1);
}
/* Header styling */
.header-container {
background: var(--primary-gradient);
padding: 2rem;
border-radius: 12px;
margin-bottom: 1.5rem;
box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
}
.header-title {
color: white;
font-size: 2.5rem;
font-weight: 700;
text-align: center;
margin-bottom: 0.5rem;
}
.header-subtitle {
color: rgba(255, 255, 255, 0.9);
text-align: center;
font-size: 1.1rem;
}
/* Tab styling */
.tab-nav button {
font-size: 1rem;
font-weight: 600;
padding: 0.75rem 1.5rem;
border-radius: 8px;
transition: all 0.3s ease;
}
.tab-nav button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
/* Card styling */
.info-card {
background: var(--card-bg);
backdrop-filter: blur(10px);
border-radius: 12px;
padding: 1.5rem;
margin: 1rem 0;
border: 1px solid rgba(255, 255, 255, 0.1);
}
/* Upload area */
.upload-area {
border: 2px dashed rgba(102, 126, 234, 0.5);
border-radius: 12px;
padding: 2rem;
text-align: center;
transition: all 0.3s ease;
}
.upload-area:hover {
border-color: #667eea;
background: rgba(102, 126, 234, 0.05);
}
/* Chat messages */
.message-bubble {
border-radius: 18px;
padding: 0.75rem 1rem;
margin: 0.5rem 0;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Source citations */
.source-citation {
background: var(--glass-bg);
border-left: 3px solid #667eea;
padding: 0.75rem;
margin: 0.5rem 0;
border-radius: 6px;
font-size: 0.9rem;
}
/* Buttons */
.primary-button {
background: var(--primary-gradient) !important;
color: white !important;
border: none !important;
padding: 0.75rem 2rem !important;
border-radius: 8px !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
}
.primary-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}
/* Status indicators */
.status-success {
color: #38ef7d;
font-weight: 600;
}
.status-error {
color: #ff6b6b;
font-weight: 600;
}
/* Loading animation */
.loading {
display: inline-block;
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* Document cards */
.doc-card {
background: var(--glass-bg);
border-radius: 12px;
padding: 1rem;
margin: 0.75rem 0;
border: 1px solid rgba(255, 255, 255, 0.1);
transition: all 0.3s ease;
}
.doc-card:hover {
transform: translateX(5px);
border-color: #667eea;
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}
/* Microphone button and recording styles */
.mic-button {
background: var(--primary-gradient) !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
padding: 0.75rem 1.5rem !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
position: relative !important;
}
.mic-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}
.mic-recording {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%) !important;
animation: pulse-recording 1.5s ease-in-out infinite !important;
}
@keyframes pulse-recording {
0%, 100% {
opacity: 1;
box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.7);
}
50% {
opacity: 0.9;
box-shadow: 0 0 0 10px rgba(255, 107, 107, 0);
}
}
.recording-indicator {
display: inline-block;
width: 8px;
height: 8px;
background: #ff6b6b;
border-radius: 50%;
margin-right: 0.5rem;
animation: blink 1s ease-in-out infinite;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.audio-status {
background: rgba(102, 126, 234, 0.1);
border-left: 3px solid #667eea;
padding: 0.75rem 1rem;
border-radius: 6px;
margin: 0.5rem 0;
font-size: 0.9rem;
}
.audio-status.processing {
border-left-color: #ffd93d;
background: rgba(255, 217, 61, 0.1);
}
.audio-status.error {
border-left-color: #ff6b6b;
background: rgba(255, 107, 107, 0.1);
}
.audio-status.success {
border-left-color: #38ef7d;
background: rgba(56, 239, 125, 0.1);
}
/* Responsive */
@media (max-width: 768px) {
.header-title {
font-size: 2rem;
}
.tab-nav button {
font-size: 0.9rem;
padding: 0.5rem 1rem;
}
}
/* ========== Document Viewer Styles ========== */
/* Document viewer container */
.document-viewer-wrapper {
height: 600px;
display: flex;
flex-direction: column;
background: var(--card-bg);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
overflow: hidden;
}
.viewer-controls {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background: rgba(102, 126, 234, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.viewer-title {
display: flex;
align-items: center;
gap: 0.5rem;
font-weight: 600;
color: #667eea;
}
.doc-icon {
font-size: 1.2rem;
}
.doc-name {
font-size: 0.95rem;
}
.viewer-actions {
display: flex;
gap: 0.5rem;
}
.viewer-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
padding: 0.4rem 0.8rem;
cursor: pointer;
transition: all 0.2s ease;
font-size: 0.9rem;
}
.viewer-btn:hover {
background: rgba(102, 126, 234, 0.3);
border-color: #667eea;
transform: translateY(-1px);
}
/* Document content area */
.document-viewer-content {
flex: 1;
overflow-y: auto;
padding: 1.5rem;
font-size: 1em;
line-height: 1.6;
}
.document-content {
max-width: 800px;
margin: 0 auto;
}
.document-header {
text-align: center;
margin-bottom: 2rem;
padding-bottom: 1rem;
border-bottom: 2px solid rgba(102, 126, 234, 0.3);
}
.document-header h3 {
color: #667eea;
margin: 0 0 0.5rem 0;
font-size: 1.5rem;
}
.doc-meta {
color: rgba(255, 255, 255, 0.6);
font-size: 0.9rem;
margin: 0;
}
/* PDF pages */
.pdf-page {
margin-bottom: 2rem;
padding: 1.5rem;
background: rgba(255, 255, 255, 0.02);
border-radius: 8px;
border: 1px solid rgba(255, 255, 255, 0.05);
}
.page-number {
font-size: 0.85rem;
color: rgba(255, 255, 255, 0.5);
font-weight: 600;
margin-bottom: 1rem;
padding-bottom: 0.5rem;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
text-align: center;
}
/* Paragraphs */
.paragraph {
margin-bottom: 1rem;
padding: 0.75rem;
border-radius: 6px;
transition: all 0.3s ease;
cursor: default;
}
.paragraph:hover {
background: rgba(255, 255, 255, 0.03);
}
/* Highlighted citations */
.highlighted-citation {
background: rgba(102, 126, 234, 0.2) !important;
border-left: 4px solid #667eea !important;
padding-left: 1rem !important;
box-shadow: 0 2px 12px rgba(102, 126, 234, 0.3);
}
@keyframes highlight-flash {
0%, 100% {
background: rgba(102, 126, 234, 0.2);
}
50% {
background: rgba(102, 126, 234, 0.4);
}
}
/* Empty state */
.document-viewer-empty {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 2rem;
color: rgba(255, 255, 255, 0.6);
}
.empty-state-icon {
font-size: 4rem;
margin-bottom: 1rem;
opacity: 0.5;
}
.document-viewer-empty h3 {
color: rgba(255, 255, 255, 0.7);
margin: 0 0 0.5rem 0;
}
.document-viewer-empty p {
color: rgba(255, 255, 255, 0.5);
margin: 0;
}
/* ========== Citation Styles ========== */
.sources-container {
margin-top: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.02);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.sources-title {
color: #667eea;
margin: 0 0 1rem 0;
font-size: 1.1rem;
font-weight: 600;
}
.citation-card {
background: var(--glass-bg);
border-left: 3px solid #667eea;
padding: 0.75rem 1rem;
margin: 0.75rem 0;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
animation: slideIn 0.3s ease;
}
.citation-card:hover {
background: rgba(102, 126, 234, 0.15);
border-left-color: #764ba2;
transform: translateX(5px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}
.citation-header {
color: #667eea;
font-size: 0.95rem;
margin-bottom: 0.5rem;
}
.citation-snippet {
color: rgba(255, 255, 255, 0.7);
font-size: 0.85rem;
font-style: italic;
line-height: 1.4;
}
/* Document selector */
.document-selector {
margin-bottom: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
}
.document-selector label {
display: block;
color: rgba(255, 255, 255, 0.8);
font-weight: 600;
margin-bottom: 0.5rem;
font-size: 0.9rem;
}
.doc-select-dropdown {
width: 100%;
padding: 0.75rem;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
color: white;
font-size: 0.95rem;
cursor: pointer;
transition: all 0.3s ease;
}
.doc-select-dropdown:hover {
background: rgba(255, 255, 255, 0.15);
border-color: #667eea;
}
.doc-select-dropdown:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}
.doc-select-dropdown option {
background: #1a1a2e;
color: white;
}
.no-docs-message {
color: rgba(255, 255, 255, 0.5);
text-align: center;
padding: 1rem;
font-style: italic;
}
/* Scrollbar styling for document viewer */
.document-viewer-content::-webkit-scrollbar {
width: 8px;
}
.document-viewer-content::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
border-radius: 4px;
}
.document-viewer-content::-webkit-scrollbar-thumb {
background: rgba(102, 126, 234, 0.5);
border-radius: 4px;
}
.document-viewer-content::-webkit-scrollbar-thumb:hover {
background: rgba(102, 126, 234, 0.7);
}
"""
def format_sources(sources: list) -> str:
"""
Format source citations for display
Args:
sources: List of source metadata
Returns:
Formatted HTML string
"""
if not sources:
return ""
html = "<div style='margin-top: 1rem; padding-top: 1rem; border-top: 1px solid rgba(255,255,255,0.1);'>"
for i, source in enumerate(sources, 1):
filename = source.get('filename', 'Unknown')
chunk_idx = source.get('chunk_index', 0)
preview = source.get('chunk_text', '')[:150]
html += f"""
<div class='source-citation'>
<strong>#{i} {filename}</strong> (Chunk {chunk_idx})
<br><span style='color: rgba(255,255,255,0.7); font-size: 0.85rem;'>{preview}...</span>
</div>
"""
html += "</div>"
return html
def format_file_size(size_bytes: int) -> str:
"""Format file size in human-readable format"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024.0:
return f"{size_bytes:.1f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.1f} TB"
def create_document_card(doc_info: dict) -> str:
"""
Create HTML card for document display
Args:
doc_info: Document information dictionary
Returns:
HTML string
"""
filename = doc_info.get('filename', 'Unknown')
num_chunks = doc_info.get('num_chunks', 0)
html = f"""
<div class='doc-card'>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<div>
<h4 style='margin: 0; color: #667eea;'>📄 {filename}</h4>
<p style='margin: 0.25rem 0 0 0; color: rgba(255,255,255,0.7); font-size: 0.9rem;'>
{num_chunks} chunks
</p>
</div>
</div>
</div>
"""
return html
def create_status_message(message: str, status_type: str = "info") -> str:
"""
Create styled status message
Args:
message: Status message text
status_type: Type of status (success, error, info, warning)
Returns:
HTML string
"""
icons = {
"success": "✓",
"error": "✗",
"info": "ℹ",
"warning": "⚠"
}
colors = {
"success": "#38ef7d",
"error": "#ff6b6b",
"info": "#667eea",
"warning": "#ffd93d"
}
icon = icons.get(status_type, "ℹ")
color = colors.get(status_type, "#667eea")
html = f"""
<div style='padding: 1rem; border-radius: 8px; background: rgba(255,255,255,0.05);
border-left: 4px solid {color}; margin: 1rem 0;'>
<span style='color: {color}; font-weight: 600; font-size: 1.1rem;'>{icon} {message}</span>
</div>
"""
return html
|