Upload 2 files
Browse files- static/script.js +51 -0
- templates/index.html +59 -0
static/script.js
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener("DOMContentLoaded", (event) => {
|
2 |
+
const chatMessages = document.getElementById("chat-messages");
|
3 |
+
const userInput = document.getElementById("user-input");
|
4 |
+
const sendButton = document.getElementById("send-button");
|
5 |
+
|
6 |
+
function addMessage(message, isUser = false) {
|
7 |
+
const messageElement = document.createElement("p");
|
8 |
+
messageElement.textContent = message;
|
9 |
+
messageElement.style.backgroundColor = isUser ? "#e6f2ff" : "#f0f0f0";
|
10 |
+
messageElement.style.padding = "10px";
|
11 |
+
messageElement.style.borderRadius = "5px";
|
12 |
+
messageElement.style.marginBottom = "10px";
|
13 |
+
chatMessages.appendChild(messageElement);
|
14 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
15 |
+
}
|
16 |
+
|
17 |
+
function sendMessage() {
|
18 |
+
const message = userInput.value.trim();
|
19 |
+
if (message) {
|
20 |
+
addMessage(`You: ${message}`, true);
|
21 |
+
userInput.value = "";
|
22 |
+
|
23 |
+
fetch("/chatbot", {
|
24 |
+
method: "POST",
|
25 |
+
headers: {
|
26 |
+
"Content-Type": "application/json",
|
27 |
+
},
|
28 |
+
body: JSON.stringify({ text: message }),
|
29 |
+
})
|
30 |
+
.then((response) => response.json())
|
31 |
+
.then((data) => {
|
32 |
+
if (data.error) {
|
33 |
+
addMessage(`Error: ${data.error}`);
|
34 |
+
} else {
|
35 |
+
addMessage(`Chatbot: ${data.response}`);
|
36 |
+
}
|
37 |
+
})
|
38 |
+
.catch((error) => {
|
39 |
+
console.error("Error:", error);
|
40 |
+
addMessage("Error: Unable to get response from the server.");
|
41 |
+
});
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
sendButton.addEventListener("click", sendMessage);
|
46 |
+
userInput.addEventListener("keypress", function (e) {
|
47 |
+
if (e.key === "Enter") {
|
48 |
+
sendMessage();
|
49 |
+
}
|
50 |
+
});
|
51 |
+
});
|
templates/index.html
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>SQL Chatbot</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
max-width: 800px;
|
11 |
+
margin: 0 auto;
|
12 |
+
padding: 20px;
|
13 |
+
background-color: #f0f0f0;
|
14 |
+
}
|
15 |
+
#chat-container {
|
16 |
+
background-color: white;
|
17 |
+
border-radius: 10px;
|
18 |
+
padding: 20px;
|
19 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
20 |
+
}
|
21 |
+
#chat-messages {
|
22 |
+
height: 400px;
|
23 |
+
overflow-y: auto;
|
24 |
+
border: 1px solid #ddd;
|
25 |
+
padding: 10px;
|
26 |
+
margin-bottom: 20px;
|
27 |
+
}
|
28 |
+
#user-input {
|
29 |
+
width: 100%;
|
30 |
+
padding: 10px;
|
31 |
+
border: 1px solid #ddd;
|
32 |
+
border-radius: 5px;
|
33 |
+
margin-bottom: 10px;
|
34 |
+
}
|
35 |
+
#send-button {
|
36 |
+
background-color: #4caf50;
|
37 |
+
color: white;
|
38 |
+
border: none;
|
39 |
+
padding: 10px 20px;
|
40 |
+
text-align: center;
|
41 |
+
text-decoration: none;
|
42 |
+
display: inline-block;
|
43 |
+
font-size: 16px;
|
44 |
+
margin: 4px 2px;
|
45 |
+
cursor: pointer;
|
46 |
+
border-radius: 5px;
|
47 |
+
}
|
48 |
+
</style>
|
49 |
+
</head>
|
50 |
+
<body>
|
51 |
+
<div id="chat-container">
|
52 |
+
<h1>SQL Chatbot</h1>
|
53 |
+
<div id="chat-messages"></div>
|
54 |
+
<input type="text" id="user-input" placeholder="Ask a question..." />
|
55 |
+
<button id="send-button">Send</button>
|
56 |
+
</div>
|
57 |
+
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
58 |
+
</body>
|
59 |
+
</html>
|