Spaces:
Running
Running
Update templates/index.html
Browse files- templates/index.html +187 -242
templates/index.html
CHANGED
|
@@ -347,240 +347,190 @@
|
|
| 347 |
</div>
|
| 348 |
|
| 349 |
<script>
|
| 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 |
-
// Language Handling Function
|
| 536 |
-
function speakResponse(text,selectedLanguage) {
|
| 537 |
-
// const selectedLanguage = document.getElementById('language-select').value; // Get the selected language
|
| 538 |
-
|
| 539 |
-
let lang;
|
| 540 |
-
|
| 541 |
-
// Map selected language to appropriate language code
|
| 542 |
-
switch (selectedLanguage) {
|
| 543 |
-
case 'hindi':
|
| 544 |
-
lang = 'hi-IN'; break;
|
| 545 |
-
case 'bengali':
|
| 546 |
-
lang = 'bn-IN'; break;
|
| 547 |
-
case 'telugu':
|
| 548 |
-
lang = 'te-IN'; break;
|
| 549 |
-
case 'marathi':
|
| 550 |
-
lang = 'mr-IN'; break;
|
| 551 |
-
case 'tamil':
|
| 552 |
-
lang = 'ta-IN'; break;
|
| 553 |
-
case 'gujarati':
|
| 554 |
-
lang = 'gu-IN'; break;
|
| 555 |
-
case 'kannada':
|
| 556 |
-
lang = 'kn-IN'; break;
|
| 557 |
-
case 'malayalam':
|
| 558 |
-
lang = 'ml-IN'; break;
|
| 559 |
-
case 'punjabi':
|
| 560 |
-
lang = 'pa-IN'; break;
|
| 561 |
-
case 'odia':
|
| 562 |
-
lang = 'or-IN'; break;
|
| 563 |
-
case 'urdu':
|
| 564 |
-
lang = 'ur-IN'; break;
|
| 565 |
-
case 'assamese':
|
| 566 |
-
lang = 'as-IN'; break;
|
| 567 |
-
case 'sanskrit':
|
| 568 |
-
lang = 'sa-IN'; break;
|
| 569 |
-
default:
|
| 570 |
-
lang = 'en-US'; break; // English (default)
|
| 571 |
-
}
|
| 572 |
-
|
| 573 |
-
speak(text, lang); // Call the speak function with the determined language
|
| 574 |
-
}
|
| 575 |
-
|
| 576 |
-
// Ensure voices are loaded before running the TTS
|
| 577 |
-
if (window.speechSynthesis.onvoiceschanged !== undefined) {
|
| 578 |
-
window.speechSynthesis.onvoiceschanged = () => {
|
| 579 |
-
console.log("Voices loaded and ready to use.");
|
| 580 |
-
};
|
| 581 |
-
}
|
| 582 |
-
|
| 583 |
-
|
| 584 |
// Event listeners for buttons
|
| 585 |
sendBtn.addEventListener('click', () => {
|
| 586 |
const message = userInput.value.trim();
|
|
@@ -590,23 +540,18 @@
|
|
| 590 |
userInput.value = ''; // Clear input field after sending
|
| 591 |
}
|
| 592 |
});
|
| 593 |
-
|
| 594 |
// Handle pressing 'Enter' key for sending messages
|
| 595 |
userInput.addEventListener('keypress', (e) => {
|
| 596 |
if (e.key === 'Enter') {
|
| 597 |
sendBtn.click(); // Trigger button click
|
| 598 |
}
|
| 599 |
});
|
| 600 |
-
|
| 601 |
// Update theme when selected from dropdown
|
| 602 |
themeSelect.addEventListener('change', (e) => {
|
| 603 |
changeTheme(e.target.value);
|
| 604 |
});
|
| 605 |
-
|
| 606 |
-
recognition.addEventListener('error', (event) => {
|
| 607 |
-
console.error("Speech recognition error", event);
|
| 608 |
-
});
|
| 609 |
-
|
| 610 |
-
</script>
|
| 611 |
</body>
|
| 612 |
</html>
|
|
|
|
| 347 |
</div>
|
| 348 |
|
| 349 |
<script>
|
| 350 |
+
const chatBox = document.getElementById('chat-box');
|
| 351 |
+
const voiceBtn = document.getElementById('voice-btn');
|
| 352 |
+
const sendBtn = document.getElementById('send-btn');
|
| 353 |
+
const userInput = document.getElementById('user-input');
|
| 354 |
+
const themeSelect = document.getElementById('theme-select');
|
| 355 |
+
const languageSelect = document.getElementById('language-select');
|
| 356 |
+
|
| 357 |
+
// Add message to chatbox
|
| 358 |
+
function addMessage(sender, text) {
|
| 359 |
+
const msgDiv = document.createElement('div');
|
| 360 |
+
msgDiv.classList.add('message', sender);
|
| 361 |
+
msgDiv.textContent = text;
|
| 362 |
+
chatBox.appendChild(msgDiv);
|
| 363 |
+
chatBox.scrollTop = chatBox.scrollHeight; // Scroll to the bottom of the chat
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// Initialize speech recognition
|
| 367 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 368 |
+
|
| 369 |
+
// Function to set language for speech recognition
|
| 370 |
+
function setRecognitionLanguage() {
|
| 371 |
+
const selectedLanguage = languageSelect.value;
|
| 372 |
+
switch (selectedLanguage) {
|
| 373 |
+
case 'telugu':
|
| 374 |
+
recognition.lang = 'te-IN'; break;
|
| 375 |
+
case 'hindi':
|
| 376 |
+
recognition.lang = 'hi-IN'; break;
|
| 377 |
+
case 'bengali':
|
| 378 |
+
recognition.lang = 'bn-IN'; break;
|
| 379 |
+
case 'marathi':
|
| 380 |
+
recognition.lang = 'mr-IN'; break;
|
| 381 |
+
case 'tamil':
|
| 382 |
+
recognition.lang = 'ta-IN'; break;
|
| 383 |
+
case 'gujarati':
|
| 384 |
+
recognition.lang = 'gu-IN'; break;
|
| 385 |
+
case 'kannada':
|
| 386 |
+
recognition.lang = 'kn-IN'; break;
|
| 387 |
+
case 'malayalam':
|
| 388 |
+
recognition.lang = 'ml-IN'; break;
|
| 389 |
+
case 'punjabi':
|
| 390 |
+
recognition.lang = 'pa-IN'; break;
|
| 391 |
+
case 'odia':
|
| 392 |
+
recognition.lang = 'or-IN'; break;
|
| 393 |
+
case 'urdu':
|
| 394 |
+
recognition.lang = 'ur-IN'; break;
|
| 395 |
+
case 'assamese':
|
| 396 |
+
recognition.lang = 'as-IN'; break;
|
| 397 |
+
case 'sanskrit':
|
| 398 |
+
recognition.lang = 'sa-IN'; break;
|
| 399 |
+
case 'english':
|
| 400 |
+
default:
|
| 401 |
+
recognition.lang = 'en-US'; break; // Default to English
|
| 402 |
+
}
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
// Event listener for voice input button
|
| 406 |
+
voiceBtn.addEventListener('click', () => {
|
| 407 |
+
setRecognitionLanguage(); // Set language
|
| 408 |
+
recognition.start(); // Start recognition
|
| 409 |
+
});
|
| 410 |
+
|
| 411 |
+
// Handle results from speech recognition
|
| 412 |
+
recognition.addEventListener('result', (e) => {
|
| 413 |
+
const transcript = e.results[0][0].transcript;
|
| 414 |
+
addMessage('user-message', transcript);
|
| 415 |
+
sendUserMessage(transcript);
|
| 416 |
+
});
|
| 417 |
+
|
| 418 |
+
// Handle errors in speech recognition
|
| 419 |
+
recognition.addEventListener('error', (event) => {
|
| 420 |
+
console.error("Speech recognition error", event);
|
| 421 |
+
});
|
| 422 |
+
|
| 423 |
+
// Change the accent color
|
| 424 |
+
function changeColor(color) {
|
| 425 |
+
document.documentElement.style.setProperty('--accent-color', color);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
// Change the theme
|
| 429 |
+
function changeTheme(theme) {
|
| 430 |
+
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
| 431 |
+
if (theme !== 'default') {
|
| 432 |
+
document.documentElement.classList.add('theme-' + theme);
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
// Function to send user input and selected language to backend
|
| 437 |
+
function sendUserMessage(message) {
|
| 438 |
+
const selectedLanguage = languageSelect.value; // Get the selected language
|
| 439 |
+
|
| 440 |
+
// Send the message and selected language to the backend
|
| 441 |
+
fetch('/chat', {
|
| 442 |
+
method: 'POST',
|
| 443 |
+
headers: {
|
| 444 |
+
'Content-Type': 'application/json',
|
| 445 |
+
},
|
| 446 |
+
body: JSON.stringify({
|
| 447 |
+
message,
|
| 448 |
+
language: selectedLanguage, // Include the selected language in the request body
|
| 449 |
+
}),
|
| 450 |
+
})
|
| 451 |
+
.then(response => response.json())
|
| 452 |
+
.then(data => {
|
| 453 |
+
const botResponse = data.response;
|
| 454 |
+
addMessage('bot-message', botResponse);
|
| 455 |
+
speakResponse(botResponse, selectedLanguage);
|
| 456 |
+
})
|
| 457 |
+
.catch(error => {
|
| 458 |
+
console.error("Error:", error);
|
| 459 |
+
addMessage('bot-message', "Sorry, I couldn't process that.");
|
| 460 |
+
});
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// Function to get the voice list and match the language
|
| 464 |
+
function getVoiceForLanguage(lang) {
|
| 465 |
+
const voices = window.speechSynthesis.getVoices();
|
| 466 |
+
return voices.find(voice => voice.lang === lang) || null; // Find a matching voice or return null
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
// Text-to-Speech function
|
| 470 |
+
// Text-to-Speech function
|
| 471 |
+
function speak(text, lang) {
|
| 472 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 473 |
+
const selectedVoice = getVoiceForLanguage(lang);
|
| 474 |
+
|
| 475 |
+
if (selectedVoice) {
|
| 476 |
+
utterance.voice = selectedVoice; // Set the matching voice
|
| 477 |
+
utterance.lang = lang; // Set the language
|
| 478 |
+
} else {
|
| 479 |
+
console.warn(`No voice found for language: ${lang}. Falling back to default.`);
|
| 480 |
+
utterance.lang = 'en-US'; // Fallback to English
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
utterance.pitch = 1; // Set pitch
|
| 484 |
+
utterance.rate = 1; // Set rate
|
| 485 |
+
window.speechSynthesis.speak(utterance); // Speak the text
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
// Language Handling Function
|
| 489 |
+
function speakResponse(text, selectedLanguage) {
|
| 490 |
+
let lang;
|
| 491 |
+
|
| 492 |
+
// Map selected language to appropriate language code
|
| 493 |
+
switch (selectedLanguage) {
|
| 494 |
+
case 'hindi':
|
| 495 |
+
lang = 'hi-IN'; break;
|
| 496 |
+
case 'bengali':
|
| 497 |
+
lang = 'bn-IN'; break;
|
| 498 |
+
case 'telugu':
|
| 499 |
+
lang = 'te-IN'; break;
|
| 500 |
+
case 'marathi':
|
| 501 |
+
lang = 'mr-IN'; break;
|
| 502 |
+
case 'tamil':
|
| 503 |
+
lang = 'ta-IN'; break;
|
| 504 |
+
case 'gujarati':
|
| 505 |
+
lang = 'gu-IN'; break;
|
| 506 |
+
case 'kannada':
|
| 507 |
+
lang = 'kn-IN'; break;
|
| 508 |
+
case 'malayalam':
|
| 509 |
+
lang = 'ml-IN'; break;
|
| 510 |
+
case 'punjabi':
|
| 511 |
+
lang = 'pa-IN'; break;
|
| 512 |
+
case 'odia':
|
| 513 |
+
lang = 'or-IN'; break;
|
| 514 |
+
case 'urdu':
|
| 515 |
+
lang = 'ur-IN'; break;
|
| 516 |
+
case 'assamese':
|
| 517 |
+
lang = 'as-IN'; break;
|
| 518 |
+
case 'sanskrit':
|
| 519 |
+
lang = 'sa-IN'; break;
|
| 520 |
+
default:
|
| 521 |
+
lang = 'en-US'; break; // English (default)
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
speak(text, lang); // Call the speak function with the determined language
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
// Ensure voices are loaded before running the TTS
|
| 528 |
+
if (window.speechSynthesis.onvoiceschanged !== undefined) {
|
| 529 |
+
window.speechSynthesis.onvoiceschanged = () => {
|
| 530 |
+
console.log("Voices loaded and ready to use.");
|
| 531 |
+
};
|
| 532 |
+
}
|
| 533 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
// Event listeners for buttons
|
| 535 |
sendBtn.addEventListener('click', () => {
|
| 536 |
const message = userInput.value.trim();
|
|
|
|
| 540 |
userInput.value = ''; // Clear input field after sending
|
| 541 |
}
|
| 542 |
});
|
| 543 |
+
|
| 544 |
// Handle pressing 'Enter' key for sending messages
|
| 545 |
userInput.addEventListener('keypress', (e) => {
|
| 546 |
if (e.key === 'Enter') {
|
| 547 |
sendBtn.click(); // Trigger button click
|
| 548 |
}
|
| 549 |
});
|
| 550 |
+
|
| 551 |
// Update theme when selected from dropdown
|
| 552 |
themeSelect.addEventListener('change', (e) => {
|
| 553 |
changeTheme(e.target.value);
|
| 554 |
});
|
| 555 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
</body>
|
| 557 |
</html>
|