forked from du2333/CS4990-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.html
More file actions
407 lines (355 loc) · 17.5 KB
/
demo.html
File metadata and controls
407 lines (355 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html lang="en">
<head>
<title>QuackGPT 3.5</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Include Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
</head>
<body>
<navbar id="vertical-nav" class="navbar-toggler">
<div>
<button onclick="newConversation(event)" id="new-conversation-button" class="btn btn-primary">New Conversation</button>
</div>
<div id="conversation-box">
<!-- "Previous Convos" text -->
<p id="previous-convos">Previous Convos</p>
<!-- Conversation titles go here -->
<div id="conversation-list">
</div>
</div>
</navbar>
<!-- Bootstrap Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="navbar-content">
<a class="navbar-brand" href="demo.html">QuackGPT 3.5</a>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#" data-toggle="modal" data-target="#loginModal">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-toggle="modal" data-target="#registerModal">Register</a>
</li>
</ul>
</div>
</nav>
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loginModalLabel">Login</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="login-user-id" class="user-id-style">User ID</label>
<input type="text" class="form-control" id="login-user-id" placeholder="Enter User ID">
</div>
<div class="form-group">
<label for="login-password" class="login-password-style">Password</label>
<input type="password" class="form-control" id="login-password" placeholder="Enter Password">
</div>
<button type="button" class="btn btn-primary" onclick="login(event)">Login</button>
</form>
</div>
</div>
</div>
</div>
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="registerModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="registerModalLabel">Register</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="register-user-id" class="user-id-style">User ID</label>
<input type="text" class="form-control" id="register-user-id" placeholder="Enter User ID for registration">
</div>
<div class="form-group">
<label for="register-password" class="login-password-style">Password</label>
<input type="password" class="form-control" id="register-password" placeholder="Enter Password for registration">
</div>
<button type="button" class="btn btn-primary" onclick="register(event)">Register</button>
</form>
</div>
</div>
</div>
</div>
<!-- Commented this out since I implemented the bootstrap and it now is redundant -->
<!-- <div id="auth" class="container">
<input id="register-user-id" type="text" placeholder="User ID for registration" class="form-control">
<input id="register-password" type="password" placeholder="Password for registration" class="form-control">
<button onclick="register(event)" id="register-button" class="btn btn-primary">Register</button>
<input id="login-user-id" type="text" placeholder="User ID for login" class="form-control">
<input id="login-password" type="password" placeholder="Password for login" class="form-control">
<button onclick="login(event)" id="login-button" class="btn btn-primary">Login</button>
</div> -->
<div id="message-window"></div>
<div id="message-input-container">
<input id="message-input" type="text" placeholder="Type your message here..." class="form-control">
<button onclick="submitForm(event)" id="send-button" class="btn btn-primary">Send</button>
</div>
<!-- <div id="div-conversation-button-box"> -->
<!-- <button onclick="newConversation(event)" id="new-conversation-button" class="btn btn-primary">New Conversation</button> -->
<!-- </div> -->
<!-- <div id="conversation-list"></div> -->
<!-- <p id="previous-convos">Previous Conversations</p> -->
<!-- Include Bootstrap JS and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
function register(event) {
event.preventDefault();
var userId = document.getElementById('register-user-id').value;
var password = document.getElementById('register-password').value;
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ user_id: userId, password })
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
alert('Registration successful');
console.log(data)
})
.catch((error) => {
console.error('Error:', error);
alert('Registration failed');
});
}
function login(event) {
event.preventDefault();
var userId = document.getElementById('login-user-id').value;
var password = document.getElementById('login-password').value;
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `username=${encodeURIComponent(userId)}&password=${encodeURIComponent(password)}&grant_type=password`
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
alert('Login successful');
localStorage.setItem('token', data.access_token);
// reload the page to show the conversation list
location.reload();
})
.catch((error) => {
console.error('Error:', error);
alert('Login failed');
});
}
document.getElementById('message-input').addEventListener('keypress', function (event) {
var input = document.getElementById('message-input').value;
if (event.key === 'Enter') {
event.preventDefault();
if (input == '') {
return;
}
submitForm(event);
}
});
// Create a chat history object
// This object will be used to keep track of the conversation for multi-turn conversations
let chatHistory = {
"content": []
};
let currentConversationId = null;
function submitForm(event) {
event.preventDefault();
var message = document.getElementById('message-input').value;
let youLabelElement = document.createElement('p');
youLabelElement.textContent = 'You';
youLabelElement.classList.add('message-label');
document.getElementById('message-window').appendChild(youLabelElement);
// The user's message is added as a new object with the role 'user' and the content of the message
chatHistory.content.push({
"role": "user",
"parts": [message]
});
let messageElement = document.createElement('p');
messageElement.textContent = message;
document.getElementById('message-window').appendChild(messageElement);
let data = JSON.stringify(
{
"content": chatHistory.content
}
);
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + localStorage.getItem('token')
},
body: data
})
.then(response => {
if (!response.ok) {
alert('Something went wrong. Please try again.');
throw new Error(`HTTP error! status: ${response.status}`);
}
// The response from the API is a ReadableStream
// This allows for the data to be read in chunks as it is being received
const reader = response.body.getReader();
let responseElement = document.getElementById('response');
let chunks = '';
let tempElement = document.createElement('p');
document.getElementById('message-window').appendChild(tempElement);
// // Start reading the response from the API
// The response is read in chunks to allow for dynamic display of the model's response
return reader.read().then(function processText({ done, value }) {
// check to see if the response has been fully received
if (done) {
// Add "AI" label above AI response
let aiLabelElement = document.createElement('p');
aiLabelElement.textContent = 'QuackGPT';
aiLabelElement.classList.add('message-label');
document.getElementById('message-window').insertBefore(aiLabelElement, tempElement);
// The model's response is added as a new object with the role 'model'
// and the content of the response
chatHistory.content.push({
"role": "model",
"parts": [chunks]
});
let responseElement = document.createElement('p');
responseElement.textContent = chunks;
responseElement.classList.add('ai-response'); // Add the class 'ai-response' for AI responses
document.getElementById('message-window').replaceChild(responseElement, tempElement);
saveChat(chatHistory);
return;
}
let chunk = new TextDecoder('utf-8').decode(value);
chunks += chunk;
tempElement.textContent = chunks;
// Continue reading the next chunk of the response
return reader.read().then(processText);
});
})
.catch((error) => {
console.error('Error:', error);
});
document.getElementById('message-input').value = '';
}
function getAllConversationTitles() {
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations', {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token')
}
})
.then(response => {
return response.json();
})
.then(data => {
let conversationListElement = document.getElementById('conversation-list');
conversationListElement.innerHTML = '';
data.forEach(conversation => {
let conversationElement = document.createElement('p');
conversationElement.textContent = conversation.title;
conversationElement.addEventListener('click', function () {
currentConversationId = conversation.conversation_id;
getConversation(conversation.conversation_id);
});
conversationListElement.appendChild(conversationElement);
});
})
.catch((error) => {
console.error('Error:', error);
});
}
function getConversation(conversationId) {
fetch(`http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations/${conversationId}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token')
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return (response.json());
})
.then(data => {
chatHistory.content = data;
let messageWindowElement = document.getElementById('message-window');
messageWindowElement.innerHTML = '';
data.forEach(message => {
let messageElement = document.createElement('p');
messageElement.textContent = message.parts.join(' ');
messageWindowElement.appendChild(messageElement);
});
})
.catch((error) => {
console.error('Error:', error);
});
}
function saveChat(chatHistory) {
fetch(`http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations/${currentConversationId}`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json'
},
body: JSON.stringify(chatHistory)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
then (response.json());
})
.then(data => {
console.log(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
function newConversation(event) {
document.getElementById('message-window').innerHTML = '';
currentConversationId = 0;
chatHistory.content = [];
saveChat(chatHistory);
getAllConversationTitles();
}
// Call getAllConversationTitles when the page loads
window.onload = getAllConversationTitles;
</script>
</body>
</html>