Spaces:
Runtime error
Runtime error
File size: 8,639 Bytes
b7453ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
<script>
let isRecording = false;
let recognition;
let responsesHistory = {}; // Holds all responses per prompt
let currentIndex = {}; // Track current response index
function initSpeechRecognition() {
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.onresult = function(event) {
const transcript = event.results[0][0].transcript;
sendMessageFromVoice(transcript);
};
recognition.onerror = function(event) {
console.log("Speech recognition error:", event.error);
};
}
const micIcon = document.getElementById('mic-icon');
micIcon.addEventListener('mousedown', function() {
isRecording = true;
micIcon.textContent = 'mic_off';
recognition.start();
});
micIcon.addEventListener('mouseup', function() {
isRecording = false;
micIcon.textContent = 'mic';
recognition.stop();
});
function appendMessage(text, className) {
let chatbox = document.getElementById('chatbox');
let messageDiv = document.createElement('div');
messageDiv.className = 'message ' + className;
messageDiv.innerHTML = text;
chatbox.appendChild(messageDiv);
chatbox.scrollTop = chatbox.scrollHeight;
}
function sendMessageFromVoice(message) {
appendMessage(message, 'user');
fetchMessageFromAI(message);
}
function sendMessage() {
let userInput = document.getElementById('user-input');
let message = userInput.value.trim();
if (message === '') return;
appendMessage(message, 'user');
userInput.value = '';
fetchMessageFromAI(message);
}
function fetchMessageFromAI(message) {
document.getElementById('typing-indicator').style.display = 'flex';
fetch('/message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: message })
})
.then(response => {
if (!response.ok) {
throw new Error(`Server error: ${response.statusText}`);
}
return response.json();
})
.then(data => {
document.getElementById('typing-indicator').style.display = 'none';
if (data.response) {
addAIResponse(data.response, message);
} else {
console.error("No response from the server.");
}
})
.catch(error => {
document.getElementById('typing-indicator').style.display = 'none';
console.error('Error:', error);
appendMessage("An error occurred. Please try again later.", 'ai');
});
}
function addAIResponse(responseText, userPrompt) {
const responseId = Date.now();
responsesHistory[responseId] = [responseText];
currentIndex[responseId] = 0;
renderAIResponse(responseText, responseId, userPrompt);
}
function renderAIResponse(responseText, responseId, userPrompt) {
let chatbox = document.getElementById('chatbox');
let messageDiv = document.createElement('div');
messageDiv.className = 'message ai';
messageDiv.dataset.responseId = responseId;
messageDiv.dataset.userPrompt = userPrompt; // Store the prompt
let responseTextDiv = document.createElement('div');
responseTextDiv.className = 'response-text';
responseTextDiv.innerHTML = responseText;
messageDiv.appendChild(responseTextDiv);
let iconsDiv = document.createElement('div');
iconsDiv.className = 'icons';
let speakerIcon = document.createElement('span');
speakerIcon.className = 'material-icons';
speakerIcon.innerText = 'volume_up';
speakerIcon.onclick = () => speakText(responseId);
let copyIcon = document.createElement('span');
copyIcon.className = 'material-icons';
copyIcon.innerText = 'content_copy';
copyIcon.onclick = () => copyResponse(responseId);
let regenerateIcon = document.createElement('span');
regenerateIcon.className = 'material-icons';
regenerateIcon.innerText = 'replay';
regenerateIcon.onclick = () => regenerateResponse(responseId, responseTextDiv, iconsDiv);
iconsDiv.appendChild(speakerIcon);
iconsDiv.appendChild(copyIcon);
iconsDiv.appendChild(regenerateIcon);
messageDiv.appendChild(iconsDiv);
chatbox.appendChild(messageDiv);
chatbox.scrollTop = chatbox.scrollHeight;
}
function regenerateResponse(responseId, responseTextDiv, iconsDiv) {
responseTextDiv.innerHTML = 'Generating new response...';
// Get the original prompt from the dataset
const messageDiv = document.querySelector(`[data-response-id="${responseId}"]`);
const originalPrompt = messageDiv.dataset.userPrompt;
fetch('/message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: originalPrompt }) // Use the original prompt
})
.then(response => response.json())
.then(data => {
if (data.response) {
responsesHistory[responseId].push(data.response);
currentIndex[responseId] = responsesHistory[responseId].length - 1;
displayUpdatedResponse(responseId, data.response, iconsDiv);
} else {
responseTextDiv.innerHTML = 'Error generating response';
}
})
.catch(error => {
console.error('Error:', error);
responseTextDiv.innerHTML = 'Error generating response';
});
}
function displayUpdatedResponse(responseId, newResponse, iconsDiv) {
let messageDiv = document.querySelector(`[data-response-id="${responseId}"]`);
let responseTextDiv = messageDiv.querySelector('.response-text');
responseTextDiv.innerHTML = newResponse;
// Remove existing navigation if present
const existingNav = messageDiv.querySelector('.navigation');
if (existingNav) {
existingNav.remove();
}
let navigationDiv = document.createElement('div');
navigationDiv.className = 'navigation';
iconsDiv.appendChild(navigationDiv);
let backIcon = document.createElement('span');
backIcon.className = 'material-icons';
backIcon.innerText = 'arrow_back';
backIcon.onclick = () => navigateResponse(responseId, -1, responseTextDiv, navigationDiv);
let nextIcon = document.createElement('span');
nextIcon.className = 'material-icons';
nextIcon.innerText = 'arrow_forward';
nextIcon.onclick = () => navigateResponse(responseId, 1, responseTextDiv, navigationDiv);
let responseIndexText = document.createElement('span');
responseIndexText.className = 'response-index';
navigationDiv.appendChild(backIcon);
navigationDiv.appendChild(responseIndexText);
navigationDiv.appendChild(nextIcon);
updateResponseIndex(responseId, responseIndexText);
}
function navigateResponse(responseId, direction, responseTextDiv, navigationDiv) {
currentIndex[responseId] += direction;
currentIndex[responseId] = Math.max(0, Math.min(currentIndex[responseId], responsesHistory[responseId].length - 1));
responseTextDiv.innerHTML = responsesHistory[responseId][currentIndex[responseId]];
updateResponseIndex(responseId, navigationDiv.querySelector('.response-index'));
}
function updateResponseIndex(responseId, responseIndexText) {
responseIndexText.innerText = `${currentIndex[responseId] + 1}/${responsesHistory[responseId].length}`;
}
// Updated function to speak the current response
function speakText(responseId) {
const text = responsesHistory[responseId][currentIndex[responseId]];
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
}
// Updated function to copy the current response
function copyResponse(responseId) {
const text = responsesHistory[responseId][currentIndex[responseId]];
navigator.clipboard.writeText(text);
alert("Copied: " + text);
}
document.addEventListener('DOMContentLoaded', initSpeechRecognition); |