Ritesh-hf's picture
Update templates/chat.html
4f69e40 verified
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ADAFSA RAG Chatbot Demo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Urbanist:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">
<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=Rubik:ital,wght@0,300..900;1,300..900&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,300..900;1,300..900&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<style>
*:lang(ar-AR){
font-family: "Rubik", sans-serif;
font-optical-sizing: auto;
font-style: normal;
}
.main-chat-container{
position: relative;
background-color: rgb(0, 0, 0, 0.4);
font-family: "DM Sans", sans-serif;
}
.chat-container-wrapper{
width: 100%;
height: 100vh;
background-color: white;
border: 1px solid rgb(221, 221, 221);
display: flex;
flex-direction: column;
box-shadow: 0px 0px 15px 8px rgba(255,255,255,0.1);
z-index: 9999;
font-family: "DM Sans", sans-serif;
}
.chat-container-wrapper * {
font-family: "DM Sans", sans-serif;
}
.chat-container-wrapper a{
color: rgb(11, 172, 235);
text-decoration:underline;
}
.d-none{
display: none;
}
.main-chat-container{
transition: opacity 0.5s ease-in-out;
}
/* Hide scrollbar for Chrome, Safari and Opera */
.styled-scrolllbar::-webkit-scrollbar {
display: none;
}
/* Hide scrollbar for IE, Edge and Firefox */
::-webkit-scrollbar {
width: 6px;
border-radius: 5px;
}
/* Track */
::-webkit-scrollbar-track {
background: #f1f1f1;
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 10px;
}
/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: #555;
}
/* Hide scrollbar for Chrome, Safari and Opera */
.hide-scrolllbar::-webkit-scrollbar {
display: none;
}
/* Hide scrollbar for IE, Edge and Firefox */
.hide-scrolllbar {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
.question-item:hover{
background-color: rgb(245, 245, 245);
}
.sendbtn{
background-color: black;
transition: all 0.2s ease-in-out;
animation: scale-out 0.1s;
}
.sendbtn:hover{
color: white;
scale: 1.1;
}
.sendbtn:hover svg{
stroke: white
}
div[contenteditable]:empty:before {
content: attr(data-placeholder);
display: inline-block;
pointer-events: none;
}
.chat-loader {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 10px;
}
.chat-loader .dot {
width: 8px;
aspect-ratio: 1;
border-radius: 50%;
background-color: gray;
animation: l5 0.6s ease-in-out infinite;
}
.chat-loader .dot:nth-child(1){
animation-delay: 0s;
}
.chat-loader .dot:nth-child(2){
animation-delay: 0.2s;
}
.chat-loader .dot:nth-child(3){
animation-delay: 0.3s;
}
.message-content *{
width: 100%;
font-size: 18px;
}
.neumorphic-box-shadow{
border-radius: 50px;
background: #e0e0e0;
box-shadow: 20px 20px 17px #cecece,
-20px -20px 17px #f2f2f2;
}
@keyframes l5 {
0% {scale: 0.2; opacity: 0.2;}
33% {scale: 0.5; opacity: 0.5;}
66% {scale: 0.7; opacity: 0.7;}
100%{scale: 1; opacity: 1; }
}
.pulse {
animation: pulse-animation 2s infinite;
}
@keyframes pulse-animation {
0% {
scale: 0.9;
box-shadow: 0 0 0 0px rgba(0, 0, 0, 0.2);
}
50%{
scale: 1
}
100% {
scale: 0.9;
box-shadow: 0 0 0 20px rgba(0, 0, 0, 0);
}
}
#userwayAccessibilityIcon{
display: none;
}
.bg-special-secondary{
background-color: rgb(230, 230, 230);
}
.message-content *:not(i){
font-family: "DM Sans", sans-serif;
}
.message-content strong{
color: black;
font-weight: bold;
}
.message-content h3:not(:first-of-type){
margin: 18px 0px 10px 0px;
color: black;
}
.message-content h4{
margin: 15px 0px 8px 0px;
}
.message-content h5, .message-content h6{
margin: 12px 0px 6px 0px;
}
.message-content ol, .message-content ul{
margin: 18px 0px;
list-style-type:disc;
list-style-position: outside;
padding-right: 40px;
}
.message-content ol li, .message-content ul li{
margin: 12px 0px ;
}
.response-block .message-content p{
margin-bottom: 18px;
text-wrap: pretty;
}
.message-content a{
text-wrap: pretty;
}
.loading-text{
position: relative;
}
.loading-text::after{
content: '....';
animation: loading-text-animation 1s infinite;
}
@keyframes loading-text-animation {
0%{
content: ''
}
25%{
content: '.'
}
50%{
content: '..'
}
75%{
content: '...'
}
100%{
content: '....'
}
}
*:not(.special-span){
unicode-bidi:bidi-override;
direction: RTL;
}
#question-box span[dir="rtl"] {
direction: rtl;
unicode-bidi: embed;
}
#question-box span[dir="ltr"] {
direction: ltr;
unicode-bidi: embed;
}
ol, ul{
list-style: disc;
padding-right: 40px;
list-style-position: outside;
}
[dir="ltr"] {
direction: ltr;
unicode-bidi: embed;
}
</style>
</head>
<body>
<div class="main-chat-container show-chatbot flex items-center justify-center">
<div class="w-full chat-container-wrapper hide-scrolllbar p-sm-4 p-3 relative">
<div class="max-w-5xl mx-auto chatbot-header w-full flex flex-row-reverse items-center justify-between pb-2" style="direction: ltr;">
<div style="display: flex; align-items: center; gap: 12px;">
<img src="./static/logow.png" class="w-52" alt="logo">
<!-- <span class="text-dark mb-0 chatbot-title" style="font-weight: 800; font-size: 20px; direction: ltr;">ADAFSA Chatbot</span> -->
</div>
<div class="flex items-center" style="gap: 5px;">
<button class="clearBtn flex items-center justify-center gap-1 px-4 py-2 bg-black rounded-full z-10" type="button" style="direction: ltr;">
<span class="flex items-center justify-center" style="width: 20px; height: 20px;">
<svg width="24px" height="24px" viewBox="-3.2 -3.2 22.40 22.40" xmlns="http://www.w3.org/2000/svg" fill="#050505" stroke="#ffffff" stroke-width="0.064"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path fill="#ffffff" d="M13.9907,1.31133017e-07 C14.8816,1.31133017e-07 15.3277,1.07714 14.6978,1.70711 L13.8556,2.54922 C14.421,3.15654 14.8904,3.85028 15.2448,4.60695 C15.8028,5.79836 16.0583,7.109 15.9888,8.42277 C15.9193,9.73654 15.5268,11.0129 14.8462,12.1388 C14.1656,13.2646 13.2178,14.2053 12.0868,14.8773 C10.9558,15.5494 9.67655,15.9322 8.3623,15.9918 C7.04804,16.0514 5.73937,15.7859 4.55221,15.2189 C3.36505,14.652 2.33604,13.8009 1.55634,12.7413 C0.776635,11.6816 0.270299,10.446 0.0821822,9.14392 C0.00321229,8.59731 0.382309,8.09018 0.928918,8.01121 C1.47553,7.93224 1.98266,8.31133 2.06163,8.85794 C2.20272,9.83451 2.58247,10.7612 3.16725,11.556 C3.75203,12.3507 4.52378,12.989 5.41415,13.4142 C6.30452,13.8394 7.28602,14.0385 8.27172,13.9939 C9.25741,13.9492 10.2169,13.6621 11.0651,13.158 C11.9133,12.6539 12.6242,11.9485 13.1346,11.1041 C13.6451,10.2597 13.9395,9.30241 13.9916,8.31708 C14.0437,7.33175 13.8521,6.34877 13.4336,5.45521 C13.178,4.90949 12.8426,4.40741 12.4402,3.96464 L11.7071,4.69779 C11.0771,5.32776 9.99996,4.88159 9.99996,3.99069 L9.99996,1.31133017e-07 L13.9907,1.31133017e-07 Z M1.499979,4 C2.05226,4 2.499979,4.44772 2.499979,5 C2.499979,5.55229 2.05226,6 1.499979,6 C0.947694,6 0.499979,5.55228 0.499979,5 C0.499979,4.44772 0.947694,4 1.499979,4 Z M3.74998,1.25 C4.30226,1.25 4.74998,1.69772 4.74998,2.25 C4.74998,2.80229 4.30226,3.25 3.74998,3.25 C3.19769,3.25 2.74998,2.80228 2.74998,2.25 C2.74998,1.69772 3.19769,1.25 3.74998,1.25 Z M6.99998,0 C7.55226,0 7.99998,0.447716 7.99998,1 C7.99998,1.55229 7.55226,2 6.99998,2 C6.44769,2 5.99998,1.55229 5.99998,1 C5.99998,0.447716 6.44769,0 6.99998,0 Z"></path> </g></svg>
</span>
<div class="text-white" style="direction: ltr;">Clear</div>
</button>
</div>
</div>
<hr>
<div class="relative chat-container w-full mx-auto hide-scrolllbar rounded-xl mb-20 flex flex-col my-8" style="max-width: 900px; flex-grow: 1; padding: 20px 0px; text-wrap: pretty; overflow-y: auto; overflow-x: hidden;">
<div class="start-block load-chat-block w-full flex-grow flex flex-col items-center justify-center bg-white z-10">
<div class="w-full flex-grow flex flex-col items-center justify-center y-4 text-center">
<img src="./static/bg-image.jpg" class="w-1/2" alt="adafsa-logo">
<h1 class="text-5xl font-semibold mb-4" style="direction: ltr;">
ADAFSA Chatbot Demo
</h1>
<h3 class="max-w-xl text-xl text-center text-dark font-medium d-block mx-auto mb-3">
ابدأ بطرح أسئلتك وسأجيب عليها أو يمكنك استخدام الأسئلة التالية كنقطة بداية
</h3>
</div>
<div class="w-full flex-end mx-auto" style="max-width: 800px;">
<div class="grid md:grid-cols-3 grid-cols-2 m-0 p-0">
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
الموسم المناسب لزراعة الذرة العلفية ؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
ما هي الاحتياجات المائية لتربية الحيوانات؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
ما كمية أعلاف الجت المستلمة في منطقة الظفرة عام <span class="special-span inline-block" dir="ltr"> 2022 </span>
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
الموسم المناسب لزراعة الطماطم في الحقل المكشوف بدولة الإمارات؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
ما هي خطوات إنتاج الشتلات؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
شروط اختيار مكان منحل العسل؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
ما هو تقييم مطعم قصر نجد؟
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 ">
ما مساحات المزارع المروية بالتنقيط في منطقة الرحبة عام <span class="special-span inline-block" dir="ltr"> 2020 </span>
</small>
</div>
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
ما مساحات المزارع المروية بالتنقيط في منطقة أبوظبي عام <span class="special-span inline-block" dir="ltr"> 2020 </span>
</small>
</div>
</div>
</div>
</div>
</div>
<div class="absolute w-full h-full top-0 left-0 z-0 flex items-center justify-center">
<img src="./static/image.png" class="opacity-5" alt="greyscale-image">
</div>
<div class="w-full bg-transparent mx-auto pb-3 absolute bottom-0 left-1/2 z-10 -translate-x-1/2" style="max-width: 900px;">
<div class="input-options-container w-5xl p-3 flex flex-row items-center justify-between border-2 border-gray-400 rounded-full shadow-2xl mx-auto bg-gray-50">
<div id="question-box" dir="auto" class="inline-block px-4 text-2xl overflow-hidden" contenteditable="true" data-placeholder="أكتب سؤالك هنا..." style="width: 100%; max-height: 100px; outline: none; font-weight: 500; font-size: 14px;"></div>
<button class="sendbtn " style="width: 40px; height: 40px; padding: 8px; aspect-ratio: 1; display: flex; align-items: center; justify-content: center; border-radius: 30px; border: none; color: white;">
<svg width="30px" height="30px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 6V18M12 6L7 11M12 6L17 11" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
</button>
</div>
</div>
</div>
</div>
<!-- SocketIO -->
<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script>
<!-- Showdown.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js" integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
// Initialize Socket.IO client
var socket = io.connect('https://ritesh-hf-adafsa-flask-app-demo.hf.space', {
transports: ['websocket']
});
setInterval(()=>{
socket.emit('ping', { "message": "I am alive!"});
}, 30000)
// Empty response variable to keep track of response
var response="";
// Converter for Markdown to HTML
var converter = new showdown.Converter();
// Function to load a chat block indicating that the system is searching
function loadChatBlock(){
let lastMessageElement = $(".chat-container div:last-child");
if(lastMessageElement.hasClass("response-block")){
return;
}
const isRTL = true;
$(".chat-container").append(
`
<div class="chat-block load-chat-block w-full flex flex-row-reverse items-start justify-end bg-secondary p-5 rounded-2xl rtl z-10 bg-gray-100/60">
<div class="flex items-center order-2" >
<span class="bg-white" style="width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50px; padding: 5px; border: 2px solid #c2c2c2;">
<svg fill="#000000" width="36px" height="36px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path d="M21.928 11.607c-.202-.488-.635-.605-.928-.633V8c0-1.103-.897-2-2-2h-6V4.61c.305-.274.5-.668.5-1.11a1.5 1.5 0 0 0-3 0c0 .442.195.836.5 1.11V6H5c-1.103 0-2 .897-2 2v2.997l-.082.006A1 1 0 0 0 1.99 12v2a1 1 0 0 0 1 1H3v5c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5a1 1 0 0 0 1-1v-1.938a1.006 1.006 0 0 0-.072-.455zM5 20V8h14l.001 3.996L19 12v2l.001.005.001 5.995H5z"></path><ellipse cx="8.5" cy="12" rx="1.5" ry="2"></ellipse><ellipse cx="15.5" cy="12" rx="1.5" ry="2"></ellipse><path d="M8 16h8v2H8z"></path></g></svg>
</span>
</div>
<div class="px-3 py-1 order-1">
<div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; font-weight: 500; text-wrap: pretty; text-align: right;">
<span class="loading-text">البحث</span>
</div>
</div>
</div>
`
);
}
function appendQuestion(question) {
if (!$(".start-block").hasClass("d-none")) {
$(".start-block").slideUp(200);
$(".start-block").addClass("d-none");
$(".start-block").removeClass("flex");
}
const englishRegex = /[A-Za-z0-9]+/g;
// Wrap English text and numbers in <span dir="ltr">
formatted_question = question.replace(englishRegex, function(match) {
return `<span class="special-span" dir="ltr"> ${match} </span>`;
});
formatted_question = converter.makeHtml(formatted_question);
$(".chat-container").append(`
<div class="chat-block question-block w-full flex flex-row-reverse items-start justify-end p-5 z-10 rounded-2xl bg-white/60">
<div class="flex items-center order-2 bg-white rounded-full">
<span style="width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50px; padding: 3px; border: 2px solid #c2c2c2;">
<svg fill="#000000" width="36px" height="36px" viewBox="0 0 256 256" id="Flat" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M128,76a44,44,0,1,1-44,44A43.99983,43.99983,0,0,1,128,76Zm48-12h16V80a8,8,0,0,0,16,0V64h16a8,8,0,0,0,0-16H208V32a8,8,0,0,0-16,0V48H176a8,8,0,0,0,0,16Zm45.56006,40.95605a8.00039,8.00039,0,0,0-6.64893,9.15381A89.00044,89.00044,0,0,1,216,128a87.63672,87.63672,0,0,1-22.24182,58.41016,79.7044,79.7044,0,0,0-24.431-22.97461,59.83641,59.83641,0,0,1-82.6543,0A79.70345,79.70345,0,0,0,62.2417,186.41016,87.9498,87.9498,0,0,1,128,40a89.02966,89.02966,0,0,1,13.89062,1.08887,7.99994,7.99994,0,1,0,2.50391-15.80274A104.0826,104.0826,0,0,0,24,128a103.74716,103.74716,0,0,0,33.81934,76.68066,7.94507,7.94507,0,0,0,1.32629,1.18946,103.784,103.784,0,0,0,137.71252-.00293,7.94633,7.94633,0,0,0,1.31678-1.18115A103.74751,103.74751,0,0,0,232,128a105.04749,105.04749,0,0,0-1.28613-16.39453A7.99752,7.99752,0,0,0,221.56006,104.95605Z"></path> </g></svg>
</span>
</div>
<div class="px-3 py-1 order-1">
<div class="message-content pr-2 font-medium" dir="auto" style="width: 100%; margin: auto; text-wrap: pretty; font-size: 14px; text-align: right;">
${formatted_question}
</div>
</div>
</div>
`);
$(".chat-container").animate({
scrollTop: $('.chat-container')[0].scrollHeight
}, 1000);
}
// Function to handle sending a question via Socket.IO and receiving an answer
function provideQuestionToAnswer(){
response = "";
let question = $("#question-box").text();
appendQuestion(question);
$("#question-box").text("");
setTimeout(loadChatBlock, 600);
// // Emit question via Socket.IO
// socket.emit('message', { question: question, session_id: 'abc123' });
// Emit question and language via Socket.IO
socket.emit('message', { question: question, session_id: 'abc123' });
}
function addLtrToEnglishContent() {
// Regular expression to detect English content (letters, numbers, punctuation)
const englishOnlyRegex = /^[A-Za-z0-9.,%:\/\-\s]+$/;
// Get all the elements inside the parent element
const parent = $('.message-content').last();
console.log(parent[0]);
let elements = $(parent[0]).find("*");
console.log(elements);
// Loop through each element and check its text content
elements.forEach(function(element) {
// Trim whitespace from the content and check if it matches the regex
const textContent = element.textContent.trim();
if (englishOnlyRegex.test(textContent)) {
// If the content is in English, add dir="ltr" to the element
element.setAttribute('dir', 'ltr');
}
});
}
function addLtrAttribute(htmlString) {
// Regular expression to match opening tags
const tagPattern = /<([a-zA-Z1-6]+)([^>]*)>\s*([a-zA-Z0-9\s]+)\s*<\/\1>/g;
// Function to add dir="ltr" attribute if not present
return htmlString.replace(tagPattern, (match, tagName, attributes, content) => {
// Check if the tag already contains a dir attribute
if (!/dir\s*=\s*['"]?ltr['"]?/.test(attributes)) {
attributes = `${attributes} dir="ltr"`;
}
// Reconstruct the tag with the new attribute
return `<${tagName}${attributes}>${content}</${tagName}>`;
});
}
function appendAnswer(answer) {
let lastElement = $(".chat-container .chat-block:last-child");
const englishRegex = /[0-9]+(?:\.[0-9]+)?/g;
// Wrap English text and numbers in <span dir="ltr">
let formatted_answer = answer.replace(englishRegex, function(match) {
return `<span class="special-span px-1" dir="ltr">${match}</span>`;
});
formatted_answer = addLtrAttribute(formatted_answer);
// Replace numbers and round them to two decimal places
formatted_answer = formatted_answer.replace(/\d+\.\d+/g, function(match) {
return parseFloat(match).toFixed(2);
});
if (lastElement.hasClass("response-block")) {
$(".chat-container .chat-block:last-child").find(".message-content").html(formatted_answer);
} else {
lastElement.remove();
$(".chat-container").append(`
<div class="chat-block response-block w-full flex flex-row-reverse items-start justify-end bg-secondary p-5 rounded-2xl rtl z-10 bg-gray-100/60">
<div class="flex items-center order-2" >
<span class="bg-white" style="width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50px; padding: 5px; border: 2px solid #c2c2c2;">
<svg fill="#000000" width="36px" height="36px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path d="M21.928 11.607c-.202-.488-.635-.605-.928-.633V8c0-1.103-.897-2-2-2h-6V4.61c.305-.274.5-.668.5-1.11a1.5 1.5 0 0 0-3 0c0 .442.195.836.5 1.11V6H5c-1.103 0-2 .897-2 2v2.997l-.082.006A1 1 0 0 0 1.99 12v2a1 1 0 0 0 1 1H3v5c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5a1 1 0 0 0 1-1v-1.938a1.006 1.006 0 0 0-.072-.455zM5 20V8h14l.001 3.996L19 12v2l.001.005.001 5.995H5z"></path><ellipse cx="8.5" cy="12" rx="1.5" ry="2"></ellipse><ellipse cx="15.5" cy="12" rx="1.5" ry="2"></ellipse><path d="M8 16h8v2H8z"></path></g></svg>
</span>
</div>
<div class="px-3 py-1">
<div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; text-wrap: pretty;" style="-webkit-locale: "ar";" >
${formatted_answer}
</div>
</div>
</div>
`);
}
// addLtrToEnglishContent();
$(".chat-container").scrollTop($(".chat-container")[0].scrollHeight);
}
// Function to handle the answer received from the server
socket.on('response', (data) => {
response += data;
// model_response = converter.makeHtml(response);
appendAnswer(response);
});
// Open the chatbot interface
function openchatbot(){
console.log("working here");
$(".main-chat-container").removeClass("hide-chatbot");
$(".main-chat-container").addClass("show-chatbot");
$(".startchatbtn").addClass("d-none");
$(".startchatbtn").removeClass("flex");
}
// Close the chatbot interface
$(".startchatbtn").click(openchatbot);
$(".closechatbot").click(function(){
$(".main-chat-container").removeClass("show-chatbot");
$(".main-chat-container").addClass("hide-chatbot");
$(".startchatbtn").removeClass("d-none");
$(".startchatbtn").addClass("flex");
});
// Send the question and get the answer
$(".sendbtn").click(provideQuestionToAnswer);
// Handle the Enter key press in the question box
$(document).ready(() => {
$("#question-box").text("Write your question here...");
$("#question-box").text("");
$('#question-box').on('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
provideQuestionToAnswer();
}
});
});
// Handle connection errors
socket.on('connect_error', (error) => {
console.error("Connection error:", error);
appendMessage("Sorry, there was a problem connecting to the server. Please try again later.");
});
// Handle disconnection
socket.on('disconnect', (reason) => {
console.warn("Disconnected from server:", reason);
response = "";
// appendAnswer("You have been disconnected from the server. Please refresh the page to reconnect.");
socket = io.connect('https://ritesh-hf-adafsa-flask-app-demo.hf.space', {
transports: ['websocket']
});
});
// Clear the chat container
$(".clearBtn").click(() => {
$('.chat-container').find('.chat-block').remove();
$(".start-block").removeClass('d-none');
$(".start-block").addClass('flex');
$(".start-block").slideDown();
});
// Handle click on example questions
$(".question-item").on("click", (e) => {
let exampleQuestion = e.target.innerText;
$("#question-box").text(exampleQuestion);
$(".sendbtn").click();
});
</script>
<script>
const inputDiv = document.getElementById('question-box');
inputDiv.addEventListener('input', function(event) {
// Save the caret position
const caretPosition = saveCaretPosition(this);
// Process the content
processBiDiContent(this);
// Restore the caret position
restoreCaretPosition(this, caretPosition);
});
function processBiDiContent(element) {
let content = element.innerText;
// Regex for English letters and numbers
const englishRegex = /[A-Za-z0-9]+/g;
// Wrap English text and numbers in <span dir="ltr">
content = content.replace(englishRegex, function(match) {
return `<span dir="ltr">${match}</span>`;
});
// Update the element's HTML
element.innerHTML = content;
}
function escapeHTML(str) {
return str.replace(/[&<>"']/g, function(m) {
return {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}[m];
});
}
function saveCaretPosition(context) {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(context);
preCaretRange.setEnd(range.endContainer, range.endOffset);
const caretOffset = preCaretRange.toString().length;
return caretOffset;
}
function restoreCaretPosition(context, offset) {
let charIndex = 0;
const range = document.createRange();
range.setStart(context, 0);
range.collapse(true);
const nodeStack = [context];
let node, foundStart = false;
while (!foundStart && (node = nodeStack.pop())) {
if (node.nodeType === Node.TEXT_NODE) {
const nextCharIndex = charIndex + node.length;
if (offset >= charIndex && offset <= nextCharIndex) {
range.setStart(node, offset - charIndex);
range.collapse(true);
foundStart = true;
}
charIndex = nextCharIndex;
} else {
let i = node.childNodes.length;
while (i--) {
nodeStack.push(node.childNodes[i]);
}
}
}
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
</script>
</body>
</html>