File size: 57,349 Bytes
fe0ef09
 
 
 
 
 
 
 
 
 
98a0731
 
fe0ef09
 
 
d2136dc
 
fe0ef09
 
 
a43dc1c
 
 
 
79859a0
c58c8df
 
 
79859a0
 
 
c58c8df
 
a43dc1c
 
 
 
fe0ef09
a43dc1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
 
a43dc1c
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43dc1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
f310d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98a0731
 
 
 
 
 
 
 
fe0ef09
 
 
 
 
 
 
 
a43dc1c
 
 
 
fe0ef09
 
 
 
 
 
98a0731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622f9de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98a0731
622f9de
fe0ef09
 
 
 
622f9de
 
 
fe0ef09
622f9de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
622f9de
fe0ef09
 
622f9de
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
3d66778
622f9de
3d66778
622f9de
3d66778
 
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91099df
 
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91099df
fe0ef09
 
 
91099df
 
 
 
aaeb58e
622f9de
91099df
622f9de
 
91099df
622f9de
25a8945
622f9de
 
 
71b0c15
622f9de
 
 
 
 
1e5d194
622f9de
 
 
91099df
622f9de
 
 
1e5d194
622f9de
 
fe0ef09
 
 
 
 
 
5d456ad
 
 
fe0ef09
 
 
 
91099df
 
fe0ef09
91099df
fe0ef09
 
 
aaeb58e
 
5d456ad
f127ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91099df
 
fe0ef09
91099df
 
 
 
 
 
 
 
 
fe0ef09
91099df
622f9de
5d456ad
 
 
622f9de
5d456ad
 
91099df
 
622f9de
25a8945
622f9de
 
 
 
 
 
 
 
 
 
 
 
ffea308
5d456ad
 
 
 
 
 
 
 
 
 
91099df
 
 
5d456ad
 
 
 
 
 
 
f127ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d456ad
91099df
 
5d456ad
ffea308
fe0ef09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e351eb9
 
 
 
 
 
 
 
 
 
 
 
a43dc1c
 
 
 
 
 
 
 
 
 
 
 
e351eb9
 
 
 
 
 
 
fe0ef09
e351eb9
 
a43dc1c
 
 
 
 
 
 
 
 
 
 
e351eb9
 
 
 
66cc7dd
e351eb9
 
 
 
 
 
 
 
 
fe0ef09
e351eb9
 
 
 
79859a0
e351eb9
 
79859a0
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
e351eb9
 
 
 
 
fe0ef09
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6697b32
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ef09
e351eb9
 
 
 
 
fe0ef09
 
e351eb9
 
fe0ef09
 
 
e351eb9
 
 
 
 
 
 
79859a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6697b32
79859a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6697b32
e351eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43dc1c
614acaa
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
import streamlit as st
import requests
import os
import json
import uuid
from datetime import datetime, timedelta
from sentence_transformers import SentenceTransformer
import chromadb
from langchain_text_splitters import RecursiveCharacterTextSplitter
import re
import shutil
from git import Repo

# Page configuration
st.set_page_config(
    page_title="RAG Chat Flow ✘",
    page_icon="✘",
    initial_sidebar_state="expanded"
)

# Initialize dark mode state
if 'dark_mode' not in st.session_state:
    st.session_state.dark_mode = False

# Define personality questions - reduced to general ones
PERSONALITY_QUESTIONS = [
    "What is [name]'s personality like?",
    "What does [name] do for work?",
    "What are [name]'s hobbies?",
    "What makes [name] special?",
    "Tell me about [name]"
]

# Enhanced CSS styling with dark mode support
def get_css_styles():
    if st.session_state.dark_mode:
        return """
<style>
    /* Dark Mode Styles */
    .stApp {
        background: #0e1117;
        color: #fafafa;
    }
    
    .main .block-container {
        max-width: 900px;
    }
    
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    header {visibility: hidden;}
    .stDeployButton {display: none;}
    
    /* Sidebar dark mode */
    .css-1d391kg {
        background-color: #1e1e1e !important;
    }
    
    .css-1cypcdb {
        background-color: #1e1e1e !important;
    }
    
    /* Chat messages dark mode */
    .stChatMessage {
        background-color: #262730 !important;
        border: 1px solid #404040 !important;
    }
    
    /* Input fields dark mode */
    .stTextInput > div > div > input {
        background-color: #262730 !important;
        color: #fafafa !important;
        border-color: #404040 !important;
    }
    
    .stTextArea > div > div > textarea {
        background-color: #262730 !important;
        color: #fafafa !important;
        border-color: #404040 !important;
    }
    
    .model-id {
        color: #4ade80;
        font-family: monospace;
    }
    
    .model-attribution {
        color: #4ade80;
        font-size: 0.8em;
        font-style: italic;
    }
    
    .rag-attribution {
        color: #a78bfa;
        font-size: 0.8em;
        font-style: italic;
        background: #1f2937;
        padding: 8px;
        border-radius: 4px;
        border-left: 3px solid #a78bfa;
        margin-top: 8px;
    }
    
    /* Dark mode toggle button */
    .dark-mode-toggle {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 8px 16px;
        margin: 4px 0;
        border-radius: 8px;
        border: none;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.9em;
        width: 100%;
        text-align: center;
    }
    
    .dark-mode-toggle:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    }
    
    /* NEW CHAT BUTTON - Black background for dark mode */
    .stButton > button[kind="primary"] {
        background-color: #1f2937 !important;
        border-color: #374151 !important;
        color: #fafafa !important;
    }
    
    .stButton > button[kind="primary"]:hover {
        background-color: #374151 !important;
        border-color: #4b5563 !important;
        color: #fafafa !important;
    }
    
    /* Regular buttons dark mode */
    .stButton > button {
        background-color: #374151 !important;
        border-color: #4b5563 !important;
        color: #fafafa !important;
    }
    
    .stButton > button:hover {
        background-color: #4b5563 !important;
        border-color: #6b7280 !important;
        color: #fafafa !important;
    }
    
    /* Personality Questions Styling Dark Mode */
    .personality-question {
        background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        color: white;
        padding: 8px 12px;
        margin: 4px 0;
        border-radius: 8px;
        border: none;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.85em;
        width: 100%;
        text-align: left;
    }
    
    .personality-question:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
    }
    
    .personality-section {
        background: #1f2937;
        color: #e5e7eb;
        padding: 12px;
        border-radius: 8px;
        border-left: 4px solid #4f46e5;
        margin: 10px 0;
    }
    
    /* Chat history styling dark mode */
    .chat-history-item {
        padding: 8px 12px;
        margin: 4px 0;
        border-radius: 8px;
        border: 1px solid #374151;
        background: #1f2937;
        color: #e5e7eb;
        cursor: pointer;
        transition: all 0.2s;
    }
    
    .chat-history-item:hover {
        background: #374151;
        border-color: #4ade80;
    }
    
    .document-status {
        background: #1e3a8a;
        color: #bfdbfe;
        padding: 10px;
        border-radius: 8px;
        border-left: 4px solid #3b82f6;
        margin: 10px 0;
    }
    
    .github-status {
        background: #581c87;
        color: #e9d5ff;
        padding: 10px;
        border-radius: 8px;
        border-left: 4px solid #a78bfa;
        margin: 10px 0;
    }
    
    .rag-stats {
        background: #581c87;
        color: #e9d5ff;
        padding: 8px;
        border-radius: 6px;
        font-size: 0.85em;
    }
    
    /* Expander dark mode */
    .streamlit-expanderHeader {
        background-color: #1f2937 !important;
        color: #fafafa !important;
    }
    
    .streamlit-expanderContent {
        background-color: #111827 !important;
        color: #fafafa !important;
    }
    
    /* Checkbox dark mode */
    .stCheckbox {
        color: #fafafa !important;
    }
    
    /* Select box dark mode */
    .stSelectbox > div > div {
        background-color: #262730 !important;
        color: #fafafa !important;
    }
    
    /* File uploader dark mode */
    .stFileUploader {
        background-color: #1f2937 !important;
        border-color: #374151 !important;
    }
    
    /* Progress bar dark mode */
    .stProgress .st-bo {
        background-color: #374151 !important;
    }
    
    /* Success/Error/Warning messages dark mode */
    .stSuccess {
        background-color: #064e3b !important;
        color: #6ee7b7 !important;
    }
    
    .stError {
        background-color: #7f1d1d !important;
        color: #fca5a5 !important;
    }
    
    .stWarning {
        background-color: #78350f !important;
        color: #fcd34d !important;
    }
    
    .stInfo {
        background-color: #1e3a8a !important;
        color: #93c5fd !important;
    }
    
    /* Caption text dark mode */
    .caption {
        color: #9ca3af !important;
    }
    
    /* Divider dark mode */
    hr {
        border-color: #374151 !important;
    }
</style>
"""
    else:
        return """
<style>
    /* Light Mode Styles */
    .stApp {
        background: white;
        color: #000000;
    }
    
    .main .block-container {
        max-width: 900px;
    }
    
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    header {visibility: hidden;}
    .stDeployButton {display: none;}
    
    .model-id {
        color: #28a745;
        font-family: monospace;
    }
    
    .model-attribution {
        color: #28a745;
        font-size: 0.8em;
        font-style: italic;
    }
    
    .rag-attribution {
        color: #6f42c1;
        font-size: 0.8em;
        font-style: italic;
        background: #f8f9fa;
        padding: 8px;
        border-radius: 4px;
        border-left: 3px solid #6f42c1;
        margin-top: 8px;
    }
    
    /* Light mode toggle button */
    .dark-mode-toggle {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 8px 16px;
        margin: 4px 0;
        border-radius: 8px;
        border: none;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.9em;
        width: 100%;
        text-align: center;
    }
    
    .dark-mode-toggle:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
    }
    
    /* NEW CHAT BUTTON - Black background */
    .stButton > button[kind="primary"] {
        background-color: #000000 !important;
        border-color: #000000 !important;
        color: #ffffff !important;
    }
    
    .stButton > button[kind="primary"]:hover {
        background-color: #333333 !important;
        border-color: #333333 !important;
        color: #ffffff !important;
    }
    
    /* Personality Questions Styling */
    .personality-question {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 8px 12px;
        margin: 4px 0;
        border-radius: 8px;
        border: none;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 0.85em;
        width: 100%;
        text-align: left;
    }
    
    .personality-question:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
    }
    
    .personality-section {
        background: #f8f9ff;
        padding: 12px;
        border-radius: 8px;
        border-left: 4px solid #667eea;
        margin: 10px 0;
    }
    
    /* Chat history styling */
    .chat-history-item {
        padding: 8px 12px;
        margin: 4px 0;
        border-radius: 8px;
        border: 1px solid #e0e0e0;
        background: #f8f9fa;
        cursor: pointer;
        transition: all 0.2s;
    }
    
    .chat-history-item:hover {
        background: #e9ecef;
        border-color: #28a745;
    }
    
    .document-status {
        background: #e3f2fd;
        padding: 10px;
        border-radius: 8px;
        border-left: 4px solid #2196f3;
        margin: 10px 0;
    }
    
    .github-status {
        background: #f3e5f5;
        padding: 10px;
        border-radius: 8px;
        border-left: 4px solid #6f42c1;
        margin: 10px 0;
    }
    
    .rag-stats {
        background: #f3e5f5;
        padding: 8px;
        border-radius: 6px;
        font-size: 0.85em;
        color: #4a148c;
    }
</style>
"""

# Apply CSS styles
st.markdown(get_css_styles(), unsafe_allow_html=True)

# File paths
HISTORY_FILE = "rag_chat_history.json"
SESSIONS_FILE = "rag_chat_sessions.json"
USERS_FILE = "online_users.json"

# ================= GITHUB INTEGRATION =================

def clone_github_repo():
    """Clone or update GitHub repository with documents"""
    github_token = os.getenv("GITHUB_TOKEN")
    
    if not github_token:
        st.error("πŸ”‘ GITHUB_TOKEN not found in environment variables")
        return False
    
    try:
        repo_url = f"https://{github_token}@github.com/Umer-K/family-profiles.git"
        repo_dir = "family_profiles"
        
        # Clean up existing directory if it exists
        if os.path.exists(repo_dir):
            shutil.rmtree(repo_dir)
        
        # Clone the repository
        with st.spinner("πŸ”„ Cloning private repository..."):
            Repo.clone_from(repo_url, repo_dir)
        
        # Copy txt files to documents folder
        documents_dir = "documents"
        os.makedirs(documents_dir, exist_ok=True)
        
        # Clear existing documents
        for file in os.listdir(documents_dir):
            if file.endswith('.txt'):
                os.remove(os.path.join(documents_dir, file))
        
        # Copy new txt files from repo
        txt_files_found = 0
        for root, dirs, files in os.walk(repo_dir):
            for file in files:
                if file.endswith('.txt'):
                    src_path = os.path.join(root, file)
                    dst_path = os.path.join(documents_dir, file)
                    shutil.copy2(src_path, dst_path)
                    txt_files_found += 1
        
        # Clean up repo directory
        shutil.rmtree(repo_dir)
        
        st.success(f"βœ… Successfully synced {txt_files_found} documents from GitHub!")
        return True
        
    except Exception as e:
        st.error(f"❌ GitHub sync failed: {str(e)}")
        return False

def check_github_status():
    """Check GitHub token availability and repo access"""
    github_token = os.getenv("GITHUB_TOKEN")
    
    if not github_token:
        return {
            "status": "missing",
            "message": "No GitHub token found",
            "color": "red"
        }
    
    try:
        # Test token by making a simple API call
        headers = {
            "Authorization": f"token {github_token}",
            "Accept": "application/vnd.github.v3+json"
        }
        
        response = requests.get(
            "https://api.github.com/repos/Umer-K/family-profiles",
            headers=headers,
            timeout=10
        )
        
        if response.status_code == 200:
            return {
                "status": "connected",
                "message": "GitHub access verified",
                "color": "green"
            }
        elif response.status_code == 404:
            return {
                "status": "not_found",
                "message": "Repository not found or no access",
                "color": "orange"
            }
        elif response.status_code == 401:
            return {
                "status": "unauthorized",
                "message": "Invalid GitHub token",
                "color": "red"
            }
        else:
            return {
                "status": "error",
                "message": f"GitHub API error: {response.status_code}",
                "color": "orange"
            }
            
    except Exception as e:
        return {
            "status": "error",
            "message": f"Connection error: {str(e)}",
            "color": "orange"
        }

# ================= RAG SYSTEM CLASS =================

@st.cache_resource
def initialize_rag_system():
    """Initialize RAG system with caching"""
    return ProductionRAGSystem()

class ProductionRAGSystem:
    def __init__(self, collection_name="streamlit_rag_docs"):
        self.collection_name = collection_name
        
        # Initialize embedding model
        try:
            self.model = SentenceTransformer('all-mpnet-base-v2')
        except Exception as e:
            st.error(f"Error loading embedding model: {e}")
            self.model = None
            return
        
        # Initialize ChromaDB
        try:
            self.client = chromadb.PersistentClient(path="./chroma_db")
            try:
                self.collection = self.client.get_collection(collection_name)
            except:
                self.collection = self.client.create_collection(collection_name)
        except Exception as e:
            st.error(f"Error initializing ChromaDB: {e}")
            self.client = None
            return
        
        # Initialize text splitter
        self.text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=800,
            chunk_overlap=100,
            length_function=len,
            separators=["\n\n", "\n", ". ", " ", ""]
        )
    
    def get_collection_count(self):
        """Get number of documents in collection"""
        try:
            return self.collection.count() if self.collection else 0
        except:
            return 0
    
    def load_documents_from_folder(self, folder_path="documents"):
        """Load documents from folder"""
        if not os.path.exists(folder_path):
            return []
        
        txt_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')]
        if not txt_files:
            return []
        
        all_chunks = []
        for filename in txt_files:
            filepath = os.path.join(folder_path, filename)
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    content = f.read().strip()
                    
                if content:
                    chunks = self.text_splitter.split_text(content)
                    for i, chunk in enumerate(chunks):
                        all_chunks.append({
                            'content': chunk,
                            'source_file': filename,
                            'chunk_index': i,
                            'char_count': len(chunk)
                        })
            except Exception as e:
                st.error(f"Error reading {filename}: {e}")
        
        return all_chunks
    
    def index_documents(self, document_folder="documents"):
        """Index documents with progress bar"""
        if not self.model or not self.client:
            return False
        
        chunks = self.load_documents_from_folder(document_folder)
        if not chunks:
            return False
        
        # Clear existing collection
        try:
            self.client.delete_collection(self.collection_name)
            self.collection = self.client.create_collection(self.collection_name)
        except:
            pass
        
        # Create embeddings with progress bar
        progress_bar = st.progress(0)
        status_text = st.empty()
        
        chunk_texts = [chunk['content'] for chunk in chunks]
        
        try:
            status_text.text("Creating embeddings...")
            embeddings = self.model.encode(chunk_texts, show_progress_bar=False)
            
            status_text.text("Storing in database...")
            for i, (chunk, embedding) in enumerate(zip(chunks, embeddings)):
                chunk_id = f"{chunk['source_file']}_{chunk['chunk_index']}"
                
                metadata = {
                    "source_file": chunk['source_file'],
                    "chunk_index": chunk['chunk_index'],
                    "char_count": chunk['char_count']
                }
                
                self.collection.add(
                    documents=[chunk['content']],
                    ids=[chunk_id],
                    embeddings=[embedding.tolist()],
                    metadatas=[metadata]
                )
                
                progress_bar.progress((i + 1) / len(chunks))
            
            progress_bar.empty()
            status_text.empty()
            return True
            
        except Exception as e:
            st.error(f"Error during indexing: {e}")
            progress_bar.empty()
            status_text.empty()
            return False
    
    def expand_query_with_family_terms(self, query):
        """Expand query to include family relationship synonyms"""
        family_mappings = {
            'mother': ['mama', 'mom', 'ammi'],
            'mama': ['mother', 'mom', 'ammi'],
            'father': ['papa', 'dad', 'abbu'],
            'papa': ['father', 'dad', 'abbu'],
            'brother': ['bhai', 'bro'],
            'bhai': ['brother', 'bro'],
            'sister': ['behn', 'sis'],
            'behn': ['sister', 'sis']
        }
        
        expanded_terms = [query]
        query_lower = query.lower()
        
        for key, synonyms in family_mappings.items():
            if key in query_lower:
                for synonym in synonyms:
                    expanded_terms.append(query_lower.replace(key, synonym))
        
        return expanded_terms
    
    def search(self, query, n_results=5):
        """Search for relevant chunks with family relationship mapping"""
        if not self.model or not self.collection:
            return None
        
        try:
            # Expand query with family terms
            expanded_queries = self.expand_query_with_family_terms(query)
            all_results = []
            
            # Search with all expanded terms
            for search_query in expanded_queries:
                query_embedding = self.model.encode([search_query])[0].tolist()
                
                results = self.collection.query(
                    query_embeddings=[query_embedding],
                    n_results=n_results
                )
                
                if results['documents'][0]:
                    for chunk, distance, metadata in zip(
                        results['documents'][0],
                        results['distances'][0],
                        results['metadatas'][0]
                    ):
                        similarity = max(0, 1 - distance)
                        all_results.append({
                            'content': chunk,
                            'metadata': metadata,
                            'similarity': similarity,
                            'query_used': search_query
                        })
            
            if not all_results:
                return None
            
            # Remove duplicates and sort by similarity
            seen_chunks = set()
            unique_results = []
            for result in all_results:
                chunk_id = f"{result['metadata']['source_file']}_{result['content'][:50]}"
                if chunk_id not in seen_chunks:
                    seen_chunks.add(chunk_id)
                    unique_results.append(result)
            
            # Sort by similarity and take top results
            unique_results.sort(key=lambda x: x['similarity'], reverse=True)
            search_results = unique_results[:n_results]
            
            # Debug: Show search results for troubleshooting
            print(f"Search for '{query}' (expanded to {len(expanded_queries)} terms) found {len(search_results)} results")
            for i, result in enumerate(search_results[:3]):
                print(f"  {i+1}. Similarity: {result['similarity']:.3f} | Source: {result['metadata']['source_file']} | Query: {result['query_used']}")
                print(f"     Content preview: {result['content'][:100]}...")
            
            return search_results
        except Exception as e:
            st.error(f"Search error: {e}")
            return None
    
    def extract_direct_answer(self, query, content):
        """Extract direct answer from content"""
        query_lower = query.lower()
        sentences = re.split(r'[.!?]+', content)
        sentences = [s.strip() for s in sentences if len(s.strip()) > 20]
        
        query_words = set(query_lower.split())
        scored_sentences = []
        
        for sentence in sentences:
            sentence_words = set(sentence.lower().split())
            exact_matches = len(query_words.intersection(sentence_words))
            
            # Bonus scoring for key terms
            bonus_score = 0
            if '401k' in query_lower and ('401' in sentence.lower() or 'retirement' in sentence.lower()):
                bonus_score += 3
            if 'sick' in query_lower and 'sick' in sentence.lower():
                bonus_score += 3
            if 'vacation' in query_lower and 'vacation' in sentence.lower():
                bonus_score += 3
            
            total_score = exact_matches * 2 + bonus_score
            
            if total_score > 0:
                scored_sentences.append((sentence, total_score))
        
        if scored_sentences:
            scored_sentences.sort(key=lambda x: x[1], reverse=True)
            best_sentence = scored_sentences[0][0]
            if not best_sentence.endswith('.'):
                best_sentence += '.'
            return best_sentence
        
        # Fallback
        for sentence in sentences:
            if len(sentence) > 30:
                return sentence + ('.' if not sentence.endswith('.') else '')
        
        return content[:200] + "..."
    
    def generate_answer(self, query, search_results, use_ai_enhancement=True, unlimited_tokens=False):
        """Generate both AI and extracted answers with proper token handling"""
        if not search_results:
            return {
                'ai_answer': "No information found in documents.",
                'extracted_answer': "No information found in documents.",
                'sources': [],
                'confidence': 0,
                'has_both': False
            }
        
        best_result = search_results[0]
        sources = list(set([r['metadata']['source_file'] for r in search_results[:2]]))
        avg_confidence = sum(r['similarity'] for r in search_results[:2]) / len(search_results[:2])
        
        # Always generate extracted answer
        extracted_answer = self.extract_direct_answer(query, best_result['content'])
        
        # Try AI answer if requested and API key available
        ai_answer = None
        openrouter_key = os.environ.get("OPENROUTER_API_KEY")
        
        if use_ai_enhancement and openrouter_key:
            # Build context from search results
            context = "\n\n".join([f"Source: {r['metadata']['source_file']}\nContent: {r['content']}" 
                                 for r in search_results[:3]])
            
            # Create focused prompt for rich, engaging family responses
            if unlimited_tokens:
                prompt = f"""You are a warm, caring family assistant who knows everyone well. Based on the family information below, provide a rich, detailed, and engaging response.
Family Document Context:
{context}
Question: {query}
Instructions:
- Use the document information as your foundation
- Expand with logical personality traits and qualities someone like this would have
- Add 3-4 additional lines of thoughtful insights about their character
- Use 5-6 relevant emojis throughout the response to make it warm and engaging
- Write in a caring, family-friend tone
- If someone asks about relationships (like "mother" = "mama"), make those connections
- Make the response feel personal and detailed, not just a basic fact
- Include both strengths and endearing qualities
- Keep it warm but informative (4-6 sentences total)
- Sprinkle emojis naturally throughout, not just at the end
Remember: You're helping someone learn about their family members in a meaningful way! πŸ’"""
                max_tokens = 400  # Increased for richer responses
                temperature = 0.3  # Slightly more creative
            else:
                # Shorter but still enhanced prompt for conservative mode
                prompt = f"""Based on this family info: {extracted_answer}
Question: {query}
Give a warm, detailed answer with 3-4 emojis spread throughout. Add 2-3 more qualities this person likely has. Make it caring and personal! πŸ’"""
                max_tokens = 150  # Better than 50 for family context
                temperature = 0.2
            
            try:
                response = requests.post(
                    "https://openrouter.ai/api/v1/chat/completions",
                    headers={
                        "Authorization": f"Bearer {openrouter_key}",
                        "Content-Type": "application/json",
                        "HTTP-Referer": "https://huggingface.co/spaces",
                        "X-Title": "RAG Chatbot"
                    },
                    json={
                        "model": "openai/gpt-3.5-turbo",
                        "messages": [{"role": "user", "content": prompt}],
                        "max_tokens": max_tokens,
                        "temperature": temperature
                    },
                    timeout=15
                )
                
                if response.status_code == 200:
                    ai_response = response.json()['choices'][0]['message']['content'].strip()
                    ai_answer = ai_response if len(ai_response) > 10 else extracted_answer
                else:
                    # Log the actual error for debugging
                    error_detail = ""
                    try:
                        error_detail = response.json().get('error', {}).get('message', '')
                    except:
                        pass
                    
                    if response.status_code == 402:
                        st.warning("πŸ’³ OpenRouter credits exhausted. Using extracted answers only.")
                    elif response.status_code == 429:
                        st.warning("⏱️ Rate limit reached. Using extracted answers only.")
                    elif response.status_code == 401:
                        st.error("πŸ”‘ Invalid API key. Check your OpenRouter key.")
                    elif response.status_code == 400:
                        st.error(f"❌ Bad request: {error_detail}")
                    else:
                        st.warning(f"API Error {response.status_code}: {error_detail}. Using extracted answers only.")
                    
            except requests.exceptions.Timeout:
                st.warning("⏱️ API timeout. Using extracted answers only.")
            except Exception as e:
                st.warning(f"API Exception: {str(e)}. Using extracted answers only.")
        
        return {
            'ai_answer': ai_answer,
            'extracted_answer': extracted_answer,
            'sources': sources,
            'confidence': avg_confidence,
            'has_both': ai_answer is not None
        }

def get_general_ai_response(query, unlimited_tokens=False):
    """Get AI response for general questions with family-friendly enhancement"""
    openrouter_key = os.environ.get("OPENROUTER_API_KEY")
    
    if not openrouter_key:
        return "I can only answer questions about your family members from the uploaded documents. Please add an OpenRouter API key for general conversations. πŸ’"
    
    try:
        # Adjust parameters based on token availability
        if unlimited_tokens:
            max_tokens = 350  # Good limit for detailed family responses
            temperature = 0.5
            prompt = f"""You are a caring family assistant. Someone is asking about their family but I couldn't find specific information in their family documents.
Question: {query}
Please provide a warm, helpful response that:
- Acknowledges I don't have specific information about their family member
- Suggests they might want to add more details to their family profiles
- Offers to help in other ways
- Uses a caring, family-friendly tone with appropriate emojis
- Keep it supportive and understanding πŸ’"""
        else:
            max_tokens = 100  # Reasonable for conservative mode
            temperature = 0.4
            prompt = f"Family question: {query[:100]} - I don't have info about this family member. Give a caring, helpful response with emojis πŸ’"
        
        response = requests.post(
            "https://openrouter.ai/api/v1/chat/completions",
            headers={
                "Authorization": f"Bearer {openrouter_key}",
                "Content-Type": "application/json",
                "HTTP-Referer": "https://huggingface.co/spaces",
                "X-Title": "RAG Chatbot"
            },
            json={
                "model": "openai/gpt-3.5-turbo",
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": max_tokens,
                "temperature": temperature
            },
            timeout=15
        )
        
        if response.status_code == 200:
            return response.json()['choices'][0]['message']['content'].strip()
        else:
            # Get detailed error information
            error_detail = ""
            try:
                error_detail = response.json().get('error', {}).get('message', '')
            except:
                pass
            
            if response.status_code == 402:
                return "Sorry, OpenRouter credits exhausted. Please add more credits or top up your account."
            elif response.status_code == 429:
                return "Rate limit reached. Please try again in a moment."
            elif response.status_code == 401:
                return "Invalid API key. Please check your OpenRouter API key configuration."
            elif response.status_code == 400:
                return f"Bad request: {error_detail}. Please try rephrasing your question."
            else:
                return f"API error (Status: {response.status_code}): {error_detail}. Please try again."
            
    except requests.exceptions.Timeout:
        return "Request timeout. Please try again."
    except Exception as e:
        return f"Error: {str(e)}"

def get_user_id():
    """Get unique ID for this user session"""
    if 'user_id' not in st.session_state:
        st.session_state.user_id = str(uuid.uuid4())[:8]
    return st.session_state.user_id

def update_online_users():
    """Update user status"""
    try:
        users = {}
        if os.path.exists(USERS_FILE):
            with open(USERS_FILE, 'r') as f:
                users = json.load(f)

        user_id = get_user_id()
        users[user_id] = {
            'last_seen': datetime.now().isoformat(),
            'name': f'User-{user_id}',
            'session_start': users.get(user_id, {}).get('session_start', datetime.now().isoformat())
        }

        # Clean up old users
        current_time = datetime.now()
        active_users = {}
        for uid, data in users.items():
            try:
                last_seen = datetime.fromisoformat(data['last_seen'])
                if current_time - last_seen < timedelta(minutes=5):
                    active_users[uid] = data
            except:
                continue

        with open(USERS_FILE, 'w') as f:
            json.dump(active_users, f, indent=2)

        return len(active_users)
    except:
        return 1

def load_chat_history():
    """Load chat history"""
    try:
        if os.path.exists(HISTORY_FILE):
            with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
    except:
        pass
    return []

def save_chat_history(messages):
    """Save chat history"""
    try:
        with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
            json.dump(messages, f, ensure_ascii=False, indent=2)
    except Exception as e:
        st.error(f"Error saving history: {e}")

def start_new_chat():
    """Start new chat session"""
    st.session_state.messages = []
    st.session_state.session_id = str(uuid.uuid4())

# ================= MAIN APP =================

# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = load_chat_history()

if "session_id" not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())

# Initialize RAG system
rag_system = initialize_rag_system()

# Header with dark mode toggle
col1, col2 = st.columns([4, 1])
with col1:
    st.title("RAG Chat Flow ✘")
    st.caption("Ask questions about your documents with AI-powered retrieval")

with col2:
    # Dark mode toggle button
    mode_text = "🌞 Light" if st.session_state.dark_mode else "πŸŒ™ Dark"
    if st.button(mode_text, use_container_width=True):
        st.session_state.dark_mode = not st.session_state.dark_mode
        st.rerun()

# Sidebar
with st.sidebar:
    # New Chat Button
    if st.button("βž• New Chat", use_container_width=True, type="primary"):
        start_new_chat()
        st.rerun()
    
    st.divider()
    
    # Dark Mode Toggle in Sidebar too
    st.header("🎨 Theme")
    theme_status = "Dark Mode ✨" if st.session_state.dark_mode else "Light Mode β˜€οΈ"
    if st.button(f"πŸ”„ Switch to {'Light' if st.session_state.dark_mode else 'Dark'} Mode", use_container_width=True):
        st.session_state.dark_mode = not st.session_state.dark_mode
        st.rerun()
    
    st.info(f"Current: {theme_status}")
    
    st.divider()
    
    # Personality Questions Section
    st.header("🎭 Personality Questions")
    
    # Name input for personalizing questions
    name_input = st.text_input("Enter name for personalized questions:", placeholder="First name only", help="Replace [name] in questions with this name")
    
    if name_input.strip():
        name = name_input.strip()
        st.markdown(f"""
        <div class="personality-section">
            <strong>πŸ’« Quick Questions for {name}:</strong><br>
            <small>Click any question to ask about {name}</small>
        </div>
        """, unsafe_allow_html=True)
        
        # Display personality questions as clickable buttons
        for i, question in enumerate(PERSONALITY_QUESTIONS):
            formatted_question = question.replace("[name]", name)
            if st.button(formatted_question, key=f"pq_{i}", use_container_width=True):
                # Add the question to chat and set flag to process it
                user_message = {"role": "user", "content": formatted_question}
                st.session_state.messages.append(user_message)
                st.session_state.process_personality_question = formatted_question
                st.rerun()
    else:
        st.markdown("""
        <div class="personality-section">
            <strong>πŸ’« Sample Questions:</strong><br>
            <small>Enter a name above to personalize these questions</small>
        </div>
        """, unsafe_allow_html=True)
        
        # Show sample questions without names
        for question in PERSONALITY_QUESTIONS[:5]:  # Show first 5 as examples
            st.markdown(f"β€’ {question}")
    
    st.divider()
    
    # GitHub Integration
    st.header("πŸ™ GitHub Integration")
    
    github_status = check_github_status()
    
    if github_status["status"] == "connected":
        st.markdown(f"""
        <div class="github-status">
            <strong>🟒 GitHub:</strong> {github_status['message']}<br>
            <strong>πŸ“‚ Repo:</strong> family-profiles (private)
        </div>
        """, unsafe_allow_html=True)
        
        # Sync from GitHub button
        if st.button("πŸ”„ Sync from GitHub", use_container_width=True):
            if clone_github_repo():
                # Auto-index after successful sync
                if rag_system and rag_system.model:
                    with st.spinner("Auto-indexing synced documents..."):
                        if rag_system.index_documents("documents"):
                            st.success("βœ… Documents synced and indexed!")
                            st.rerun()
                        else:
                            st.warning("⚠️ Sync successful but indexing failed")
    else:
        color_map = {"red": "πŸ”΄", "orange": "🟠", "green": "🟒"}
        color_icon = color_map.get(github_status["color"], "πŸ”΄")
        
        st.markdown(f"""
        <div class="github-status">
            <strong>{color_icon} GitHub:</strong> {github_status['message']}<br>
            <strong>πŸ“‹ Setup:</strong> Add GITHUB_TOKEN to Hugging Face secrets
        </div>
        """, unsafe_allow_html=True)
    
    st.divider()
    
    # Document Management
    st.header("πŸ“‚ Document Management")
    
    if rag_system and rag_system.model:
        doc_count = rag_system.get_collection_count()
        
        if doc_count > 0:
            st.markdown(f"""
            <div class="document-status">
                <strong>πŸ“Š Documents Indexed:</strong> {doc_count} chunks<br>
                <strong>πŸ” Status:</strong> Ready for queries
            </div>
            """, unsafe_allow_html=True)
        else:
            st.warning("No documents indexed. Sync from GitHub or upload documents to get started.")
        
        # Document indexing
        if st.button("πŸ”„ Re-index Documents", use_container_width=True):
            with st.spinner("Indexing documents..."):
                if rag_system.index_documents("documents"):
                    st.success("Documents indexed successfully!")
                    st.rerun()
                else:
                    st.error("Failed to index documents. Check your documents folder.")
        
        # Show document count only (hidden)
        if os.path.exists("documents"):
            txt_files = [f for f in os.listdir("documents") if f.endswith('.txt')]
            if txt_files:
                st.info(f"πŸ“„ {len(txt_files)} documents loaded (hidden)")
        
        # Manual upload interface (fallback)
        st.subheader("πŸ“€ Manual Upload")
        uploaded_files = st.file_uploader(
            "Upload text files (fallback)",
            type=['txt'],
            accept_multiple_files=True,
            help="Upload .txt files if GitHub sync is not available"
        )
        
        if uploaded_files:
            if st.button("πŸ’Ύ Save & Index Files"):
                os.makedirs("documents", exist_ok=True)
                saved_files = []
                
                for uploaded_file in uploaded_files:
                    file_path = os.path.join("documents", uploaded_file.name)
                    with open(file_path, "wb") as f:
                        f.write(uploaded_file.getbuffer())
                    saved_files.append(uploaded_file.name)
                
                st.success(f"Saved {len(saved_files)} files!")
                
                # Auto-index
                with st.spinner("Auto-indexing new documents..."):
                    if rag_system.index_documents("documents"):
                        st.success("Documents indexed successfully!")
                        st.rerun()
    else:
        st.error("RAG system initialization failed. Check your setup.")
    
    st.divider()
    
    # Online Users
    st.header("πŸ‘₯ Online Users")
    online_count = update_online_users()
    
    if online_count == 1:
        st.success("🟒 Just you online")
    else:
        st.success(f"🟒 {online_count} people online")
    
    st.divider()
    
    # Settings
    st.header("βš™οΈ Settings")
    
    # API Status with better checking
    openrouter_key = os.environ.get("OPENROUTER_API_KEY")
    if openrouter_key:
        st.success(" βœ… API Connected")
        # Quick API test
        if st.button("Test API Connection", use_container_width=True):
            try:
                test_response = requests.post(
                    "https://openrouter.ai/api/v1/chat/completions",
                    headers={
                        "Authorization": f"Bearer {openrouter_key}",
                        "Content-Type": "application/json"
                    },
                    json={
                        "model": "openai/gpt-3.5-turbo",
                        "messages": [{"role": "user", "content": "test"}],
                        "max_tokens": 5
                    },
                    timeout=5
                )
                if test_response.status_code == 200:
                    st.success("βœ… API working correctly!")
                elif test_response.status_code == 402:
                    st.error("❌ Credits exhausted")
                elif test_response.status_code == 429:
                    st.warning("⏱️ Rate limited")
                else:
                    st.error(f"❌ API Error: {test_response.status_code}")
            except Exception as e:
                st.error(f"❌ API Test Failed: {str(e)}")
    else:
        st.error("❌ No OpenRouter API Key")
        st.info("Add OPENROUTER_API_KEY in Hugging Face Space settings β†’ Variables and secrets")
    
    # Enhanced Settings
    st.subheader("πŸš€ Token Settings")
    unlimited_tokens = st.checkbox("πŸ”₯ Unlimited Tokens Mode", value=True, help="Use higher token limits for detailed responses")
    use_ai_enhancement = st.checkbox("πŸ‘Ύ AI Enhancement", value=bool(openrouter_key), help="Enhance answers with AI when documents are found")
    
    st.subheader("πŸŽ›οΈ Display Settings")
    show_sources = st.checkbox("πŸ“ Show Sources", value=True)
    show_confidence = st.checkbox("🎯 Show Confidence Scores", value=True)
    
    # Token mode indicator
    if unlimited_tokens:
        st.success("πŸ”₯ Unlimited mode: Detailed responses enabled")
    else:
        st.info("πŸ’° Conservative mode: Limited tokens to save credits")
    
    st.divider()
    
    # Chat History Controls
    st.header("πŸ’Ύ Chat History")
    
    if st.session_state.messages:
        st.info(f"Messages: {len(st.session_state.messages)}")
    
    col1, col2 = st.columns(2)
    with col1:
        if st.button("πŸ’Ύ Save", use_container_width=True):
            save_chat_history(st.session_state.messages)
            st.success("Saved!")
    
    with col2:
        if st.button("πŸ—‘οΈ Clear", use_container_width=True):
            start_new_chat()
            st.success("Cleared!")
            st.rerun()

# ================= MAIN CHAT AREA =================

# Display chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        if message["role"] == "assistant" and "rag_info" in message:
            # Display AI answer
            st.markdown(message["content"])
            
            # Display RAG information
            rag_info = message["rag_info"]
            
            if show_sources and rag_info.get("sources"):
                confidence_text = f"{rag_info['confidence']*100:.1f}%" if show_confidence else ""
                st.markdown(f"""
                <div class="rag-attribution">
                    <strong>πŸ“ Sources:</strong> {', '.join(rag_info['sources'])}<br>
                    <strong>🎯 Confidence:</strong> {confidence_text}
                </div>
                """, unsafe_allow_html=True)
            
            # Show extracted answer if different
            if rag_info.get("extracted_answer") and rag_info["extracted_answer"] != message["content"]:
                st.markdown("**πŸ“„ Extracted Answer:**")
                st.markdown(f"_{rag_info['extracted_answer']}_")
        else:
            st.markdown(message["content"])

# Check if we need to process a personality question
if hasattr(st.session_state, 'process_personality_question'):
    prompt = st.session_state.process_personality_question
    del st.session_state.process_personality_question  # Clear the flag
    
    # Display user message
    with st.chat_message("user"):
        st.markdown(prompt)
    
    # Process the question using the same logic as chat input
    # Update user tracking
    update_online_users()
    
    # Get RAG response
    with st.chat_message("assistant"):
        if rag_system and rag_system.model and rag_system.get_collection_count() > 0:
            # Search documents first
            search_results = rag_system.search(prompt, n_results=5)
            
            # Debug output for troubleshooting
            if search_results:
                st.info(f"πŸ” Found {len(search_results)} potential matches. Best similarity: {search_results[0]['similarity']:.3f}")
            else:
                st.warning("πŸ” No search results returned from vector database")
            
            # Check if we found relevant documents (very low threshold)
            if search_results and search_results[0]['similarity'] > 0.001:  # Ultra-low threshold
                # Generate document-based answer
                result = rag_system.generate_answer(
                    prompt, 
                    search_results, 
                    use_ai_enhancement=use_ai_enhancement,
                    unlimited_tokens=unlimited_tokens
                )
                
                # Display AI answer or extracted answer
                if use_ai_enhancement and result['has_both']:
                    answer_text = result['ai_answer']
                    st.markdown(f"πŸ‘Ύ **AI Enhanced Answer:** {answer_text}")
                    
                    # Also show extracted answer for comparison if different
                    if result['extracted_answer'] != answer_text:
                        with st.expander("πŸ“„ View Extracted Answer"):
                            st.markdown(result['extracted_answer'])
                else:
                    answer_text = result['extracted_answer']
                    st.markdown(f"πŸ“„ **Document Answer:** {answer_text}")
                    
                    # Show why AI enhancement wasn't used
                    if use_ai_enhancement and not result['has_both']:
                        st.info("πŸ’‘ AI enhancement failed - showing extracted answer from documents")
                
                # Show RAG info with more details
                if show_sources and result['sources']:
                    confidence_text = f"{result['confidence']*100:.1f}%" if show_confidence else ""
                    st.markdown(f"""
                    <div class="rag-attribution">
                        <strong>πŸ“ Sources:</strong> {', '.join(result['sources'])}<br>
                        <strong>🎯 Confidence:</strong> {confidence_text}<br>
                        <strong>πŸ“Š Found:</strong> {len(search_results)} relevant sections<br>
                        <strong>πŸ” Best Match:</strong> {search_results[0]['similarity']:.3f} similarity
                    </div>
                    """, unsafe_allow_html=True)
                
                # Add to messages with RAG info
                assistant_message = {
                    "role": "assistant", 
                    "content": answer_text,
                    "rag_info": {
                        "sources": result['sources'],
                        "confidence": result['confidence'],
                        "extracted_answer": result['extracted_answer'],
                        "has_ai": result['has_both']
                    }
                }
                
            else:
                # No relevant documents found - show debug info
                if search_results:
                    st.warning(f"πŸ“„ Found documents but similarity too low (best: {search_results[0]['similarity']:.3f}). Using general AI...")
                else:
                    st.warning("πŸ“„ No documents found in search. Using general AI...")
                    
                general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
                st.markdown(f"πŸ’¬ **General AI:** {general_response}")
                
                assistant_message = {
                    "role": "assistant", 
                    "content": general_response,
                    "rag_info": {"sources": [], "confidence": 0, "mode": "general"}
                }
        
        else:
            # RAG system not ready - use general AI
            if rag_system and rag_system.get_collection_count() == 0:
                st.warning("No documents indexed. Sync from GitHub or upload documents first...")
            else:
                st.error("RAG system not ready. Using general AI mode...")
            
            general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
            st.markdown(f"πŸ’¬ **General AI:** {general_response}")
            
            assistant_message = {
                "role": "assistant", 
                "content": general_response,
                "rag_info": {"sources": [], "confidence": 0, "mode": "general"}
            }
    
    # Add assistant message to history
    st.session_state.messages.append(assistant_message)
    
    # Auto-save
    save_chat_history(st.session_state.messages)

# Chat input
if prompt := st.chat_input("Ask questions about your documents..."):
    # Update user tracking
    update_online_users()
    
    # Add user message
    user_message = {"role": "user", "content": prompt}
    st.session_state.messages.append(user_message)
    
    # Display user message
    with st.chat_message("user"):
        st.markdown(prompt)
    
    # Get RAG response
    with st.chat_message("assistant"):
        if rag_system and rag_system.model and rag_system.get_collection_count() > 0:
            # Search documents first
            search_results = rag_system.search(prompt, n_results=5)
            
            # Debug output for troubleshooting
            if search_results:
                st.info(f"πŸ” Found {len(search_results)} potential matches. Best similarity: {search_results[0]['similarity']:.3f}")
            else:
                st.warning("πŸ” No search results returned from vector database")
            
            # Check if we found relevant documents (very low threshold)
            if search_results and search_results[0]['similarity'] > 0.001:  # Ultra-low threshold
                # Generate document-based answer
                result = rag_system.generate_answer(
                    prompt, 
                    search_results, 
                    use_ai_enhancement=use_ai_enhancement,
                    unlimited_tokens=unlimited_tokens
                )
                
                # Display AI answer or extracted answer
                if use_ai_enhancement and result['has_both']:
                    answer_text = result['ai_answer']
                    st.markdown(f"πŸ‘Ύ **AI Enhanced Answer:** {answer_text}")
                    
                    # Also show extracted answer for comparison if different
                    if result['extracted_answer'] != answer_text:
                        with st.expander("πŸ“„ View Extracted Answer"):
                            st.markdown(result['extracted_answer'])
                else:
                    answer_text = result['extracted_answer']
                    st.markdown(f"πŸ“„ **Document Answer:** {answer_text}")
                    
                    # Show why AI enhancement wasn't used
                    if use_ai_enhancement and not result['has_both']:
                        st.info("πŸ’‘ AI enhancement failed - showing extracted answer from documents")
                
                # Show RAG info with more details
                if show_sources and result['sources']:
                    confidence_text = f"{result['confidence']*100:.1f}%" if show_confidence else ""
                    st.markdown(f"""
                    <div class="rag-attribution">
                        <strong>πŸ“ Sources:</strong> {', '.join(result['sources'])}<br>
                        <strong>🎯 Confidence:</strong> {confidence_text}<br>
                        <strong>πŸ“Š Found:</strong> {len(search_results)} relevant sections<br>
                        <strong>πŸ” Best Match:</strong> {search_results[0]['similarity']:.3f} similarity
                    </div>
                    """, unsafe_allow_html=True)
                
                # Add to messages with RAG info
                assistant_message = {
                    "role": "assistant", 
                    "content": answer_text,
                    "rag_info": {
                        "sources": result['sources'],
                        "confidence": result['confidence'],
                        "extracted_answer": result['extracted_answer'],
                        "has_ai": result['has_both']
                    }
                }
                
            else:
                # No relevant documents found - show debug info
                if search_results:
                    st.warning(f"πŸ“„ Found documents but similarity too low (best: {search_results[0]['similarity']:.3f}). Using general AI...")
                else:
                    st.warning("πŸ“„ No documents found in search. Using general AI...")
                    
                general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
                st.markdown(f"πŸ’¬ **General AI:** {general_response}")
                
                assistant_message = {
                    "role": "assistant", 
                    "content": general_response,
                    "rag_info": {"sources": [], "confidence": 0, "mode": "general"}
                }
        
        else:
            # RAG system not ready - use general AI
            if rag_system and rag_system.get_collection_count() == 0:
                st.warning("No documents indexed. Sync from GitHub or upload documents first...")
            else:
                st.error("RAG system not ready. Using general AI mode...")
            
            general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
            st.markdown(f"πŸ’¬ **General AI:** {general_response}")
            
            assistant_message = {
                "role": "assistant", 
                "content": general_response,
                "rag_info": {"sources": [], "confidence": 0, "mode": "general"}
            }
    
    # Add assistant message to history
    st.session_state.messages.append(assistant_message)
    
    # Auto-save
    save_chat_history(st.session_state.messages)

# Footer info
if rag_system and rag_system.model:
    doc_count = rag_system.get_collection_count()
    token_mode = "πŸ”₯ Unlimited" if unlimited_tokens else "πŸ’° Conservative"
    github_status = check_github_status()
    github_icon = "🟒" if github_status["status"] == "connected" else "πŸ”΄"
    theme_icon = "πŸŒ™" if st.session_state.dark_mode else "β˜€οΈ"
    st.caption(f"πŸ“š Knowledge Base: {doc_count} indexed chunks | πŸ” RAG System Active | {token_mode} Token Mode | {github_icon} GitHub {github_status['status'].title()} | {theme_icon} {theme_status}")