Spaces:
Runtime error
Runtime error
Srinivasulu kethanaboina
commited on
Create script.js
Browse files- static/script.js +30 -0
static/script.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.getElementById("send-button").addEventListener("click", async () => {
|
| 2 |
+
const input = document.getElementById("user-input");
|
| 3 |
+
const message = input.value;
|
| 4 |
+
|
| 5 |
+
// Add user message to chat history
|
| 6 |
+
addMessage("User", message);
|
| 7 |
+
|
| 8 |
+
// Clear input field
|
| 9 |
+
input.value = "";
|
| 10 |
+
|
| 11 |
+
// Send message to the backend
|
| 12 |
+
const response = await fetch("/chat/", {
|
| 13 |
+
method: "POST",
|
| 14 |
+
headers: {
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
},
|
| 17 |
+
body: JSON.stringify({ message })
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
const data = await response.json();
|
| 21 |
+
addMessage("Bot", data.response);
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
function addMessage(sender, message) {
|
| 25 |
+
const chatHistory = document.getElementById("chat-history");
|
| 26 |
+
const messageElement = document.createElement("div");
|
| 27 |
+
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
|
| 28 |
+
chatHistory.appendChild(messageElement);
|
| 29 |
+
chatHistory.scrollTop = chatHistory.scrollHeight; // Scroll to the bottom
|
| 30 |
+
}
|