Update templates/index.html
Browse files- templates/index.html +87 -33
templates/index.html
CHANGED
|
@@ -2,48 +2,102 @@
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1
|
| 6 |
-
<title>Chat with
|
|
|
|
|
|
|
| 7 |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 8 |
-
|
| 9 |
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
| 10 |
-
|
| 11 |
-
<
|
| 12 |
|
| 13 |
-
<
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
</div>
|
| 22 |
-
</
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
</div>
|
| 28 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
// Append user message
|
| 36 |
-
this.messages.push({ role: "You", content: userMessage });
|
| 37 |
-
this.userInput = "";
|
| 38 |
-
|
| 39 |
-
axios.post("/chat", { message: userMessage })
|
| 40 |
-
.then(response => {
|
| 41 |
-
this.messages.push({ role: "Bot", content: response.data.response });
|
| 42 |
-
document.getElementById("chatBox").scrollTop = document.getElementById("chatBox").scrollHeight;
|
| 43 |
-
})
|
| 44 |
-
.catch(error => console.error("Error:", error));
|
| 45 |
}
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
</body>
|
| 49 |
</html>
|
|
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Chat with AI</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 |
+
background-color: #f8f9fa;
|
| 18 |
+
}
|
| 19 |
+
.chat-box {
|
| 20 |
+
height: 400px;
|
| 21 |
+
overflow-y: auto;
|
| 22 |
+
background: white;
|
| 23 |
+
padding: 15px;
|
| 24 |
+
border-radius: 10px;
|
| 25 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
| 26 |
+
}
|
| 27 |
+
.message {
|
| 28 |
+
padding: 8px 12px;
|
| 29 |
+
border-radius: 8px;
|
| 30 |
+
margin-bottom: 10px;
|
| 31 |
+
}
|
| 32 |
+
.user-message {
|
| 33 |
+
background-color: #d1e7dd;
|
| 34 |
+
text-align: right;
|
| 35 |
+
}
|
| 36 |
+
.ai-message {
|
| 37 |
+
background-color: #e2e3e5;
|
| 38 |
+
}
|
| 39 |
+
</style>
|
| 40 |
+
</head>
|
| 41 |
+
<body>
|
| 42 |
|
| 43 |
+
<div class="container mt-5" x-data="chatApp()">
|
| 44 |
+
<h2 class="text-center mb-4">💬 Chat with AI</h2>
|
| 45 |
+
|
| 46 |
+
<div class="chat-box mb-3" id="chatBox">
|
| 47 |
+
<template x-for="message in messages">
|
| 48 |
+
<div :class="{'user-message': message.role === 'user', 'ai-message': message.role === 'ai'}" class="message">
|
| 49 |
+
<strong x-text="message.role === 'user' ? 'You: ' : 'AI: '"></strong>
|
| 50 |
+
<span x-text="message.text"></span>
|
| 51 |
</div>
|
| 52 |
+
</template>
|
| 53 |
+
</div>
|
| 54 |
|
| 55 |
+
<div class="input-group">
|
| 56 |
+
<input type="text" class="form-control" placeholder="Type a message..." x-model="userMessage" @keydown.enter="sendMessage">
|
| 57 |
+
<button class="btn btn-primary" @click="sendMessage">Send</button>
|
|
|
|
| 58 |
</div>
|
| 59 |
+
</div>
|
| 60 |
+
|
| 61 |
+
<script>
|
| 62 |
+
function chatApp() {
|
| 63 |
+
return {
|
| 64 |
+
userMessage: '',
|
| 65 |
+
messages: [],
|
| 66 |
+
|
| 67 |
+
async sendMessage() {
|
| 68 |
+
if (!this.userMessage.trim()) return;
|
| 69 |
+
|
| 70 |
+
let messageText = this.userMessage;
|
| 71 |
+
this.messages.push({ role: 'user', text: messageText });
|
| 72 |
+
this.userMessage = ''; // Clear input
|
| 73 |
+
|
| 74 |
+
let aiResponse = { role: 'ai', text: '' };
|
| 75 |
+
this.messages.push(aiResponse);
|
| 76 |
+
|
| 77 |
+
let response = await fetch('/chat', {
|
| 78 |
+
method: 'POST',
|
| 79 |
+
headers: { 'Content-Type': 'application/json' },
|
| 80 |
+
body: JSON.stringify({ message: messageText })
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
const reader = response.body.getReader();
|
| 84 |
+
const decoder = new TextDecoder();
|
| 85 |
+
|
| 86 |
+
while (true) {
|
| 87 |
+
const { done, value } = await reader.read();
|
| 88 |
+
if (done) break;
|
| 89 |
+
aiResponse.text += decoder.decode(value, { stream: true });
|
| 90 |
+
}
|
| 91 |
|
| 92 |
+
// Scroll to the latest message
|
| 93 |
+
this.$nextTick(() => {
|
| 94 |
+
let chatBox = document.getElementById('chatBox');
|
| 95 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 96 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
+
};
|
| 99 |
+
}
|
| 100 |
+
</script>
|
| 101 |
|
| 102 |
</body>
|
| 103 |
</html>
|