Spaces:
Running
Running
Gopikanth123
commited on
Update templates/index.html
Browse files- templates/index.html +91 -116
templates/index.html
CHANGED
@@ -336,84 +336,69 @@
|
|
336 |
</div>
|
337 |
|
338 |
<script>
|
339 |
-
const chatBox = document.getElementById('chat-box');
|
340 |
-
const voiceBtn = document.getElementById('voice-btn');
|
341 |
-
const sendBtn = document.getElementById('send-btn');
|
342 |
-
const userInput = document.getElementById('user-input');
|
343 |
-
const themeSelect = document.getElementById('theme-select');
|
344 |
const languageSelect = document.getElementById('language-select');
|
345 |
-
|
346 |
-
// Add message to chatbox with visual effects
|
347 |
-
function addMessage(sender, text) {
|
348 |
-
const msgDiv = document.createElement('div');
|
349 |
-
msgDiv.classList.add('message', sender);
|
350 |
-
msgDiv.textContent = text;
|
351 |
-
chatBox.appendChild(msgDiv);
|
352 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
353 |
-
}
|
354 |
-
|
355 |
-
// Speech Recognition Setup
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
switch (selectedLanguage) {
|
372 |
-
case 'telugu':
|
373 |
-
recognition.lang = 'te-IN'; // Telugu
|
374 |
-
break;
|
375 |
-
case 'hindi':
|
376 |
-
recognition.lang = 'hi-IN'; // Hindi
|
377 |
-
break;
|
378 |
-
default:
|
379 |
-
recognition.lang = 'en-US'; // English
|
380 |
-
break;
|
381 |
-
}
|
382 |
-
}
|
383 |
-
|
384 |
-
voiceBtn.addEventListener('click', () => {
|
385 |
-
setRecognitionLanguage(); // Set the language before starting speech recognition
|
386 |
-
recognition.start();
|
387 |
-
});
|
388 |
-
|
389 |
-
recognition.addEventListener('result', (e) => {
|
390 |
-
const transcript = e.results[0][0].transcript;
|
391 |
-
addMessage('user-message', transcript);
|
392 |
-
sendUserMessage(transcript);
|
393 |
-
});
|
394 |
-
|
395 |
-
recognition.addEventListener('error', (event) => {
|
396 |
-
console.error("Speech recognition error", event);
|
397 |
-
});
|
398 |
-
}
|
399 |
-
|
400 |
-
// Function to change the accent color
|
401 |
-
function changeColor(color) {
|
402 |
-
document.documentElement.style.setProperty('--accent-color', color);
|
403 |
-
}
|
404 |
-
|
405 |
-
// Function to change the theme
|
406 |
-
function changeTheme(theme) {
|
407 |
-
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
408 |
-
if (theme !== 'default') {
|
409 |
-
document.documentElement.classList.add('theme-' + theme);
|
410 |
}
|
411 |
}
|
412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
413 |
// Function to send user input and selected language to backend
|
414 |
function sendUserMessage(message) {
|
415 |
const selectedLanguage = languageSelect.value; // Get the selected language
|
416 |
-
|
417 |
// Send the message and selected language to the backend
|
418 |
fetch('/chat', {
|
419 |
method: 'POST',
|
@@ -436,11 +421,17 @@
|
|
436 |
addMessage('bot-message', "Sorry, I couldn't process that.");
|
437 |
});
|
438 |
}
|
439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
// Text-to-Speech Function
|
441 |
function speakResponse(text) {
|
442 |
const utterance = new SpeechSynthesisUtterance(text);
|
443 |
-
|
444 |
// Set the language for text-to-speech based on the selected language
|
445 |
const selectedLanguage = languageSelect.value;
|
446 |
switch (selectedLanguage) {
|
@@ -454,52 +445,36 @@
|
|
454 |
utterance.lang = 'en-US'; // English
|
455 |
break;
|
456 |
}
|
457 |
-
|
458 |
-
|
459 |
-
window.speechSynthesis.speak(utterance);
|
460 |
-
} else {
|
461 |
-
console.error("Text-to-Speech not supported in this browser.");
|
462 |
-
}
|
463 |
}
|
464 |
|
465 |
-
// Event listeners for buttons
|
466 |
-
sendBtn.addEventListener('click', () => {
|
467 |
-
const message = userInput.value.trim();
|
468 |
-
if (message) {
|
469 |
-
addMessage('user-message', message);
|
470 |
-
sendUserMessage(message);
|
471 |
-
userInput.value = ''; // Clear input field after sending
|
472 |
-
}
|
473 |
-
});
|
474 |
-
|
475 |
-
// Handle pressing 'Enter' key for sending messages
|
476 |
-
userInput.addEventListener('keypress', (e) => {
|
477 |
-
if (e.key === 'Enter') {
|
478 |
-
sendBtn.click(); // Trigger button click
|
479 |
-
}
|
480 |
-
});
|
481 |
-
|
482 |
-
// Update theme when selected from dropdown
|
483 |
-
themeSelect.addEventListener('change', (e) => {
|
484 |
-
changeTheme(e.target.value);
|
485 |
-
});
|
486 |
-
|
487 |
-
// Ensure language is set when page loads
|
488 |
-
window.addEventListener('load', () => {
|
489 |
-
setRecognitionLanguage(); // Set default language for recognition on load
|
490 |
-
});
|
491 |
-
// Function to change the accent color
|
492 |
-
function changeColor(color) {
|
493 |
-
document.documentElement.style.setProperty('--accent-color', color);
|
494 |
-
}
|
495 |
|
496 |
-
//
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
document.documentElement.classList.add('theme-' + theme);
|
501 |
}
|
502 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
</script>
|
504 |
</body>
|
505 |
</html>
|
|
|
336 |
</div>
|
337 |
|
338 |
<script>
|
339 |
+
const chatBox = document.getElementById('chat-box');
|
340 |
+
const voiceBtn = document.getElementById('voice-btn');
|
341 |
+
const sendBtn = document.getElementById('send-btn');
|
342 |
+
const userInput = document.getElementById('user-input');
|
343 |
+
const themeSelect = document.getElementById('theme-select');
|
344 |
const languageSelect = document.getElementById('language-select');
|
345 |
+
|
346 |
+
// Add message to chatbox with visual effects
|
347 |
+
function addMessage(sender, text) {
|
348 |
+
const msgDiv = document.createElement('div');
|
349 |
+
msgDiv.classList.add('message', sender);
|
350 |
+
msgDiv.textContent = text;
|
351 |
+
chatBox.appendChild(msgDiv);
|
352 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
353 |
+
}
|
354 |
+
|
355 |
+
// Speech Recognition Setup
|
356 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
357 |
+
// recognition.lang = 'en-US';
|
358 |
+
// Function to set language for speech recognition
|
359 |
+
function setRecognitionLanguage() {
|
360 |
+
const selectedLanguage = languageSelect.value;
|
361 |
+
switch (selectedLanguage) {
|
362 |
+
case 'telugu':
|
363 |
+
recognition.lang = 'te-IN'; // Telugu
|
364 |
+
break;
|
365 |
+
case 'hindi':
|
366 |
+
recognition.lang = 'hi-IN'; // Hindi
|
367 |
+
break;
|
368 |
+
default:
|
369 |
+
recognition.lang = 'en-US'; // English
|
370 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
}
|
372 |
}
|
373 |
+
// voiceBtn.addEventListener('click', () => recognition.start());
|
374 |
+
voiceBtn.addEventListener('click', () => {
|
375 |
+
setRecognitionLanguage(); // Set the language before starting speech recognition
|
376 |
+
recognition.start();
|
377 |
+
});
|
378 |
+
|
379 |
+
recognition.addEventListener('result', (e) => {
|
380 |
+
const transcript = e.results[0][0].transcript;
|
381 |
+
addMessage('user-message', transcript);
|
382 |
+
sendUserMessage(transcript);
|
383 |
+
});
|
384 |
+
|
385 |
+
// Function to change the accent color
|
386 |
+
function changeColor(color) {
|
387 |
+
document.documentElement.style.setProperty('--accent-color', color);
|
388 |
+
}
|
389 |
+
|
390 |
+
// Function to change the theme
|
391 |
+
function changeTheme(theme) {
|
392 |
+
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
393 |
+
if (theme !== 'default') {
|
394 |
+
document.documentElement.classList.add('theme-' + theme);
|
395 |
+
}
|
396 |
+
}
|
397 |
+
|
398 |
// Function to send user input and selected language to backend
|
399 |
function sendUserMessage(message) {
|
400 |
const selectedLanguage = languageSelect.value; // Get the selected language
|
401 |
+
|
402 |
// Send the message and selected language to the backend
|
403 |
fetch('/chat', {
|
404 |
method: 'POST',
|
|
|
421 |
addMessage('bot-message', "Sorry, I couldn't process that.");
|
422 |
});
|
423 |
}
|
424 |
+
|
425 |
+
// // Text-to-Speech Function
|
426 |
+
// function speakResponse(text) {
|
427 |
+
// const utterance = new SpeechSynthesisUtterance(text);
|
428 |
+
// utterance.lang = 'en-US';
|
429 |
+
// window.speechSynthesis.speak(utterance);
|
430 |
+
// }
|
431 |
// Text-to-Speech Function
|
432 |
function speakResponse(text) {
|
433 |
const utterance = new SpeechSynthesisUtterance(text);
|
434 |
+
|
435 |
// Set the language for text-to-speech based on the selected language
|
436 |
const selectedLanguage = languageSelect.value;
|
437 |
switch (selectedLanguage) {
|
|
|
445 |
utterance.lang = 'en-US'; // English
|
446 |
break;
|
447 |
}
|
448 |
+
|
449 |
+
window.speechSynthesis.speak(utterance);
|
|
|
|
|
|
|
|
|
450 |
}
|
451 |
|
452 |
+
// Event listeners for buttons
|
453 |
+
sendBtn.addEventListener('click', () => {
|
454 |
+
const message = userInput.value.trim();
|
455 |
+
if (message) {
|
456 |
+
addMessage('user-message', message);
|
457 |
+
sendUserMessage(message);
|
458 |
+
userInput.value = ''; // Clear input field after sending
|
459 |
+
}
|
460 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
|
462 |
+
// Handle pressing 'Enter' key for sending messages
|
463 |
+
userInput.addEventListener('keypress', (e) => {
|
464 |
+
if (e.key === 'Enter') {
|
465 |
+
sendBtn.click(); // Trigger button click
|
|
|
466 |
}
|
467 |
+
});
|
468 |
+
|
469 |
+
// Update theme when selected from dropdown
|
470 |
+
themeSelect.addEventListener('change', (e) => {
|
471 |
+
changeTheme(e.target.value);
|
472 |
+
});
|
473 |
+
|
474 |
+
recognition.addEventListener('error', (event) => {
|
475 |
+
console.error("Speech recognition error", event);
|
476 |
+
});
|
477 |
+
|
478 |
</script>
|
479 |
</body>
|
480 |
</html>
|