Spaces:
Runtime error
Runtime error
Ajey95
commited on
Commit
Β·
5614a15
1
Parent(s):
7e52c8f
Fix: tools addition
Browse files- templates/index.html +236 -233
templates/index.html
CHANGED
|
@@ -364,13 +364,15 @@
|
|
| 364 |
<em>ΰ€ΰ€ͺΰ€ΰ€Ύ ΰ€
ΰ€§ΰ₯ΰ€―ΰ€―ΰ€¨ ΰ€Έΰ€Ύΰ€₯ΰ₯ (Your Study Companion)</em> β¨
|
| 365 |
</div>
|
| 366 |
</div>
|
| 367 |
-
</div>
|
|
|
|
| 368 |
<div class="quick-actions">
|
| 369 |
<button class="quick-btn" onclick="populateInput('Explain ')">π Explain Topic</button>
|
| 370 |
<button class="quick-btn" onclick="populateInput('What are the side effects of ')">βοΈ Drug Info</button>
|
| 371 |
<button class="quick-btn" onclick="populateInput('Make a quiz on ')">β Create Quiz</button>
|
| 372 |
<button class="quick-btn" onclick="populateInput('Mnemonic for ')">π§ Mnemonics</button>
|
| 373 |
</div>
|
|
|
|
| 374 |
|
| 375 |
|
| 376 |
<div class="input-container">
|
|
@@ -387,279 +389,82 @@
|
|
| 387 |
</div>
|
| 388 |
</div>
|
| 389 |
</div>
|
|
|
|
|
|
|
| 390 |
|
| 391 |
-
<script>
|
| 392 |
-
|
| 393 |
-
let isProcessing = false;
|
| 394 |
-
|
| 395 |
-
async function sendMessage() {
|
| 396 |
-
const input = document.getElementById('messageInput');
|
| 397 |
-
const message = input.value.trim();
|
| 398 |
-
if (!message || isProcessing) return;
|
| 399 |
-
|
| 400 |
-
addMessage(message, 'user');
|
| 401 |
-
input.value = '';
|
| 402 |
-
showLoading(true);
|
| 403 |
-
|
| 404 |
-
try {
|
| 405 |
-
const response = await fetch('/chat', {
|
| 406 |
-
method: 'POST',
|
| 407 |
-
headers: { 'Content-Type': 'application/json' },
|
| 408 |
-
body: JSON.stringify({ query: message })
|
| 409 |
-
});
|
| 410 |
-
const data = await response.json();
|
| 411 |
-
showLoading(false);
|
| 412 |
-
if (data.success) {
|
| 413 |
-
addMessage(data.message, 'bot', data.agent_used);
|
| 414 |
-
} else {
|
| 415 |
-
addMessage(`β Error: ${data.error || 'Something went wrong'}`, 'bot', 'error');
|
| 416 |
-
}
|
| 417 |
-
} catch (error) {
|
| 418 |
-
showLoading(false);
|
| 419 |
-
addMessage(`β Connection error: ${error.message}`, 'bot', 'error');
|
| 420 |
-
}
|
| 421 |
-
}
|
| 422 |
-
|
| 423 |
-
function addMessage(text, sender, agentType = '') {
|
| 424 |
-
const messagesContainer = document.getElementById('chatMessages');
|
| 425 |
-
const messageDiv = document.createElement('div');
|
| 426 |
-
messageDiv.className = `message ${sender}`;
|
| 427 |
-
const agentIcons = { 'academic': 'π Academic Agent', 'drug_info': 'π Drug Info Agent', 'quiz_generation': 'β Quiz Master' };
|
| 428 |
-
const agentBadge = sender === 'bot' ? `<div class="agent-badge">${agentIcons[agentType] || 'π€ AI Assistant'}</div>` : '';
|
| 429 |
-
const formattedText = marked.parse(text || 'Sorry, I received an empty response.');
|
| 430 |
-
messageDiv.innerHTML = `<div class="message-bubble">${agentBadge}${formattedText}</div>`;
|
| 431 |
-
messagesContainer.appendChild(messageDiv);
|
| 432 |
-
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 433 |
-
}
|
| 434 |
-
|
| 435 |
-
function showLoading(show) {
|
| 436 |
-
document.getElementById('sendBtn').disabled = show;
|
| 437 |
-
const existingLoading = document.getElementById('loading-indicator');
|
| 438 |
-
if (existingLoading) existingLoading.remove();
|
| 439 |
-
|
| 440 |
-
if (show) {
|
| 441 |
-
const messagesContainer = document.getElementById('chatMessages');
|
| 442 |
-
const loadingDiv = document.createElement('div');
|
| 443 |
-
loadingDiv.className = 'message bot';
|
| 444 |
-
loadingDiv.id = 'loading-indicator';
|
| 445 |
-
loadingDiv.innerHTML = `<div class="message-bubble"><div class="typing-indicator"><span></span><span></span><span></span></div></div>`;
|
| 446 |
-
messagesContainer.appendChild(loadingDiv);
|
| 447 |
-
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 448 |
-
}
|
| 449 |
-
}
|
| 450 |
-
|
| 451 |
-
function handleKeyPress(event) {
|
| 452 |
-
if (event.key === 'Enter' && !event.shiftKey) {
|
| 453 |
-
event.preventDefault();
|
| 454 |
-
sendMessage();
|
| 455 |
-
}
|
| 456 |
-
}
|
| 457 |
-
|
| 458 |
-
function populateInput(templateText) {
|
| 459 |
-
const input = document.getElementById('messageInput');
|
| 460 |
-
input.value = templateText;
|
| 461 |
-
input.focus();
|
| 462 |
-
}
|
| 463 |
-
|
| 464 |
-
document.addEventListener('DOMContentLoaded', () => {
|
| 465 |
-
document.getElementById('messageInput').focus();
|
| 466 |
-
});
|
| 467 |
-
</script>
|
| 468 |
-
// Global variables
|
| 469 |
-
{% comment %} let isProcessing = false;
|
| 470 |
-
|
| 471 |
-
// In templates/index.html, inside the <script> tag
|
| 472 |
-
|
| 473 |
-
// --- File Management Functions ---
|
| 474 |
-
|
| 475 |
-
async function uploadFile() {
|
| 476 |
-
const fileInput = document.getElementById('fileInput');
|
| 477 |
-
const file = fileInput.files[0];
|
| 478 |
-
if (!file) {
|
| 479 |
-
addMessage('Please select a file to upload first.', 'bot', 'error');
|
| 480 |
-
return;
|
| 481 |
-
}
|
| 482 |
-
|
| 483 |
-
const formData = new FormData();
|
| 484 |
-
formData.append('file', file);
|
| 485 |
-
|
| 486 |
-
showLoading(true);
|
| 487 |
-
isProcessing = true;
|
| 488 |
-
|
| 489 |
-
try {
|
| 490 |
-
const response = await fetch('/upload', {
|
| 491 |
-
method: 'POST',
|
| 492 |
-
body: formData,
|
| 493 |
-
});
|
| 494 |
-
const data = await response.json();
|
| 495 |
-
if (data.success) {
|
| 496 |
-
addMessage(`β
${data.message}`, 'bot', 'academic');
|
| 497 |
-
updateFileList(data.files);
|
| 498 |
-
} else {
|
| 499 |
-
addMessage(`β Upload Error: ${data.error}`, 'bot', 'error');
|
| 500 |
-
}
|
| 501 |
-
} catch (error) {
|
| 502 |
-
addMessage(`β Connection Error: ${error.message}`, 'bot', 'error');
|
| 503 |
-
} finally {
|
| 504 |
-
showLoading(false);
|
| 505 |
-
isProcessing = false;
|
| 506 |
-
fileInput.value = ''; // Clear the file input
|
| 507 |
-
}
|
| 508 |
-
}
|
| 509 |
-
|
| 510 |
-
function updateFileList(files = []) {
|
| 511 |
-
const fileListDiv = document.getElementById('fileList');
|
| 512 |
-
if (files.length > 0) {
|
| 513 |
-
let fileLinks = files.map(f => `<span>${f.original_name}</span>`).join(', ');
|
| 514 |
-
fileListDiv.innerHTML = `<div style="font-size: 0.9em; margin-top: 10px;"><strong>Active Files:</strong> ${fileLinks} <button onclick="clearFiles()" style="margin-left: 10px; background: #800000; color: white; border: none; border-radius: 5px; cursor: pointer; padding: 2px 8px;">Clear All</button></div>`;
|
| 515 |
-
} else {
|
| 516 |
-
fileListDiv.innerHTML = '';
|
| 517 |
-
}
|
| 518 |
-
}
|
| 519 |
-
|
| 520 |
-
async function clearFiles() {
|
| 521 |
-
showLoading(true);
|
| 522 |
-
isProcessing = true;
|
| 523 |
-
try {
|
| 524 |
-
const response = await fetch('/clear_files', { method: 'POST' });
|
| 525 |
-
const data = await response.json();
|
| 526 |
-
if (data.success) {
|
| 527 |
-
addMessage('π§Ή All uploaded files and sessions have been cleared.', 'bot', 'academic');
|
| 528 |
-
updateFileList([]);
|
| 529 |
-
} else {
|
| 530 |
-
addMessage(`β Error: ${data.error}`, 'bot', 'error');
|
| 531 |
-
}
|
| 532 |
-
} catch (error) {
|
| 533 |
-
addMessage(`β Connection Error: ${error.message}`, 'bot', 'error');
|
| 534 |
-
} finally {
|
| 535 |
-
showLoading(false);
|
| 536 |
-
isProcessing = false;
|
| 537 |
-
}
|
| 538 |
-
}
|
| 539 |
-
|
| 540 |
-
// Update the file list on page load
|
| 541 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 542 |
-
fetch('/files').then(res => res.json()).then(data => {
|
| 543 |
-
updateFileList(data.files);
|
| 544 |
-
});
|
| 545 |
-
});
|
| 546 |
-
// Send message function
|
| 547 |
async function sendMessage() {
|
| 548 |
const input = document.getElementById('messageInput');
|
| 549 |
const message = input.value.trim();
|
| 550 |
-
|
| 551 |
if (!message || isProcessing) return;
|
| 552 |
-
|
| 553 |
-
// Add user message to chat
|
| 554 |
addMessage(message, 'user');
|
| 555 |
input.value = '';
|
| 556 |
-
|
| 557 |
-
// Show loading
|
| 558 |
showLoading(true);
|
| 559 |
-
|
| 560 |
-
|
| 561 |
try {
|
| 562 |
const response = await fetch('/chat', {
|
| 563 |
method: 'POST',
|
| 564 |
-
headers: {
|
| 565 |
-
'Content-Type': 'application/json',
|
| 566 |
-
},
|
| 567 |
body: JSON.stringify({ query: message })
|
| 568 |
});
|
| 569 |
-
|
| 570 |
const data = await response.json();
|
| 571 |
-
|
| 572 |
if (data.success) {
|
| 573 |
addMessage(data.message, 'bot', data.agent_used);
|
| 574 |
} else {
|
| 575 |
addMessage(`β Error: ${data.error || 'Something went wrong'}`, 'bot', 'error');
|
| 576 |
}
|
| 577 |
-
|
| 578 |
} catch (error) {
|
| 579 |
-
addMessage(`β Connection error: ${error.message}`, 'bot', 'error');
|
| 580 |
-
} finally {
|
| 581 |
showLoading(false);
|
| 582 |
-
|
| 583 |
}
|
| 584 |
}
|
| 585 |
|
| 586 |
-
// Add message to chat
|
| 587 |
function addMessage(text, sender, agentType = '') {
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
'quiz_generation': 'β Quiz Master',
|
| 598 |
-
'mnemonic_creation': 'π§ Memory Master',
|
| 599 |
-
'viva_practice': 'π£οΈ Viva Coach',
|
| 600 |
-
'error': 'β οΈ System'
|
| 601 |
-
};
|
| 602 |
-
agentBadge = `<div class="agent-badge">${agentIcons[agentType] || 'π€ AI Assistant'}</div>`;
|
| 603 |
-
}
|
| 604 |
-
|
| 605 |
-
// THE FIX: Use marked.parse() to convert markdown to HTML
|
| 606 |
-
// instead of a simple replace.
|
| 607 |
-
const formattedText = marked.parse(text || 'Sorry, I received an empty response.');
|
| 608 |
-
|
| 609 |
-
messageDiv.innerHTML = `
|
| 610 |
-
<div class="message-bubble">
|
| 611 |
-
${agentBadge}
|
| 612 |
-
${formattedText}
|
| 613 |
-
</div>
|
| 614 |
-
`;
|
| 615 |
-
|
| 616 |
-
messagesContainer.appendChild(messageDiv);
|
| 617 |
-
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 618 |
}
|
| 619 |
|
| 620 |
-
// Show/hide loading
|
| 621 |
function showLoading(show) {
|
| 622 |
-
|
| 623 |
-
const
|
|
|
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
}
|
| 628 |
|
| 629 |
-
// Handle Enter key press
|
| 630 |
function handleKeyPress(event) {
|
| 631 |
if (event.key === 'Enter' && !event.shiftKey) {
|
| 632 |
event.preventDefault();
|
| 633 |
sendMessage();
|
| 634 |
}
|
| 635 |
}
|
| 636 |
-
|
| 637 |
-
// Quick message function
|
| 638 |
function populateInput(templateText) {
|
| 639 |
const input = document.getElementById('messageInput');
|
| 640 |
input.value = templateText;
|
| 641 |
-
input.focus();
|
| 642 |
-
}
|
| 643 |
-
|
| 644 |
-
// Get new quote function
|
| 645 |
-
async function getNewQuote() {
|
| 646 |
-
try {
|
| 647 |
-
const response = await fetch('/quote');
|
| 648 |
-
const data = await response.json();
|
| 649 |
-
document.getElementById('quoteContainer').innerHTML = `πΏ ${data.quote}`;
|
| 650 |
-
} catch (error) {
|
| 651 |
-
console.error('Failed to fetch new quote:', error);
|
| 652 |
-
}
|
| 653 |
}
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
// Get new quote every 5 minutes
|
| 661 |
-
setInterval(getNewQuote, 5 * 60 * 1000);
|
| 662 |
-
}); {% endcomment %}
|
| 663 |
|
| 664 |
</body>
|
| 665 |
</html>
|
|
@@ -871,5 +676,203 @@
|
|
| 871 |
setInterval(getNewQuote, 5 * 60 * 1000); // Update quote every 5 mins
|
| 872 |
});
|
| 873 |
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
</body>
|
| 875 |
</html>-->
|
|
|
|
| 364 |
<em>ΰ€ΰ€ͺΰ€ΰ€Ύ ΰ€
ΰ€§ΰ₯ΰ€―ΰ€―ΰ€¨ ΰ€Έΰ€Ύΰ€₯ΰ₯ (Your Study Companion)</em> β¨
|
| 365 |
</div>
|
| 366 |
</div>
|
| 367 |
+
</div>
|
| 368 |
+
<div class="input-area">
|
| 369 |
<div class="quick-actions">
|
| 370 |
<button class="quick-btn" onclick="populateInput('Explain ')">π Explain Topic</button>
|
| 371 |
<button class="quick-btn" onclick="populateInput('What are the side effects of ')">βοΈ Drug Info</button>
|
| 372 |
<button class="quick-btn" onclick="populateInput('Make a quiz on ')">β Create Quiz</button>
|
| 373 |
<button class="quick-btn" onclick="populateInput('Mnemonic for ')">π§ Mnemonics</button>
|
| 374 |
</div>
|
| 375 |
+
</div>
|
| 376 |
|
| 377 |
|
| 378 |
<div class="input-container">
|
|
|
|
| 389 |
</div>
|
| 390 |
</div>
|
| 391 |
</div>
|
| 392 |
+
<script>
|
| 393 |
+
let isProcessing = false;
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
async function sendMessage() {
|
| 396 |
const input = document.getElementById('messageInput');
|
| 397 |
const message = input.value.trim();
|
|
|
|
| 398 |
if (!message || isProcessing) return;
|
| 399 |
+
|
|
|
|
| 400 |
addMessage(message, 'user');
|
| 401 |
input.value = '';
|
|
|
|
|
|
|
| 402 |
showLoading(true);
|
| 403 |
+
|
|
|
|
| 404 |
try {
|
| 405 |
const response = await fetch('/chat', {
|
| 406 |
method: 'POST',
|
| 407 |
+
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
|
| 408 |
body: JSON.stringify({ query: message })
|
| 409 |
});
|
|
|
|
| 410 |
const data = await response.json();
|
| 411 |
+
showLoading(false);
|
| 412 |
if (data.success) {
|
| 413 |
addMessage(data.message, 'bot', data.agent_used);
|
| 414 |
} else {
|
| 415 |
addMessage(`β Error: ${data.error || 'Something went wrong'}`, 'bot', 'error');
|
| 416 |
}
|
|
|
|
| 417 |
} catch (error) {
|
|
|
|
|
|
|
| 418 |
showLoading(false);
|
| 419 |
+
addMessage(`β Connection error: ${error.message}`, 'bot', 'error');
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
|
|
|
| 423 |
function addMessage(text, sender, agentType = '') {
|
| 424 |
+
const messagesContainer = document.getElementById('chatMessages');
|
| 425 |
+
const messageDiv = document.createElement('div');
|
| 426 |
+
messageDiv.className = `message ${sender}`;
|
| 427 |
+
const agentIcons = { 'academic': 'π Academic Agent', 'drug_info': 'π Drug Info Agent', 'quiz_generation': 'β Quiz Master' };
|
| 428 |
+
const agentBadge = sender === 'bot' ? `<div class="agent-badge">${agentIcons[agentType] || 'π€ AI Assistant'}</div>` : '';
|
| 429 |
+
const formattedText = marked.parse(text || 'Sorry, I received an empty response.');
|
| 430 |
+
messageDiv.innerHTML = `<div class="message-bubble">${agentBadge}${formattedText}</div>`;
|
| 431 |
+
messagesContainer.appendChild(messageDiv);
|
| 432 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
}
|
| 434 |
|
|
|
|
| 435 |
function showLoading(show) {
|
| 436 |
+
document.getElementById('sendBtn').disabled = show;
|
| 437 |
+
const existingLoading = document.getElementById('loading-indicator');
|
| 438 |
+
if (existingLoading) existingLoading.remove();
|
| 439 |
|
| 440 |
+
if (show) {
|
| 441 |
+
const messagesContainer = document.getElementById('chatMessages');
|
| 442 |
+
const loadingDiv = document.createElement('div');
|
| 443 |
+
loadingDiv.className = 'message bot';
|
| 444 |
+
loadingDiv.id = 'loading-indicator';
|
| 445 |
+
loadingDiv.innerHTML = `<div class="message-bubble"><div class="typing-indicator"><span></span><span></span><span></span></div></div>`;
|
| 446 |
+
messagesContainer.appendChild(loadingDiv);
|
| 447 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 448 |
+
}
|
| 449 |
}
|
| 450 |
|
|
|
|
| 451 |
function handleKeyPress(event) {
|
| 452 |
if (event.key === 'Enter' && !event.shiftKey) {
|
| 453 |
event.preventDefault();
|
| 454 |
sendMessage();
|
| 455 |
}
|
| 456 |
}
|
| 457 |
+
|
|
|
|
| 458 |
function populateInput(templateText) {
|
| 459 |
const input = document.getElementById('messageInput');
|
| 460 |
input.value = templateText;
|
| 461 |
+
input.focus();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
}
|
| 463 |
+
|
| 464 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 465 |
+
document.getElementById('messageInput').focus();
|
| 466 |
+
});
|
| 467 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
</body>
|
| 470 |
</html>
|
|
|
|
| 676 |
setInterval(getNewQuote, 5 * 60 * 1000); // Update quote every 5 mins
|
| 677 |
});
|
| 678 |
</script>
|
| 679 |
+
old
|
| 680 |
+
<script>
|
| 681 |
+
// Global variables
|
| 682 |
+
let isProcessing = false;
|
| 683 |
+
|
| 684 |
+
// In templates/index.html, inside the <script> tag
|
| 685 |
+
|
| 686 |
+
// --- File Management Functions ---
|
| 687 |
+
|
| 688 |
+
async function uploadFile() {
|
| 689 |
+
const fileInput = document.getElementById('fileInput');
|
| 690 |
+
const file = fileInput.files[0];
|
| 691 |
+
if (!file) {
|
| 692 |
+
addMessage('Please select a file to upload first.', 'bot', 'error');
|
| 693 |
+
return;
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
const formData = new FormData();
|
| 697 |
+
formData.append('file', file);
|
| 698 |
+
|
| 699 |
+
showLoading(true);
|
| 700 |
+
isProcessing = true;
|
| 701 |
+
|
| 702 |
+
try {
|
| 703 |
+
const response = await fetch('/upload', {
|
| 704 |
+
method: 'POST',
|
| 705 |
+
body: formData,
|
| 706 |
+
});
|
| 707 |
+
const data = await response.json();
|
| 708 |
+
if (data.success) {
|
| 709 |
+
addMessage(`β
${data.message}`, 'bot', 'academic');
|
| 710 |
+
updateFileList(data.files);
|
| 711 |
+
} else {
|
| 712 |
+
addMessage(`β Upload Error: ${data.error}`, 'bot', 'error');
|
| 713 |
+
}
|
| 714 |
+
} catch (error) {
|
| 715 |
+
addMessage(`β Connection Error: ${error.message}`, 'bot', 'error');
|
| 716 |
+
} finally {
|
| 717 |
+
showLoading(false);
|
| 718 |
+
isProcessing = false;
|
| 719 |
+
fileInput.value = ''; // Clear the file input
|
| 720 |
+
}
|
| 721 |
+
}
|
| 722 |
+
|
| 723 |
+
function updateFileList(files = []) {
|
| 724 |
+
const fileListDiv = document.getElementById('fileList');
|
| 725 |
+
if (files.length > 0) {
|
| 726 |
+
let fileLinks = files.map(f => `<span>${f.original_name}</span>`).join(', ');
|
| 727 |
+
fileListDiv.innerHTML = `<div style="font-size: 0.9em; margin-top: 10px;"><strong>Active Files:</strong> ${fileLinks} <button onclick="clearFiles()" style="margin-left: 10px; background: #800000; color: white; border: none; border-radius: 5px; cursor: pointer; padding: 2px 8px;">Clear All</button></div>`;
|
| 728 |
+
} else {
|
| 729 |
+
fileListDiv.innerHTML = '';
|
| 730 |
+
}
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
async function clearFiles() {
|
| 734 |
+
showLoading(true);
|
| 735 |
+
isProcessing = true;
|
| 736 |
+
try {
|
| 737 |
+
const response = await fetch('/clear_files', { method: 'POST' });
|
| 738 |
+
const data = await response.json();
|
| 739 |
+
if (data.success) {
|
| 740 |
+
addMessage('π§Ή All uploaded files and sessions have been cleared.', 'bot', 'academic');
|
| 741 |
+
updateFileList([]);
|
| 742 |
+
} else {
|
| 743 |
+
addMessage(`β Error: ${data.error}`, 'bot', 'error');
|
| 744 |
+
}
|
| 745 |
+
} catch (error) {
|
| 746 |
+
addMessage(`β Connection Error: ${error.message}`, 'bot', 'error');
|
| 747 |
+
} finally {
|
| 748 |
+
showLoading(false);
|
| 749 |
+
isProcessing = false;
|
| 750 |
+
}
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
// Update the file list on page load
|
| 754 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 755 |
+
fetch('/files').then(res => res.json()).then(data => {
|
| 756 |
+
updateFileList(data.files);
|
| 757 |
+
});
|
| 758 |
+
});
|
| 759 |
+
// Send message function
|
| 760 |
+
async function sendMessage() {
|
| 761 |
+
const input = document.getElementById('messageInput');
|
| 762 |
+
const message = input.value.trim();
|
| 763 |
+
|
| 764 |
+
if (!message || isProcessing) return;
|
| 765 |
+
|
| 766 |
+
// Add user message to chat
|
| 767 |
+
addMessage(message, 'user');
|
| 768 |
+
input.value = '';
|
| 769 |
+
|
| 770 |
+
// Show loading
|
| 771 |
+
showLoading(true);
|
| 772 |
+
isProcessing = true;
|
| 773 |
+
|
| 774 |
+
try {
|
| 775 |
+
const response = await fetch('/chat', {
|
| 776 |
+
method: 'POST',
|
| 777 |
+
headers: {
|
| 778 |
+
'Content-Type': 'application/json',
|
| 779 |
+
},
|
| 780 |
+
body: JSON.stringify({ query: message })
|
| 781 |
+
});
|
| 782 |
+
|
| 783 |
+
const data = await response.json();
|
| 784 |
+
|
| 785 |
+
if (data.success) {
|
| 786 |
+
addMessage(data.message, 'bot', data.agent_used);
|
| 787 |
+
} else {
|
| 788 |
+
addMessage(`β Error: ${data.error || 'Something went wrong'}`, 'bot', 'error');
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
} catch (error) {
|
| 792 |
+
addMessage(`β Connection error: ${error.message}`, 'bot', 'error');
|
| 793 |
+
} finally {
|
| 794 |
+
showLoading(false);
|
| 795 |
+
isProcessing = false;
|
| 796 |
+
}
|
| 797 |
+
}
|
| 798 |
+
|
| 799 |
+
// Add message to chat
|
| 800 |
+
function addMessage(text, sender, agentType = '') {
|
| 801 |
+
const messagesContainer = document.getElementById('chatMessages');
|
| 802 |
+
const messageDiv = document.createElement('div');
|
| 803 |
+
messageDiv.className = `message ${sender}`;
|
| 804 |
+
|
| 805 |
+
let agentBadge = '';
|
| 806 |
+
if (sender === 'bot' && agentType) {
|
| 807 |
+
const agentIcons = {
|
| 808 |
+
'academic': 'π Academic Agent',
|
| 809 |
+
'drug_info': 'π Drug Info Agent',
|
| 810 |
+
'quiz_generation': 'β Quiz Master',
|
| 811 |
+
'mnemonic_creation': 'π§ Memory Master',
|
| 812 |
+
'viva_practice': 'π£οΈ Viva Coach',
|
| 813 |
+
'error': 'β οΈ System'
|
| 814 |
+
};
|
| 815 |
+
agentBadge = `<div class="agent-badge">${agentIcons[agentType] || 'π€ AI Assistant'}</div>`;
|
| 816 |
+
}
|
| 817 |
+
|
| 818 |
+
// THE FIX: Use marked.parse() to convert markdown to HTML
|
| 819 |
+
// instead of a simple replace.
|
| 820 |
+
const formattedText = marked.parse(text || 'Sorry, I received an empty response.');
|
| 821 |
+
|
| 822 |
+
messageDiv.innerHTML = `
|
| 823 |
+
<div class="message-bubble">
|
| 824 |
+
${agentBadge}
|
| 825 |
+
${formattedText}
|
| 826 |
+
</div>
|
| 827 |
+
`;
|
| 828 |
+
|
| 829 |
+
messagesContainer.appendChild(messageDiv);
|
| 830 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 831 |
+
}
|
| 832 |
+
|
| 833 |
+
// Show/hide loading
|
| 834 |
+
function showLoading(show) {
|
| 835 |
+
const loading = document.getElementById('loading');
|
| 836 |
+
const sendBtn = document.getElementById('sendBtn');
|
| 837 |
+
|
| 838 |
+
loading.classList.toggle('show', show);
|
| 839 |
+
sendBtn.disabled = show;
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
+
// Handle Enter key press
|
| 843 |
+
function handleKeyPress(event) {
|
| 844 |
+
if (event.key === 'Enter' && !event.shiftKey) {
|
| 845 |
+
event.preventDefault();
|
| 846 |
+
sendMessage();
|
| 847 |
+
}
|
| 848 |
+
}
|
| 849 |
+
|
| 850 |
+
// Quick message function
|
| 851 |
+
function populateInput(templateText) {
|
| 852 |
+
const input = document.getElementById('messageInput');
|
| 853 |
+
input.value = templateText;
|
| 854 |
+
input.focus(); // This handily puts the cursor in the text box for you!
|
| 855 |
+
}
|
| 856 |
+
|
| 857 |
+
// Get new quote function
|
| 858 |
+
async function getNewQuote() {
|
| 859 |
+
try {
|
| 860 |
+
const response = await fetch('/quote');
|
| 861 |
+
const data = await response.json();
|
| 862 |
+
document.getElementById('quoteContainer').innerHTML = `πΏ ${data.quote}`;
|
| 863 |
+
} catch (error) {
|
| 864 |
+
console.error('Failed to fetch new quote:', error);
|
| 865 |
+
}
|
| 866 |
+
}
|
| 867 |
+
|
| 868 |
+
// Initialize app
|
| 869 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 870 |
+
// Focus on input
|
| 871 |
+
document.getElementById('messageInput').focus();
|
| 872 |
+
|
| 873 |
+
// Get new quote every 5 minutes
|
| 874 |
+
setInterval(getNewQuote, 5 * 60 * 1000);
|
| 875 |
+
});
|
| 876 |
+
</script>
|
| 877 |
</body>
|
| 878 |
</html>-->
|