Spaces:
Runtime error
Runtime error
Gopikanth123
commited on
Update templates/index.html
Browse files- templates/index.html +179 -206
templates/index.html
CHANGED
@@ -347,211 +347,184 @@
|
|
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 |
-
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();
|
537 |
-
if (message) {
|
538 |
-
addMessage('user-message', message);
|
539 |
-
sendUserMessage(message);
|
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>
|
|
|
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': recognition.lang = 'te-IN'; break;
|
374 |
+
case 'hindi': recognition.lang = 'hi-IN'; break;
|
375 |
+
case 'bengali': recognition.lang = 'bn-IN'; break;
|
376 |
+
case 'marathi': recognition.lang = 'mr-IN'; break;
|
377 |
+
case 'tamil': recognition.lang = 'ta-IN'; break;
|
378 |
+
case 'gujarati': recognition.lang = 'gu-IN'; break;
|
379 |
+
case 'kannada': recognition.lang = 'kn-IN'; break;
|
380 |
+
case 'malayalam': recognition.lang = 'ml-IN'; break;
|
381 |
+
case 'punjabi': recognition.lang = 'pa-IN'; break;
|
382 |
+
case 'odia': recognition.lang = 'or-IN'; break;
|
383 |
+
case 'urdu': recognition.lang = 'ur-IN'; break;
|
384 |
+
case 'assamese': recognition.lang = 'as-IN'; break;
|
385 |
+
case 'sanskrit': recognition.lang = 'sa-IN'; break;
|
386 |
+
case 'english':
|
387 |
+
default: recognition.lang = 'en-US'; break; // Default to English
|
388 |
+
}
|
389 |
+
}
|
390 |
+
|
391 |
+
// Event listener for voice input button
|
392 |
+
voiceBtn.addEventListener('click', () => {
|
393 |
+
setRecognitionLanguage(); // Set language
|
394 |
+
recognition.start(); // Start recognition
|
395 |
+
});
|
396 |
+
|
397 |
+
// Handle results from speech recognition
|
398 |
+
recognition.addEventListener('result', (e) => {
|
399 |
+
const transcript = e.results[0][0].transcript;
|
400 |
+
addMessage('user-message', transcript);
|
401 |
+
sendUserMessage(transcript);
|
402 |
+
});
|
403 |
+
|
404 |
+
// Handle errors in speech recognition
|
405 |
+
recognition.addEventListener('error', (event) => {
|
406 |
+
console.error("Speech recognition error", event);
|
407 |
+
});
|
408 |
+
|
409 |
+
// Change the accent color
|
410 |
+
function changeColor(color) {
|
411 |
+
document.documentElement.style.setProperty('--accent-color', color);
|
412 |
+
}
|
413 |
+
|
414 |
+
// Change the theme
|
415 |
+
function changeTheme(theme) {
|
416 |
+
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
417 |
+
if (theme !== 'default') {
|
418 |
+
document.documentElement.classList.add('theme-' + theme);
|
419 |
+
}
|
420 |
+
}
|
421 |
+
|
422 |
+
// Function to send user input and selected language to backend
|
423 |
+
function sendUserMessage(message) {
|
424 |
+
const selectedLanguage = languageSelect.value; // Get the selected language
|
425 |
+
|
426 |
+
// Send the message and selected language to the backend
|
427 |
+
fetch('/chat', {
|
428 |
+
method: 'POST',
|
429 |
+
headers: {
|
430 |
+
'Content-Type': 'application/json',
|
431 |
+
},
|
432 |
+
body: JSON.stringify({
|
433 |
+
message,
|
434 |
+
language: selectedLanguage, // Include the selected language in the request body
|
435 |
+
}),
|
436 |
+
})
|
437 |
+
.then(response => response.json())
|
438 |
+
.then(data => {
|
439 |
+
const botResponse = data.response;
|
440 |
+
addMessage('bot-message', botResponse);
|
441 |
+
speakResponse(botResponse, selectedLanguage);
|
442 |
+
})
|
443 |
+
.catch(error => {
|
444 |
+
console.error("Error:", error);
|
445 |
+
addMessage('bot-message', "Sorry, I couldn't process that.");
|
446 |
+
});
|
447 |
+
}
|
448 |
+
|
449 |
+
// Function to return the voice list and find a matching language voice
|
450 |
+
function getVoiceForLanguage(lang) {
|
451 |
+
const voices = window.speechSynthesis.getVoices();
|
452 |
+
const matchingVoice = voices.find(voice => voice.lang.startsWith(lang.split('-')[0])); // Match based on language code
|
453 |
+
return matchingVoice || null; // Return first match or null if not found
|
454 |
+
}
|
455 |
+
|
456 |
+
// Text-to-Speech function
|
457 |
+
function speak(text, lang) {
|
458 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
459 |
+
const selectedVoice = getVoiceForLanguage(lang);
|
460 |
+
|
461 |
+
if (selectedVoice) {
|
462 |
+
utterance.voice = selectedVoice; // Set the matching voice
|
463 |
+
utterance.lang = lang; // Set the language
|
464 |
+
} else {
|
465 |
+
console.warn(`No voice found for language: ${lang}. Falling back to default.`);
|
466 |
+
utterance.lang = 'en-US'; // Fallback to English
|
467 |
+
}
|
468 |
+
|
469 |
+
utterance.pitch = 1; // Set pitch
|
470 |
+
utterance.rate = 1; // Set rate
|
471 |
+
window.speechSynthesis.speak(utterance); // Speak the text
|
472 |
+
}
|
473 |
+
|
474 |
+
// Language Handling Function
|
475 |
+
function speakResponse(text, selectedLanguage) {
|
476 |
+
let lang;
|
477 |
+
|
478 |
+
// Map selected language to appropriate language code
|
479 |
+
switch (selectedLanguage) {
|
480 |
+
case 'hindi': lang = 'hi-IN'; break;
|
481 |
+
case 'bengali': lang = 'bn-IN'; break;
|
482 |
+
case 'telugu': lang = 'te-IN'; break;
|
483 |
+
case 'marathi': lang = 'mr-IN'; break;
|
484 |
+
case 'tamil': lang = 'ta-IN'; break;
|
485 |
+
case 'gujarati': lang = 'gu-IN'; break;
|
486 |
+
case 'kannada': lang = 'kn-IN'; break;
|
487 |
+
case 'malayalam': lang = 'ml-IN'; break;
|
488 |
+
case 'punjabi': lang = 'pa-IN'; break;
|
489 |
+
case 'odia': lang = 'or-IN'; break;
|
490 |
+
case 'urdu': lang = 'ur-IN'; break;
|
491 |
+
case 'assamese': lang = 'as-IN'; break;
|
492 |
+
case 'sanskrit': lang = 'sa-IN'; break;
|
493 |
+
default: lang = 'en-US'; break; // English (default)
|
494 |
+
}
|
495 |
+
|
496 |
+
speak(text, lang); // Call the speak function with the determined language
|
497 |
+
}
|
498 |
+
|
499 |
+
// Ensure voices are loaded before running the TTS
|
500 |
+
window.speechSynthesis.onvoiceschanged = () => {
|
501 |
+
console.log("Voices loaded and ready to use.");
|
502 |
+
// Populate voices (optional, you may also load voices on demand)
|
503 |
+
const voices = window.speechSynthesis.getVoices();
|
504 |
+
voices.forEach(voice => console.log(`Voice: ${voice.name}, Language: ${voice.lang}`));
|
505 |
+
};
|
506 |
+
|
507 |
+
// Event listeners for buttons
|
508 |
+
sendBtn.addEventListener('click', () => {
|
509 |
+
const message = userInput.value.trim();
|
510 |
+
if (message) {
|
511 |
+
addMessage('user-message', message);
|
512 |
+
sendUserMessage(message);
|
513 |
+
userInput.value = ''; // Clear input field after sending
|
514 |
+
}
|
515 |
+
});
|
516 |
+
|
517 |
+
// Handle pressing 'Enter' key for sending messages
|
518 |
+
userInput.addEventListener('keypress', (e) => {
|
519 |
+
if (e.key === 'Enter') {
|
520 |
+
sendBtn.click(); // Trigger button click
|
521 |
+
}
|
522 |
+
});
|
523 |
+
|
524 |
+
// Update theme when selected from dropdown
|
525 |
+
themeSelect.addEventListener('change', (e) => {
|
526 |
+
changeTheme(e.target.value);
|
527 |
+
});
|
528 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
529 |
</body>
|
530 |
</html>
|