JJ94 commited on
Commit
784284e
·
verified ·
1 Parent(s): 8d8525b

Update templates/index.html

Browse files
Files changed (1) hide show
  1. 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.0">
6
- <title>Chat with LLaMA</title>
 
 
7
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
8
- <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
9
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>
10
- </head>
11
- <body class="bg-light">
12
 
13
- <div class="container py-5" x-data="{ messages: [], userInput: '' }">
14
- <h2 class="mb-4 text-center">Chat with LLaMA 🤖</h2>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- <div class="card shadow">
17
- <div class="card-body" style="height: 300px; overflow-y: auto;" id="chatBox">
18
- <template x-for="(msg, index) in messages" :key="index">
19
- <p><b x-text="msg.role + ':'"></b> <span x-text="msg.content"></span></p>
20
- </template>
 
 
 
21
  </div>
22
- </div>
 
23
 
24
- <div class="input-group mt-3">
25
- <input type="text" class="form-control" x-model="userInput" placeholder="Type a message..." @keydown.enter="sendMessage">
26
- <button class="btn btn-primary" @click="sendMessage">Send</button>
27
- </div>
28
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- <script>
31
- function sendMessage() {
32
- const userMessage = this.userInput;
33
- if (!userMessage) return;
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
- </script>
 
 
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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
10
+ <!-- Alpine.js -->
11
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/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>