Spaces:
Runtime error
Runtime error
Update it.
Browse files- index.html +171 -149
index.html
CHANGED
|
@@ -1,163 +1,185 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
-
<meta charset="UTF-8"
|
| 5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0"
|
| 6 |
-
<title
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<style>
|
| 9 |
-
body {
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
</style>
|
| 18 |
</head>
|
| 19 |
-
<body class="
|
| 20 |
-
<
|
| 21 |
-
<h1 class="text-2xl font-bold text-center mb-4 text-gray-700">π Emotional Support Assistant</h1>
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
<button id="stop-btn" class="bg-red-500 text-white px-3 py-2 rounded hover:bg-red-600">βΉοΈ Stop</button>
|
| 33 |
-
</div>
|
| 34 |
</div>
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
const
|
| 38 |
-
const
|
| 39 |
-
const
|
| 40 |
-
const
|
| 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 |
</body>
|
| 163 |
-
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>π Emotional Support Assistant</title>
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<style>
|
| 9 |
+
body { transition: background 1s ease; }
|
| 10 |
+
#mic-indicator {
|
| 11 |
+
width: 20px; height: 20px; border-radius: 50%;
|
| 12 |
+
margin-left: 10px; display: inline-block;
|
| 13 |
+
background-color: gray;
|
| 14 |
+
box-shadow: 0 0 5px rgba(0,0,0,0.2);
|
| 15 |
+
transition: background-color 0.3s ease, box-shadow 0.3s ease;
|
| 16 |
}
|
| 17 |
+
.listening {
|
| 18 |
+
background-color: #22c55e;
|
| 19 |
+
box-shadow: 0 0 15px #22c55e;
|
| 20 |
+
}
|
| 21 |
+
.speaking {
|
| 22 |
+
animation: pulse 1s infinite;
|
| 23 |
+
}
|
| 24 |
+
@keyframes pulse {
|
| 25 |
+
0% { opacity: 0.3; }
|
| 26 |
+
50% { opacity: 1; }
|
| 27 |
+
100% { opacity: 0.3; }
|
| 28 |
}
|
| 29 |
</style>
|
| 30 |
</head>
|
| 31 |
+
<body class="min-h-screen flex flex-col items-center justify-center bg-gradient-to-br from-yellow-100 to-yellow-300 p-4">
|
| 32 |
+
<h1 class="text-2xl font-bold mb-4 text-center">π Emotional Support Assistant</h1>
|
|
|
|
| 33 |
|
| 34 |
+
<div id="chat-box" class="w-full max-w-2xl h-96 overflow-y-auto bg-white p-4 rounded shadow mb-4 border"></div>
|
| 35 |
|
| 36 |
+
<div class="flex gap-2 w-full max-w-2xl">
|
| 37 |
+
<input id="user-input" type="text" placeholder="Type your message..."
|
| 38 |
+
class="flex-1 border p-2 rounded" />
|
| 39 |
+
<button id="send-btn" class="bg-blue-500 text-white px-4 py-2 rounded">Send</button>
|
| 40 |
+
</div>
|
| 41 |
+
|
| 42 |
+
<div class="mt-4 flex items-center gap-3">
|
| 43 |
+
<button id="start-btn" class="bg-green-500 text-white px-4 py-2 rounded">ποΈ Start Listening</button>
|
| 44 |
+
<button id="stop-btn" class="bg-red-500 text-white px-4 py-2 rounded">βΉοΈ Stop</button>
|
| 45 |
+
<div id="mic-indicator" title="Mic status"></div>
|
| 46 |
+
</div>
|
| 47 |
|
| 48 |
+
<div id="speaking-status" class="text-gray-600 mt-3 hidden">
|
| 49 |
+
π€ Bot is speaking...
|
|
|
|
|
|
|
| 50 |
</div>
|
| 51 |
+
|
| 52 |
+
<script>
|
| 53 |
+
const chatBox = document.getElementById("chat-box");
|
| 54 |
+
const userInput = document.getElementById("user-input");
|
| 55 |
+
const sendBtn = document.getElementById("send-btn");
|
| 56 |
+
const startBtn = document.getElementById("start-btn");
|
| 57 |
+
const stopBtn = document.getElementById("stop-btn");
|
| 58 |
+
const micIndicator = document.getElementById("mic-indicator");
|
| 59 |
+
const speakingStatus = document.getElementById("speaking-status");
|
| 60 |
+
|
| 61 |
+
let recognition;
|
| 62 |
+
let isListening = false;
|
| 63 |
+
let synth = window.speechSynthesis;
|
| 64 |
+
let currentUtterance = null;
|
| 65 |
+
|
| 66 |
+
// π€ Initialize SpeechRecognition
|
| 67 |
+
if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
|
| 68 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 69 |
+
recognition = new SpeechRecognition();
|
| 70 |
+
recognition.continuous = true;
|
| 71 |
+
recognition.interimResults = false;
|
| 72 |
+
recognition.lang = "en-US";
|
| 73 |
+
|
| 74 |
+
recognition.onstart = () => {
|
| 75 |
+
micIndicator.classList.add("listening");
|
| 76 |
+
isListening = true;
|
| 77 |
+
startBtn.disabled = true;
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
recognition.onend = () => {
|
| 81 |
+
micIndicator.classList.remove("listening");
|
| 82 |
+
isListening = false;
|
| 83 |
+
startBtn.disabled = false;
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
recognition.onresult = (event) => {
|
| 87 |
+
const text = event.results[event.results.length - 1][0].transcript.trim();
|
| 88 |
+
userInput.value = text;
|
| 89 |
+
sendMessage();
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
recognition.onerror = (event) => {
|
| 93 |
+
console.error("Speech recognition error:", event.error);
|
| 94 |
+
micIndicator.classList.remove("listening");
|
| 95 |
+
isListening = false;
|
| 96 |
+
startBtn.disabled = false;
|
| 97 |
+
};
|
| 98 |
+
} else {
|
| 99 |
+
alert("Speech recognition not supported in this browser. Try using Chrome.");
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// π¬ Send user message to Flask backend
|
| 103 |
+
async function sendMessage() {
|
| 104 |
+
const text = userInput.value.trim();
|
| 105 |
+
if (!text) return;
|
| 106 |
+
addMessage("π§ You", text);
|
| 107 |
+
userInput.value = "";
|
| 108 |
+
|
| 109 |
+
try {
|
| 110 |
+
const res = await fetch("/chat", {
|
| 111 |
+
method: "POST",
|
| 112 |
+
headers: { "Content-Type": "application/json" },
|
| 113 |
+
body: JSON.stringify({ message: text }),
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
const data = await res.json();
|
| 117 |
+
addMessage("π€ Bot", data.text);
|
| 118 |
+
speakText(data.text);
|
| 119 |
+
updateBackground(data.emotion);
|
| 120 |
+
} catch (err) {
|
| 121 |
+
console.error("Error:", err);
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// π£οΈ Speak text aloud and show animation
|
| 126 |
+
function speakText(text) {
|
| 127 |
+
if (synth.speaking) synth.cancel();
|
| 128 |
+
|
| 129 |
+
currentUtterance = new SpeechSynthesisUtterance(text);
|
| 130 |
+
currentUtterance.lang = "en-US";
|
| 131 |
+
|
| 132 |
+
speakingStatus.classList.remove("hidden");
|
| 133 |
+
speakingStatus.classList.add("speaking");
|
| 134 |
+
|
| 135 |
+
currentUtterance.onend = () => {
|
| 136 |
+
speakingStatus.classList.add("hidden");
|
| 137 |
+
speakingStatus.classList.remove("speaking");
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
synth.speak(currentUtterance);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// π§ Button handlers
|
| 144 |
+
startBtn.onclick = () => {
|
| 145 |
+
if (recognition && !isListening) recognition.start();
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
stopBtn.onclick = () => {
|
| 149 |
+
if (isListening && recognition) recognition.stop();
|
| 150 |
+
if (synth.speaking) synth.cancel();
|
| 151 |
+
isListening = false;
|
| 152 |
+
micIndicator.classList.remove("listening");
|
| 153 |
+
speakingStatus.classList.add("hidden");
|
| 154 |
+
startBtn.disabled = false;
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
sendBtn.onclick = sendMessage;
|
| 158 |
+
|
| 159 |
+
// π¬ Display messages in chat
|
| 160 |
+
function addMessage(sender, text) {
|
| 161 |
+
const bubble = document.createElement("div");
|
| 162 |
+
bubble.className = sender.includes("Bot")
|
| 163 |
+
? "text-left mb-2 bg-yellow-100 p-2 rounded"
|
| 164 |
+
: "text-right mb-2 bg-blue-100 p-2 rounded";
|
| 165 |
+
bubble.innerHTML = `<strong>${sender}:</strong> ${text}`;
|
| 166 |
+
chatBox.appendChild(bubble);
|
| 167 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// π¨ Change background smoothly by emotion
|
| 171 |
+
function updateBackground(emotion) {
|
| 172 |
+
let color;
|
| 173 |
+
switch (emotion?.toLowerCase()) {
|
| 174 |
+
case "happy": color = "linear-gradient(135deg, #fff176, #ffd54f)"; break;
|
| 175 |
+
case "sad": color = "linear-gradient(135deg, #64b5f6, #1976d2)"; break;
|
| 176 |
+
case "angry": color = "linear-gradient(135deg, #ff7043, #f44336)"; break;
|
| 177 |
+
case "calm": color = "linear-gradient(135deg, #a5d6a7, #66bb6a)"; break;
|
| 178 |
+
case "motivated": color = "linear-gradient(135deg, #ffb74d, #fb8c00)"; break;
|
| 179 |
+
default: color = "linear-gradient(135deg, #e0e0e0, #bdbdbd)";
|
| 180 |
+
}
|
| 181 |
+
document.body.style.background = color;
|
| 182 |
+
}
|
| 183 |
+
</script>
|
| 184 |
</body>
|
| 185 |
+
</html>
|