Ashrafb commited on
Commit
48cc8ce
1 Parent(s): 6f1a6c4

Update static/index.html

Browse files
Files changed (1) hide show
  1. static/index.html +136 -124
static/index.html CHANGED
@@ -302,155 +302,167 @@
302
  </div>
303
  </div>
304
  <script>
305
- const textarea = document.querySelector("textarea"),
306
- chatContainer = document.querySelector(".chat-container"),
307
- sendChatBtn = document.querySelector("span.material-symbols-rounded"),
308
- toggleIcon = document.querySelector(".typing-controls span"),
309
- defaultText = document.querySelector(".default-text");
 
310
 
311
- let userMessage = null;
312
 
313
- toggleIcon.onclick = () => {
314
- document.body.classList.toggle("light-mode");
315
- };
 
 
316
 
317
- const createTypingAnimation = () => {
318
- const html = `
319
- <div class="chat-content">
320
- <div class="chat-details">
321
- <img src="img/chatbot.jpg" alt="">
322
- <div class="typing-animation">
323
- <div class="typing-dot" style="--delay: 0s"></div>
324
- <div class="typing-dot" style="--delay: 0.15s"></div>
325
- <div class="typing-dot" style="--delay: 0.3s"></div>
 
326
  </div>
327
- </div>
328
- </div>`;
329
- const incomingChat = document.createElement("div");
330
- incomingChat.classList.add("chat", "incoming");
331
- incomingChat.innerHTML = html;
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
- fetch(API_URL, requestOptions).then(res => res.json()).then(data => {
354
- chatElement.querySelector(".typing-animation").remove();
355
- const pElement = document.createElement("p");
356
- pElement.textContent = data.response;
357
- chatElement.querySelector(".chat-details").appendChild(pElement);
 
 
 
 
 
 
 
 
 
 
 
 
358
 
359
- // Add regenerate icon
360
- const regenerateIcon = document.createElement("span");
361
- regenerateIcon.classList.add("material-symbols-rounded");
362
- regenerateIcon.textContent = "refresh";
363
- regenerateIcon.addEventListener("click", () => regenerateResponse(chatElement, userMessage));
364
- chatElement.querySelector(".icon-container").appendChild(regenerateIcon);
365
 
366
- }).catch(() => {
367
- chatElement.querySelector(".typing-animation").remove();
368
- const pElement = document.createElement("p");
369
- pElement.classList.add("error");
370
- pElement.textContent = "Oops! Something went wrong. Please try again.";
371
- chatElement.querySelector(".chat-details").appendChild(pElement);
372
- }).finally(() => chatContainer.scrollTo(0, chatContainer.scrollHeight));
373
- };
374
 
375
- const regenerateResponse = (chatElement, message) => {
376
- const typingAnimation = createTypingAnimation();
377
- chatElement.replaceWith(typingAnimation);
378
- generateResponse(typingAnimation);
379
- };
 
 
 
380
 
381
- const copyMessage = (element) => {
382
- navigator.clipboard.writeText(element.innerText).then(() => {
383
- element.textContent = "Copied";
384
- setTimeout(() => {
385
- element.textContent = "content_copy";
386
- }, 1000);
387
- });
388
- };
389
 
390
- const createChatElement = (message, className) => {
391
- const chatElement = document.createElement("div");
392
- chatElement.classList.add("chat", className);
 
 
 
 
 
393
 
394
- const html = `
395
- <div class="chat-content">
396
- <div class="chat-details">
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
- if (className === "outgoing") {
408
- const editIcon = chatElement.querySelector(".material-symbols-rounded:nth-child(3)");
409
- editIcon.addEventListener("click", () => {
410
- textarea.value = message;
411
- chatElement.remove();
412
- textarea.focus();
413
- });
414
- }
 
 
 
 
415
 
416
- const copyIcon = chatElement.querySelector(".material-symbols-rounded:nth-child(2)");
417
- copyIcon.addEventListener("click", () => copyMessage(copyIcon));
 
 
 
 
 
 
 
 
418
 
419
- return chatElement;
420
- };
 
 
421
 
422
- const handleChat = () => {
423
- userMessage = textarea.value.trim();
424
- if (!userMessage) return;
425
 
426
- textarea.value = "";
427
- textarea.style.height = `${textarea.scrollHeight}px`;
 
428
 
429
- if (!chatContainer.querySelector(".chat")) defaultText.style.display = "none";
 
430
 
431
- chatContainer.appendChild(createChatElement(userMessage, "outgoing"));
432
- chatContainer.scrollTo(0, chatContainer.scrollHeight);
433
 
434
- const incomingChat = createTypingAnimation();
435
- chatContainer.appendChild(incomingChat);
436
- chatContainer.scrollTo(0, chatContainer.scrollHeight);
437
 
438
- generateResponse(incomingChat);
439
- };
 
440
 
441
- textarea.addEventListener("input", () => {
442
- textarea.style.height = "auto";
443
- textarea.style.height = `${textarea.scrollHeight}px`;
444
- });
 
 
 
 
445
 
446
- textarea.addEventListener("keydown", (e) => {
447
- if (e.key === "Enter" && !e.shiftKey) {
448
- e.preventDefault();
449
- handleChat();
 
 
450
  }
451
- });
452
 
453
- sendChatBtn.addEventListener("click", handleChat);
 
 
 
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>