Spaces:
Runtime error
Runtime error
Create script.js
Browse files
script.js
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener("DOMContentLoaded", () => {
|
2 |
+
const chatBox = document.getElementById("chatBox");
|
3 |
+
|
4 |
+
// Append a message to the chat box
|
5 |
+
function appendMessage(text, sender = "bot") {
|
6 |
+
const msg = document.createElement("div");
|
7 |
+
msg.className = sender === "bot" ? "bot-message" : "user-message";
|
8 |
+
msg.innerText = text;
|
9 |
+
chatBox.appendChild(msg);
|
10 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
11 |
+
}
|
12 |
+
|
13 |
+
// Send user message to backend
|
14 |
+
window.sendMessage = async () => {
|
15 |
+
const input = document.getElementById("userInput");
|
16 |
+
const message = input.value.trim();
|
17 |
+
if (!message) return;
|
18 |
+
|
19 |
+
appendMessage(message, "user");
|
20 |
+
input.value = "";
|
21 |
+
|
22 |
+
try {
|
23 |
+
// Show loading
|
24 |
+
appendMessage("Loading...", "bot");
|
25 |
+
|
26 |
+
// Make request to backend
|
27 |
+
const res = await fetch("/api", {
|
28 |
+
method: "POST",
|
29 |
+
headers: { "Content-Type": "application/json" },
|
30 |
+
body: JSON.stringify({ question: message })
|
31 |
+
});
|
32 |
+
|
33 |
+
const data = await res.json();
|
34 |
+
|
35 |
+
// Replace loading with response
|
36 |
+
const lastBotMsg = document.querySelector(".bot-message:last-child");
|
37 |
+
if (lastBotMsg) lastBotMsg.remove();
|
38 |
+
|
39 |
+
appendMessage(data.answer || "No response from AI.", "bot");
|
40 |
+
} catch (err) {
|
41 |
+
appendMessage("Failed to get response: " + err.message, "bot");
|
42 |
+
}
|
43 |
+
};
|
44 |
+
|
45 |
+
// Send message on Enter key
|
46 |
+
document.getElementById("userInput").addEventListener("keydown", (e) => {
|
47 |
+
if (e.key === "Enter") {
|
48 |
+
e.preventDefault();
|
49 |
+
sendMessage();
|
50 |
+
}
|
51 |
+
});
|
52 |
+
|
53 |
+
// Voice Input Button
|
54 |
+
const micBtn = document.getElementById("micBtn");
|
55 |
+
if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
|
56 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
57 |
+
recognition.lang = "en-IN";
|
58 |
+
|
59 |
+
recognition.onstart = () => appendMessage("🎙️ Listening...", "bot");
|
60 |
+
|
61 |
+
recognition.onresult = (e) => {
|
62 |
+
const transcript = e.results[0][0].transcript;
|
63 |
+
document.getElementById("userInput").value = transcript;
|
64 |
+
sendMessage();
|
65 |
+
};
|
66 |
+
|
67 |
+
recognition.onerror = (e) => {
|
68 |
+
appendMessage("Mic error: " + e.error, "bot");
|
69 |
+
};
|
70 |
+
|
71 |
+
micBtn.onclick = () => recognition.start();
|
72 |
+
} else {
|
73 |
+
micBtn.disabled = true;
|
74 |
+
micBtn.title = "Voice input not supported in this browser.";
|
75 |
+
}
|
76 |
+
});
|