File size: 42,083 Bytes
96db95c
 
 
5e9d705
96db95c
 
 
5e9d705
96db95c
 
 
 
 
 
 
5e9d705
 
 
 
96db95c
 
5e9d705
 
1727409
 
3be436d
1727409
5e9d705
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96db95c
 
 
 
 
 
5e9d705
 
 
 
 
 
646f8cc
 
 
 
 
 
 
 
5e9d705
06aa57a
 
 
 
5e9d705
 
 
 
 
 
 
 
 
 
06aa57a
5e9d705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96db95c
 
 
 
 
 
 
 
 
 
5e9d705
96db95c
5e9d705
96db95c
 
646f8cc
96db95c
5e9d705
96db95c
5e9d705
96db95c
 
5e9d705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96db95c
5e9d705
96db95c
646f8cc
 
 
 
 
96db95c
 
646f8cc
 
 
 
 
96db95c
 
646f8cc
 
 
 
 
96db95c
 
646f8cc
 
 
 
 
96db95c
 
646f8cc
 
 
 
 
96db95c
 
646f8cc
 
 
 
 
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
 
 
 
 
 
 
 
96db95c
 
 
 
 
5e9d705
96db95c
 
5e9d705
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
 
 
 
 
 
 
 
 
 
 
 
96db95c
 
 
 
 
 
 
 
 
 
5e9d705
 
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
96db95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96db95c
5e9d705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96db95c
 
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
import os
import datetime
import requests
import textwrap
from offres_emploi import Api
from offres_emploi.utils import dt_to_str_iso
from dash import Dash, html, dcc, callback, Output, Input, dash_table, State, _dash_renderer
import dash_bootstrap_components as dbc
import plotly.express as px
import dash_mantine_components as dmc
from dash_iconify import DashIconify
import pandas as pd
from dotenv import load_dotenv
_dash_renderer._set_react_version("18.2.0")
import plotly.io as pio
from langchain_community.llms import HuggingFaceEndpoint
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain.schema.output_parser import StrOutputParser

from flask import Flask

server = Flask(__name__)

# external JavaScript files
external_scripts = [
    'https://datacipen-eventia.hf.space/copilot/index.js'
]
# Create a customized version of the plotly_dark theme with a modified background color
custom_plotly_dark_theme = {
    "layout": {
        "paper_bgcolor": "#1E1E1E",  # Update the paper background color
        "plot_bgcolor": "#1E1E1E",   # Update the plot background color
        "font": {
            "color": "#FFFFFF"       # Update the font color
        },
        "xaxis": {
            "gridcolor": "#333333",  # Update the x-axis grid color
            "zerolinecolor": "#666666"  # Update the x-axis zero line color
        },
        "yaxis": {
            "gridcolor": "#333333",  # Update the y-axis grid color
            "zerolinecolor": "#666666"  # Update the y-axis zero line color
        }
    }
}

# Apply the customized theme to your Plotly figures
pio.templates["custom_plotly_dark"] = custom_plotly_dark_theme
pio.templates.default = "custom_plotly_dark"

load_dotenv()

def localisation():
    ListCentroids = [
        { "ID": "01", "Longitude": 5.3245259, "Latitude":46.0666003 },
        { "ID": "02", "Longitude": 3.5960246, "Latitude": 49.5519632 },
        { "ID": "03", "Longitude": 3.065278, "Latitude": 46.4002783 },
        { "ID": "04", "Longitude": 6.2237688, "Latitude": 44.1105837 },
        { "ID": "05", "Longitude": 6.2018836, "Latitude": 44.6630487 },
    	{ "ID": "06", "Longitude": 7.0755745, "Latitude":43.9463082 },
        { "ID": "07", "Longitude": 4.3497308, "Latitude": 44.7626044 },
        { "ID": "08", "Longitude": 4.6234893, "Latitude": 49.6473884 },
        { "ID": "09", "Longitude": 1.6037147, "Latitude": 42.9696091 },
        { "ID": "10", "Longitude": 4.1394954, "Latitude": 48.2963286 },
    	{ "ID": "11", "Longitude": 2.3140163, "Latitude": 43.1111427 },
    	{ "ID": "12", "Longitude": 2.7365234, "Latitude": 44.2786323 },
    	{ "ID": "13", "Longitude": 5.0515492, "Latitude": 43.5539098 },
    	{ "ID": "14", "Longitude": -0.3930779, "Latitude": 49.1024215 },
    	{ "ID": "15", "Longitude": 2.6367657, "Latitude": 44.9643217 },
    	{ "ID": "16", "Longitude": 0.180475, "Latitude": 45.706264 },
    	{ "ID": "17", "Longitude": -0.7082589, "Latitude": 45.7629699 },
    	{ "ID": "18", "Longitude": 2.5292424, "Latitude": 47.0926687 },
    	{ "ID": "19", "Longitude": 1.8841811, "Latitude": 45.3622055 },
    	{ "ID": "2A", "Longitude": 8.9906834, "Latitude": 41.8619761 },
    	{ "ID": "2B", "Longitude": 9.275489, "Latitude": 42.372014 },
    	{ "ID": "21", "Longitude": 4.7870471, "Latitude": 47.4736746 },
    	{ "ID": "22", "Longitude": -2.9227591, "Latitude": 48.408402 },
    	{ "ID": "23", "Longitude": 2.0265508, "Latitude": 46.0837382 },
    	{ "ID": "24", "Longitude": 0.7140145, "Latitude": 45.1489678 },
    	{ "ID": "25", "Longitude": 6.3991355, "Latitude": 47.1879451 },
    	{ "ID": "26", "Longitude": 5.1717552, "Latitude": 44.8055408 },
    	{ "ID": "27", "Longitude": 0.9488116, "Latitude": 49.1460288 },
    	{ "ID": "28", "Longitude": 1.2793491, "Latitude": 48.3330017 },
    	{ "ID": "29", "Longitude": -4.1577074, "Latitude": 48.2869945 },
    	{ "ID": "30", "Longitude": 4.2650329, "Latitude": 43.9636468 },
    	{ "ID": "31", "Longitude": 1.2728958, "Latitude": 43.3671081 },
    	{ "ID": "32", "Longitude": 0.4220039, "Latitude": 43.657141 },
    	{ "ID": "33", "Longitude": -0.5760716, "Latitude": 44.8406068 },
    	{ "ID": "34", "Longitude": 3.4197556, "Latitude": 43.62585 },
    	{ "ID": "35", "Longitude": -1.6443812, "Latitude": 48.1801254 },
    	{ "ID": "36", "Longitude": 1.6509938, "Latitude": 46.7964222 },
    	{ "ID": "37", "Longitude": 0.7085619, "Latitude": 47.2802601 },
    	{ "ID": "38", "Longitude": 5.6230772, "Latitude": 45.259805 },
    	{ "ID": "39", "Longitude": 5.612871, "Latitude": 46.7398138 },
    	{ "ID": "40", "Longitude": -0.8771738, "Latitude": 44.0161251 },
    	{ "ID": "41", "Longitude": 1.3989178, "Latitude": 47.5866519 },
    	{ "ID": "42", "Longitude": 4.2262355, "Latitude": 45.7451186 },
    	{ "ID": "43", "Longitude": 3.8118151, "Latitude": 45.1473029 },
    	{ "ID": "44", "Longitude": -1.7642949, "Latitude": 47.4616509 },
    	{ "ID": "45", "Longitude": 2.2372695, "Latitude": 47.8631395 },
    	{ "ID": "46", "Longitude": 1.5732157, "Latitude": 44.6529284 },
    	{ "ID": "47", "Longitude": 0.4788052, "Latitude": 44.4027215 },
    	{ "ID": "48", "Longitude": 3.4991239, "Latitude": 44.5191573 },
    	{ "ID": "49", "Longitude": -0.5136056, "Latitude": 47.3945201 },
    	{ "ID": "50", "Longitude": -1.3203134, "Latitude": 49.0162072 },
    	{ "ID": "51", "Longitude": 4.2966555, "Latitude": 48.9479636 },
    	{ "ID": "52", "Longitude": 5.1325796, "Latitude": 48.1077196 },
    	{ "ID": "53", "Longitude": -0.7073921, "Latitude": 48.1225795 },
    	{ "ID": "54", "Longitude": 6.144792, "Latitude": 48.7995163 },
    	{ "ID": "55", "Longitude": 5.2888292, "Latitude": 49.0074545 },
    	{ "ID": "56", "Longitude": -2.8746938, "Latitude": 47.9239486 },
    	{ "ID": "57", "Longitude": 6.5610683, "Latitude": 49.0399233 },
    	{ "ID": "58", "Longitude": 3.5544332, "Latitude": 47.1122301 },
    	{ "ID": "59", "Longitude": 3.2466616, "Latitude": 50.4765414 },
    	{ "ID": "60", "Longitude": 2.4161734, "Latitude": 49.3852913 },
    	{ "ID": "61", "Longitude": 0.2248368, "Latitude": 48.5558919 },
    	{ "ID": "62", "Longitude": 2.2555152, "Latitude": 50.4646795 },
    	{ "ID": "63", "Longitude": 3.1322144, "Latitude": 45.7471805 },
    	{ "ID": "64", "Longitude": -0.793633, "Latitude": 43.3390984 },
    	{ "ID": "65", "Longitude": 0.1478724, "Latitude": 43.0526238 },
    	{ "ID": "66", "Longitude": 2.5239855, "Latitude": 42.5825094 },
    	{ "ID": "67", "Longitude": 7.5962225, "Latitude": 48.662515 },
    	{ "ID": "68", "Longitude": 7.2656284, "Latitude": 47.8586205 },
    	{ "ID": "69", "Longitude": 4.6859896, "Latitude": 45.8714754 },
    	{ "ID": "70", "Longitude": 6.1388571, "Latitude": 47.5904191 },
    	{ "ID": "71", "Longitude": 4.6394021, "Latitude": 46.5951234 },
    	{ "ID": "72", "Longitude": 0.1947322, "Latitude": 48.0041421 },
    	{ "ID": "73", "Longitude": 6.4662232, "Latitude": 45.4956055 },
    	{ "ID": "74", "Longitude": 6.3609606, "Latitude": 46.1045902 },
    	{ "ID": "75", "Longitude": 2.3416082, "Latitude": 48.8626759 },
    	{ "ID": "76", "Longitude": 1.025579, "Latitude": 49.6862911 },
    	{ "ID": "77", "Longitude": 2.8977309, "Latitude": 48.5957831 },
    	{ "ID": "78", "Longitude": 1.8080138, "Latitude": 48.7831982 },
    	{ "ID": "79", "Longitude": -0.3159014, "Latitude": 46.5490257 },
    	{ "ID": "80", "Longitude": 2.3380595, "Latitude": 49.9783317 },
    	{ "ID": "81", "Longitude": 2.2072751, "Latitude": 43.8524305 },
    	{ "ID": "82", "Longitude": 1.2649374, "Latitude": 44.1254902 },
    	{ "ID": "83", "Longitude": 6.1486127, "Latitude": 43.5007903 },
    	{ "ID": "84", "Longitude": 5.065418, "Latitude": 44.0001599 },
    	{ "ID": "85", "Longitude": -1.3956692, "Latitude": 46.5929102 },
    	{ "ID": "86", "Longitude": 0.4953679, "Latitude": 46.5719095 },
    	{ "ID": "87", "Longitude": 1.2500647, "Latitude": 45.9018644 },
    	{ "ID": "88", "Longitude": 6.349702, "Latitude": 48.1770451 },
    	{ "ID": "89", "Longitude": 3.5634078, "Latitude": 47.8474664 },
    	{ "ID": "90", "Longitude": 6.9498114, "Latitude": 47.6184394 },
    	{ "ID": "91", "Longitude": 2.2714555, "Latitude": 48.5203114 },
    	{ "ID": "92", "Longitude": 2.2407148, "Latitude": 48.835321 },
    	{ "ID": "93", "Longitude": 2.4811577, "Latitude": 48.9008719 },
    	{ "ID": "94", "Longitude": 2.4549766, "Latitude": 48.7832368 },
    	{ "ID": "95", "Longitude": 2.1802056, "Latitude": 49.076488 },
    	{ "ID": "974", "Longitude": 55.536384, "Latitude": -21.115141 },
    	{ "ID": "973", "Longitude": -53.125782, "Latitude": 3.933889 },
    	{ "ID": "972", "Longitude": -61.024174, "Latitude": 14.641528 },
    	{ "ID": "971", "Longitude": -61.551, "Latitude": 16.265 }
    ]
    
    return ListCentroids

def connexion_France_Travail():
    client = Api(client_id=os.getenv('POLE_EMPLOI_CLIENT_ID'),
                client_secret=os.getenv('POLE_EMPLOI_CLIENT_SECRET'))
    return client

def API_France_Travail(romeListArray):
    client = connexion_France_Travail()
    todayDate = datetime.datetime.today()
    month, year = (todayDate.month-1, todayDate.year) if todayDate.month != 1 else (12, todayDate.year-1)
    start_dt = todayDate.replace(day=1, month=month, year=year)
    end_dt = datetime.datetime.today()
    results = []
    for k in romeListArray:
        if k[0:1] == ' ':
            k = k[1:]
        params = {"motsCles": k.replace('/', '').replace('-', '').replace(',', '').replace(' ', ','),'minCreationDate': dt_to_str_iso(start_dt),'maxCreationDate': dt_to_str_iso(end_dt),'range':'0-149'}
        try:
            search_on_big_data = client.search(params=params)
            results += search_on_big_data["resultats"]
        except:
            print("Il n'y a pas d'offres d'emploi.")
        
    results_df = pd.DataFrame(results)
    return results_df

theme_toggle = dmc.Tooltip(
    dmc.ActionIcon(
    [
        dmc.Paper(DashIconify(icon="radix-icons:sun", width=25), darkHidden=True),
        dmc.Paper(DashIconify(icon="radix-icons:moon", width=25), lightHidden=True),
    ],
    variant="transparent",
    color="yellow",
    id="color-scheme-toggle",
    size="lg",
    ms="auto",
    ),
    label="Changez de thème",
    position="left",
    withArrow=True,
    arrowSize=6,
)

styleTitle = {
    "textAlign": "center"
}
styleUSERIA = {
    "textAlign": "right",
    "marginBottom" : "5px"
}
styleSUBMITIA = {
    "marginLeft":"auto",
    "marginRight":"auto",
    "marginTop": "5px",
    "marginBottom" : "5px"
}
styleSYSIA = {
    "marginTop":"10px",
    "marginBottom":"120px",
}
styleTopvar = {
    "display": "none"
}

styleToggle = {
    "marginTop":"25px",
    "textAlign": "right",
}
styleSubmitBox = {
    "position":"fixed",
    "width": "100%",
    "top": "calc(100vh - 100px)",
    "right": "0"
}
datadefault = [
    {"value": "K2105", "label": "K2105"},
    {"value": "L1101", "label": "L1101"},
    {"value": "L1202", "label": "L1202"},
    {"value": "L1507", "label": "L1507"},
    {"value": "L1508", "label": "L1508"},
    {"value": "L1509", "label": "L1509"},
]

def custom_error_handler(err):
# This function defines what we want to happen when an exception occurs
# For now, we just print the exception to the terminal with additional text
    print(f"The app raised the following exception: {err}")
def textbox(text, box="AI", name="Philippe"):
    text = text.replace(f"{name}:", "").replace("You:", "")
    #text = textile.textile(text)
    style = {
        "max-width": "60%",
        "width": "max-content",
        "padding": "5px 10px",
        "border-radius": 25,
        "margin-bottom": 20,
    }

    if box == "user":
        style["margin-left"] = "auto"
        style["margin-right"] = 0

        #return dbc.Card(text, style=style, body=True, color="primary", inverse=True)
        return html.Div(dmc.Button(text, variant="gradient", gradient={"from": "grape", "to": "pink", "deg": 35}), style=styleUSERIA)

    elif box == "AI":
        style["margin-left"] = 0
        style["margin-right"] = "auto"

        thumbnail = html.Img(
            src=app.get_asset_url("sparkles.gif"),
            style={
                "border-radius": 50,
                "height": 36,
                "margin-right": 5,
                "float": "left",
            },
        )
        #textbox = dbc.Card(text, style=style, body=True, color="light", inverse=False)
        #textbox = dmc.Blockquote(text, style=styleSYSIA)
        textbox = dmc.Card(children=[dmc.Text(text,size="sm",c="dimmed")],withBorder=False,w="100%", style=styleSYSIA)
        return html.Div([thumbnail, textbox])

    else:
        raise ValueError("Incorrect option for `box`.")


#description = """
#Philippe is the principal architect at a condo-development firm in Paris. He lives with his girlfriend of five years in a 2-bedroom condo, with a small dog named Coco. Since the pandemic, his firm has seen a  significant drop in condo requests. As such, he’s been spending less time designing and more time on cooking,  his favorite hobby. He loves to cook international foods, venturing beyond French cuisine. But, he is eager  to get back to architecture and combine his hobby with his occupation. That’s why he’s looking to create a  new design for the kitchens in the company’s current inventory. Can you give him advice on how to do that?
#"""

# Authentication
#openai.api_key = os.getenv("OPENAI_KEY")


# Define Layout
conversation = html.Div(
    html.Div(id="display-conversation"),
    style={
        "overflow-y": "auto",
        "display": "flex",
        "height": "calc(100vh - 100px)",
        "flex-direction": "column-reverse",
    },
)

controls = dbc.InputGroup(
    children=[
        dmc.TextInput(id="user-input", placeholder="Ecrire votre requête...", w="400", style=styleSUBMITIA),
        dbc.InputGroupAddon(dmc.Button(leftSection=DashIconify("Envoyer", icon="tabler:send", width=20), id="submit"), addon_type="append", style=styleTitle),
        
        #dbc.Input(id="user-input", placeholder="Ecrire votre requête...", type="text"),
        #dbc.InputGroupAddon(dbc.Button("Submit", id="submit"), addon_type="append"),
    ],style=styleSubmitBox
)
class CustomDash(Dash):
    def interpolate_index(self, **kwargs):
        # Inspect the arguments by printing them
        return '''
        <!DOCTYPE html>
        <html>
            <head>
                <title>Dashboard des compétences</title>
            </head>
            <body>

                <div id="custom-topbar"></div>
                {app_entry}
                {config}
                {scripts}
                {renderer}
                <div id="custom-footer">My custom footer</div>
            </body>
        </html>
        '''.format(
            app_entry=kwargs['app_entry'],
            config=kwargs['config'],
            scripts=kwargs['scripts'],
            renderer=kwargs['renderer'])
        
#app = Dash(__name__, external_scripts=external_scripts, external_stylesheets=dmc.styles.ALL, on_error=custom_error_handler)
app = CustomDash(__name__, server=server, external_scripts=external_scripts, external_stylesheets=dmc.styles.ALL, on_error=custom_error_handler)

app.layout = dmc.MantineProvider(
    [
        html.Div(
            children=[
                dmc.Container(
                    children=[
                        dmc.Grid(
                            children=[
                                dmc.GridCol(html.Div(
                                    children=[
                                    dmc.MultiSelect(
                                        placeholder="Selectionnez vos Codes ROME",
                                        id="framework-multi-select",
                                        value=['K2105', 'L1101', 'L1202', 'L1507', 'L1508', 'L1509'],
                                        data=datadefault,
                                        w=600,
                                        mt=10,
                                        styles={
                                            "input": {"borderColor": "grey"},
                                            "label": {"color": dmc.DEFAULT_THEME["colors"]["orange"][4]},
                                        },
                                    ),
                                    dmc.Drawer(
                                            title="Mistral répond à vos questions sur les datas de l'emploi et des compétences.",
                                            children=[dbc.Container(
                                                            fluid=False,
                                                            children=[
                                                                dcc.Store(id="store-conversation", data=""),
                                                                html.Div(dmc.Button("Bonjour, Mistral est à votre écoute!", variant="gradient", gradient={"from": "grape", "to": "pink", "deg": 35}), style=styleUSERIA),
                                                                conversation,
                                                                dcc.Loading(html.Div(id="loading-component"),type="default"),
                                                                controls,
                                                                #dbc.Spinner(html.Div(id="loading-component")),
                                                            ],
                                                        )
                                            ],
                                            id="drawer-simple",
                                            padding="md",
                                            size="50%",
                                            position="right"
                                    ),]
                                ), span=5),
                                dmc.GridCol(html.Div(dmc.Title(f"Le marché et les statistiques de l'emploi", order=1, size="30", my="20", id="chainlit-call-fn", style=styleTitle)), span=5),
                                dmc.GridCol(html.Div(theme_toggle, style=styleToggle), span=1),
                                dmc.GridCol(html.Div(dmc.Tooltip(dmc.Button(leftSection=DashIconify(icon="tabler:sparkles", width=30), id="drawer-demo-button"), label="IA générative sur les données",position="left",withArrow=True,arrowSize=6,), style=styleToggle), span=1),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingRepartition",
                                        children=(dcc.Graph(id="figRepartition",selectedData={'points': [{'hovertext': ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','2A','2B','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','971','972','973','974']}]})),
                                        type="default",
                                    )
                                ), span=6),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingEmplois",
                                        children=(dcc.Graph(id="figEmplois")),
                                        type="default",
                                        )
                                ), span=6),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingContrats",
                                        children=(dcc.Graph(id="figContrats")),
                                        type="default",
                                    )
                                ), span=6),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingExperiences",
                                        children=(dcc.Graph(id="figExperiences")),
                                        type="default",
                                    )
                                ), span=6),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingCompetences",
                                        children=(dcc.Graph(id="figCompetences")),
                                        type="default",
                                    )
                                ), span=6),
                                dmc.GridCol(html.Div(
                                    dcc.Loading(
                                        id="loadingTransversales",
                                        children=(dcc.Graph(id="figTransversales")),
                                        type="default",
                                    )
                                ), span=6),
                            ],
                            gutter="xs",
                        )
                    ],size="xxl",fluid=True
                ),
            ]
        )
    ],
    id="mantine-provider",
    forceColorScheme="dark",
    theme={
        "primaryColor": "indigo",
         "fontFamily": "'Inter', sans-serif",
         "components": {
             "Button": {"defaultProps": {"fw": 400}},
             "Alert": {"styles": {"title": {"fontWeight": 500}}},
             "AvatarGroup": {"styles": {"truncated": {"fontWeight": 500}}},
             "Badge": {"styles": {"root": {"fontWeight": 500}}},
             "Progress": {"styles": {"label": {"fontWeight": 500}}},
             "RingProgress": {"styles": {"label": {"fontWeight": 500}}},
             "CodeHighlightTabs": {"styles": {"file": {"padding": 12}}},
             "Table": {
                 "defaultProps": {
                     "highlightOnHover": True,
                     "withTableBorder": True,
                     "verticalSpacing": "sm",
                     "horizontalSpacing": "md",
                 }
             },
         },
         # add your colors
        "colors": {
            "deepBlue": ["#E9EDFC", "#C1CCF6", "#99ABF0"], # 10 color elements
        },
        "shadows": {
            # other shadows (xs, sm, lg) will be merged from default theme
            "md": "1px 1px 3px rgba(0,0,0,.25)",
            "xl": "5px 5px 3px rgba(0,0,0,.25)",
        },
        "headings": {
            "fontFamily": "Roboto, sans-serif",
            "sizes": {
                "h1": {"fontSize": 30},
            },
        },
    },
)
@callback(
    Output("mantine-provider", "forceColorScheme"),
    Input("color-scheme-toggle", "n_clicks"),
    State("mantine-provider", "forceColorScheme"),
    prevent_initial_call=True,
)
def switch_theme(_, theme):
    return "dark" if theme == "light" else "light"

@callback(
    Output("drawer-simple", "opened"),
    Input("drawer-demo-button", "n_clicks"),
    prevent_initial_call=True,
)
def drawer_demo(n_clicks):
    return True

@callback(
    Output(component_id='figRepartition', component_property='figure'),
    Output(component_id='figCompetences', component_property='figure'),
    Output(component_id='figTransversales', component_property='figure'),
    Input(component_id='framework-multi-select', component_property='value'),
    Input('figEmplois', 'selectedData'),
    Input("mantine-provider", "forceColorScheme"),
)
def create_repartition(array_value, selectedData, theme):
    if theme == "dark":
        template = "plotly_dark"
        paper_bgcolor = 'rgba(36, 36, 36, 1)'
        plot_bgcolor = 'rgba(36, 36, 36, 1)'
    else:
        template = "ggplot2"
        paper_bgcolor = 'rgba(255, 255, 255, 1)'
        plot_bgcolor = 'rgba(255, 255, 255, 1)'
    
    df_FT = API_France_Travail(array_value)
    df = df_FT[['intitule','typeContratLibelle','experienceLibelle','lieuTravail']].copy()
    df["lieuTravail"] = df["lieuTravail"].apply(lambda x: x['libelle']).apply(lambda x: x[0:3]).apply(lambda x: x.strip())
    df.drop(df[df['lieuTravail'] == 'Fra'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'FRA'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Ile'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Mar'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Bou'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == '976'].index, inplace = True)
    
    ######## Filtre Emplois ########
    options = []
    if selectedData != None:
        if type(selectedData['points'][0]['y']) == str:
            options.append(selectedData['points'][0]['y'][:-3])
        else:
            options = selectedData['points'][0]['y'][:-3]
    else:
        options = df['intitule'].values.tolist()
    df = df[df['intitule'].isin(options)]
    
    
    ######## localisation ########
    ListCentroids = localisation()
    df_localisation = df.groupby('lieuTravail').size().reset_index(name='obs')
    df_localisation = df_localisation.sort_values(by=['lieuTravail'])
    df_localisation['longitude'] = df_localisation['lieuTravail']
    df_localisation['latitude'] = df_localisation['lieuTravail']
    df_localisation["longitude"] = df_localisation['longitude'].apply(lambda x:[loc['Longitude'] for loc in ListCentroids if loc['ID'] == x]).apply(lambda x:''.join(map(str, x)))
    df_localisation["longitude"] = pd.to_numeric(df_localisation["longitude"], downcast="float")
    df_localisation["latitude"] = df_localisation['latitude'].apply(lambda x:[loc['Latitude'] for loc in ListCentroids if loc['ID'] == x]).apply(lambda x:''.join(map(str, x)))
    df_localisation["latitude"] = pd.to_numeric(df_localisation["latitude"], downcast="float")
    
    
    res = requests.get(
            "https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/france-regions.geojson"
        )
    fig_localisation = px.scatter_mapbox(df_localisation, lat="latitude", lon="longitude", height=600, template=template, title="La répartition géographique des emplois", hover_name="lieuTravail", size="obs").update_layout(
        mapbox={
            "style": "carto-positron",
            "center": {"lon": 2, "lat" : 47},
            "zoom": 4.5,
            "layers": [
                {
                    "source": res.json(),
                    "type": "line",
                    "color": "green",
                    "line": {"width": 0},
                }
            ],
        },font=dict(size=10),paper_bgcolor=paper_bgcolor,autosize=True,clickmode='event+select'
    )
    
    df_FT.dropna(subset=['qualitesProfessionnelles','formations','competences'], inplace=True)
    df_FT["competences"] = df_FT["competences"].apply(lambda x:[str(e['libelle']) for e in x]).apply(lambda x:'; '.join(map(str, x)))
    df_FT["qualitesProfessionnelles"] = df_FT["qualitesProfessionnelles"].apply(lambda x:[str(e['libelle']) + ": " + str(e['description']) for e in x]).apply(lambda x:'; '.join(map(str, x)))
    
    ######## Compétences professionnelles ########
    df_comp = df_FT
    df_comp['competences'] = df_FT['competences'].str.split(';')
    df_comp = df_comp.explode('competences')
    df_comp = df_comp.groupby('competences').size().reset_index(name='obs')
    df_comp = df_comp.sort_values(by=['obs'])
    df_comp = df_comp.iloc[-20:]
    fig_competences = px.bar(df_comp, x='obs', y='competences', orientation='h', color='obs', height=600, template=template, title="Les principales compétences professionnelles", labels={'obs':'nombre'}, color_continuous_scale="Teal", text_auto=True).update_layout(font=dict(size=10),paper_bgcolor=paper_bgcolor,plot_bgcolor=plot_bgcolor,clickmode='event+select',autosize=True).update_traces(hovertemplate=df_comp["competences"] + ' <br>Nombre : %{x}', y=[y[:100] + "..." for y in df_comp['competences']], showlegend=False)
        
    ######## Compétences transversales ########
    df_transversales = df_FT
    df_transversales['qualitesProfessionnelles'] = df_FT['qualitesProfessionnelles'].str.split(';')
    df_comptransversales = df_transversales.explode('qualitesProfessionnelles')
    df_comptransversales = df_comptransversales.groupby('qualitesProfessionnelles').size().reset_index(name='obs')
    df_comptransversales = df_comptransversales.sort_values(by=['obs'])
    df_comptransversales = df_comptransversales.iloc[-20:]
    fig_transversales = px.bar(df_comptransversales, x='obs', y='qualitesProfessionnelles', orientation='h', color='obs', height=600, template=template, title="Les principales compétences transversales", labels={'obs':'nombre'}, color_continuous_scale="Teal", text_auto=True).update_layout(font=dict(size=10),paper_bgcolor=paper_bgcolor,plot_bgcolor=plot_bgcolor,autosize=True).update_traces(hovertemplate=df_comptransversales["qualitesProfessionnelles"] + ' <br>Nombre : %{x}', y=[y[:80] + "..." for y in df_comptransversales["qualitesProfessionnelles"]], showlegend=False)
        
    return fig_localisation, fig_competences, fig_transversales

def create_emploi(df, theme):
    if theme == "dark":
        template = "plotly_dark"
        paper_bgcolor = 'rgba(36, 36, 36, 1)'
        plot_bgcolor = 'rgba(36, 36, 36, 1)'
    else:
        template = "ggplot2"
        paper_bgcolor = 'rgba(255, 255, 255, 1)'
        plot_bgcolor = 'rgba(255, 255, 255, 1)'
    ######## Emplois ########
    df_intitule = df.groupby('intitule').size().reset_index(name='obs')
    df_intitule = df_intitule.sort_values(by=['obs'])
    df_intitule = df_intitule.iloc[-25:]
    fig_intitule = px.bar(df_intitule, x='obs', y='intitule', height=600, orientation='h', color='obs', template=template, title="Les principaux emplois", labels={'obs':'nombre'}, color_continuous_scale="Teal", text_auto=True).update_layout(font=dict(size=10),paper_bgcolor=paper_bgcolor,plot_bgcolor=plot_bgcolor,clickmode='event+select',autosize=True).update_traces(hovertemplate=df_intitule["intitule"] + ' <br>Nombre : %{x}', y=[y[:100] + "..." for y in df_intitule["intitule"]], showlegend=False)
    
    return fig_intitule

def create_contrat(df, theme):
    if theme == "dark":
        template = "plotly_dark"
        paper_bgcolor = 'rgba(36, 36, 36, 1)'
    else:
        template = "ggplot2"
        paper_bgcolor = 'rgba(255, 255, 255, 1)'
    
    ######## Types de contrat ########
    df_contrat = df.groupby('typeContratLibelle').size().reset_index(name='obs')
    fig_contrat = px.pie(df_contrat, names='typeContratLibelle', values='obs', color='obs', height=600, template=template, title="Les types de contrat", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe).update_traces(textposition='inside', textinfo='percent+label').update_layout(font=dict(size=10),paper_bgcolor=paper_bgcolor)
    
    return fig_contrat

def create_experience(df, theme):
    if theme == "dark":
        template = "plotly_dark"
        paper_bgcolor = 'rgba(36, 36, 36, 1)'
    else:
        template = "ggplot2"
        paper_bgcolor = 'rgba(255, 255, 255, 1)'
    ######## Expériences professionnelles ########
    df_experience = df.groupby('experienceLibelle').size().reset_index(name='obs')
    fig_experience = px.pie(df_experience, names='experienceLibelle', values='obs', color='obs', height=600, template=template, title="Les expériences professionnelles", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe).update_traces(textposition='inside', textinfo='percent+label').update_layout(font=dict(size=10),paper_bgcolor=paper_bgcolor)
    
    return fig_experience

@callback(
    Output(component_id='figEmplois', component_property='figure'),
    Input('figRepartition', 'selectedData'),
    Input(component_id='framework-multi-select', component_property='value'),
    Input("mantine-provider", "forceColorScheme"),
)

def update_emploi(selectedData, array_value, theme):
    options = []
    if selectedData != None:
        if type(selectedData['points'][0]['hovertext']) == str:
            options.append(selectedData['points'][0]['hovertext'])
        else:
            options = selectedData['points'][0]['hovertext']
    else:
        options = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','2A','2B','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','971','972','973','974']
    
    df_FT = API_France_Travail(array_value)
    df = df_FT[['intitule','typeContratLibelle','experienceLibelle','lieuTravail']].copy()
    df["lieuTravail"] = df["lieuTravail"].apply(lambda x: x['libelle']).apply(lambda x: x[0:3]).apply(lambda x: x.strip())
    df.drop(df[df['lieuTravail'] == 'Fra'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'FRA'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Ile'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Mar'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Bou'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == '976'].index, inplace = True)
    df = df[df['lieuTravail'].isin(options)]
    return create_emploi(df, theme)

@callback(
    Output(component_id='figContrats', component_property='figure'),
    Input('figRepartition', 'selectedData'),
    Input(component_id='framework-multi-select', component_property='value'),
    Input("mantine-provider", "forceColorScheme"),
)

def update_contrat(selectedData, array_value, theme):
    options = []
    if selectedData != None:
        if type(selectedData['points'][0]['hovertext']) == str:
            options.append(selectedData['points'][0]['hovertext'])
        else:
            options = selectedData['points'][0]['hovertext']
    else:
        options = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','2A','2B','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','971','972','973','974']
    
    df_FT = API_France_Travail(array_value)
    df = df_FT[['intitule','typeContratLibelle','experienceLibelle','lieuTravail']].copy()
    df["lieuTravail"] = df["lieuTravail"].apply(lambda x: x['libelle']).apply(lambda x: x[0:3]).apply(lambda x: x.strip())
    df.drop(df[df['lieuTravail'] == 'Fra'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'FRA'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Ile'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Mar'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Bou'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == '976'].index, inplace = True)
    df = df[df['lieuTravail'].isin(options)]
        
    return create_contrat(df, theme)

@callback(
    Output(component_id='figExperiences', component_property='figure'),
    Input('figRepartition', 'selectedData'),
    Input(component_id='framework-multi-select', component_property='value'),
    Input("mantine-provider", "forceColorScheme"),
)

def update_experience(selectedData, array_value, theme):
    options = []
    if selectedData != None:
        if type(selectedData['points'][0]['hovertext']) == str:
            options.append(selectedData['points'][0]['hovertext'])
        else:
            options = selectedData['points'][0]['hovertext']
    else:
        options = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','2A','2B','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','971','972','973','974']
    
    df_FT = API_France_Travail(array_value)
    df = df_FT[['intitule','typeContratLibelle','experienceLibelle','lieuTravail']].copy()
    df["lieuTravail"] = df["lieuTravail"].apply(lambda x: x['libelle']).apply(lambda x: x[0:3]).apply(lambda x: x.strip())
    df.drop(df[df['lieuTravail'] == 'Fra'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'FRA'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Ile'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Mar'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == 'Bou'].index, inplace = True)
    df.drop(df[df['lieuTravail'] == '976'].index, inplace = True)
    df = df[df['lieuTravail'].isin(options)]
    
    return create_experience(df, theme)

########### IA Chatbot ###########
@app.callback(
    Output("display-conversation", "children"), [Input("store-conversation", "data")]
)
def update_display(chat_history):
    return [
        textbox(x, box="user") if i % 2 == 0 else textbox(x, box="AI")
        for i, x in enumerate(chat_history.split("<split>")[:-1])
    ]


@app.callback(
    Output("user-input", "value"),
    [Input("submit", "n_clicks"), Input("user-input", "n_submit")],
)
def clear_input(n_clicks, n_submit):
    return ""

@app.callback(
    [Output("store-conversation", "data"), Output("loading-component", "children")],
    [Input("submit", "n_clicks"), Input("user-input", "n_submit")],
    [State("user-input", "value"), State("store-conversation", "data")],
    Input(component_id='framework-multi-select', component_property='value'),
)
def run_chatbot(n_clicks, n_submit, user_input, chat_history, array_value):
    if n_clicks == 0 and n_submit is None:
        return "", None

    if user_input is None or user_input == "":
        return chat_history, None

    df_FT = API_France_Travail(array_value)
    
    df_FT_Select = df_FT[['intitule','typeContratLibelle','experienceLibelle','competences','description','qualitesProfessionnelles','salaire','lieuTravail','formations']].copy()
    list_FT = df_FT_Select.values.tolist()
    context = ''
    for i in range(0,len(list_FT)):
        context += "\n✔️ Emploi : " + str(list_FT[i][0]) + ";\n◉ Contrat : " + str(list_FT[i][1]) + ";\n◉ Compétences professionnelles : " + str(list_FT[i][3]) + ";\n" + "◉ Salaire : " + str(list_FT[i][6]) + ";\n◉ Qualification : " + str(list_FT[i][5]).replace("'libelle'","\n• 'libelle") + ";\n◉ Localisation : " + str(list_FT[i][7]) + ";\n◉ Expérience : " + str(list_FT[i][2]) + ";\n◉ Niveau de qualification : " + str(list_FT[i][8]) + ";\n◉ Description de l'emploi : " + str(list_FT[i][4]) + "\n"
        #context = df_FT.to_string(index=False)
    template = """<s>[INST] Vous êtes un ingénieur pédagogique de l'enseignement supérieur et vous êtes doué pour faire des analyses des formations de l'enseignement supérieur et de faire le rapprochement entre les compétences académiques et les compétences professionnelles attendues par le marché de l'emploi et les les recruteurs, en fonction des critères définis ci-avant. En fonction des informations suivantes et du contexte suivant seulement et strictement, répondez en langue française strictement à la question ci-dessous, en 5000 mots au moins. Lorsque cela est possible, cite les sources du contexte. Si vous ne pouvez pas répondre à la question sur la base des informations, dites que vous ne trouvez pas de réponse ou que vous ne parvenez pas à trouver de réponse. Essayez donc de comprendre en profondeur le contexte et répondez uniquement en vous basant sur les informations fournies. Ne générez pas de réponses non pertinentes.
    Répondez à la question ci-dessous à partir du contexte ci-dessous :
    {context}
    {question} [/INST] </s>
    """
    context_p = context[:28500]
    name = "Mistral"
    chat_history += f"Vous: {user_input}<split>{name}:"

    model_input = template + chat_history.replace("<split>", "\n")
    #model_input = template
    
    prompt = PromptTemplate(template=model_input, input_variables=["question","context"])
    
    #prompt = dedent(
    #    f"""
    #{description}

    #Vous: Bonjour {name}!
    #{name}: Bonjour! Ravi de parler avec vous aujourd'hui.
    #"""
    #)

    # First add the user input to the chat history
    
    os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HUGGINGFACEHUB_API_TOKEN']
    #repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
    repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
    #repo_id = "microsoft/Phi-3.5-mini-instruct"
    #mistral_url = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x22B-Instruct-v0.1"
    llm = HuggingFaceEndpoint(
        repo_id=repo_id, task="text2text-generation", max_new_tokens=8000, temperature=0.001, streaming=True
    )
    model_output = ""
    chain = prompt | llm
    for s in chain.stream({"question":user_input,"context":context_p}):
        model_output = model_output + s
        print(s, end="", flush=True)
    
  
  #response = openai.Completion.create(
    #    engine="davinci",
    #    prompt=model_input,
    #    max_tokens=250,
    #    stop=["You:"],
    #    temperature=0.9,
    #)
    #model_output = response.choices[0].text.strip()

    chat_history += f"{model_output}<split>"
        
    return chat_history, None

if __name__ == '__main__':
    app.run_server(debug=True)