File size: 32,465 Bytes
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03ed5c8
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03ed5c8
02223fb
 
 
 
 
 
af912a1
 
 
 
 
 
 
 
 
 
 
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df068a
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df068a
 
02223fb
 
 
 
 
7df068a
 
02223fb
 
 
 
 
 
 
 
 
 
 
af912a1
02223fb
 
7df068a
02223fb
 
 
 
 
 
 
 
af912a1
 
 
02223fb
 
 
 
 
 
 
 
189cd2f
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af912a1
 
 
 
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03ed5c8
 
 
 
7df068a
03ed5c8
02223fb
 
03ed5c8
02223fb
 
 
 
 
 
03ed5c8
 
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df068a
 
 
 
 
 
 
02223fb
 
7df068a
02223fb
 
 
 
 
 
 
 
 
 
af912a1
7df068a
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
7df068a
 
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189cd2f
02223fb
 
 
 
 
 
 
 
 
 
 
 
 
 
7df068a
02223fb
 
 
 
 
af912a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02223fb
 
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<!DOCTYPE html>
<html lang="en">
<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">
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>

    <script src="https://cdn.tailwindcss.com"></script>

    <style>

        .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;
            /* border: 10 px solid red; */
            font-family: "DM Sans", sans-serif;
        }

        /* @media(max-width: 800px){
        .chat-container-wrapper{
            width: calc(100% - 30px);
            margin: auto;
        }
        } */

        .chat-container-wrapper * {
            font-family: "DM Sans", sans-serif;
        }

        /* .chat-container-wrapper .chatbot-header{
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: space-between;
        } */

        .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;
        }

        /* div[contenteditable]:focus:empty:before {
            content: 'Start typing';
        } */
        /* HTML: <div class="loader"></div> */
        /* HTML: <div class="loader"></div> */
        .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: 16px;
        }
        .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;
        }


        /*small{*/
        /*font-size: 12px;*/
        /*}*/

        .bg-special-secondary{
        background-color: rgb(230, 230, 230);
        }
        .message-content *:not(i){
            font-family: "DM Sans", sans-serif;
        }

        .message-content *:not(h1,h2,h3,h4,h5,h6){
            font-weight: 500;
        }

        .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;
        }
        .message-content ol li, .message-content ul li{
            margin: 12px 0px ;
        }
        .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; 
        }

    </style>

    <style>
        #question-box span[dir="rtl"] {
            direction: rtl;
            unicode-bidi: embed;
        }
        #question-box span[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" 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-2" style="max-width: 800px; flex-grow: 1; padding: 20px 0px; display: flex; flex-direction: column; gap: 10px; 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 class="absolute w-full h-full top-0 left-0 z-0 flex items-center justify-center">
                    <img src="./static/image.png" class="opacity-10" alt="greyscale-image">
                </div>

            </div>

            <div class="w-full bg-transparent">
                <div class="input-options-container  max-w-3xl p-3 flex flex-row items-center justify-between border rounded-full shadow-xl mx-auto">
                    <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-center justify-end bg-secondary p-3 rounded-xl z-10" style="background-color: rgba(242, 255, 225, 0.678);">
                        <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>`;
            });

            $(".chat-container").append(`
                <div class="chat-block question-block w-full flex flex-row-reverse items-center justify-end p-3 z-10" style="background-color: rgba(255, 255, 255, 0.529);">
                    <div class="flex items-center order-2">
                        <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 order-1">
                        <div class="message-content pr-2" 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 appendAnswer(answer) {
            let lastElement = $(".chat-container .chat-block:last-child");

            const englishRegex = /[A-Za-z0-9]+(?:\.[0-9]+)?/g;

            // Wrap English text and numbers in <span dir="ltr">
            formatted_answer = answer.replace(englishRegex, function(match) {
                return `<span class="special-span px-1" dir="ltr">${match}</span>`;
            });

            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-3 rounded-xl rtl z-10" style="background-color: rgba(242, 255, 225, 0.529);">
                        <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; font-weight: 500; text-wrap: pretty;" style="-webkit-locale: "ar";" >
                                ${formatted_answer}
                            </div>
                        </div>
                    </div>
                `);
            }

            $(".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('connect', (reason) => {
        //     // Emit website category via Socket.IO
        //     socket.emit('website', { website: "UAE_Legislation", session_id: 'abc123'  });
        // });
        
        // 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>