Spaces:
Running
Running
Update templates/index.html
Browse files- templates/index.html +117 -132
templates/index.html
CHANGED
@@ -340,139 +340,124 @@
|
|
340 |
</div>
|
341 |
|
342 |
<script>
|
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 |
-
function sendUserMessage(message) {
|
405 |
-
fetch('/chat', {
|
406 |
-
method: 'POST',
|
407 |
-
headers: {
|
408 |
-
'Content-Type': 'application/json',
|
409 |
-
},
|
410 |
-
body: JSON.stringify({ message: message }),
|
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 |
-
// Function to handle language-specific logic
|
469 |
-
function updateBotLanguage(language) {
|
470 |
-
// Placeholder function for your chatbot's language integration
|
471 |
-
alert(`Chatbot will now respond in: ${language.toUpperCase()}`);
|
472 |
-
// Integrate language translations for bot messages here.
|
473 |
-
}
|
474 |
-
|
475 |
-
</script>
|
476 |
</body>
|
477 |
|
478 |
</html>
|
|
|
340 |
</div>
|
341 |
|
342 |
<script>
|
343 |
+
const chatBox = document.getElementById('chat-box');
|
344 |
+
const voiceBtn = document.getElementById('voice-btn');
|
345 |
+
const sendBtn = document.getElementById('send-btn');
|
346 |
+
const userInput = document.getElementById('user-input');
|
347 |
+
const themeSelect = document.getElementById('theme-select');
|
348 |
+
|
349 |
+
// Function to add messages to the chatbox
|
350 |
+
function addMessage(sender, text) {
|
351 |
+
const msgDiv = document.createElement('div');
|
352 |
+
msgDiv.classList.add('message', sender);
|
353 |
+
msgDiv.textContent = text;
|
354 |
+
chatBox.appendChild(msgDiv);
|
355 |
+
chatBox.scrollTop = chatBox.scrollHeight; // Scroll to the bottom
|
356 |
+
}
|
357 |
+
|
358 |
+
// Speech Recognition Setup
|
359 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
360 |
+
recognition.lang = 'en-US';
|
361 |
+
recognition.interimResults = false; // Set to true for interim results
|
362 |
+
|
363 |
+
// Start speech recognition when voice button is clicked
|
364 |
+
voiceBtn.addEventListener('click', () => {
|
365 |
+
recognition.start();
|
366 |
+
});
|
367 |
+
|
368 |
+
// Process the speech result
|
369 |
+
recognition.addEventListener('result', (e) => {
|
370 |
+
const transcript = e.results[0][0].transcript;
|
371 |
+
addMessage('user-message', transcript);
|
372 |
+
sendUserMessage(transcript);
|
373 |
+
});
|
374 |
+
|
375 |
+
// Handle cases when no result is found
|
376 |
+
recognition.addEventListener('nomatch', () => {
|
377 |
+
const message = 'No speech detected';
|
378 |
+
addMessage('user-message', message);
|
379 |
+
sendUserMessage(message);
|
380 |
+
});
|
381 |
+
|
382 |
+
// Handle speech recognition errors
|
383 |
+
recognition.addEventListener('error', (e) => {
|
384 |
+
console.error("Speech recognition error", e);
|
385 |
+
const message = 'No speech detected';
|
386 |
+
addMessage('user-message', message);
|
387 |
+
sendUserMessage(message);
|
388 |
+
});
|
389 |
+
|
390 |
+
// Send user input to the backend
|
391 |
+
function sendUserMessage(message) {
|
392 |
+
fetch('/chat', {
|
393 |
+
method: 'POST',
|
394 |
+
headers: {
|
395 |
+
'Content-Type': 'application/json',
|
396 |
+
},
|
397 |
+
body: JSON.stringify({ message: message }),
|
398 |
+
})
|
399 |
+
.then(response => response.json())
|
400 |
+
.then(data => {
|
401 |
+
const botResponse = data.response;
|
402 |
+
addMessage('bot-message', botResponse);
|
403 |
+
speakResponse(botResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
404 |
})
|
405 |
+
.catch(error => {
|
406 |
+
console.error("Error:", error);
|
407 |
+
addMessage('bot-message', "Sorry, I couldn't process that.");
|
408 |
+
});
|
409 |
+
}
|
410 |
+
|
411 |
+
// Text-to-Speech Function
|
412 |
+
function speakResponse(text) {
|
413 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
414 |
+
utterance.lang = 'en-US';
|
415 |
+
window.speechSynthesis.speak(utterance);
|
416 |
+
}
|
417 |
+
|
418 |
+
// Event listeners for sending messages
|
419 |
+
sendBtn.addEventListener('click', () => {
|
420 |
+
const message = userInput.value.trim();
|
421 |
+
if (message) {
|
422 |
+
addMessage('user-message', message);
|
423 |
+
sendUserMessage(message);
|
424 |
+
userInput.value = ''; // Clear the input field after sending
|
425 |
+
}
|
426 |
+
});
|
427 |
+
|
428 |
+
// Handle pressing 'Enter' key to send messages
|
429 |
+
userInput.addEventListener('keypress', (e) => {
|
430 |
+
if (e.key === 'Enter') {
|
431 |
+
sendBtn.click(); // Trigger button click
|
432 |
+
}
|
433 |
+
});
|
434 |
+
|
435 |
+
// Theme selection handling
|
436 |
+
themeSelect.addEventListener('change', (e) => {
|
437 |
+
changeTheme(e.target.value);
|
438 |
+
});
|
439 |
+
|
440 |
+
// Function to change the theme based on selection
|
441 |
+
function changeTheme(theme) {
|
442 |
+
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
443 |
+
if (theme !== 'default') {
|
444 |
+
document.documentElement.classList.add('theme-' + theme);
|
445 |
+
}
|
446 |
+
}
|
447 |
+
|
448 |
+
// Event listener for language selection, if integrated
|
449 |
+
document.getElementById("language-select").addEventListener("change", function () {
|
450 |
+
const selectedLanguage = this.value;
|
451 |
+
console.log(`Language selected: ${selectedLanguage}`);
|
452 |
+
updateBotLanguage(selectedLanguage); // Function to handle language updates
|
453 |
+
});
|
454 |
+
|
455 |
+
// Function to handle language-specific logic
|
456 |
+
function updateBotLanguage(language) {
|
457 |
+
alert(`Chatbot will now respond in: ${language.toUpperCase()}`);
|
458 |
+
// You might want to integrate translation logic here.
|
459 |
+
}
|
460 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
</body>
|
462 |
|
463 |
</html>
|