-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
469 lines (402 loc) · 13.2 KB
/
index.html
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0, user-scalable=yes">
<title>PaLM</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
/* Add styles for the navbar */
.navbar {
display: flex;
align-items: center;
position: sticky;
top: 0;
background-color: #007bff;
color: #fff;
padding: 10px;
z-index: 2;
}
.navbar-title {
font-size: 20px;
font-weight: bold;
text-align: center !important;
margin-left: auto;
margin-right: auto;
}
.menu-dropdown {
position: relative;
display: inline-block;
z-index: 3;
}
.menu-content {
display: none;
position: absolute;
background-color: #007bff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 4;
right: 0;
max-width: calc(100vw - 10px);
}
.menu-dropdown:hover .menu-content {
display: block;
}
.menu-content button {
width: 100%;
text-align: left;
padding: 8px 12px;
border: none;
background-color: #fff;
color: #000;
cursor: pointer;
width: 100%;
max-width: 100%;
}
.menu-content button:hover {
background-color: #fff;
}
.chat-section {
position: sticky;
top: 0;
background-color: #f2f2f2;
padding: 0px;
}
#chat-container {
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
max-height: calc(100vh - 100px); /* Adjust the height as per your needs */
overflow-y: scroll;
word-wrap: break-word;
width: calc(100% - 22px); /* Adjust the width by accounting for the border and padding */
}
.user, .bot {
margin-bottom: 5px;
word-wrap: break-word;
padding: 8px;
border-radius: 5px;
}
.user {
text-align: right;
background-color: #007bff;
color: #fff;
margin-left: auto;
align-self: flex-end;
display: inline-flex;
}
.bot {
text-align: left;
background-color: #f2f2f2;
color: #333;
margin-right: auto;
}
#input-container {
display: flex;
align-items: center;
padding: 10px;
background-color: #f2f2f2;
}
#user-input {
flex-grow: 1;
padding: 5px;
border: 1px solid #ddd;
border-radius: 3px;
}
#send-button {
margin-left: 10px;
padding: 5px 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
#send-button:hover {
background-color: #0056b3;
}
#settings-popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #f2f2f2;
padding: 20px;
z-index: 999;
}
#settings-popup input[type="text"] {
margin-bottom: 10px;
width: 100%;
padding: 5px;
}
#settings-popup button {
margin-right: 10px;
padding: 5px 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
#settings-popup button:hover {
background-color: #0056b3;
}
#close-button {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
background-color: #ccc;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
#close-button:hover {
background-color: #999;
}
.typing-indicator {
display: inline-block;
animation: typing 1s infinite;
font-size: 24px;
color: #aaa;
margin-left: 5px;
}
.timestamp {
font-size: 12px;
color: #999;
margin-top: 4px;
}
@keyframes typing {
0% { opacity: 0.3; }
50% { opacity: 1; }
100% { opacity: 0.3; }
}
</style>
</head>
<body>
<!-- Add the navbar -->
<div class="navbar">
<div class="navbar-title title">PaLM</div>
<div class="menu-dropdown">
<button class="dropbtn">☰</button>
<div class="menu-content">
<button id="settings-button">Settings</button>
<button id="download-button">Download</button>
<button id="clear-button">Clear Conversation</button>
<a href="https://github.com/dishapatel010/PaLM" target="_blank" rel="noopener noreferrer">
<button>GitHub Repository</button>
</a>
</div>
</div>
</div>
<!-- Wrap the chat container and input container in a parent div with class "chat-section" -->
<div class="chat-section">
<div id="chat-container"></div>
<div id="input-container">
<input type="text" id="user-input" placeholder="Type your message here">
<button id="send-button" disabled>Send</button>
</div>
</div>
<div id="settings-popup">
<button id="close-button">Close</button>
<h3>API Key Configuration</h3>
<p>Please enter your API key:</p>
<input type="text" id="api-key-input">
<button id="save-button">Save</button>
<p>To get an API key, please visit the <a href="https://makersuite.google.com/" target="_blank" rel="noopener noreferrer">Google MakeSuite website</a>.</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
const chatContainer = document.getElementById('chat-container');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
const settingsButton = document.getElementById('settings-button');
const downloadButton = document.getElementById('download-button');
const settingsPopup = document.getElementById('settings-popup');
const apiKeyInput = document.getElementById('api-key-input');
const saveButton = document.getElementById('save-button');
const closeButton = document.getElementById('close-button');
const apiUrl = 'https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=';
const clearButton = document.getElementById('clear-button');
// Load conversation history from local storage
const conversationHistory = JSON.parse(localStorage.getItem('conversationHistory')) || [];
// Display conversation history on page load
conversationHistory.forEach(([message, sender, timestamp]) => {
displayMessage(message, sender, timestamp);
});
// Event listener for user input changes and keydown
userInput.addEventListener('input', (event) => {
sendButton.disabled = userInput.value.trim() === '';
});
userInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
if (userInput.value.trim() !== '') {
sendButton.click();
}
}
});
userInput.addEventListener('submit', (event) => {
event.preventDefault();
if (userInput.value.trim() !== '') {
sendButton.click();
}
});
// Event listener for send button click
sendButton.addEventListener('click', () => {
const userMessage = userInput.value.trim(); // Trim any leading/trailing spaces
if (userMessage === '') {
return; // Do not send if the message is empty
}
// Clear the input field
userInput.value = '';
const utimestamp = new Date().toLocaleTimeString();
// Display user message in the chat container
displayMessage(userMessage, 'user', utimestamp);
// Add user message to conversation history
conversationHistory.push([userMessage, 'user', utimestamp]);
saveConversationHistory();
// Send user message to the backend API
sendUserMessage(userMessage);
chatContainer.scrollTop = chatContainer.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
});
// Event listener for settings button click
settingsButton.addEventListener('click', () => {
settingsPopup.style.display = 'block';
});
// Event listener for close button click in the settings popup
closeButton.addEventListener('click', () => {
settingsPopup.style.display = 'none';
});
// Add event listener for clear button click
clearButton.addEventListener('click', () => {
clearConversation();
});
// Event listener for download button click
downloadButton.addEventListener('click', () => {
downloadConversationHistory();
});
// Event listener for save button click in the settings popup
saveButton.addEventListener('click', () => {
const apiKey = apiKeyInput.value;
saveApiKey(apiKey);
settingsPopup.style.display = 'none';
});
// Function to display a message in the chat container
function displayMessage(message, sender, timestamp) {
const messageElement = document.createElement('div');
messageElement.classList.add(sender);
// Convert the bot's response from Markdown to HTML using marked.js
if (sender === 'bot') {
message = marked.parse(message);
}
messageElement.innerHTML = message; // Use innerHTML to parse HTML tags
chatContainer.appendChild(messageElement);
const timestampElement = document.createElement('div');
timestampElement.classList.add('timestamp');
timestampElement.innerText = timestamp;
chatContainer.appendChild(timestampElement);
// Add a horizontal line after the timestamp
const lineElement = document.createElement('hr');
chatContainer.appendChild(lineElement);
// Scroll to the bottom of the chat container
chatContainer.scrollTop = chatContainer.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
}
// Function to save the API key to local storage
function saveApiKey(apiKey) {
localStorage.setItem('apiKey', apiKey);
}
// Function to get the API key from local storage
function getApiKey() {
return localStorage.getItem('apiKey');
}
// Function to clear the conversation history
function clearConversation() {
// Display a confirmation prompt
const confirmation = confirm("Are you sure you want to clear the conversation?");
// If the user confirms, clear the conversation
if (confirmation) {
// Clear the chat container
chatContainer.innerHTML = '';
// Clear the conversation history array
conversationHistory.length = 0;
// Remove conversation history from local storage
localStorage.removeItem('conversationHistory');
}
}
// Function to save the conversation history to local storage
function saveConversationHistory() {
const formattedHistory = conversationHistory.map(([message, sender]) => {
const timestamp = new Date().toLocaleTimeString();
return [message, sender, timestamp];
});
localStorage.setItem('conversationHistory', JSON.stringify(formattedHistory));
}
// Function to send user message to the backend API
function sendUserMessage(message) {
const apiKey = getApiKey();
if (!apiKey) {
displayMessage('Please configure the API key in the settings.', 'bot');
return;
}
const requestUrl = apiUrl + apiKey;
const requestBody = {
prompt: { messages: [{ content: message }] },
temperature: 0.1,
candidate_count: 1
};
const typingIndicator = document.createElement('span');
typingIndicator.classList.add('typing-indicator');
typingIndicator.innerText = '...';
chatContainer.appendChild(typingIndicator);
fetch(requestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => response.json())
.then(data => {
chatContainer.removeChild(typingIndicator);
const botResponse = data.candidates[0].content; // Extract bot's response from candidates array
const btimestamp = new Date().toLocaleTimeString();
// Display bot response in the chat container
displayMessage(botResponse, 'bot', btimestamp);
// Add bot response to conversation history
conversationHistory.push([botResponse, 'bot', btimestamp]);
saveConversationHistory();
})
.catch(error => {
chatContainer.removeChild(typingIndicator);
console.error('Error:', error);
displayMessage('An error occurred.', 'bot');
});
}
// Function to convert conversation history to a downloadable file
function downloadConversationHistory() {
const formattedHistory = conversationHistory.map(([message, sender, timestamp]) => {
return `${timestamp} [${sender}]: ${message}`;
});
const formattedContent = formattedHistory.join('\n');
const blob = new Blob([formattedContent], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'conversation_history.txt';
link.click();
}
window.addEventListener('DOMContentLoaded', () => {
userInput.focus();
});
</script>
</body>
</html>