-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.html
420 lines (377 loc) · 11.6 KB
/
gemini.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gemini Pro Chat</title>
<link rel="icon" href="https://t3.ftcdn.net/jpg/04/60/06/54/360_F_460065489_gHW9RP5dClJg6EAaexqu5uYxlcX7XVmJ.jpg" type="image/x-icon">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.7/css/all.css">
<script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/markdown-it/13.0.1/markdown-it.min.js"></script>
<style>
.chat-box {
height: 100%; /* subtract the space occupied by the navbar and footer */
overflow-y: scroll;
width: 100%;
}
@media only screen and (max-width: 480px) {
.chat-box {
height: calc(100vh - 300px); /* adjust the height value as per your requirement */
overflow-y: scroll;
}
}
.message {
margin-bottom: 10px;
padding: 10px;
padding-bottom: 0;
border-radius: 10px;
display: inline-block;
max-width: 85%;
word-wrap: break-word;
white-space: normal;
}
.left-side {
background-color: lightgray;
float: left;
}
.right-side {
background-color: lightgreen;
float: right;
}
.popup {
position: fixed;
bottom: 30vh;
left: 50%;
transform: translateX(-50%);
background-color: rgba(0, 0, 0, 0.6);
color: white;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
display: none;
}
/* Toggle Switch */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
}
input:checked + .slider {
background-color: #555261;
}
input:checked + .slider:before {
transform: translateX(26px);
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
/* Dark Theme */
.dark-mode .dark-theme {
background-color: #333;
color: #fff;
}
.dark-mode .nav{
background-color: #333;
color: #fff;
}
.dark-mode .dark-text {
color: #fff;
}
.dark-mode .card {
background-color: #333;
color: #fff;
}
.dark-mode .popup {
background-color: #fff;
color: #333;
}
.dark-mode .fa-clipboard {
color: #212529;
background-color: #7cc;
border-color: #5bc2c2
}
.dark-mode .fa-clipboard:hover {
color: #212529;
background-color: #52bebe;
border-color: #8ad3d3
}
.bg-skyblue{
background-color: #e3f2fd;
}
</style>
</head>
<body class=" bg-secondary">
<div class="container-fluid">
<div class="row ">
<div class="col-md-12 mb-3">
<div class="card mt-3">
<div class="card-header row">
<div class="col-6 ">
<h3>Gemini Pro Vision</h3>
</div>
<div class="col-6 offset-md-3 col-md-3 text-right">
<a type="button" class="btn btn-outline-danger " onclick="clearContent()">Clear</a>
</div>
</div>
<div class="card-body chat-box rounded p1" id="chatbox"><span id="copy-popup" class="popup">Copied!</span></div>
<div class="card-footer">
<div class="form-group row">
<div class="col-md-7 mb-1">
<textarea id="userInput" rows="1" class="form-control dark-theme" placeholder="Type your message here..."></textarea>
</div>
<div class="col-md-3">
<input id="sendButton" type="button" value="Send" class=" form-control btn btn-success btn-block " />
</div>
<div class="col-md-2">
<label for="userfile" class="btn btn-secondary btn-block ">
<i class="bi bi-image "></i> <span id="fileLabel">Upload A Image</span>
<input type="file" name="file" id="userfile" accept="image/*" class="form-control" style="display: none;" onchange="updateFileLabel()">
<img id="output" width="40" height="25" style="display: none;" class="float-right "/>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
<script>
function updateFileLabel() {
const fileInput = document.getElementById("userfile");
const fileLabel = document.getElementById("fileLabel");
const image = document.getElementById('output');
const file = fileInput.files[0];
if (fileInput.files.length > 0) {
const maxLength = 25; // Set your desired maximum length
const fileName = file.name.length > maxLength ? file.name.substring(0, maxLength) + "..." : file.name;
fileLabel.innerText = fileName;
image.style.display = 'block';
image.src = URL.createObjectURL(file);
} else {
fileLabel.innerText = "Choose an Image to get its Details";
image.style.display = 'none';
}
}
</script>
<script>
const toggleSwitch = document.querySelector('#darkModeToggle');
toggleSwitch.addEventListener('change', switchTheme);
function switchTheme(event) {
if (event.target.checked) {
document.body.classList.add('dark-mode');
document.nav.classList.remove('bg-skyblue');
} else {
document.body.classList.remove('dark-mode');
}
}
</script>
<script>
function clearContent(){
document.getElementById('chatbox').innerHTML = '';
}
const url = new URL(window.location.href);
const key = url.searchParams.get('key');
if (key) {
$("#apiKey").val(key);
$("#apiKey").hide();
}
const chatbox = $("#chatbox");
const userInput = $("#userInput");
const file = $("#userfile");
const sendButton = $("#sendButton");
let contents = [];
userInput.on("keydown", (event) => {
if (event.keyCode === 13 && !event.ctrlKey && !event.shiftKey) {
event.preventDefault();
sendButton.click();
} else if (event.keyCode === 13 && (event.ctrlKey || event.shiftKey)) {
event.preventDefault();
const cursorPosition = userInput.prop("selectionStart");
const currentValue = userInput.val();
userInput.val(
currentValue.slice(0, cursorPosition) +
"\n" +
currentValue.slice(cursorPosition)
);
userInput.prop("selectionStart", cursorPosition + 1);
userInput.prop("selectionEnd", cursorPosition + 1);
}
});
const getBase64Image = (file) => {
const reader = new FileReader();
reader.readAsDataURL(file);
return new Promise((resolve, reject) => {
reader.onload = () => resolve(reader.result.split(',')[1]);
reader.onerror = (error) => reject(error);
});
};
sendButton.on("click", () => {
const input = document.getElementById("userfile");
const message = userInput.val();
if (message === "" && input.files.length === 0) {
alert("Please provide Text or Image input.");
return;
}
if (input && input.files.length > 0) {
const file = input.files[0];
const imgsrc = URL.createObjectURL(file);
getBase64Image(file).then((base64String) => {
if (message === "") {
let userMessageHtml = '<pre><div class="message right-side " ><img class="img-thumbnail mb-2" width="400" src=" ' + imgsrc + ' "/></div></pre>';
chatbox.append(userMessageHtml);
fetchreply(message, base64String);
}
else {
const displaytext = window.markdownit().render(message);
let userMessageHtml = '<pre><div class="message right-side " >' + displaytext + '</div></pre>';
chatbox.append(userMessageHtml);
fetchreply(message, base64String);
}
});
}
else {
if (message) {
contents.push({
"role": "user",
"parts": [
{
"text": message
}
]
});
const displaytext = window.markdownit().render(message);
let userMessageHtml = '<pre><div class="message right-side " >' + displaytext + '</div></pre>';
chatbox.append(userMessageHtml);
fetchMessages();
}
}
chatbox.animate({ scrollTop: 20000000 }, "slow");
userInput.val("");
sendButton.val("Generating Response...");
sendButton.prop("disabled", true);
});
function fetchreply(message,base64String) {
const apiKey = "AIzaSyDz83LLl7dKzYsCZoATDd5boMlgjbRvhhE";
var settings = {
"url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=" + apiKey,
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"contents": [
{
"parts": [
{
"text": message
},
{
"inline_data": {
"mime_type": "image/jpeg",
"data": base64String
}
}
]
}
]
})
};
$.ajax(settings).done(function(response) { processAjaxResponse(response); })
.fail(function(jqXHR, textStatus, errorThrown) { handleAjaxFailure(jqXHR);});
}
function fetchMessages() {
const apiKey = "AIzaSyDz83LLl7dKzYsCZoATDd5boMlgjbRvhhE";
var settings = {
"url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" + apiKey,
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"contents": contents
})
};
$.ajax(settings).done(function(response) { processAjaxResponse(response); })
.fail(function(jqXHR, textStatus, errorThrown) { handleAjaxFailure(jqXHR);});
}
function processAjaxResponse(response) {
console.log(response);
const reply = response.candidates[0].content;
contents.push({
"role": reply.role,
"parts": [{
"text": reply.parts[0].text
}]
});
const htmlText = window.markdownit().render(reply.parts[0].text);
const botMessageHtml = '<pre><div class="message left-side" id="' + CryptoJS.MD5(htmlText) + '">' + htmlText + '</div><i class="far fa-clipboard ml-1 btn btn-outline-dark" id="' + CryptoJS.MD5(htmlText) + '-copy"></i></pre>';
chatbox.append(botMessageHtml);
// Add event listener to the copy icon
var copyIcon = document.getElementById(CryptoJS.MD5(htmlText) + '-copy');
var copyText = document.getElementById(CryptoJS.MD5(htmlText));
copyIcon.addEventListener("click", function () {
var tempTextarea = document.createElement("textarea");
tempTextarea.value = copyText.textContent;
document.body.appendChild(tempTextarea);
tempTextarea.select();
document.execCommand("copy");
document.body.removeChild(tempTextarea);
// Display "Copied!" popup
var copyPopup = document.getElementById("copy-popup");
copyPopup.style.display = "block";
setTimeout(function () {
copyPopup.style.display = "none";
}, 1000); // Display for 1 second
});
chatbox.animate({
scrollTop: 20000000
}, "slow");
sendButton.val("SUBMIT");
sendButton.prop("disabled", false);
}
function handleAjaxFailure(jqXHR) {
// Handle failure
sendButton.val("Error");
let errorText = "Error: " + jqXHR.responseJSON.error.message;
let errorMessage = '<pre><div class="message left-side text-danger" >' + errorText + '</div></pre>';
chatbox.append(errorMessage);
chatbox.animate({
scrollTop: 20000000
}, "slow");
}
</script>
</html>