Spaces:
Sleeping
Sleeping
File size: 25,266 Bytes
939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 32aefdf 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 32aefdf 939a9f4 1489d3a 32aefdf 1489d3a 939a9f4 1489d3a 939a9f4 32aefdf af2f8e1 32aefdf 1489d3a 32aefdf 939a9f4 1489d3a 939a9f4 a7da76e 939a9f4 1489d3a 939a9f4 1489d3a 939a9f4 |
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 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document Intelligence RAG</title>
<style>
/* -----------------------------
Design tokens (neutral)
------------------------------*/
:root {
--surface: #ffffff;
--surface-subtle: #fafafa;
--bg-main: #f5f7fb;
--card-bg: #ffffff;
--accent: #2563eb;
/* calm blue */
--accent-soft: #eff6ff;
--text-main: #111827;
--text-muted: #6b7280;
--border-soft: #e5e7eb;
--success: #16a34a;
--error: #dc2626;
--info: #2563eb;
--radius-sm: 6px;
--radius-md: 10px;
--radius-lg: 14px;
}
/* -----------------------------
Reset
------------------------------*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* -----------------------------
Base
------------------------------*/
body {
font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, Helvetica, Arial, sans-serif;
background: var(--bg-main);
min-height: 100vh;
padding: 24px;
color: var(--text-main);
}
/* -----------------------------
Container
------------------------------*/
.container {
max-width: 1040px;
margin: 0 auto;
}
/* -----------------------------
Header
------------------------------*/
header {
text-align: center;
margin-bottom: 48px;
}
header h1 {
font-size: 2.2rem;
font-weight: 600;
letter-spacing: -0.02em;
margin-bottom: 8px;
}
header p {
font-size: 1rem;
color: var(--text-muted);
margin-bottom: 20px;
}
header nav {
display: flex;
justify-content: center;
gap: 12px;
flex-wrap: wrap;
}
.eval-button {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 18px;
background: var(--accent);
color: white;
text-decoration: none;
border-radius: var(--radius-md);
font-size: 0.9rem;
font-weight: 500;
transition: background 0.15s ease, transform 0.15s ease,
box-shadow 0.15s ease;
border: none;
cursor: pointer;
}
.eval-button:hover {
background: #1d4ed8;
transform: translateY(-1px);
box-shadow: 0 6px 16px rgba(37, 99, 235, 0.25);
}
.eval-button:active {
transform: translateY(0);
}
/* Dark mode for eval button */
[data-theme="dark"] .eval-button {
background: #60a5fa;
}
[data-theme="dark"] .eval-button:hover {
background: #3b82f6;
}
/* -----------------------------
Layout
------------------------------*/
.main-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 24px;
margin-bottom: 24px;
}
/* -----------------------------
Cards
------------------------------*/
.card {
background: var(--surface);
border-radius: var(--radius-lg);
padding: 28px;
border: 1px solid var(--border-soft);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.05);
}
.card h2 {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 18px;
}
/* -----------------------------
Upload area
------------------------------*/
.upload-area {
border: 2px dashed var(--border-soft);
border-radius: var(--radius-md);
padding: 40px 24px;
text-align: center;
cursor: pointer;
background: var(--surface-subtle);
transition: border-color 0.2s ease, background 0.2s ease;
}
.upload-area:hover {
border-color: var(--accent);
background: var(--accent-soft);
}
.upload-area.dragover {
border-color: var(--accent);
background: var(--accent-soft);
}
.upload-area p {
color: var(--text-muted);
margin-bottom: 14px;
}
.upload-area input {
display: none;
}
/* -----------------------------
Buttons
------------------------------*/
.btn {
background: var(--accent);
color: white;
border: none;
padding: 11px 20px;
border-radius: var(--radius-md);
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: background 0.15s ease, transform 0.15s ease,
box-shadow 0.15s ease;
}
.btn:hover {
background: #1d4ed8;
transform: translateY(-1px);
box-shadow: 0 6px 16px rgba(37, 99, 235, 0.25);
}
.btn:active {
transform: translateY(0);
}
.btn-secondary {
background: #f3f4f6;
color: #374151;
}
.btn-secondary:hover {
background: #e5e7eb;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
/* -----------------------------
Query input
------------------------------*/
.query-input {
display: flex;
gap: 12px;
margin-bottom: 20px;
}
.query-input input {
flex: 1;
padding: 12px 14px;
border: 1.5px solid var(--border-soft);
border-radius: var(--radius-md);
font-size: 0.9rem;
}
.query-input input:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
/* -----------------------------
Status messages
------------------------------*/
.status {
padding: 14px 16px;
border-radius: var(--radius-md);
margin-bottom: 16px;
font-size: 0.9rem;
}
.status.success {
background: #ecfdf3;
color: var(--success);
border-left: 4px solid var(--success);
}
.status.error {
background: #fef2f2;
color: var(--error);
border-left: 4px solid var(--error);
}
.status.loading {
background: #eff6ff;
color: var(--info);
border-left: 4px solid var(--info);
}
/* -----------------------------
Answer
------------------------------*/
.answer-box {
background: var(--card-bg);
border-left: 4px solid var(--accent);
padding: 18px;
border-radius: var(--radius-md);
margin-bottom: 20px;
}
.answer-box h3 {
font-size: 1rem;
margin-bottom: 8px;
color: var(--text-main);
}
.answer-box p {
color: var(--text-main);
line-height: 1.6;
}
[data-theme="dark"] .answer-box {
background: #020617;
/* slightly darker than cards */
}
/* -----------------------------
Sources
------------------------------*/
.sources {
background: var(--surface);
border-radius: var(--radius-md);
padding: 16px;
border: 1px solid var(--border-soft);
}
.sources h4 {
font-size: 0.85rem;
margin-bottom: 12px;
color: var(--text-muted);
}
.source-item {
background: var(--surface-subtle);
border-radius: var(--radius-sm);
padding: 12px;
margin-bottom: 10px;
border-left: 3px solid var(--accent);
}
.source-item .relevance {
font-size: 0.8rem;
font-weight: 600;
color: var(--accent);
margin-bottom: 6px;
}
.source-item .text {
font-size: 0.85rem;
color: var(--text-muted);
font-style: italic;
}
/* -----------------------------
Stats
------------------------------*/
.stats {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
margin-bottom: 20px;
}
.stat-box {
background: var(--surface-subtle);
padding: 14px;
border-radius: var(--radius-md);
text-align: center;
border: 1px solid var(--border-soft);
}
.stat-box .number {
font-size: 1.4rem;
font-weight: 600;
color: var(--accent);
}
.stat-box .label {
font-size: 0.75rem;
color: var(--text-muted);
margin-top: 6px;
}
/* -----------------------------
System status grid
------------------------------*/
.status-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 12px;
}
/* -----------------------------
Spinner
------------------------------*/
.loading-spinner {
display: inline-block;
width: 18px;
height: 18px;
border: 3px solid #e5e7eb;
border-top: 3px solid var(--accent);
border-radius: 50%;
animation: spin 1s linear infinite;
margin-right: 8px;
vertical-align: middle;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
/* -----------------------------
Utilities
------------------------------*/
.full-width {
grid-column: 1 / -1;
}
.upload-area,
.stat-box,
.sources,
.source-item,
.card {
border-color: var(--border-soft);
}
.hidden {
display: none;
}
/* -----------------------------
Responsive
------------------------------*/
@media (max-width: 768px) {
.main-grid {
grid-template-columns: 1fr;
}
header h1 {
font-size: 1.7rem;
}
header nav {
flex-direction: column;
}
.eval-button {
width: 100%;
justify-content: center;
}
.stats {
grid-template-columns: 1fr;
}
.status-grid {
grid-template-columns: repeat(2, 1fr);
}
}
[data-theme="dark"] {
--bg-main: #0f172a;
--card-bg: #020617;
--accent: #60a5fa;
--accent-soft: #0b1b33;
--text-main: #e5e7eb;
--text-muted: #9ca3af;
--border-soft: #1f2937;
--success: #22c55e;
--error: #f87171;
--info: #60a5fa;
--surface: #020617;
--surface-subtle: #020617;
}
/* smoother transitions */
body,
.card,
.upload-area,
.btn,
input,
.status,
.answer-box,
.sources,
.stat-box,
.eval-button {
transition: background-color 0.25s ease,
color 0.25s ease,
border-color 0.25s ease;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>π Document Intelligence RAG</h1>
<p>Ask questions about your research papers</p>
<nav>
<a href="/evaluation" class="eval-button">π Evaluation Dashboard</a>
<a href="/ragas-demo" class="eval-button">π¬ RAGAS Evaluation</a>
</nav>
<button id="themeToggle" aria-label="Toggle dark mode" style="
position: fixed;
top: 16px;
right: 20px;
background: none;
border: none;
padding: 6px;
font-size: 0.9rem;
cursor: pointer;
color: #6b7280;
">
π Dark
</button>
</header>
<div class="main-grid">
<!-- Upload Section -->
<div class="card">
<h2>π€ Upload Documents</h2>
<div class="upload-area" id="uploadArea">
<p>π Drag & drop PDFs here or click to browse</p>
<input type="file" id="fileInput" multiple accept=".pdf">
<button class="btn" onclick="document.getElementById('fileInput').click()">
Choose Files
</button>
</div>
<div id="uploadStatus" class="status hidden"></div>
<div id="stats" class="stats">
<div class="stat-box">
<div class="number" id="totalChunks">0</div>
<div class="label">Total Chunks</div>
</div>
<div class="stat-box">
<div class="number" id="docCount">0</div>
<div class="label">Documents</div>
</div>
</div>
<button class="btn btn-secondary" onclick="loadStats()">
π Refresh Stats
</button>
<button class="btn btn-secondary" style="background: #ff6b6b; color: white; margin-top: 10px;"
onclick="resetSystem()">
ποΈ Delete All Documents
</button>
<p style="font-size: 0.85em; color: #999; margin-top: 10px;">
πΎ Documents are stored persistently. They remain after restart.
</p>
</div>
<!-- Query Section -->
<div class="card">
<h2>β Ask Questions</h2>
<div class="query-input">
<input type="text" id="queryInput" placeholder="Ask about your documents?"
onkeypress="if(event.key==='Enter') submitQuery()">
<button class="btn" onclick="submitQuery()">Search</button>
</div>
<div id="queryStatus" class="status hidden"></div>
<div id="answerContainer" class="hidden">
<div class="answer-box">
<h3>Answer</h3>
<p id="answerText"></p>
</div>
<div class="sources" id="sourcesBox">
<h4>π Sources Used</h4>
<div id="sourcesList"></div>
</div>
</div>
</div>
</div>
<!-- Status Indicators -->
<div class="card full-width">
<h2>π§ System Status</h2>
<div id="healthStatus" class="status-grid">Loading...</div>
</div>
</div>
<script>
const API_URL = window.location.origin; // 'http://localhost:8000';
// Upload handlers
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
// uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
handleFiles(e.dataTransfer.files);
});
fileInput.addEventListener('change', (e) => {
handleFiles(e.target.files);
});
async function handleFiles(files) {
const statusDiv = document.getElementById('uploadStatus');
for (const file of files) {
if (!file.name.endsWith('.pdf')) {
showStatus(statusDiv, `Skipping ${file.name} - only PDFs supported`, 'error');
continue;
}
showStatus(statusDiv, `Uploading ${file.name}...`, 'loading');
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch(`${API_URL}/ingest`, {
method: 'POST',
body: formData
});
if (response.ok) {
const data = await response.json();
showStatus(
statusDiv,
`β ${file.name}: ${data.chunks_embedded} chunks ingested`,
'success'
);
loadStats();
} else {
const error = await response.json();
showStatus(statusDiv, `β ${file.name}: ${error.detail}`, 'error');
}
} catch (error) {
showStatus(statusDiv, `β Upload failed: ${error.message}`, 'error');
}
}
fileInput.value = '';
}
async function submitQuery() {
const query = document.getElementById('queryInput').value.trim();
if (!query) {
showStatus(
document.getElementById('queryStatus'),
'Please enter a question',
'error'
);
return;
}
const statusDiv = document.getElementById('queryStatus');
showStatus(statusDiv, 'Searching your documents...', 'loading');
try {
const response = await fetch(`${API_URL}/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, top_k: 3 })
});
if (response.ok) {
const data = await response.json();
displayAnswer(data);
statusDiv.classList.add('hidden');
} else {
const error = await response.json();
showStatus(statusDiv, error.error || 'Query failed', 'error');
}
} catch (error) {
showStatus(statusDiv, `Error: ${error.message}`, 'error');
}
}
function displayAnswer(data) {
document.getElementById('answerText').textContent = data.answer;
const sourcesList = document.getElementById('sourcesList');
sourcesList.innerHTML = data.sources.map(source => `
<div class="source-item">
<div class="relevance">π Relevance: ${(source.similarity * 100).toFixed(0)}%</div>
<div class="text">${source.preview}</div>
</div>
`).join('');
document.getElementById('answerContainer').classList.remove('hidden');
}
async function loadStats() {
try {
const response = await fetch(`${API_URL}/stats`);
if (response.ok) {
const data = await response.json();
document.getElementById('totalChunks').textContent = data.total_chunks;
}
} catch (error) {
console.error('Failed to load stats:', error);
}
}
async function loadHealth() {
try {
const response = await fetch(`${API_URL}/health`);
if (response.ok) {
const data = await response.json();
// Get embedding backend name
let embeddingName = data.embedding_backend || 'Unknown';
// Format nicely
if (embeddingName === 'sentence-transformers') {
embeddingName = 'Sentence-Transformers';
} else if (embeddingName === 'ollama') {
embeddingName = 'Ollama';
}
const healthHtml = `
<div class="stat-box">
<div class="number">${data.embedding_backend ? 'β' : 'β'}</div>
<div class="label">${embeddingName} (Embeddings)</div>
</div>
<div class="stat-box">
<div class="number">${data.groq === 'β' ? 'β' : 'β'}</div>
<div class="label">Groq (LLM)</div>
</div>
<div class="stat-box">
<div class="number">${data.chroma.status === 'β' ? 'β' : 'β'}</div>
<div class="label">Chroma (Vector DB)</div>
</div>
<div class="stat-box">
<div class="number">${data.status === 'healthy' ? 'β' : 'β '}</div>
<div class="label">Overall Status</div>
</div>
`;
document.getElementById('healthStatus').innerHTML = healthHtml;
}
} catch (error) {
document.getElementById('healthStatus').innerHTML =
`<div style="grid-column: 1/-1; padding: 15px; background: #f8d7da; color: #721c24; border-radius: 8px;">Cannot connect to API at ${API_URL}</div>`;
}
}
async function resetSystem() {
if (!confirm('β οΈ Delete ALL documents and embeddings? This cannot be undone!')) {
return;
}
const statusDiv = document.getElementById('uploadStatus');
showStatus(statusDiv, 'Resetting system...', 'loading');
try {
const response = await fetch(`${API_URL}/reset`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
showStatus(statusDiv, 'β All documents deleted!', 'success');
loadStats();
} else {
const error = await response.json();
showStatus(statusDiv, `Reset failed: ${error.detail || 'Unknown error'}`, 'error');
}
} catch (error) {
showStatus(statusDiv, `Error: ${error.message}`, 'error');
}
}
function showStatus(element, message, type) {
element.textContent = message;
element.className = `status ${type}`;
element.classList.remove('hidden');
}
// Load stats and health on page load
window.addEventListener('load', () => {
loadStats();
loadHealth();
setInterval(loadHealth, 30000); // Refresh every 30s
});
// -----------------------------
// Dark mode toggle
// -----------------------------
const themeToggle = document.getElementById("themeToggle");
const root = document.documentElement;
// Load saved theme or system preference
const savedTheme = localStorage.getItem("theme");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (savedTheme) {
root.setAttribute("data-theme", savedTheme);
} else if (prefersDark) {
root.setAttribute("data-theme", "dark");
}
// Update button text
function updateToggleText() {
const isDark = root.getAttribute("data-theme") === "dark";
themeToggle.textContent = isDark ? "βοΈ Light mode" : "π Dark mode";
}
updateToggleText();
// Toggle on click
themeToggle.addEventListener("click", () => {
const isDark = root.getAttribute("data-theme") === "dark";
const newTheme = isDark ? "light" : "dark";
root.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
updateToggleText();
});
</script>
</body>
</html> |