Spaces:
Sleeping
Sleeping
Create index.html
Browse files- index.html +119 -0
index.html
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 {
|
| 10 |
+
transition: background 1s ease, color 1s ease;
|
| 11 |
+
}
|
| 12 |
+
</style>
|
| 13 |
+
</head>
|
| 14 |
+
<body class="bg-yellow-100 text-gray-800 flex flex-col items-center justify-center min-h-screen p-4">
|
| 15 |
+
<h1 class="text-3xl font-bold mb-6 text-center">💛 Emotional Support Assistant</h1>
|
| 16 |
+
<div id="chat-box" class="w-full max-w-md h-96 bg-white rounded shadow overflow-y-auto p-4 mb-4"></div>
|
| 17 |
+
|
| 18 |
+
<div class="flex gap-2 w-full max-w-md">
|
| 19 |
+
<input id="message" type="text" class="flex-grow p-2 border rounded" placeholder="Type your message..." />
|
| 20 |
+
<button onclick="sendMessage()" class="bg-blue-500 text-white px-4 py-2 rounded">Send</button>
|
| 21 |
+
</div>
|
| 22 |
+
|
| 23 |
+
<div class="flex gap-2 mt-4">
|
| 24 |
+
<button id="listenBtn" onclick="startListening()" class="bg-green-500 text-white px-4 py-2 rounded">🎙️ Start Listening</button>
|
| 25 |
+
<button id="stopBtn" onclick="stopListening()" class="bg-red-500 text-white px-4 py-2 rounded">⏹️ Stop</button>
|
| 26 |
+
</div>
|
| 27 |
+
|
| 28 |
+
<script>
|
| 29 |
+
const chatBox = document.getElementById("chat-box");
|
| 30 |
+
const messageInput = document.getElementById("message");
|
| 31 |
+
let recognition;
|
| 32 |
+
let listening = false;
|
| 33 |
+
|
| 34 |
+
const emotionColors = {
|
| 35 |
+
joy: "bg-gradient-to-r from-yellow-200 to-yellow-400",
|
| 36 |
+
sadness: "bg-blue-200",
|
| 37 |
+
anger: "bg-red-300",
|
| 38 |
+
calm: "bg-green-200",
|
| 39 |
+
optimism: "bg-orange-300",
|
| 40 |
+
surprise: "bg-purple-200",
|
| 41 |
+
neutral: "bg-gray-100"
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
async function sendMessage() {
|
| 45 |
+
const message = messageInput.value.trim();
|
| 46 |
+
if (!message) return;
|
| 47 |
+
appendMessage("You", message);
|
| 48 |
+
messageInput.value = "";
|
| 49 |
+
|
| 50 |
+
const res = await fetch("/chat", {
|
| 51 |
+
method: "POST",
|
| 52 |
+
headers: { "Content-Type": "application/json" },
|
| 53 |
+
body: JSON.stringify({ message }),
|
| 54 |
+
});
|
| 55 |
+
const data = await res.json();
|
| 56 |
+
appendMessage("Assistant", data.reply);
|
| 57 |
+
|
| 58 |
+
const emotion = data.emotion.toLowerCase();
|
| 59 |
+
document.body.className = "transition-all min-h-screen flex flex-col items-center justify-center p-4 " + (emotionColors[emotion] || "bg-yellow-100");
|
| 60 |
+
|
| 61 |
+
speak(data.reply);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function appendMessage(sender, text) {
|
| 65 |
+
const div = document.createElement("div");
|
| 66 |
+
div.className = sender === "You" ? "text-right mb-2" : "text-left mb-2";
|
| 67 |
+
div.innerHTML = `<strong>${sender}:</strong> ${text}`;
|
| 68 |
+
chatBox.appendChild(div);
|
| 69 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
function speak(text) {
|
| 73 |
+
const speech = new SpeechSynthesisUtterance(text);
|
| 74 |
+
speech.rate = 1;
|
| 75 |
+
speech.pitch = 1;
|
| 76 |
+
speech.lang = "en-US";
|
| 77 |
+
window.speechSynthesis.speak(speech);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function startListening() {
|
| 81 |
+
if (!("webkitSpeechRecognition" in window)) {
|
| 82 |
+
alert("Sorry, your browser doesn't support speech recognition.");
|
| 83 |
+
return;
|
| 84 |
+
}
|
| 85 |
+
recognition = new webkitSpeechRecognition();
|
| 86 |
+
recognition.continuous = false;
|
| 87 |
+
recognition.interimResults = false;
|
| 88 |
+
recognition.lang = "en-US";
|
| 89 |
+
|
| 90 |
+
recognition.onstart = () => {
|
| 91 |
+
listening = true;
|
| 92 |
+
appendMessage("System", "Listening...");
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
recognition.onresult = (event) => {
|
| 96 |
+
const transcript = event.results[0][0].transcript;
|
| 97 |
+
appendMessage("You (voice)", transcript);
|
| 98 |
+
messageInput.value = transcript;
|
| 99 |
+
sendMessage();
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
recognition.onend = () => {
|
| 103 |
+
listening = false;
|
| 104 |
+
};
|
| 105 |
+
|
| 106 |
+
recognition.start();
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
function stopListening() {
|
| 110 |
+
if (recognition && listening) {
|
| 111 |
+
recognition.stop();
|
| 112 |
+
listening = false;
|
| 113 |
+
appendMessage("System", "Stopped listening.");
|
| 114 |
+
window.speechSynthesis.cancel(); // stop any ongoing speech
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
</script>
|
| 118 |
+
</body>
|
| 119 |
+
</html>aria-valuenow
|