Update templates/index.html
Browse files- templates/index.html +45 -68
templates/index.html
CHANGED
|
@@ -3,92 +3,69 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
-
<title>
|
| 7 |
-
|
| 8 |
-
<!-- Bootstrap CSS -->
|
| 9 |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 10 |
-
<!-- Alpine.js -->
|
| 11 |
-
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
| 12 |
-
<!-- Axios -->
|
| 13 |
-
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
| 14 |
-
|
| 15 |
<style>
|
| 16 |
-
body {
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
}
|
| 22 |
-
.message {
|
| 23 |
-
padding: 8px 12px; border-radius: 8px; margin-bottom: 10px;
|
| 24 |
-
}
|
| 25 |
-
.user-message { background-color: #d1e7dd; text-align: right; }
|
| 26 |
-
.ai-message { background-color: #e2e3e5; }
|
| 27 |
</style>
|
| 28 |
</head>
|
| 29 |
<body>
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
<div class="
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
<template x-for="message in messages">
|
| 36 |
-
<div :class="{'user-message': message.role === 'user', 'ai-message': message.role === 'ai'}" class="message">
|
| 37 |
-
<strong x-text="message.role === 'user' ? 'You: ' : 'AI: '"></strong>
|
| 38 |
-
<span x-text="message.text"></span>
|
| 39 |
-
</div>
|
| 40 |
-
</template>
|
| 41 |
</div>
|
| 42 |
|
| 43 |
-
<
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
this.messages.push({ role: 'user', text: messageText });
|
| 60 |
-
this.userMessage = '';
|
| 61 |
-
|
| 62 |
-
let aiResponse = { role: 'ai', text: '' };
|
| 63 |
-
this.messages.push(aiResponse);
|
| 64 |
-
|
| 65 |
-
let response = await fetch('/chat', {
|
| 66 |
-
method: 'POST',
|
| 67 |
-
headers: { 'Content-Type': 'application/json' },
|
| 68 |
-
body: JSON.stringify({ message: messageText })
|
| 69 |
});
|
| 70 |
-
|
| 71 |
const reader = response.body.getReader();
|
| 72 |
const decoder = new TextDecoder();
|
| 73 |
-
|
| 74 |
while (true) {
|
| 75 |
const { done, value } = await reader.read();
|
| 76 |
if (done) break;
|
| 77 |
-
|
| 78 |
let token = decoder.decode(value, { stream: true });
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
//
|
| 82 |
-
|
| 83 |
-
let chatBox = document.getElementById('chatBox');
|
| 84 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
| 85 |
-
});
|
| 86 |
}
|
| 87 |
}
|
| 88 |
-
|
| 89 |
-
}
|
| 90 |
-
</script>
|
| 91 |
-
|
| 92 |
</body>
|
| 93 |
</html>
|
| 94 |
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Chatbot</title>
|
|
|
|
|
|
|
| 7 |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
<style>
|
| 9 |
+
body { padding: 20px; }
|
| 10 |
+
#chatBox { height: 400px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; border-radius: 5px; background: #f8f9fa; }
|
| 11 |
+
.message { margin-bottom: 10px; padding: 8px 12px; border-radius: 8px; max-width: 75%; }
|
| 12 |
+
.user { background: #007bff; color: white; align-self: flex-end; }
|
| 13 |
+
.bot { background: #e9ecef; color: black; align-self: flex-start; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
</style>
|
| 15 |
</head>
|
| 16 |
<body>
|
| 17 |
+
<div class="container">
|
| 18 |
+
<h2 class="text-center mb-4">Chatbot</h2>
|
| 19 |
+
<div id="chatBox" class="d-flex flex-column"></div>
|
| 20 |
|
| 21 |
+
<div class="input-group mt-3">
|
| 22 |
+
<input type="text" id="userInput" class="form-control" placeholder="Type a message...">
|
| 23 |
+
<button class="btn btn-primary" onclick="sendMessage()">Send</button>
|
| 24 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
</div>
|
| 26 |
|
| 27 |
+
<script>
|
| 28 |
+
async function sendMessage() {
|
| 29 |
+
let inputField = document.getElementById("userInput");
|
| 30 |
+
let chatBox = document.getElementById("chatBox");
|
| 31 |
+
let userMessage = inputField.value.trim();
|
| 32 |
+
|
| 33 |
+
if (!userMessage) return;
|
| 34 |
+
inputField.value = "";
|
| 35 |
+
|
| 36 |
+
// Display user message
|
| 37 |
+
let userMsgElement = document.createElement("div");
|
| 38 |
+
userMsgElement.className = "message user align-self-end";
|
| 39 |
+
userMsgElement.textContent = userMessage;
|
| 40 |
+
chatBox.appendChild(userMsgElement);
|
| 41 |
|
| 42 |
+
// Add bot response container
|
| 43 |
+
let botMsgElement = document.createElement("div");
|
| 44 |
+
botMsgElement.className = "message bot align-self-start";
|
| 45 |
+
chatBox.appendChild(botMsgElement);
|
| 46 |
+
|
| 47 |
+
// Fetch response from backend
|
| 48 |
+
let response = await fetch("/chat", {
|
| 49 |
+
method: "POST",
|
| 50 |
+
headers: { "Content-Type": "application/json" },
|
| 51 |
+
body: JSON.stringify({ message: userMessage })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
});
|
| 53 |
+
|
| 54 |
const reader = response.body.getReader();
|
| 55 |
const decoder = new TextDecoder();
|
| 56 |
+
|
| 57 |
while (true) {
|
| 58 |
const { done, value } = await reader.read();
|
| 59 |
if (done) break;
|
| 60 |
+
|
| 61 |
let token = decoder.decode(value, { stream: true });
|
| 62 |
+
botMsgElement.textContent += token;
|
| 63 |
+
|
| 64 |
+
// Auto-scroll
|
| 65 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
}
|
| 68 |
+
</script>
|
|
|
|
|
|
|
|
|
|
| 69 |
</body>
|
| 70 |
</html>
|
| 71 |
|