Update script.js
Browse files
script.js
CHANGED
|
@@ -8,13 +8,13 @@ if (!('webkitSpeechRecognition' in window)) {
|
|
| 8 |
recognition.interimResults = false;
|
| 9 |
recognition.lang = 'en-IN'; // Default language (can be 'hi-IN' for Hindi)
|
| 10 |
|
| 11 |
-
const
|
| 12 |
const userInput = document.getElementById('userInput'); // Text input field
|
| 13 |
-
const
|
| 14 |
|
| 15 |
// Voice Input Button Click Event
|
| 16 |
-
|
| 17 |
-
if (
|
| 18 |
recognition.stop(); // Stop if already active
|
| 19 |
} else {
|
| 20 |
recognition.start(); // Start listening
|
|
@@ -23,100 +23,143 @@ if (!('webkitSpeechRecognition' in window)) {
|
|
| 23 |
|
| 24 |
// When speech recognition starts
|
| 25 |
recognition.onstart = () => {
|
| 26 |
-
|
| 27 |
-
console.log('Speech recognition started');
|
| 28 |
};
|
| 29 |
|
| 30 |
// When speech recognition ends
|
| 31 |
recognition.onend = () => {
|
| 32 |
-
|
| 33 |
-
console.log('Speech recognition ended');
|
| 34 |
};
|
| 35 |
|
| 36 |
// When speech recognition result is detected
|
| 37 |
recognition.onresult = (event) => {
|
| 38 |
const transcript = event.results[0][0].transcript;
|
| 39 |
-
console.log('Speech recognition result:', transcript);
|
| 40 |
userInput.value = transcript; // Fill the input with transcribed text
|
| 41 |
sendMessage(); // Call sendMessage after getting the input
|
| 42 |
};
|
| 43 |
|
| 44 |
// Handle errors during speech recognition
|
| 45 |
recognition.onerror = (event) => {
|
| 46 |
-
console.error('Speech recognition error detected:', event.error);
|
| 47 |
alert('Speech recognition error: ' + event.error);
|
| 48 |
-
|
| 49 |
};
|
| 50 |
}
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
// Text-to-Speech function
|
| 53 |
-
function speakText(text) {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
} else {
|
| 59 |
-
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
-
// Function to handle sending a message (with or without voice)
|
| 64 |
function sendMessage() {
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
},
|
| 81 |
body: JSON.stringify({ message: message })
|
| 82 |
})
|
| 83 |
.then(response => {
|
| 84 |
-
console.log("Response status:", response.status); // Check response status
|
| 85 |
return response.json();
|
| 86 |
})
|
| 87 |
.then(data => {
|
| 88 |
-
console.log("Received data from backend:", data); // Log the received data
|
| 89 |
const botResponse = data.response;
|
|
|
|
|
|
|
| 90 |
appendMessage(botResponse, 'bot-message');
|
| 91 |
-
|
| 92 |
// Speak the bot's response using Text-to-Speech
|
| 93 |
-
speakText(botResponse);
|
| 94 |
|
| 95 |
// Enable the voiceInputBtn after response
|
| 96 |
voiceInputBtn.disabled = false;
|
| 97 |
})
|
| 98 |
.catch(error => {
|
| 99 |
-
console.error('Error:', error);
|
| 100 |
alert('Failed to connect to the backend. Please try again later.');
|
| 101 |
-
|
| 102 |
// Enable the voiceInputBtn after error
|
| 103 |
voiceInputBtn.disabled = false;
|
| 104 |
});
|
| 105 |
-
|
| 106 |
}
|
| 107 |
|
| 108 |
-
//
|
|
|
|
| 109 |
function appendMessage(text, className) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
const messageElement = document.createElement('div');
|
| 111 |
-
messageElement.classList.add('message', className);
|
| 112 |
messageElement.textContent = text;
|
| 113 |
-
|
| 114 |
-
|
| 115 |
}
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
recognition.lang = selectedLanguage === 'hindi' ? 'hi-IN' : 'en-IN'; // Switch language for speech recognition
|
| 121 |
-
console.log('Speech recognition language switched to:', recognition.lang);
|
| 122 |
-
});
|
|
|
|
| 8 |
recognition.interimResults = false;
|
| 9 |
recognition.lang = 'en-IN'; // Default language (can be 'hi-IN' for Hindi)
|
| 10 |
|
| 11 |
+
const micButton = document.getElementById('micButton'); // Microphone icon
|
| 12 |
const userInput = document.getElementById('userInput'); // Text input field
|
| 13 |
+
const chatBody = document.getElementById('chatBody'); // Chat history container
|
| 14 |
|
| 15 |
// Voice Input Button Click Event
|
| 16 |
+
micButton.addEventListener('click', () => {
|
| 17 |
+
if (micButton.classList.contains('active')) {
|
| 18 |
recognition.stop(); // Stop if already active
|
| 19 |
} else {
|
| 20 |
recognition.start(); // Start listening
|
|
|
|
| 23 |
|
| 24 |
// When speech recognition starts
|
| 25 |
recognition.onstart = () => {
|
| 26 |
+
micButton.classList.add('active'); // Change mic color to green
|
|
|
|
| 27 |
};
|
| 28 |
|
| 29 |
// When speech recognition ends
|
| 30 |
recognition.onend = () => {
|
| 31 |
+
micButton.classList.remove('active'); // Reset mic color to red
|
|
|
|
| 32 |
};
|
| 33 |
|
| 34 |
// When speech recognition result is detected
|
| 35 |
recognition.onresult = (event) => {
|
| 36 |
const transcript = event.results[0][0].transcript;
|
|
|
|
| 37 |
userInput.value = transcript; // Fill the input with transcribed text
|
| 38 |
sendMessage(); // Call sendMessage after getting the input
|
| 39 |
};
|
| 40 |
|
| 41 |
// Handle errors during speech recognition
|
| 42 |
recognition.onerror = (event) => {
|
|
|
|
| 43 |
alert('Speech recognition error: ' + event.error);
|
| 44 |
+
micButton.classList.remove('active'); // Reset mic color to red if error occurs
|
| 45 |
};
|
| 46 |
}
|
| 47 |
|
| 48 |
+
// Add an event listener to the input field to detect the Enter key
|
| 49 |
+
const userInput = document.getElementById('userInput');
|
| 50 |
+
|
| 51 |
+
userInput.addEventListener('keydown', function(event) {
|
| 52 |
+
// Check if the Enter key (key code 13) is pressed
|
| 53 |
+
if (event.key === 'Enter') {
|
| 54 |
+
event.preventDefault(); // Prevent the default behavior (new line)
|
| 55 |
+
sendMessage(); // Call your send message function
|
| 56 |
+
}
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
function toggleHistory() {
|
| 61 |
+
const historyPanel = document.querySelector('.history-panel');
|
| 62 |
+
const chatHistoryText = document.querySelector('.chat-history-text');
|
| 63 |
+
|
| 64 |
+
// Toggle the width of the history panel
|
| 65 |
+
if (historyPanel.style.width === '0px') {
|
| 66 |
+
historyPanel.style.width = '250px'; // Open the panel
|
| 67 |
+
chatHistoryText.style.display = 'block'; // Show "Chat History" text
|
| 68 |
+
} else {
|
| 69 |
+
historyPanel.style.width = '0px'; // Close the panel
|
| 70 |
+
chatHistoryText.style.display = 'none'; // Hide "Chat History" text
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
function toggleMenu() {
|
| 76 |
+
const menu = document.querySelector('.three-dot-menu .menu-options');
|
| 77 |
+
menu.classList.toggle('active');
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
// Text-to-Speech function
|
| 81 |
+
function speakText(text, language) {
|
| 82 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 83 |
+
|
| 84 |
+
// Set the correct language for speech synthesis
|
| 85 |
+
if (language === 'hi') {
|
| 86 |
+
utterance.lang = 'hi-IN'; // Hindi
|
| 87 |
} else {
|
| 88 |
+
utterance.lang = 'en-IN'; // English (India)
|
| 89 |
}
|
| 90 |
+
|
| 91 |
+
// Speak the text
|
| 92 |
+
window.speechSynthesis.speak(utterance);
|
| 93 |
+
|
| 94 |
+
// if ('speechSynthesis' in window) {
|
| 95 |
+
// const speech = new SpeechSynthesisUtterance(text);
|
| 96 |
+
// speech.lang = 'en-IN'; // Can set 'hi-IN' for Hindi response
|
| 97 |
+
// window.speechSynthesis.speak(speech);
|
| 98 |
+
// } else {
|
| 99 |
+
// alert('Text-to-Speech not supported in your browser.');
|
| 100 |
+
// }
|
| 101 |
}
|
| 102 |
|
|
|
|
| 103 |
function sendMessage() {
|
| 104 |
+
// const message = document.getElementById('userInput').value;
|
| 105 |
+
const message = userInput.value;
|
| 106 |
+
|
| 107 |
+
if (message.trim() !== "") {
|
| 108 |
+
appendMessage(message, 'user-message')
|
| 109 |
+
userInput.value = '';
|
| 110 |
+
|
| 111 |
+
// Disable the voiceInputBtn to prevent multiple submissions during processing
|
| 112 |
+
micButton.disabled = true;
|
| 113 |
+
|
| 114 |
+
// Send message to the backend for processing
|
| 115 |
+
fetch('https://thevera-backendvera.hf.space/generate_result', {
|
| 116 |
+
method: 'POST',
|
| 117 |
+
headers: {
|
| 118 |
+
'Content-Type': 'application/json',
|
| 119 |
},
|
| 120 |
body: JSON.stringify({ message: message })
|
| 121 |
})
|
| 122 |
.then(response => {
|
|
|
|
| 123 |
return response.json();
|
| 124 |
})
|
| 125 |
.then(data => {
|
|
|
|
| 126 |
const botResponse = data.response;
|
| 127 |
+
const language = data.language || 'en';
|
| 128 |
+
|
| 129 |
appendMessage(botResponse, 'bot-message');
|
| 130 |
+
|
| 131 |
// Speak the bot's response using Text-to-Speech
|
| 132 |
+
speakText(botResponse, language);
|
| 133 |
|
| 134 |
// Enable the voiceInputBtn after response
|
| 135 |
voiceInputBtn.disabled = false;
|
| 136 |
})
|
| 137 |
.catch(error => {
|
|
|
|
| 138 |
alert('Failed to connect to the backend. Please try again later.');
|
|
|
|
| 139 |
// Enable the voiceInputBtn after error
|
| 140 |
voiceInputBtn.disabled = false;
|
| 141 |
});
|
| 142 |
+
}
|
| 143 |
}
|
| 144 |
|
| 145 |
+
let isFirstMessage = true; // Flag to track if it's the first message
|
| 146 |
+
|
| 147 |
function appendMessage(text, className) {
|
| 148 |
+
// Remove the default message when the user sends the first message
|
| 149 |
+
if (isFirstMessage) {
|
| 150 |
+
const welcomeMessage = document.querySelector('.welcome-message');
|
| 151 |
+
if (welcomeMessage) {
|
| 152 |
+
welcomeMessage.remove();
|
| 153 |
+
}
|
| 154 |
+
isFirstMessage = false;
|
| 155 |
+
}
|
| 156 |
const messageElement = document.createElement('div');
|
| 157 |
+
messageElement.classList.add('user-message', className);
|
| 158 |
messageElement.textContent = text;
|
| 159 |
+
chatBody.appendChild(messageElement);
|
| 160 |
+
chatBody.scrollTop = chatBody.scrollHeight; // Auto scroll to the latest message
|
| 161 |
}
|
| 162 |
|
| 163 |
+
function activateVoice() {
|
| 164 |
+
alert('Voice input activated! This can be integrated with a voice recognition API.');
|
| 165 |
+
}
|
|
|
|
|
|
|
|
|