Update static/index.html
Browse files- static/index.html +136 -124
static/index.html
CHANGED
@@ -302,155 +302,167 @@
|
|
302 |
</div>
|
303 |
</div>
|
304 |
<script>
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
|
|
310 |
|
311 |
-
|
312 |
|
313 |
-
|
314 |
-
|
315 |
-
|
|
|
|
|
316 |
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
|
|
326 |
</div>
|
327 |
-
</div
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
return incomingChat;
|
333 |
-
};
|
334 |
-
|
335 |
-
const generateResponse = (chatElement) => {
|
336 |
-
const API_URL = "/generate/";
|
337 |
-
|
338 |
-
const requestOptions = {
|
339 |
-
method: "POST",
|
340 |
-
headers: {
|
341 |
-
"Content-Type": "application/x-www-form-urlencoded"
|
342 |
-
},
|
343 |
-
body: new URLSearchParams({
|
344 |
-
prompt: userMessage,
|
345 |
-
history: "[]",
|
346 |
-
temperature: "0.9",
|
347 |
-
max_new_tokens: "512",
|
348 |
-
top_p: "0.95",
|
349 |
-
repetition_penalty: "1.0"
|
350 |
-
})
|
351 |
};
|
352 |
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
chatElement.querySelector(".icon-container").appendChild(regenerateIcon);
|
365 |
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
}).finally(() => chatContainer.scrollTo(0, chatContainer.scrollHeight));
|
373 |
-
};
|
374 |
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
|
|
|
|
|
|
380 |
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
}, 1000);
|
387 |
-
});
|
388 |
-
};
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
-
const
|
395 |
-
|
396 |
-
|
397 |
-
<img src="${className === "outgoing" ? "img/user.jpg" : "img/chatbot.jpg"}" alt="">
|
398 |
-
<p>${message}</p>
|
399 |
-
</div>
|
400 |
-
<div class="icon-container">
|
401 |
-
<span class="material-symbols-rounded">content_copy</span>
|
402 |
-
${className === "outgoing" ? '<span class="material-symbols-rounded">edit</span>' : ''}
|
403 |
-
</div>
|
404 |
-
</div>`;
|
405 |
-
chatElement.innerHTML = html;
|
406 |
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
|
|
415 |
|
416 |
-
|
417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
|
419 |
-
|
420 |
-
|
|
|
|
|
421 |
|
422 |
-
|
423 |
-
|
424 |
-
if (!userMessage) return;
|
425 |
|
426 |
-
|
427 |
-
|
|
|
428 |
|
429 |
-
|
|
|
430 |
|
431 |
-
|
432 |
-
chatContainer.scrollTo(0, chatContainer.scrollHeight);
|
433 |
|
434 |
-
|
435 |
-
|
436 |
-
chatContainer.scrollTo(0, chatContainer.scrollHeight);
|
437 |
|
438 |
-
|
439 |
-
|
|
|
440 |
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
|
|
|
|
|
|
|
|
445 |
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
|
|
|
|
450 |
}
|
451 |
-
});
|
452 |
|
453 |
-
|
|
|
|
|
|
|
454 |
</script>
|
455 |
</body>
|
456 |
</html>
|
|
|
302 |
</div>
|
303 |
</div>
|
304 |
<script>
|
305 |
+
document.addEventListener("DOMContentLoaded", () => {
|
306 |
+
const textarea = document.querySelector("textarea"),
|
307 |
+
chatContainer = document.querySelector(".chat-container"),
|
308 |
+
sendChatBtn = document.querySelector("span.material-symbols-rounded"),
|
309 |
+
toggleIcon = document.querySelector(".typing-controls span"),
|
310 |
+
defaultText = document.querySelector(".default-text");
|
311 |
|
312 |
+
let userMessage = null;
|
313 |
|
314 |
+
if (toggleIcon) {
|
315 |
+
toggleIcon.onclick = () => {
|
316 |
+
document.body.classList.toggle("light-mode");
|
317 |
+
};
|
318 |
+
}
|
319 |
|
320 |
+
const createTypingAnimation = () => {
|
321 |
+
const html = `
|
322 |
+
<div class="chat-content">
|
323 |
+
<div class="chat-details">
|
324 |
+
<img src="img/chatbot.jpg" alt="">
|
325 |
+
<div class="typing-animation">
|
326 |
+
<div class="typing-dot" style="--delay: 0s"></div>
|
327 |
+
<div class="typing-dot" style="--delay: 0.15s"></div>
|
328 |
+
<div class="typing-dot" style="--delay: 0.3s"></div>
|
329 |
+
</div>
|
330 |
</div>
|
331 |
+
</div>`;
|
332 |
+
const incomingChat = document.createElement("div");
|
333 |
+
incomingChat.classList.add("chat", "incoming");
|
334 |
+
incomingChat.innerHTML = html;
|
335 |
+
return incomingChat;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
};
|
337 |
|
338 |
+
const generateResponse = (chatElement) => {
|
339 |
+
const API_URL = "/generate/";
|
340 |
+
|
341 |
+
const requestOptions = {
|
342 |
+
method: "POST",
|
343 |
+
headers: {
|
344 |
+
"Content-Type": "application/x-www-form-urlencoded"
|
345 |
+
},
|
346 |
+
body: new URLSearchParams({
|
347 |
+
prompt: userMessage,
|
348 |
+
history: "[]",
|
349 |
+
temperature: "0.9",
|
350 |
+
max_new_tokens: "512",
|
351 |
+
top_p: "0.95",
|
352 |
+
repetition_penalty: "1.0"
|
353 |
+
})
|
354 |
+
};
|
355 |
|
356 |
+
fetch(API_URL, requestOptions).then(res => res.json()).then(data => {
|
357 |
+
chatElement.querySelector(".typing-animation").remove();
|
358 |
+
const pElement = document.createElement("p");
|
359 |
+
pElement.textContent = data.response;
|
360 |
+
chatElement.querySelector(".chat-details").appendChild(pElement);
|
|
|
361 |
|
362 |
+
// Add regenerate icon
|
363 |
+
const regenerateIcon = document.createElement("span");
|
364 |
+
regenerateIcon.classList.add("material-symbols-rounded");
|
365 |
+
regenerateIcon.textContent = "refresh";
|
366 |
+
regenerateIcon.addEventListener("click", () => regenerateResponse(chatElement, userMessage));
|
367 |
+
chatElement.querySelector(".icon-container").appendChild(regenerateIcon);
|
|
|
|
|
368 |
|
369 |
+
}).catch(() => {
|
370 |
+
chatElement.querySelector(".typing-animation").remove();
|
371 |
+
const pElement = document.createElement("p");
|
372 |
+
pElement.classList.add("error");
|
373 |
+
pElement.textContent = "Oops! Something went wrong. Please try again.";
|
374 |
+
chatElement.querySelector(".chat-details").appendChild(pElement);
|
375 |
+
}).finally(() => chatContainer.scrollTo(0, chatContainer.scrollHeight));
|
376 |
+
};
|
377 |
|
378 |
+
const regenerateResponse = (chatElement, message) => {
|
379 |
+
const typingAnimation = createTypingAnimation();
|
380 |
+
chatElement.replaceWith(typingAnimation);
|
381 |
+
generateResponse(typingAnimation);
|
382 |
+
};
|
|
|
|
|
|
|
383 |
|
384 |
+
const copyMessage = (element) => {
|
385 |
+
navigator.clipboard.writeText(element.innerText).then(() => {
|
386 |
+
element.textContent = "Copied";
|
387 |
+
setTimeout(() => {
|
388 |
+
element.textContent = "content_copy";
|
389 |
+
}, 1000);
|
390 |
+
});
|
391 |
+
};
|
392 |
|
393 |
+
const createChatElement = (message, className) => {
|
394 |
+
const chatElement = document.createElement("div");
|
395 |
+
chatElement.classList.add("chat", className);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
|
397 |
+
const html = `
|
398 |
+
<div class="chat-content">
|
399 |
+
<div class="chat-details">
|
400 |
+
<img src="${className === "outgoing" ? "img/user.jpg" : "img/chatbot.jpg"}" alt="">
|
401 |
+
<p>${message}</p>
|
402 |
+
</div>
|
403 |
+
<div class="icon-container">
|
404 |
+
<span class="material-symbols-rounded">content_copy</span>
|
405 |
+
${className === "outgoing" ? '<span class="material-symbols-rounded">edit</span>' : ''}
|
406 |
+
</div>
|
407 |
+
</div>`;
|
408 |
+
chatElement.innerHTML = html;
|
409 |
|
410 |
+
if (className === "outgoing") {
|
411 |
+
const editIcon = chatElement.querySelector(".material-symbols-rounded:nth-child(3)");
|
412 |
+
if (editIcon) {
|
413 |
+
editIcon.addEventListener("click", () => {
|
414 |
+
textarea.value = message;
|
415 |
+
chatElement.remove();
|
416 |
+
textarea.focus();
|
417 |
+
});
|
418 |
+
}
|
419 |
+
}
|
420 |
|
421 |
+
const copyIcon = chatElement.querySelector(".material-symbols-rounded:nth-child(2)");
|
422 |
+
if (copyIcon) {
|
423 |
+
copyIcon.addEventListener("click", () => copyMessage(copyIcon));
|
424 |
+
}
|
425 |
|
426 |
+
return chatElement;
|
427 |
+
};
|
|
|
428 |
|
429 |
+
const handleChat = () => {
|
430 |
+
userMessage = textarea.value.trim();
|
431 |
+
if (!userMessage) return;
|
432 |
|
433 |
+
textarea.value = "";
|
434 |
+
textarea.style.height = `${textarea.scrollHeight}px`;
|
435 |
|
436 |
+
if (!chatContainer.querySelector(".chat")) defaultText.style.display = "none";
|
|
|
437 |
|
438 |
+
chatContainer.appendChild(createChatElement(userMessage, "outgoing"));
|
439 |
+
chatContainer.scrollTo(0, chatContainer.scrollHeight);
|
|
|
440 |
|
441 |
+
const incomingChat = createTypingAnimation();
|
442 |
+
chatContainer.appendChild(incomingChat);
|
443 |
+
chatContainer.scrollTo(0, chatContainer.scrollHeight);
|
444 |
|
445 |
+
generateResponse(incomingChat);
|
446 |
+
};
|
447 |
+
|
448 |
+
if (textarea) {
|
449 |
+
textarea.addEventListener("input", () => {
|
450 |
+
textarea.style.height = "auto";
|
451 |
+
textarea.style.height = `${textarea.scrollHeight}px`;
|
452 |
+
});
|
453 |
|
454 |
+
textarea.addEventListener("keydown", (e) => {
|
455 |
+
if (e.key === "Enter" && !e.shiftKey) {
|
456 |
+
e.preventDefault();
|
457 |
+
handleChat();
|
458 |
+
}
|
459 |
+
});
|
460 |
}
|
|
|
461 |
|
462 |
+
if (sendChatBtn) {
|
463 |
+
sendChatBtn.addEventListener("click", handleChat);
|
464 |
+
}
|
465 |
+
});
|
466 |
</script>
|
467 |
</body>
|
468 |
</html>
|