Spaces:
Runtime error
Runtime error
Update index.html
Browse files- index.html +66 -34
index.html
CHANGED
|
@@ -36,7 +36,6 @@
|
|
| 36 |
@keyframes spin {
|
| 37 |
to { transform: rotate(360deg); }
|
| 38 |
}
|
| 39 |
-
/* Responsive adjustments */
|
| 40 |
@media (max-width: 768px) {
|
| 41 |
.avatar svg { width: 80px; height: 80px; }
|
| 42 |
h1 { font-size: 2rem; }
|
|
@@ -57,9 +56,9 @@
|
|
| 57 |
<p class="text-center mb-4 text-gray-700">I'm here to listen, support, and share knowledge. Let's connect!</p>
|
| 58 |
|
| 59 |
<div class="flex gap-4 mb-4 flex-wrap justify-center">
|
| 60 |
-
<button id="modeBtn" onclick="toggleMode()" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Toggle
|
| 61 |
-
<button onclick="clearChat()" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Clear
|
| 62 |
-
<select id="voiceSelect" class="bg-white border rounded-lg px-3 py-2 shadow-lg" aria-label="Select
|
| 63 |
<option value="calming male">Calming Male</option>
|
| 64 |
<option value="calming female">Calming Female</option>
|
| 65 |
<option value="soothing motivating male">Soothing Motivating Male</option>
|
|
@@ -72,16 +71,16 @@
|
|
| 72 |
</select>
|
| 73 |
</div>
|
| 74 |
|
| 75 |
-
<div id="chat-box" class="w-full max-w-lg h-96 bg-white rounded-lg shadow-xl overflow-y-auto p-4 mb-4" role="log" aria-live="polite" aria-label="Chat
|
| 76 |
|
| 77 |
<div class="flex gap-2 w-full max-w-lg flex-wrap">
|
| 78 |
-
<input id="message" type="text" class="flex-grow p-3 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500" placeholder="Share what's on your mind..." aria-label="Type
|
| 79 |
-
<button onclick="sendMessage()" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Send
|
| 80 |
</div>
|
| 81 |
|
| 82 |
<div class="flex gap-2 mt-4 flex-wrap justify-center">
|
| 83 |
-
<button id="listenBtn" onclick="startListening()" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="
|
| 84 |
-
<button id="stopBtn" onclick="stopListening()" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Stop voice
|
| 85 |
</div>
|
| 86 |
|
| 87 |
<script>
|
|
@@ -96,22 +95,20 @@
|
|
| 96 |
let currentMode = "emotional_support";
|
| 97 |
|
| 98 |
const emotionColors = {
|
| 99 |
-
joy: ["bg-gradient-to-r from-yellow-200 to-yellow-300"
|
| 100 |
-
sadness: ["bg-gradient-to-r from-blue-200 to-blue-300"
|
| 101 |
-
anger: ["bg-gradient-to-r from-red-200 to-red-300"
|
| 102 |
-
calm: ["bg-gradient-to-r from-green-200 to-green-300"
|
| 103 |
-
optimism: ["bg-gradient-to-r from-orange-200 to-orange-300"
|
| 104 |
-
surprise: ["bg-gradient-to-r from-purple-200 to-purple-300"
|
| 105 |
-
neutral: ["bg-gradient-to-r from-gray-200 to-gray-300"
|
| 106 |
};
|
| 107 |
|
| 108 |
-
// Load chat history from localStorage
|
| 109 |
function loadHistory() {
|
| 110 |
const history = JSON.parse(localStorage.getItem("chatHistory") || "[]");
|
| 111 |
history.forEach(msg => appendMessage(msg.sender, msg.text, false));
|
| 112 |
}
|
| 113 |
|
| 114 |
-
// Save chat history to localStorage
|
| 115 |
function saveHistory() {
|
| 116 |
const messages = Array.from(chatBox.children).map(div => ({
|
| 117 |
sender: div.querySelector("strong").textContent.replace(":", ""),
|
|
@@ -124,10 +121,7 @@
|
|
| 124 |
const message = messageInput.value.trim();
|
| 125 |
if (!message) return;
|
| 126 |
|
| 127 |
-
|
| 128 |
-
if (!confirm("Send this voice message?")) return;
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
appendMessage("You", message);
|
| 132 |
messageInput.value = "";
|
| 133 |
showLoading(true);
|
|
@@ -138,13 +132,16 @@
|
|
| 138 |
headers: { "Content-Type": "application/json" },
|
| 139 |
body: JSON.stringify({ message, mode: currentMode, voice_tone: voiceSelect.value }),
|
| 140 |
});
|
|
|
|
| 141 |
const data = await res.json();
|
|
|
|
| 142 |
appendMessage("Assistant", data.reply);
|
| 143 |
updateBackground(data.emotion);
|
| 144 |
speak(data.reply);
|
| 145 |
saveHistory();
|
| 146 |
} catch (error) {
|
| 147 |
-
|
|
|
|
| 148 |
} finally {
|
| 149 |
showLoading(false);
|
| 150 |
}
|
|
@@ -161,29 +158,25 @@
|
|
| 161 |
|
| 162 |
function updateBackground(emotion) {
|
| 163 |
const colors = emotionColors[emotion.toLowerCase()] || emotionColors["neutral"];
|
| 164 |
-
|
| 165 |
-
document.body.className = `transition-all min-h-screen flex flex-col items-center justify-center p-4 ${randomColor}`;
|
| 166 |
}
|
| 167 |
|
| 168 |
function speak(text) {
|
| 169 |
if ("speechSynthesis" in window) {
|
| 170 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 171 |
const voices = speechSynthesis.getVoices();
|
| 172 |
-
// Map voice tone to available voices (basic mapping; adjust based on browser)
|
| 173 |
let selectedVoice = voices.find(v => v.name.toLowerCase().includes(voiceSelect.value.split(' ')[0]));
|
| 174 |
-
if (!selectedVoice) selectedVoice = voices[0];
|
| 175 |
utterance.voice = selectedVoice;
|
| 176 |
-
utterance.rate = 0.8;
|
| 177 |
utterance.pitch = 1;
|
| 178 |
speechSynthesis.speak(utterance);
|
| 179 |
-
} else {
|
| 180 |
-
alert("Speech synthesis not supported in your browser.");
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
function startListening() {
|
| 185 |
if (!("webkitSpeechRecognition" in window)) {
|
| 186 |
-
alert("
|
| 187 |
return;
|
| 188 |
}
|
| 189 |
recognition = new webkitSpeechRecognition();
|
|
@@ -200,9 +193,8 @@
|
|
| 200 |
|
| 201 |
recognition.onresult = (event) => {
|
| 202 |
const transcript = event.results[0][0].transcript;
|
| 203 |
-
appendMessage("You (voice)", transcript);
|
| 204 |
messageInput.value = transcript;
|
| 205 |
-
sendMessage(true);
|
| 206 |
};
|
| 207 |
|
| 208 |
recognition.onend = () => {
|
|
@@ -214,4 +206,44 @@
|
|
| 214 |
recognition.start();
|
| 215 |
}
|
| 216 |
|
| 217 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@keyframes spin {
|
| 37 |
to { transform: rotate(360deg); }
|
| 38 |
}
|
|
|
|
| 39 |
@media (max-width: 768px) {
|
| 40 |
.avatar svg { width: 80px; height: 80px; }
|
| 41 |
h1 { font-size: 2rem; }
|
|
|
|
| 56 |
<p class="text-center mb-4 text-gray-700">I'm here to listen, support, and share knowledge. Let's connect!</p>
|
| 57 |
|
| 58 |
<div class="flex gap-4 mb-4 flex-wrap justify-center">
|
| 59 |
+
<button id="modeBtn" onclick="toggleMode()" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Toggle modes">Mode: Emotional Support</button>
|
| 60 |
+
<button onclick="clearChat()" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Clear chat">Clear Chat</button>
|
| 61 |
+
<select id="voiceSelect" class="bg-white border rounded-lg px-3 py-2 shadow-lg" aria-label="Select voice">
|
| 62 |
<option value="calming male">Calming Male</option>
|
| 63 |
<option value="calming female">Calming Female</option>
|
| 64 |
<option value="soothing motivating male">Soothing Motivating Male</option>
|
|
|
|
| 71 |
</select>
|
| 72 |
</div>
|
| 73 |
|
| 74 |
+
<div id="chat-box" class="w-full max-w-lg h-96 bg-white rounded-lg shadow-xl overflow-y-auto p-4 mb-4" role="log" aria-live="polite" aria-label="Chat"></div>
|
| 75 |
|
| 76 |
<div class="flex gap-2 w-full max-w-lg flex-wrap">
|
| 77 |
+
<input id="message" type="text" class="flex-grow p-3 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500" placeholder="Share what's on your mind..." aria-label="Type message" />
|
| 78 |
+
<button onclick="sendMessage()" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Send">Send</button>
|
| 79 |
</div>
|
| 80 |
|
| 81 |
<div class="flex gap-2 mt-4 flex-wrap justify-center">
|
| 82 |
+
<button id="listenBtn" onclick="startListening()" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Voice input" disabled>🎙️ Listen</button>
|
| 83 |
+
<button id="stopBtn" onclick="stopListening()" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow-lg transition" aria-label="Stop voice" disabled>⏹️ Stop</button>
|
| 84 |
</div>
|
| 85 |
|
| 86 |
<script>
|
|
|
|
| 95 |
let currentMode = "emotional_support";
|
| 96 |
|
| 97 |
const emotionColors = {
|
| 98 |
+
joy: ["bg-gradient-to-r from-yellow-200 to-yellow-300"],
|
| 99 |
+
sadness: ["bg-gradient-to-r from-blue-200 to-blue-300"],
|
| 100 |
+
anger: ["bg-gradient-to-r from-red-200 to-red-300"],
|
| 101 |
+
calm: ["bg-gradient-to-r from-green-200 to-green-300"],
|
| 102 |
+
optimism: ["bg-gradient-to-r from-orange-200 to-orange-300"],
|
| 103 |
+
surprise: ["bg-gradient-to-r from-purple-200 to-purple-300"],
|
| 104 |
+
neutral: ["bg-gradient-to-r from-gray-200 to-gray-300"]
|
| 105 |
};
|
| 106 |
|
|
|
|
| 107 |
function loadHistory() {
|
| 108 |
const history = JSON.parse(localStorage.getItem("chatHistory") || "[]");
|
| 109 |
history.forEach(msg => appendMessage(msg.sender, msg.text, false));
|
| 110 |
}
|
| 111 |
|
|
|
|
| 112 |
function saveHistory() {
|
| 113 |
const messages = Array.from(chatBox.children).map(div => ({
|
| 114 |
sender: div.querySelector("strong").textContent.replace(":", ""),
|
|
|
|
| 121 |
const message = messageInput.value.trim();
|
| 122 |
if (!message) return;
|
| 123 |
|
| 124 |
+
console.log("Sending message:", message);
|
|
|
|
|
|
|
|
|
|
| 125 |
appendMessage("You", message);
|
| 126 |
messageInput.value = "";
|
| 127 |
showLoading(true);
|
|
|
|
| 132 |
headers: { "Content-Type": "application/json" },
|
| 133 |
body: JSON.stringify({ message, mode: currentMode, voice_tone: voiceSelect.value }),
|
| 134 |
});
|
| 135 |
+
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
| 136 |
const data = await res.json();
|
| 137 |
+
console.log("Received response:", data);
|
| 138 |
appendMessage("Assistant", data.reply);
|
| 139 |
updateBackground(data.emotion);
|
| 140 |
speak(data.reply);
|
| 141 |
saveHistory();
|
| 142 |
} catch (error) {
|
| 143 |
+
console.error("Fetch error:", error);
|
| 144 |
+
appendMessage("Assistant", "Something went wrong. Check your API key or try again.");
|
| 145 |
} finally {
|
| 146 |
showLoading(false);
|
| 147 |
}
|
|
|
|
| 158 |
|
| 159 |
function updateBackground(emotion) {
|
| 160 |
const colors = emotionColors[emotion.toLowerCase()] || emotionColors["neutral"];
|
| 161 |
+
document.body.className = `transition-all min-h-screen flex flex-col items-center justify-center p-4 ${colors[0]}`;
|
|
|
|
| 162 |
}
|
| 163 |
|
| 164 |
function speak(text) {
|
| 165 |
if ("speechSynthesis" in window) {
|
| 166 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 167 |
const voices = speechSynthesis.getVoices();
|
|
|
|
| 168 |
let selectedVoice = voices.find(v => v.name.toLowerCase().includes(voiceSelect.value.split(' ')[0]));
|
| 169 |
+
if (!selectedVoice) selectedVoice = voices[0];
|
| 170 |
utterance.voice = selectedVoice;
|
| 171 |
+
utterance.rate = 0.8;
|
| 172 |
utterance.pitch = 1;
|
| 173 |
speechSynthesis.speak(utterance);
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
}
|
| 176 |
|
| 177 |
function startListening() {
|
| 178 |
if (!("webkitSpeechRecognition" in window)) {
|
| 179 |
+
alert("Voice not supported.");
|
| 180 |
return;
|
| 181 |
}
|
| 182 |
recognition = new webkitSpeechRecognition();
|
|
|
|
| 193 |
|
| 194 |
recognition.onresult = (event) => {
|
| 195 |
const transcript = event.results[0][0].transcript;
|
|
|
|
| 196 |
messageInput.value = transcript;
|
| 197 |
+
sendMessage(true);
|
| 198 |
};
|
| 199 |
|
| 200 |
recognition.onend = () => {
|
|
|
|
| 206 |
recognition.start();
|
| 207 |
}
|
| 208 |
|
| 209 |
+
function stopListening() {
|
| 210 |
+
if (recognition && listening) {
|
| 211 |
+
recognition.stop();
|
| 212 |
+
listening = false;
|
| 213 |
+
listenBtn.disabled = false;
|
| 214 |
+
stopBtn.disabled = true;
|
| 215 |
+
appendMessage("System", "Stopped.");
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
function toggleMode() {
|
| 220 |
+
currentMode = currentMode === "emotional_support" ? "knowledge" : "emotional_support";
|
| 221 |
+
modeBtn.textContent = `Mode: ${currentMode === "emotional_support" ? "Emotional Support" : "Knowledge"}`;
|
| 222 |
+
console.log("Mode switched to:", currentMode);
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
function clearChat() {
|
| 226 |
+
chatBox.innerHTML = "";
|
| 227 |
+
localStorage.removeItem("chatHistory");
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
function showLoading(show) {
|
| 231 |
+
const btn = document.querySelector("button[onclick='sendMessage()']");
|
| 232 |
+
if (show) {
|
| 233 |
+
btn.innerHTML = '<div class="loading"></div>';
|
| 234 |
+
btn.disabled = true;
|
| 235 |
+
} else {
|
| 236 |
+
btn.innerHTML = "Send";
|
| 237 |
+
btn.disabled = false;
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
messageInput.addEventListener('keypress', function(e) {
|
| 242 |
+
if (e.key === 'Enter') sendMessage();
|
| 243 |
+
});
|
| 244 |
+
|
| 245 |
+
loadHistory();
|
| 246 |
+
appendMessage("Assistant", "Hi! How are you?");
|
| 247 |
+
</script>
|
| 248 |
+
</body>
|
| 249 |
+
</html>
|