Ritesh Thawkar commited on
Commit
5ea28c8
1 Parent(s): bedef20

make some formating changes

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. app.py +2 -1
  3. templates/chat.html +51 -7
.gitignore CHANGED
@@ -1,2 +1,2 @@
1
  .env
2
- .adafsa-env
 
1
  .env
2
+ adafsa-env/
app.py CHANGED
@@ -46,7 +46,7 @@ app.config['SECRET_KEY'] = SECRET_KEY
46
 
47
  embed_model = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-multilingual-base", model_kwargs={"trust_remote_code":True})
48
  llm = ChatGroq(
49
- model="llama-3.1-70b-versatile",
50
  temperature=0.0,
51
  max_tokens=1024,
52
  max_retries=2
@@ -140,6 +140,7 @@ def handle_message(data):
140
  try:
141
  for chunk in rag_chain.stream(question):
142
  emit('response', chunk, room=request.sid)
 
143
  except Exception as e:
144
  emit('response', {"error": "An error occurred while processing your request."}, room=request.sid)
145
 
 
46
 
47
  embed_model = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-multilingual-base", model_kwargs={"trust_remote_code":True})
48
  llm = ChatGroq(
49
+ model="llama-3.1-8b-instant",
50
  temperature=0.0,
51
  max_tokens=1024,
52
  max_retries=2
 
140
  try:
141
  for chunk in rag_chain.stream(question):
142
  emit('response', chunk, room=request.sid)
143
+ print(chunk)
144
  except Exception as e:
145
  emit('response', {"error": "An error occurred while processing your request."}, room=request.sid)
146
 
templates/chat.html CHANGED
@@ -283,6 +283,11 @@
283
  padding-right: 40px;
284
  list-style-position: outside;
285
  }
 
 
 
 
 
286
  </style>
287
 
288
 
@@ -413,7 +418,7 @@
413
  <script>
414
 
415
  // Initialize Socket.IO client
416
- var socket = io.connect('https://ritesh-hf-adafsa-flask-app-demo.hf.space', {
417
  transports: ['websocket']
418
  });
419
 
@@ -505,16 +510,53 @@
505
  socket.emit('message', { question: question, session_id: 'abc123' });
506
  }
507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  function appendAnswer(answer) {
509
  let lastElement = $(".chat-container .chat-block:last-child");
510
 
511
  const englishRegex = /[0-9]+(?:\.[0-9]+)?/g;
512
 
513
  // Wrap English text and numbers in <span dir="ltr">
514
- formatted_answer = answer.replace(englishRegex, function(match) {
515
  return `<span class="special-span px-1" dir="ltr">${match}</span>`;
516
  });
517
 
 
 
518
  if (lastElement.hasClass("response-block")) {
519
  $(".chat-container .chat-block:last-child").find(".message-content").html(formatted_answer);
520
  } else {
@@ -533,6 +575,8 @@
533
  </div>
534
  </div>
535
  `);
 
 
536
  }
537
 
538
  $(".chat-container").scrollTop($(".chat-container")[0].scrollHeight);
@@ -592,7 +636,7 @@
592
  console.warn("Disconnected from server:", reason);
593
  response = "";
594
  // appendAnswer("You have been disconnected from the server. Please refresh the page to reconnect.");
595
- socket = io.connect('https://ritesh-hf-adafsa-flask-app-demo.hf.space', {
596
  transports: ['websocket']
597
  });
598
  });
@@ -682,10 +726,10 @@
682
  }
683
  charIndex = nextCharIndex;
684
  } else {
685
- let i = node.childNodes.length;
686
- while (i--) {
687
- nodeStack.push(node.childNodes[i]);
688
- }
689
  }
690
  }
691
 
 
283
  padding-right: 40px;
284
  list-style-position: outside;
285
  }
286
+
287
+ [dir="ltr"] {
288
+ direction: ltr;
289
+ unicode-bidi: embed;
290
+ }
291
  </style>
292
 
293
 
 
418
  <script>
419
 
420
  // Initialize Socket.IO client
421
+ var socket = io.connect('http://127.0.0.1:5000', {
422
  transports: ['websocket']
423
  });
424
 
 
510
  socket.emit('message', { question: question, session_id: 'abc123' });
511
  }
512
 
513
+ function addLtrToEnglishContent(parentElement) {
514
+ // Regular expression to detect English content (letters, numbers, punctuation)
515
+ const englishOnlyRegex = /^[A-Za-z0-9.,%:\/\-\s]+$/;
516
+
517
+ // Get all the elements inside the parent element
518
+ const elements = parentElement.querySelectorAll('*');
519
+
520
+ // Loop through each element and check its text content
521
+ elements.forEach(function(element) {
522
+ // Trim whitespace from the content and check if it matches the regex
523
+ const textContent = element.textContent.trim();
524
+ if (englishOnlyRegex.test(textContent)) {
525
+ // If the content is in English, add dir="ltr" to the element
526
+ element.setAttribute('dir', 'ltr');
527
+ }
528
+ });
529
+ }
530
+
531
+ function addLtrAttribute(htmlString) {
532
+ // Regular expression to match opening tags
533
+ const tagPattern = /<([a-zA-Z1-6]+)([^>]*)>\s*([a-zA-Z0-9\s]+)\s*<\/\1>/g;
534
+
535
+ // Function to add dir="ltr" attribute if not present
536
+ return htmlString.replace(tagPattern, (match, tagName, attributes, content) => {
537
+ // Check if the tag already contains a dir attribute
538
+ if (!/dir\s*=\s*['"]?ltr['"]?/.test(attributes)) {
539
+ attributes = `${attributes} dir="ltr"`;
540
+ }
541
+
542
+ // Reconstruct the tag with the new attribute
543
+ return `<${tagName}${attributes}>${content}</${tagName}>`;
544
+ });
545
+ }
546
+
547
+
548
  function appendAnswer(answer) {
549
  let lastElement = $(".chat-container .chat-block:last-child");
550
 
551
  const englishRegex = /[0-9]+(?:\.[0-9]+)?/g;
552
 
553
  // Wrap English text and numbers in <span dir="ltr">
554
+ let formatted_answer = answer.replace(englishRegex, function(match) {
555
  return `<span class="special-span px-1" dir="ltr">${match}</span>`;
556
  });
557
 
558
+ formatted_answer = addLtrAttribute(formatted_answer);
559
+
560
  if (lastElement.hasClass("response-block")) {
561
  $(".chat-container .chat-block:last-child").find(".message-content").html(formatted_answer);
562
  } else {
 
575
  </div>
576
  </div>
577
  `);
578
+ const parentEle = document.querySelectorAll('.response-block .message-content')[-1];
579
+ addLtrToEnglishContent(parentEle);
580
  }
581
 
582
  $(".chat-container").scrollTop($(".chat-container")[0].scrollHeight);
 
636
  console.warn("Disconnected from server:", reason);
637
  response = "";
638
  // appendAnswer("You have been disconnected from the server. Please refresh the page to reconnect.");
639
+ socket = io.connect('http://127.0.0.1:5000', {
640
  transports: ['websocket']
641
  });
642
  });
 
726
  }
727
  charIndex = nextCharIndex;
728
  } else {
729
+ let i = node.childNodes.length;
730
+ while (i--) {
731
+ nodeStack.push(node.childNodes[i]);
732
+ }
733
  }
734
  }
735