-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
538 lines (460 loc) · 15.1 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# -*- coding: utf-8 -*-
"""Index.html
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1jqJQzddoDY-IWGdm3z5qCIrpFCZisDvF
###Index.html
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Demonstration of ChatGPT API in a Python Flask Application">
<title>Gemini AI Model - Free Chatbot</title>
<link rel="shortcut icon" type="image/x-icon" href="{{ url_for('static', filename='images/iba_logo.png') }}">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<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=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/annyang.min.js"></script>
<style>
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(to right, #0f0c29, #302b63, #24243e);
color: #fff;
height: 100vh;
margin: 0;
display: flex;
transition: margin-left 0.5s; /* Add transition for smooth sliding */
}
.previous-chats-container {
width: 300px;
background-color: #000000; /* Change the background color to black */
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
overflow-y: auto;
transition: transform 0.5s;
transform: translateX(0); /* Make it permanent */
position: fixed; /* Fix the position */
top: 0;
left: 0;
bottom: 0;
color: #ffffff; /* Change the text color to white for better visibility */
z-index: 1; /* Ensure it's above other elements */
}
#chat-history-list {
list-style-type: none;
padding: 0;
}
#chat-history-list li {
color: #ffffff; /* Set text color to white */
background-color: rgba(255, 255, 255, 0.2);
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
cursor: pointer;
}
#chat-history-list li:hover {
background-color: rgba(255, 255, 255, 0.3);
}
.chat-container {
flex: 1;
padding: 20px;
display: flex;
flex-direction: column;
transition: flex-basis 0.5s; /* Add transition for smooth resizing */
margin-left: 300px; /* Adjust for previous chats container width */
position: relative; /* Ensure relative positioning */
}
.chat-header {
text-align: center;
margin-bottom: 20px;
}
.chat-header h1 {
font-weight: 600;
text-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
#chat-history {
background-color: rgba(255, 255, 255, 0.2);
padding: 10px;
border-radius: 8px;
overflow-y: auto;
flex: 1;
}
.chat-item {
margin-bottom: 10px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.user-message, .bot-message {
border-radius: 8px;
max-width: 80%;
}
.user-message {
background-color: #007bff;
margin-right: auto;
margin-bottom: 5px;
}
.bot-message {
background-color: #28a745;
margin-left: auto;
}
.image-container {
max-width: 200px;
max-height: 200px;
float: left;
}
.chat-message {
color: white;
padding: 10px;
line-height: 1.5;
word-wrap: break-word;
white-space: pre-wrap;
}
.input-group {
margin-top: 20px;
}
#chat-input {
background-color: rgba(255, 255, 255, 0.2);
border: none;
color: #fff;
}
#chat-input:focus {
background-color: rgba(255, 255, 255, 0.3);
box-shadow: none;
}
#gpt-button {
background-color: #007bff;
border-color: #007bff;
color: white;
border-radius: 0 5px 5px 0;
transition: background-color 0.3s ease;
padding: 13px; /* Adjust padding */
font-size: 16px; /* Adjust font size */
}
#gpt-button:hover {
background-color: #0056b3;
border-color: #0056b3;
}
.previous-chats-container h2 {
text-align: center;
margin-bottom: 20px;
}
#chat-history-list {
list-style-type: none;
padding: 0;
}
#chat-history-list li {
background-color: rgba(255, 255, 255, 0.2);
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
cursor: pointer;
}
#microphone-btn svg {
width: 20px;
height: 20px;
}
#microphone-btn {
padding: 8px;
border-radius: 50%;
background: #ccc;
}
#microphone-btn svg {
vertical-align: middle;
}
</style>
</head>
<body>
<div class="previous-chats-container">
<h2>Previous Chats</h2>
<ul id="chat-history-list" class="list-group">
<!-- Older chat histories will be added here -->
</ul>
</div>
<div class="chat-container">
<div class="chat-header">
<h1>Gemini AI Model - Free Chatbot</h1>
<button class="btn btn-secondary" id="toggle-sidebar-btn">Previous chats</button>
<button class="btn btn-secondary" id="new-chat-btn">New Chat</button>
</div>
<div id="chat-history">
{% for chat in chat_history %}
<div class="chat-item">
<pre class="chat-message user-message">{{ chat[1] }}</pre>
{% if chat[3] %}
<img class="image-container" src="data:image/png;base64,{{ chat[3] }}" alt="User Image">
{% endif %}
<pre class="chat-message bot-message">{{ chat[2] }}</pre>
</div>
{% endfor %}
</div>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<label for="pdf-input" class="btn btn-primary">
<i class="fas fa-file-pdf"></i>PDF
</label>
<input type="file" id="pdf-input" accept=".pdf" style="display: none;">
<label for="image-input" class="btn btn-primary">
<i class="fas fa-image"></i>Img
</label>
<input type="file" id="image-input" accept="image/*" style="display: none;">
<img id="preview-image" src="#" alt="Preview" style="max-width: 50px; max-height: 50px; display: none;">
</span>
</div>
<input type="text" class="form-control" id="chat-input" placeholder="Type your message...">
<div class="input-group-append">
<button id="gpt-button" class="btn btn-primary">Ask Gemini</button>
<button id="microphone-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-mic" viewBox="0 0 16 16">
<path d="M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5z"/>
<path d="M10 8a2 2 0 1 1-4 0V3a2 2 0 1 1 4 0v5zM8 0a3 3 0 0 0-3 3v5a3 3 0 0 0 6 0V3a3 3 0 0 0-3-3z"/>
</svg>
</button>
</div>
</div>
</div>
<!-- Chat History Modal -->
<div class="modal fade" id="chatHistoryModal" tabindex="-1" aria-labelledby="chatHistoryModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="chatHistoryModalLabel">Chat History</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="chat-history-modal-content"></div>
<div class="input-group mt-3">
<input type="text" class="form-control" id="chat-history-name"
placeholder="Enter a name for this chat history">
<div class="input-group-append">
<button class="btn btn-primary" id="save-chat-history-btn">Save</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://kit.fontawesome.com/your-font-awesome-kit.js"></script>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
function handlePDFUpload(file) {
if (file) {
// Show notification
alert("PDF uploaded successfully!");
}
}
// Attach event listener to PDF file input
$("#pdf-input").change(function() {
var file = this.files[0];
handlePDFUpload(file);
});
var sidebarOpen = false;
// Function to toggle sidebar
function toggleSidebar() {
var sidebar = $(".previous-chats-container");
var chatContainer = $(".chat-container");
if (!sidebarOpen) {
sidebar.css("transform", "translateX(0)");
chatContainer.css("margin-left", "300px");
} else {
sidebar.css("transform", "translateX(-300px)");
chatContainer.css("margin-left", "0");
}
sidebarOpen = !sidebarOpen;
}
// Function to close sidebar when chat history is clicked
function closeSidebar() {
if (sidebarOpen) {
toggleSidebar();
}
}
// Call toggleSidebar when toggle sidebar button is clicked
$("#toggle-sidebar-btn").click(function () {
toggleSidebar();
});
// Call toggleSidebar when new chat button is clicked
$("#new-chat-btn").click(function () {
closeSidebar(); // Close sidebar when starting a new chat
});
// Call closeSidebar when chat history is clicked
$("#chat-history-list li").click(function () {
closeSidebar();
});
var chatHistories = [];
var currentImage = null;
function appendMessage(message, isUser, imageSrc, isFormatted) {
var chatHistory = $('#chat-history');
var messageClass = isUser ? 'user-message' : 'bot-message';
// Check if the message is not empty or an image is provided
if (message.trim() !== '' || imageSrc) {
var messageDiv = $('<div class="chat-item"></div>');
var messageElement = $('<pre class="chat-message ' + messageClass + '"></pre>').text(message);
messageDiv.append(messageElement);
if (imageSrc) {
var imageNameElement = $('<div class="image-name"></div>');
var imageElement = $('<img src="#" class="image-container">');
imageElement.attr('src', imageSrc);
messageDiv.append(imageNameElement);
messageDiv.append(imageElement);
}
if (!isUser) {
var loadingDotsDiv = $('<div class="loading-dots"><div></div><div></div><div></div></div>');
messageDiv.append(loadingDotsDiv);
}
chatHistory.append(messageDiv);
}
chatHistory.scrollTop(chatHistory[0].scrollHeight); // Scroll to bottom
}
var recognizing;
var recognition;
function startRecognition() {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = "en-US";
recognition.onresult = function(event) {
var transcript = event.results[event.resultIndex][0].transcript;
$("#chat-input").val(transcript);
};
recognition.start();
recognizing = true;
}
function stopRecognition() {
if (recognizing) {
recognition.stop();
recognizing = false;
}
}
// Start voice recognition when microphone clicked
$("#microphone-btn").click(function() {
if (recognizing) {
stopRecognition();
} else {
startRecognition();
}
});
// Stop recognition when chat input focused
$("#chat-input").focus(function() {
stopRecognition();
});
$("#camera-btn").click(function() {
$("#image-input").click();
});
$("#camera-btn").click(function() {
navigator.mediaDevices.getUserMedia({video: true})
.then(stream => {
// Show video stream in img tag
})
.catch(error => {
// Handle error
});
});
function clearChatHistory() {
$('#chat-history').empty();
}
function saveChatHistory(name) {
var chatHistory = $('#chat-history').html();
chatHistories.push({ name: name, content: chatHistory });
updateChatHistoryList();
}
function updateChatHistoryList() {
var chatHistoryList = $('#chat-history-list');
chatHistoryList.empty();
chatHistories.forEach(function(history, index) {
var chatHistoryItem = $('<li class="list-group-item d-flex justify-content-between align-items-center"></li>').text(history.name);
chatHistoryItem.data('chat-history-index', index);
// Delete button
var deleteButton = $('<button class="btn btn-danger btn-sm delete-btn">Delete</button>');
deleteButton.click(function(event) {
event.stopPropagation(); // Prevent triggering li click event
deleteChatHistory(index);
});
chatHistoryItem.append(deleteButton);
chatHistoryItem.click(function() {
showChatHistory(index);
});
chatHistoryList.append(chatHistoryItem);
});
}
function deleteChatHistory(index) {
chatHistories.splice(index, 1);
updateChatHistoryList();
}
$("#new-chat-btn").click(function() {
$('#chatHistoryModal').modal('show');
});
$("#save-chat-history-btn").click(function() {
var chatHistoryName = $('#chat-history-name').val();
if (chatHistoryName) {
saveChatHistory(chatHistoryName);
clearChatHistory();
$('#chat-history-name').val('');
$('#chatHistoryModal').modal('hide');
}
});
function showChatHistory(index) {
var chatHistory = chatHistories[index];
$('#chat-history-modal-content').html(chatHistory.content);
$('#chatHistoryModal').modal('show');
}
$("#gpt-button").click(function() {
var question = $("#chat-input").val();
var image = $("#image-input")[0].files[0];
var pdfFile = $("#pdf-input")[0].files[0];
var formData = new FormData();
formData.append('prompt', question);
formData.append('image', image);
formData.append('pdf_file', pdfFile);
$("#chat-input").val(""); // Clear input after sending
$("#image-input").val(""); // Reset image input after sending
$("#pdf-input").val("")
appendMessage(question, true);
if (image) {
var reader = new FileReader();
reader.onload = function(e) {
appendMessage('', true, e.target.result); // Append user message with image
$("#preview-image").attr("src", "#"); // Remove image preview
$("#preview-image").hide();
};
reader.readAsDataURL(image);
}
$.ajax({
type: "POST",
url: "/",
data: formData,
contentType: false,
processData: false,
success: function(data) {
var isFormatted = data.startsWith('```') || /^\d+\.\s/.test(data);
appendMessage(data, false, null, isFormatted);
}
});
});
$("#image-input").change(function() {
var file = this.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
$("#preview-image").attr("src", e.target.result);
$("#preview-image").show();
}
reader.readAsDataURL(file);
} else {
$("#preview-image").attr("src", "#");
$("#preview-image").hide();
}
});
// Trigger initial resize event
$(window).resize();
</script>
</body>
</html>