Spaces:
Runtime error
Runtime error
Update it completely.
Browse filesMake sure it runs greatly.
- index.html +123 -101
index.html
CHANGED
|
@@ -32,110 +32,132 @@
|
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
// π€ Voice recognition setup
|
| 42 |
-
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 43 |
-
const recognition = new SpeechRecognition();
|
| 44 |
-
recognition.continuous = false;
|
| 45 |
-
recognition.lang = "en-US";
|
| 46 |
-
|
| 47 |
-
document.getElementById("start-btn").onclick = () => {
|
| 48 |
-
recognition.start();
|
| 49 |
-
};
|
| 50 |
-
document.getElementById("stop-btn").onclick = () => {
|
| 51 |
-
recognition.stop();
|
| 52 |
-
};
|
| 53 |
-
|
| 54 |
-
recognition.onresult = (event) => {
|
| 55 |
-
const transcript = event.results[0][0].transcript;
|
| 56 |
-
messageInput.value = transcript;
|
| 57 |
-
sendMessage();
|
| 58 |
-
};
|
| 59 |
-
|
| 60 |
-
// π¬ Send message function
|
| 61 |
-
sendBtn.onclick = sendMessage;
|
| 62 |
-
messageInput.addEventListener("keypress", (e) => {
|
| 63 |
-
if (e.key === "Enter") sendMessage();
|
| 64 |
-
});
|
| 65 |
-
|
| 66 |
-
async function sendMessage() {
|
| 67 |
-
const message = messageInput.value.trim();
|
| 68 |
-
if (!message) return;
|
| 69 |
-
|
| 70 |
-
appendMessage("You", message, "text-right text-blue-600");
|
| 71 |
-
messageInput.value = "";
|
| 72 |
-
|
| 73 |
-
try {
|
| 74 |
-
const res = await fetch("/chat", {
|
| 75 |
-
method: "POST",
|
| 76 |
-
headers: { "Content-Type": "application/json" },
|
| 77 |
-
body: JSON.stringify({
|
| 78 |
-
message,
|
| 79 |
-
name: "User",
|
| 80 |
-
age: "25"
|
| 81 |
-
}),
|
| 82 |
-
});
|
| 83 |
-
const data = await res.json();
|
| 84 |
-
appendMessage("Bot", data.response, "text-left text-gray-800");
|
| 85 |
-
|
| 86 |
-
changeBackground(data.emotion);
|
| 87 |
-
speakText(data.response);
|
| 88 |
-
} catch (err) {
|
| 89 |
-
appendMessage("Bot", "Oops, something went wrong. Please try again.", "text-red-600");
|
| 90 |
-
console.error(err);
|
| 91 |
-
}
|
| 92 |
-
}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
function appendMessage(sender, text, style) {
|
| 96 |
-
const msg = document.createElement("div");
|
| 97 |
-
msg.className = `p-2 rounded-lg ${style}`;
|
| 98 |
-
msg.innerHTML = `<strong>${sender}:</strong> ${text}`;
|
| 99 |
-
chatBox.appendChild(msg);
|
| 100 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
| 101 |
-
}
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
case "joy":
|
| 108 |
-
gradient = "from-yellow-200 to-yellow-400";
|
| 109 |
-
break;
|
| 110 |
-
case "sadness":
|
| 111 |
-
gradient = "from-blue-200 to-blue-400";
|
| 112 |
-
break;
|
| 113 |
-
case "anger":
|
| 114 |
-
gradient = "from-red-400 to-orange-500";
|
| 115 |
-
break;
|
| 116 |
-
case "neutral":
|
| 117 |
-
case "calm":
|
| 118 |
-
gradient = "from-green-100 to-green-300";
|
| 119 |
-
break;
|
| 120 |
-
case "motivated":
|
| 121 |
-
gradient = "from-orange-300 to-orange-500";
|
| 122 |
-
break;
|
| 123 |
-
default:
|
| 124 |
-
gradient = "from-gray-100 to-gray-300";
|
| 125 |
-
}
|
| 126 |
-
document.body.className = `bg-gradient-to-br ${gradient} min-h-screen flex flex-col items-center justify-center p-4 transition-all`;
|
| 127 |
-
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
</body>
|
| 141 |
</html>accesskey
|
|
|
|
| 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 |
+
<script>
|
| 36 |
+
const chatBox = document.createElement("div");
|
| 37 |
+
const input = document.createElement("input");
|
| 38 |
+
const sendBtn = document.createElement("button");
|
| 39 |
+
const startBtn = document.createElement("button");
|
| 40 |
+
const stopBtn = document.createElement("button");
|
| 41 |
|
| 42 |
+
chatBox.id = "chat-box";
|
| 43 |
+
input.placeholder = "Type your message...";
|
| 44 |
+
sendBtn.textContent = "Send";
|
| 45 |
+
startBtn.textContent = "ποΈ Start Listening";
|
| 46 |
+
stopBtn.textContent = "βΉοΈ Stop";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
document.body.append(chatBox, input, sendBtn, startBtn, stopBtn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
let recognition;
|
| 51 |
+
let isListening = false;
|
| 52 |
+
let synth = window.speechSynthesis;
|
| 53 |
+
let currentUtterance = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
// β
Initialize SpeechRecognition
|
| 56 |
+
if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
|
| 57 |
+
const SpeechRecognition =
|
| 58 |
+
window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 59 |
+
recognition = new SpeechRecognition();
|
| 60 |
+
recognition.continuous = true;
|
| 61 |
+
recognition.interimResults = false;
|
| 62 |
+
recognition.lang = "en-US";
|
| 63 |
+
|
| 64 |
+
recognition.onresult = (event) => {
|
| 65 |
+
const text = event.results[event.results.length - 1][0].transcript.trim();
|
| 66 |
+
input.value = text;
|
| 67 |
+
sendMessage();
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
+
recognition.onerror = (event) => {
|
| 71 |
+
console.error("Speech recognition error:", event.error);
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
recognition.onend = () => {
|
| 75 |
+
isListening = false;
|
| 76 |
+
startBtn.disabled = false;
|
| 77 |
+
};
|
| 78 |
+
} else {
|
| 79 |
+
alert("Speech recognition not supported on this browser.");
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// β
Send message to backend
|
| 83 |
+
async function sendMessage() {
|
| 84 |
+
const text = input.value.trim();
|
| 85 |
+
if (!text) return;
|
| 86 |
+
addMessage("π§ You", text);
|
| 87 |
+
input.value = "";
|
| 88 |
+
|
| 89 |
+
const res = await fetch("/chat", {
|
| 90 |
+
method: "POST",
|
| 91 |
+
headers: { "Content-Type": "application/json" },
|
| 92 |
+
body: JSON.stringify({ message: text }),
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
const data = await res.json();
|
| 96 |
+
addMessage("π€ Bot", data.text);
|
| 97 |
+
speakText(data.text);
|
| 98 |
+
updateBackground(data.emotion);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// β
Speak bot response
|
| 102 |
+
function speakText(text) {
|
| 103 |
+
if (synth.speaking) synth.cancel();
|
| 104 |
+
currentUtterance = new SpeechSynthesisUtterance(text);
|
| 105 |
+
currentUtterance.lang = "en-US";
|
| 106 |
+
synth.speak(currentUtterance);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
// β
Stop everything cleanly
|
| 110 |
+
stopBtn.onclick = () => {
|
| 111 |
+
if (isListening && recognition) recognition.stop();
|
| 112 |
+
if (synth.speaking) synth.cancel();
|
| 113 |
+
isListening = false;
|
| 114 |
+
startBtn.disabled = false;
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
// β
Start listening
|
| 118 |
+
startBtn.onclick = () => {
|
| 119 |
+
if (!isListening && recognition) {
|
| 120 |
+
recognition.start();
|
| 121 |
+
isListening = true;
|
| 122 |
+
startBtn.disabled = true;
|
| 123 |
+
}
|
| 124 |
+
};
|
| 125 |
+
|
| 126 |
+
// β
Chatbox display
|
| 127 |
+
function addMessage(sender, text) {
|
| 128 |
+
const msg = document.createElement("p");
|
| 129 |
+
msg.textContent = `${sender}: ${text}`;
|
| 130 |
+
chatBox.appendChild(msg);
|
| 131 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// β
Emotion-based background
|
| 135 |
+
function updateBackground(emotion) {
|
| 136 |
+
let color;
|
| 137 |
+
switch (emotion.toLowerCase()) {
|
| 138 |
+
case "happy":
|
| 139 |
+
color = "linear-gradient(135deg, #fff176, #ffd54f)";
|
| 140 |
+
break;
|
| 141 |
+
case "sad":
|
| 142 |
+
color = "linear-gradient(135deg, #64b5f6, #1976d2)";
|
| 143 |
+
break;
|
| 144 |
+
case "angry":
|
| 145 |
+
color = "linear-gradient(135deg, #ff7043, #f44336)";
|
| 146 |
+
break;
|
| 147 |
+
case "calm":
|
| 148 |
+
color = "linear-gradient(135deg, #a5d6a7, #66bb6a)";
|
| 149 |
+
break;
|
| 150 |
+
case "motivated":
|
| 151 |
+
color = "linear-gradient(135deg, #ffb74d, #fb8c00)";
|
| 152 |
+
break;
|
| 153 |
+
default:
|
| 154 |
+
color = "linear-gradient(135deg, #e0e0e0, #bdbdbd)";
|
| 155 |
+
}
|
| 156 |
+
document.body.style.background = color;
|
| 157 |
+
document.body.style.transition = "background 1s ease";
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
sendBtn.onclick = sendMessage;
|
| 161 |
+
</script>
|
| 162 |
</body>
|
| 163 |
</html>accesskey
|