Gopikanth123 commited on
Commit
d825801
·
verified ·
1 Parent(s): ef73d42

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +117 -132
templates/index.html CHANGED
@@ -340,139 +340,124 @@
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
- // Add message to chatbox with visual effects
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;
356
- }
357
-
358
- // Speech Recognition Setup
359
- const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
360
- recognition.lang = 'en-US';
361
- recognition.maxResults = 10;
362
- recognition.interimResults = true;
363
-
364
- voiceBtn.addEventListener('click', () => recognition.start());
365
-
366
- recognition.addEventListener('result', (e) => {
367
- const transcript = e.results[0][0].transcript;
368
- addMessage('user-message', transcript);
369
- sendUserMessage(transcript);
370
- });
371
-
372
- recognition.addEventListener('noresult', () => {
373
- addMessage('user-message', 'No speech detected');
374
- sendUserMessage('No speech detected');
375
- });
376
-
377
- recognition.addEventListener('nomatch', () => {
378
- addMessage('user-message', 'No speech detected');
379
- sendUserMessage('No speech detected');
380
- });
381
-
382
- recognition.addEventListener('error', (e) => {
383
- if(e.error == 'no-speech') {
384
- addMessage('user-message', 'No speech detected');
385
- sendUserMessage('No speech detected');
386
- }
387
- });
388
-
389
-
390
- // Function to change the accent color
391
- function changeColor(color) {
392
- document.documentElement.style.setProperty('--accent-color', color);
393
- }
394
-
395
- // Function to change the theme
396
- function changeTheme(theme) {
397
- document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
398
- if (theme !== 'default') {
399
- document.documentElement.classList.add('theme-' + theme);
400
- }
401
- }
402
-
403
- // Send user input to backend (placeholder URL)
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
- .then(response => response.json())
413
- .then(data => {
414
- const botResponse = data.response;
415
- addMessage('bot-message', botResponse);
416
- speakResponse(botResponse);
417
- })
418
- .catch(error => {
419
- console.error("Error:", error);
420
- addMessage('bot-message', "Sorry, I couldn't process that.");
421
- });
422
- }
423
-
424
- // Text-to-Speech Function
425
- function speakResponse(text) {
426
- const utterance = new SpeechSynthesisUtterance(text);
427
- utterance.lang = 'en-US';
428
- window.speechSynthesis.speak(utterance);
429
- }
430
- // Event listeners for buttons
431
- sendBtn.addEventListener('click', () => {
432
- const message = userInput.value.trim();
433
- if (message) {
434
- addMessage('user-message', message);
435
- sendUserMessage(message);
436
- userInput.value = ''; // Clear input field after sending
437
- }
438
- });
439
-
440
- // Handle pressing 'Enter' key for sending messages
441
- userInput.addEventListener('keypress', (e) => {
442
- if (e.key === 'Enter') {
443
- sendBtn.click(); // Trigger button click
444
- }
445
- });
446
-
447
- // Update theme when selected from dropdown
448
- themeSelect.addEventListener('change', (e) => {
449
- changeTheme(e.target.value);
450
- });
451
-
452
- recognition.addEventListener('error', (event) => {
453
- console.error("Speech recognition error", event);
454
- });
455
- // Event listener for language selection
456
- document.getElementById("language-select").addEventListener("change", function () {
457
- const selectedLanguage = this.value;
458
-
459
- // Example: Display a message when the language is changed
460
- console.log(`Language selected: ${selectedLanguage}`);
461
-
462
- // Logic to change chatbot responses based on the selected language
463
- // You can call an API to fetch translations or configure response logic here.
464
- // For example:
465
- updateBotLanguage(selectedLanguage);
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>