File size: 61,778 Bytes
34e5315
28808c0
 
 
 
 
b1a0ec6
28808c0
 
 
b1a0ec6
 
 
28808c0
 
 
 
 
34e5315
28808c0
 
 
 
34e5315
28808c0
 
 
b1a0ec6
 
28808c0
 
 
b1a0ec6
28808c0
34e5315
28808c0
b1a0ec6
34e5315
28808c0
d68daa5
 
 
28808c0
 
 
 
 
 
 
 
34e5315
28808c0
 
 
b1a0ec6
28808c0
 
 
 
 
14659c1
 
 
 
 
 
 
 
 
28808c0
 
b1a0ec6
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
 
 
b1a0ec6
28808c0
b1a0ec6
 
 
 
28808c0
 
 
14659c1
28808c0
 
 
 
 
 
 
 
 
 
 
b1a0ec6
28808c0
 
14659c1
28808c0
14659c1
 
b1a0ec6
 
 
 
 
 
 
14659c1
 
 
 
 
 
b1a0ec6
14659c1
b1a0ec6
14659c1
 
28808c0
b1a0ec6
 
 
 
 
 
14659c1
 
 
 
 
 
28808c0
14659c1
 
 
 
 
 
28808c0
 
 
 
 
 
 
 
b1a0ec6
28808c0
 
b1a0ec6
 
28808c0
 
 
 
 
 
14659c1
b1a0ec6
28808c0
b1a0ec6
28808c0
b1a0ec6
 
 
 
 
28808c0
 
 
b1a0ec6
14659c1
 
 
b1a0ec6
03da98c
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
ab836e9
 
b1a0ec6
 
ab836e9
2dc84ee
23000fe
14659c1
28808c0
 
c150284
 
 
 
83b0f7f
 
 
c9f89c9
 
 
c150284
 
 
 
a3dabad
c150284
 
a3dabad
c150284
 
 
 
 
 
 
a3dabad
83b0f7f
 
c150284
a3dabad
 
14659c1
b1a0ec6
23000fe
 
 
 
 
b1a0ec6
23000fe
 
 
0dc1245
23000fe
 
 
 
 
 
 
b1a0ec6
 
 
 
23000fe
b1a0ec6
23000fe
 
 
 
14659c1
 
b1a0ec6
14659c1
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23000fe
28808c0
23000fe
 
 
14659c1
23000fe
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c150284
b1a0ec6
 
 
 
 
 
 
 
23000fe
 
14659c1
28808c0
23000fe
14659c1
 
28808c0
b1a0ec6
ab836e9
28808c0
b1a0ec6
28808c0
14659c1
28808c0
14659c1
 
 
 
b1a0ec6
 
 
 
 
 
23000fe
 
 
28808c0
b1a0ec6
 
28808c0
 
 
b1a0ec6
28808c0
 
b1a0ec6
 
 
 
 
 
28808c0
b1a0ec6
 
28808c0
 
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23000fe
 
b1a0ec6
 
 
 
 
23000fe
b1a0ec6
 
14659c1
28808c0
84c93e1
28808c0
84c93e1
fc67415
84c93e1
fc67415
 
 
d68daa5
fc67415
 
 
 
 
 
 
 
d68daa5
fc67415
 
 
 
 
d68daa5
fc67415
 
 
 
d68daa5
84c93e1
 
 
 
 
 
fc67415
28808c0
fc67415
 
 
84c93e1
28808c0
b1a0ec6
 
14659c1
b1a0ec6
14659c1
28808c0
23000fe
 
 
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23000fe
 
 
14659c1
 
b1a0ec6
28808c0
b1a0ec6
 
 
 
 
 
23000fe
28808c0
b1a0ec6
28808c0
b1a0ec6
 
 
28808c0
 
b1a0ec6
28808c0
 
 
 
14659c1
b1a0ec6
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
28808c0
14659c1
 
28808c0
 
b1a0ec6
14659c1
 
 
28808c0
 
b1a0ec6
 
 
28808c0
 
 
b1a0ec6
0f5217f
28808c0
0f5217f
28808c0
 
 
b1a0ec6
 
28808c0
 
b1a0ec6
 
 
 
28808c0
 
 
 
 
b1a0ec6
 
28808c0
 
b1a0ec6
14659c1
 
 
 
b1a0ec6
 
 
14659c1
28808c0
b1a0ec6
 
28808c0
b1a0ec6
28808c0
 
 
b1a0ec6
28808c0
b1a0ec6
 
 
 
28808c0
b1a0ec6
 
 
14659c1
b1a0ec6
 
 
 
14659c1
 
 
b1a0ec6
 
28808c0
b1a0ec6
 
 
28808c0
14659c1
28808c0
b1a0ec6
28808c0
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
14659c1
 
b1a0ec6
 
 
 
 
 
14659c1
b1a0ec6
 
 
 
 
 
 
 
28808c0
 
b1a0ec6
 
 
28808c0
b1a0ec6
 
 
 
 
 
28808c0
b1a0ec6
28808c0
 
b1a0ec6
 
14659c1
 
b1a0ec6
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
53082ed
 
 
 
67b8dd4
f21caca
 
2abe62d
f21caca
 
 
 
 
 
2abe62d
f21caca
 
 
2abe62d
f21caca
 
 
 
 
 
 
 
 
2abe62d
f21caca
 
 
 
 
 
 
 
 
2abe62d
f21caca
 
 
53082ed
2abe62d
53082ed
 
 
 
 
 
 
 
 
 
 
 
28808c0
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14659c1
 
b1a0ec6
14659c1
28808c0
 
b1a0ec6
 
 
 
 
 
 
 
 
14659c1
28808c0
b1a0ec6
14659c1
 
b1a0ec6
 
14659c1
 
b1a0ec6
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
 
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
 
 
 
 
b1a0ec6
 
28808c0
b1a0ec6
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
28808c0
 
 
b1a0ec6
14659c1
b1a0ec6
 
 
 
 
 
28808c0
 
b1a0ec6
 
28808c0
b1a0ec6
14659c1
28808c0
b1a0ec6
 
 
 
 
 
 
28808c0
 
14659c1
b1a0ec6
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
28808c0
b1a0ec6
28808c0
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
28808c0
da277cd
b1a0ec6
 
 
 
28808c0
da277cd
 
b1a0ec6
da277cd
b1a0ec6
ae6c5d5
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae6c5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
1132e0a
b1a0ec6
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436fe29
 
 
 
 
 
 
 
 
 
 
 
 
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1132e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1a0ec6
1132e0a
 
 
 
 
 
 
b1a0ec6
28808c0
b1a0ec6
28808c0
b1a0ec6
 
28808c0
b1a0ec6
28808c0
b1a0ec6
28808c0
b1a0ec6
 
 
 
 
 
 
28808c0
b1a0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28808c0
029feef
 
1132e0a
 
 
436fe29
1132e0a
 
 
 
436fe29
1132e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436fe29
 
1132e0a
 
 
 
 
 
 
 
 
 
029feef
 
1132e0a
 
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
import os
import sys
import json
import logging
import warnings
from pathlib import Path
from typing import List, Dict, Any, Optional, Tuple
import hashlib
import pickle
from datetime import datetime
import time
import asyncio
from concurrent.futures import ThreadPoolExecutor

# Suppress warnings for cleaner output
warnings.filterwarnings("ignore")

# Core dependencies
import gradio as gr
import numpy as np
import pandas as pd
from sentence_transformers import SentenceTransformer
import faiss
import torch
from transformers import (
    AutoTokenizer, 
    AutoModelForCausalLM, 
    BitsAndBytesConfig,
    pipeline
)

# Document processing
from llama_index.core import Document, VectorStoreIndex, Settings
from llama_index.core.node_parser import SentenceSplitter
from llama_index.vector_stores.faiss import FaissVectorStore
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import StorageContext

# PDF processing
from unstructured.partition.pdf import partition_pdf
from llama_index.core.schema import Document as LlamaDocument


# Medical knowledge validation
import re

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class MedicalFactChecker:
    """Enhanced medical fact checker with faster validation"""
    
    def __init__(self):
        self.medical_facts = self._load_medical_facts()
        self.contraindications = self._load_contraindications()
        self.dosage_patterns = self._compile_dosage_patterns()
        self.definitive_patterns = [
            re.compile(r, re.IGNORECASE) for r in [
                r'always\s+(?:use|take|apply)',
                r'never\s+(?:use|take|apply)',
                r'will\s+(?:cure|heal|fix)',
                r'guaranteed\s+to',
                r'completely\s+(?:safe|effective)'
            ]
        ]
        
    def _load_medical_facts(self) -> Dict[str, Any]:
        """Pre-loaded medical facts for Gaza context"""
        return {
            "burn_treatment": {
                "cool_water": "Use clean, cool (not ice-cold) water for 10-20 minutes",
                "no_ice": "Never apply ice directly to burns",
                "clean_cloth": "Cover with clean, dry cloth if available"
            },
            "wound_care": {
                "pressure": "Apply direct pressure to control bleeding",
                "elevation": "Elevate injured limb if possible",
                "clean_hands": "Clean hands before treating wounds when possible"
            },
            "infection_signs": {
                "redness": "Increasing redness around wound",
                "warmth": "Increased warmth at wound site",
                "pus": "Yellow or green discharge",
                "fever": "Fever may indicate systemic infection"
            }
        }
    
    def _load_contraindications(self) -> Dict[str, List[str]]:
        """Pre-loaded contraindications for common treatments"""
        return {
            "aspirin": ["children under 16", "bleeding disorders", "stomach ulcers"],
            "ibuprofen": ["kidney disease", "heart failure", "stomach bleeding"],
            "hydrogen_peroxide": ["deep wounds", "closed wounds", "eyes"],
            "tourniquets": ["non-life-threatening bleeding", "without proper training"]
        }
    
    def _compile_dosage_patterns(self) -> List[re.Pattern]:
        """Pre-compiled dosage patterns"""
        patterns = [
            r'\d+\s*mg\b',  # milligrams
            r'\d+\s*g\b',   # grams
            r'\d+\s*ml\b',  # milliliters
            r'\d+\s*tablets?\b',  # tablets
            r'\d+\s*times?\s+(?:per\s+)?day\b',  # frequency
            r'every\s+\d+\s+hours?\b'  # intervals
        ]
        return [re.compile(pattern, re.IGNORECASE) for pattern in patterns]
    
    def check_medical_accuracy(self, response: str, context: str) -> Dict[str, Any]:
        """Enhanced medical accuracy check with Gaza-specific considerations"""
        issues = []
        warnings = []
        accuracy_score = 0.0
        
        # Check for contraindications (faster keyword matching)
        response_lower = response.lower()
        for medication, contra_list in self.contraindications.items():
            if medication in response_lower:
                for contra in contra_list:
                    if any(word in response_lower for word in contra.split()):
                        issues.append(f"Potential contraindication: {medication} with {contra}")
                        accuracy_score -= 0.3
                        break
        
        # Context alignment using Jaccard similarity
        if context:
            resp_words = set(response_lower.split())
            ctx_words = set(context.lower().split())
            context_similarity = len(resp_words & ctx_words) / len(resp_words | ctx_words) if ctx_words else 0.0
            if context_similarity < 0.5:  # Lowered threshold for Gaza context
                warnings.append(f"Low context similarity: {context_similarity:.2f}")
                accuracy_score -= 0.1
        else:
            context_similarity = 0.0
        
        # Gaza-specific resource checks
        gaza_resources = ["clean water", "sterile", "hospital", "ambulance", "electricity"]
        if any(resource in response_lower for resource in gaza_resources):
            warnings.append("Consider resource limitations in Gaza context")
            accuracy_score -= 0.05
        
        # Unsupported claims check
        for pattern in self.definitive_patterns:
            if pattern.search(response):
                issues.append(f"Unsupported definitive claim detected")
                accuracy_score -= 0.4
                break
        
        # Dosage validation
        for pattern in self.dosage_patterns:
            if pattern.search(response):
                warnings.append("Dosage detected - verify with professional")
                accuracy_score -= 0.1
                break
        
        confidence_score = max(0.0, min(1.0, 0.8 + accuracy_score))
        
        return {
            "confidence_score": confidence_score,
            "issues": issues,
            "warnings": warnings,
            "context_similarity": context_similarity,
            "is_safe": len(issues) == 0 and confidence_score > 0.5
        }

class EnhancedGazaKnowledgeBase:
    """Enhanced knowledge base with better embeddings and indexing"""
    
    def __init__(self, data_dir: str = "./data"):
        self.data_dir = Path(data_dir)
        self.embedding_model = None
        self.vector_store = None
        self.index = None
        self.chunk_metadata = []
        self.index_path = self.data_dir / "enhanced_vector_store"
        
        # Enhanced medical priorities for Gaza context
        self.medical_priorities = {
            "trauma": ["gunshot", "blast", "burns?", "fracture", "shrapnel", "explosion"],
            "infectious": ["cholera", "dysentery", "infection", "sepsis", "wound infection"],
            "chronic": ["diabetes", "hypertension", "malnutrition", "kidney", "heart"],
            "emergency": ["cardiac", "bleeding", "airway", "unconscious", "shock"],
            "gaza_specific": ["siege", "blockade", "limited supplies", "no electricity", "water shortage"]
        }
        
    def initialize(self):
        """Enhanced initialization with better embedding model"""
        if not self.index_path.exists():
            self.index_path.mkdir(parents=True)
            
        # Use a more powerful medical embedding model
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
        
        # Try to use a medical-specific embedding model, fallback to general model
        try:
            # First try a medical-specific model (if available)
            self.embedding_model = HuggingFaceEmbedding(
                model_name="sentence-transformers/all-mpnet-base-v2",  # Higher dimension (768)
                device=device,
                embed_batch_size=4
            )
            logger.info("Using all-mpnet-base-v2 (768-dim) embedding model")
        except Exception as e:
            logger.warning(f"Failed to load preferred model, using fallback: {e}")
            self.embedding_model = HuggingFaceEmbedding(
                model_name="sentence-transformers/all-MiniLM-L6-v2",
                device=device,
                embed_batch_size=4
            )
            logger.info("Using all-MiniLM-L6-v2 (384-dim) embedding model")
        
        # Configure global settings
        Settings.embed_model = self.embedding_model
        Settings.chunk_size = 512  # Increased chunk size for better context
        Settings.chunk_overlap = 50  # Increased overlap
        
        # Check for existing index
        if (self.index_path / "index.faiss").exists() and (self.index_path / "docstore.json").exists():
            self._load_vector_store()
        else:
            self._create_vector_store()



    def _batch_embed_with_retry(self, texts, batch_size=16, max_retries=3, delay=2):
        """
        Embed texts in batches with retry fallback and logging
        """
        embeddings = []
        for i in range(0, len(texts), batch_size):
            batch = texts[i:i+batch_size]
        for attempt in range(max_retries):
            try:
                batch_embeddings = self.embedding_model.get_text_embedding_batch(batch)
                embeddings.extend(batch_embeddings)
                break  # success
            except Exception as e:
                if attempt < max_retries - 1:
                    logger.warning(f"Batch {i}-{i+len(batch)} failed (attempt {attempt+1}): {e}. Retrying...")
                    time.sleep(delay * (attempt + 1))
                else:
                    logger.error(f"❌ Final failure embedding batch {i}-{i+len(batch)}: {e}")
                    for text in batch:
                        try:
                            embeddings.append(self.embedding_model.get_text_embedding(text))
                        except Exception as sub_e:
                            logger.error(f"Failed to embed single text: {sub_e} β€” {text[:60]}...")
        return embeddings



    
    def _load_vector_store(self):
        """Load existing vector store with error handling"""
        try:
            # Load the FAISS index directly
            faiss_index = faiss.read_index(str(self.index_path / "index.faiss"))
            vector_store = FaissVectorStore(faiss_index=faiss_index)
            
            # Create storage context
            storage_context = StorageContext.from_defaults(
                vector_store=vector_store,
                persist_dir=str(self.index_path)
            )
            
            # Load the index
            self.index = VectorStoreIndex.load(
                storage_context=storage_context
            )
            
            # Load metadata
            metadata_path = self.index_path / "metadata.pkl"
            if metadata_path.exists():
                with open(metadata_path, 'rb') as f:
                    self.chunk_metadata = pickle.load(f)
            
            logger.info(f"Loaded existing vector store with {len(self.chunk_metadata)} chunks")
        except Exception as e:
            logger.error(f"Error loading vector store: {e}")
            # Fallback to creating new store if loading fails
            self._create_vector_store()
    
    def _create_vector_store(self):
        """Create enhanced vector store with IVF indexing"""
        documents = self._load_documents()
        
        if not documents:
            logger.warning("No documents found. Creating empty index")
            self.chunk_metadata = []
            return
        
        # Determine embedding dimension
        try:
            test_embedding = self.embedding_model.get_text_embedding("test")
            dimension = len(test_embedding)
            logger.info(f"Embedding dimension: {dimension}")
        except Exception as e:
            logger.error(f"Failed to determine embedding dimension: {e}")
            dimension = 768  # Default for all-mpnet-base-v2
        
        # Create enhanced FAISS index with IVF for better performance
        try:
            # For small datasets, use flat index; for larger ones, use IVF
            if len(documents) < 1000:
                faiss_index = faiss.IndexFlatL2(dimension)
                logger.info("Using IndexFlatL2 for small dataset")
            else:
                # Use IVF with reasonable number of clusters
                nlist = min(100, len(documents) // 10)  # Adaptive cluster count
                quantizer = faiss.IndexFlatL2(dimension)
                faiss_index = faiss.IndexIVFFlat(quantizer, dimension, nlist)
                logger.info(f"Using IndexIVFFlat with {nlist} clusters")
        except Exception as e:
            logger.error(f"Failed to create enhanced index, using flat: {e}")
            faiss_index = faiss.IndexFlatL2(dimension)
        
        vector_store = FaissVectorStore(faiss_index=faiss_index)
        
        # Create storage context
        storage_context = StorageContext.from_defaults(
            vector_store=vector_store
        )
        
        # Configure node parser with enhanced settings
        parser = SentenceSplitter(
            chunk_size=Settings.chunk_size,
            chunk_overlap=Settings.chunk_overlap,
            include_prev_next_rel=True  # Include relationships for better context
        )
        
        # Create index using global settings
        self.index = VectorStoreIndex.from_documents(
            documents,
            storage_context=storage_context,
            transformations=[parser],
            show_progress=True
        )
        
        # Train IVF index if needed
        if hasattr(faiss_index, 'train') and not faiss_index.is_trained:
            logger.info("Training IVF index...")
            # Get some embeddings for training
            sample_texts = [doc.text[:500] for doc in documents[:100]]  # Sample for training
            sample_embeddings = np.array(self._batch_embed_with_retry(sample_texts, batch_size=16)).astype('float32')
            faiss_index.train(sample_embeddings)
            logger.info("IVF index training completed")
        
        # Save metadata
        self.chunk_metadata = [
            {"text": node.text, "source": node.metadata.get("source", "unknown")}
            for node in self.index.docstore.docs.values()
        ]
        
        # Persist the index
        self.index.storage_context.persist(persist_dir=str(self.index_path))
        
        # Save metadata separately
        with open(self.index_path / "metadata.pkl", 'wb') as f:
            pickle.dump(self.chunk_metadata, f)
        
        logger.info(f"Created enhanced vector store with {len(self.chunk_metadata)} chunks")
        
    def _load_documents(self) -> List[Document]:
        """Enhanced document loading with better caching"""
        documents = []
        doc_cache = self.index_path / "document_cache.pkl"
        
        # Try loading from cache
        if doc_cache.exists():
            try:
                with open(doc_cache, 'rb') as f:
                    cached_data = pickle.load(f)
                    if isinstance(cached_data, dict) and 'documents' in cached_data:
                        cached_docs = cached_data['documents']
                        if isinstance(cached_docs, list) and all(isinstance(d, Document) for d in cached_docs):
                            logger.info(f"Loaded {len(cached_docs)} documents from cache")
                            return cached_docs
                    logger.warning("Document cache format invalid")
            except Exception as e:
                logger.warning(f"Document cache corrupted: {e}")
        
        # Process files with enhanced error handling
        processed_files = []
        for pdf_file in self.data_dir.glob("*.pdf"):
            try:
                doc_text = self._extract_pdf_text(pdf_file)
                if doc_text and len(doc_text.strip()) > 100:  # Minimum content check
                    documents.append(Document(
                        text=doc_text,
                        metadata={
                            "source": str(pdf_file.name), 
                            "type": "pdf",
                            "file_size": pdf_file.stat().st_size,
                            "processed_date": datetime.now().isoformat()
                        }
                    ))
                    processed_files.append(str(pdf_file.name))
                    logger.info(f"Processed: {pdf_file.name} ({len(doc_text)} chars)")
            except Exception as e:
                logger.error(f"Error loading {pdf_file}: {e}")
        
        # Process text files as well
        for txt_file in self.data_dir.glob("*.txt"):
            try:
                with open(txt_file, 'r', encoding='utf-8') as f:
                    doc_text = f.read()
                    if doc_text and len(doc_text.strip()) > 100:
                        documents.append(Document(
                            text=doc_text,
                            metadata={
                                "source": str(txt_file.name), 
                                "type": "txt",
                                "file_size": txt_file.stat().st_size,
                                "processed_date": datetime.now().isoformat()
                            }
                        ))
                        processed_files.append(str(txt_file.name))
                        logger.info(f"Processed: {txt_file.name} ({len(doc_text)} chars)")
            except Exception as e:
                logger.error(f"Error loading {txt_file}: {e}")
        
        # Save to cache if we found documents
        if documents:
            cache_data = {
                'documents': documents,
                'processed_files': processed_files,
                'cache_date': datetime.now().isoformat()
            }
            with open(doc_cache, 'wb') as f:
                pickle.dump(cache_data, f)
            logger.info(f"Cached {len(documents)} documents")
            
        return documents

    def _extract_pdf_text(self, pdf_path: Path) -> str:
        """Use unstructured to extract and chunk PDF text by title, and save as .txt"""
        try:
            elements = partition_pdf(filename=str(pdf_path), strategy="auto")
            if not elements:
                logger.warning(f"No elements extracted from {pdf_path}")
                return ""

            # Group by title (section-aware)
            grouped = {}
            current_title = "Untitled Section"
            for el in elements:
                if el.category == "Title" and el.text.strip():
                    current_title = el.text.strip()
                elif el.text.strip():
                    grouped.setdefault(current_title, []).append(el.text.strip())

            # Recombine into logical chunks
            sections = []
            for title, paras in grouped.items():
                section_text = f"{title}\n" + "\n".join(paras)
                sections.append(section_text.strip())

            full_text = "\n\n".join(sections)
            if len(full_text.strip()) < 100:
                logger.warning(f"Extracted text too short from {pdf_path}")
                return ""

            # Save extracted output to .txt next to original PDF
            txt_output = pdf_path.with_suffix(".extracted.txt")
            with open(txt_output, "w", encoding="utf-8") as f:
                f.write(full_text)
            logger.info(f"Saved extracted text to {txt_output.name}")

            return full_text
        except Exception as e:
            logger.error(f"Unstructured PDF parse failed for {pdf_path}: {e}")
            return ""


    
    def search(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
        """Enhanced search with better error handling and result processing"""
        if not self.index:
            logger.warning("Index not available for search")
            return []
            
        try:
            retriever = self.index.as_retriever(similarity_top_k=k)
            results = retriever.retrieve(query)
            
            # FIX: Handle the tuple object error by properly extracting node and score
            processed_results = []
            for result in results:
                try:
                    # Handle both tuple and direct node results
                    if isinstance(result, tuple):
                        node, score = result
                    else:
                        node = result
                        score = getattr(result, 'score', 0.0)
                    
                    # Extract text safely
                    text = getattr(node, 'text', str(node))
                    source = node.metadata.get("source", "unknown") if hasattr(node, 'metadata') else "unknown"
                    
                    processed_results.append({
                        "text": text,
                        "source": source,
                        "score": float(score) if score is not None else 0.0,
                        "medical_priority": self._assess_priority(text)
                    })
                except Exception as e:
                    logger.error(f"Error processing search result: {e}")
                    continue
            
            # Sort by score (higher is better)
            processed_results.sort(key=lambda x: x['score'], reverse=True)
            
            logger.info(f"Search returned {len(processed_results)} results for query: {query[:50]}...")
            return processed_results
            
        except Exception as e:
            logger.error(f"Error during search: {e}")
            return []
    
    def _assess_priority(self, text: str) -> str:
        """Enhanced medical priority assessment"""
        text_lower = text.lower()
        
        # Check priorities in order of importance
        priority_order = ["emergency", "trauma", "gaza_specific", "infectious", "chronic"]
        
        for priority in priority_order:
            keywords = self.medical_priorities.get(priority, [])
            if any(re.search(keyword, text_lower) for keyword in keywords):
                return priority
        
        return "general"

class EnhancedGazaRAGSystem:
    """Enhanced RAG system with better performance and error handling"""
    
    def __init__(self):
        self.knowledge_base = EnhancedGazaKnowledgeBase()
        self.fact_checker = MedicalFactChecker()
        self.llm = None
        self.tokenizer = None
        self.system_prompt = self._create_system_prompt()
        self.generation_pipeline = None
        self.response_cache = {}  # Simple response caching
        self.executor = ThreadPoolExecutor(max_workers=2)  # For async processing
        
    def initialize(self):
        """Enhanced initialization with better error handling"""
        logger.info("Initializing Enhanced Gaza RAG System...")
        
        try:
            self.knowledge_base.initialize()
            logger.info("Knowledge base initialized successfully")
        except Exception as e:
            logger.error(f"Failed to initialize knowledge base: {e}")
            raise
        
        # Lazy LLM loading - will load on first request
        logger.info("RAG system ready (LLM will load on first request)")
    
    def _initialize_llm(self):
        """Enhanced LLM initialization with better error handling"""
        if self.llm is not None:
            return
            
        model_name = "microsoft/Phi-3-mini-4k-instruct"
        try:
            logger.info(f"Loading LLM: {model_name}")
            
            # Enhanced quantization configuration
            quantization_config = BitsAndBytesConfig(
                load_in_4bit=True,
                bnb_4bit_use_double_quant=True,
                bnb_4bit_quant_type="nf4",
                bnb_4bit_compute_dtype=torch.float16,
            )

            
            self.tokenizer = AutoTokenizer.from_pretrained(
                model_name,
                trust_remote_code=True,
                padding_side="left"  # Better for generation
            )
            
            # Add pad token if missing
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
            
            self.llm = AutoModelForCausalLM.from_pretrained(
                model_name,
                quantization_config=quantization_config,
                device_map="auto",
                trust_remote_code=True,
                torch_dtype=torch.float16,
                low_cpu_mem_usage=True
            )
            
            # Create enhanced pipeline
            self.generation_pipeline = pipeline(
                "text-generation",
                model=self.llm,
                tokenizer=self.tokenizer,
                device_map="auto",
                torch_dtype=torch.float16,
                return_full_text=False  # Only return generated text
            )
            
            logger.info("LLM loaded successfully")
            
        except Exception as e:
            logger.error(f"Error loading primary model: {e}")
            self._initialize_fallback_llm()
    
    def _initialize_fallback_llm(self):
        """Enhanced fallback model with better error handling"""
        try:
            logger.info("Loading fallback model...")
            
            fallback_model = "microsoft/DialoGPT-small"
            self.tokenizer = AutoTokenizer.from_pretrained(fallback_model)
            self.llm = AutoModelForCausalLM.from_pretrained(
                fallback_model,
                torch_dtype=torch.float32,
                low_cpu_mem_usage=True
            )
            
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
            
            self.generation_pipeline = pipeline(
                "text-generation",
                model=self.llm,
                tokenizer=self.tokenizer,
                return_full_text=False
            )
            
            logger.info("Fallback model loaded successfully")
            
        except Exception as e:
            logger.error(f"Fallback model failed: {e}")
            self.llm = None
            self.generation_pipeline = None
    
    def _create_system_prompt(self) -> str:
        """Enhanced system prompt for Gaza context"""
        return """You are a medical AI assistant specifically designed for Gaza healthcare workers operating under siege conditions. 

CRITICAL GUIDELINES:
- Provide practical first aid guidance considering limited resources (water, electricity, medical supplies)
- Always prioritize patient safety and recommend professional medical help when available
- Consider Gaza's specific challenges: blockade, limited hospitals, frequent power outages
- Suggest alternative treatments when standard medical supplies are unavailable
- Never provide definitive diagnoses - only supportive care guidance
- Be culturally sensitive and aware of the humanitarian crisis context

RESOURCE CONSTRAINTS TO CONSIDER:
- Limited clean water availability
- Frequent electricity outages
- Restricted medical supply access
- Overwhelmed healthcare facilities
- Limited transportation for medical emergencies

Provide clear, actionable advice while emphasizing the need for professional medical care when possible."""
    
    async def generate_response_async(self, query: str, progress_callback=None) -> Dict[str, Any]:
        """Async response generation with progress tracking"""
        start_time = time.time()
        
        if progress_callback:
            progress_callback(0.1, "Checking cache...")
        
        # Check cache first
        query_hash = hashlib.md5(query.encode()).hexdigest()
        if query_hash in self.response_cache:
            cached_response = self.response_cache[query_hash]
            cached_response["cached"] = True
            cached_response["response_time"] = 0.1
            if progress_callback:
                progress_callback(1.0, "Retrieved from cache!")
            return cached_response
        
        try:
            if progress_callback:
                progress_callback(0.2, "Initializing LLM...")
            
            # Initialize LLM only when needed
            if self.llm is None:
                await asyncio.get_event_loop().run_in_executor(
                    self.executor, self._initialize_llm
                )
            
            if progress_callback:
                progress_callback(0.4, "Searching knowledge base...")
                
            # Enhanced knowledge retrieval
            search_results = await asyncio.get_event_loop().run_in_executor(
                self.executor, self.knowledge_base.search, query, 3
            )
            
            if progress_callback:
                progress_callback(0.6, "Preparing context...")
            
            context = self._prepare_context(search_results)
            
            if progress_callback:
                progress_callback(0.8, "Generating response...")
            
            # Generate response
            response = await asyncio.get_event_loop().run_in_executor(
                self.executor, self._generate_response, query, context
            )
            
            if progress_callback:
                progress_callback(0.9, "Validating safety...")
            
            # Enhanced safety check
            safety_check = self.fact_checker.check_medical_accuracy(response, context)
            
            # Prepare final response
            final_response = self._prepare_final_response(
                response, 
                search_results, 
                safety_check,
                time.time() - start_time
            )
            
            # Cache the response (limit cache size)
            if len(self.response_cache) < 100:
                self.response_cache[query_hash] = final_response
            
            if progress_callback:
                progress_callback(1.0, "Complete!")
            
            return final_response
            
        except Exception as e:
            logger.error(f"Error generating response: {e}")
            if progress_callback:
                progress_callback(1.0, f"Error: {str(e)}")
            return self._create_error_response(str(e))
    
    
    
    
    def _generate_response(self, query: str, context: str) -> str:
        """Enhanced response generation using model.generate() to avoid DynamicCache errors"""
        if self.llm is None or self.tokenizer is None:
            return self._generate_fallback_response(query, context)

        # Build prompt with Gaza-specific context
        prompt = f"""{self.system_prompt}
        MEDICAL KNOWLEDGE CONTEXT:
        {context}
        PATIENT QUESTION: {query}
        RESPONSE (provide practical, Gaza-appropriate medical guidance):"""

        try:
            # Tokenize and move to correct device
            inputs = self.tokenizer(prompt, return_tensors="pt").to(self.llm.device)

            # Generate the response
            outputs = self.llm.generate(
                **inputs,
                max_new_tokens=800,
                temperature=0.5,
                pad_token_id=self.tokenizer.eos_token_id,
                do_sample=True,
                repetition_penalty=1.15,
            )

            # Decode and clean up
            response_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            lines = response_text.split('\n')
            unique_lines = []
            for line in lines:
                line = line.strip()
                if line and line not in unique_lines:
                    unique_lines.append(line)
            return '\n'.join(unique_lines)

        except Exception as e:
            logger.error(f"Error in LLM generate(): {e}")
            return self._generate_fallback_response(query, context)


        # Decode and clean up
        response_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
        lines = response_text.split('\n')
        unique_lines = []
        for line in lines:
            line = line.strip()
            if line and line not in unique_lines:
                unique_lines.append(line)
        return '\n'.join(unique_lines)


    
    
    def _prepare_context(self, search_results: List[Dict[str, Any]]) -> str:
        """Enhanced context preparation with better formatting"""
        if not search_results:
            return "No specific medical guidance found in knowledge base. Provide general first aid principles."
        
        context_parts = []
        for i, result in enumerate(search_results, 1):
            source = result.get('source', 'unknown')
            text = result.get('text', '')
            priority = result.get('medical_priority', 'general')
            
            # Truncate long text but preserve important information
            if len(text) > 400:
                text = text[:400] + "..."
            
            context_parts.append(f"[Source {i}: {source} - Priority: {priority}]\n{text}")
        
        return "\n\n".join(context_parts)
    
    def _generate_response(self, query: str, context: str) -> str:
        """Enhanced response generation with better prompting"""
        if not self.generation_pipeline:
            return self._generate_fallback_response(query, context)
        
        # Enhanced prompt structure
        prompt = f"""{self.system_prompt}

MEDICAL KNOWLEDGE CONTEXT:
{context}

PATIENT QUESTION: {query}

RESPONSE (provide practical, Gaza-appropriate medical guidance):"""
        
        try:
            # Enhanced generation parameters
            response = self.generation_pipeline(
                prompt,
                max_new_tokens=300,  # Increased for more detailed responses
                temperature=0.2,     # Lower for more consistent medical advice
                do_sample=True,
                pad_token_id=self.tokenizer.eos_token_id,
                repetition_penalty=1.15,
                truncation=True,
                num_return_sequences=1
            )
            
            if response and len(response) > 0:
                generated_text = response[0]['generated_text']
                # Clean up the response
                generated_text = generated_text.strip()
                
                # Remove any repetitive patterns
                lines = generated_text.split('\n')
                unique_lines = []
                for line in lines:
                    if line.strip() and line.strip() not in unique_lines:
                        unique_lines.append(line.strip())
                
                return '\n'.join(unique_lines)
            else:
                return self._generate_fallback_response(query, context)
                
        except Exception as e:
            logger.error(f"Error in LLM generation: {e}")
            return self._generate_fallback_response(query, context)
    
    def _generate_fallback_response(self, query: str, context: str) -> str:
        """Enhanced fallback response with Gaza-specific guidance"""
        gaza_guidance = {
            "burn": "For burns: Use clean, cool water if available. If water is scarce, use clean cloth. Avoid ice. Seek medical help urgently.",
            "bleeding": "For bleeding: Apply direct pressure with clean cloth. Elevate if possible. If severe, seek immediate medical attention.",
            "wound": "For wounds: Clean hands if possible. Apply pressure to stop bleeding. Cover with clean material. Watch for infection signs.",
            "infection": "Signs of infection: Redness, warmth, swelling, pus, fever. Seek medical care immediately if available.",
            "pain": "For pain management: Rest, elevation, cold/warm compress as appropriate. Avoid aspirin in children."
        }
        
        query_lower = query.lower()
        for condition, guidance in gaza_guidance.items():
            if condition in query_lower:
                return f"{guidance}\n\nContext from medical sources:\n{context[:200]}..."
        
        return f"Medical guidance for: {query}\n\nGeneral advice: Prioritize safety, seek professional help when available, consider resource limitations in Gaza.\n\nRelevant information:\n{context[:300]}..."
    
    def _prepare_final_response(
        self, 
        response: str, 
        search_results: List[Dict[str, Any]], 
        safety_check: Dict[str, Any],
        response_time: float
    ) -> Dict[str, Any]:
        """Enhanced final response preparation with more metadata"""
        
        # Add safety warnings if needed
        if not safety_check["is_safe"]:
            response = f"⚠️ MEDICAL CAUTION: {response}\n\n🚨 Please verify this guidance with a medical professional when possible."
        
        # Add Gaza-specific disclaimer
        response += "\n\nπŸ“ Gaza Context: This guidance considers resource limitations. Adapt based on available supplies and seek professional medical care when accessible."
        
        # Extract unique sources
        sources = list(set(res.get("source", "unknown") for res in search_results)) if search_results else []
        
        # Calculate confidence based on multiple factors
        base_confidence = safety_check.get("confidence_score", 0.5)
        context_bonus = 0.1 if search_results else 0.0
        safety_penalty = 0.2 if not safety_check.get("is_safe", True) else 0.0
        
        final_confidence = max(0.0, min(1.0, base_confidence + context_bonus - safety_penalty))
        
        return {
            "response": response,
            "confidence": final_confidence,
            "sources": sources,
            "search_results_count": len(search_results),
            "safety_issues": safety_check.get("issues", []),
            "safety_warnings": safety_check.get("warnings", []),
            "response_time": round(response_time, 2),
            "timestamp": datetime.now().isoformat()[:19],
            "cached": False
        }
    
    def _create_error_response(self, error_msg: str) -> Dict[str, Any]:
        """Enhanced error response with helpful information"""
        return {
            "response": f"⚠️ System Error: Unable to process your medical query at this time.\n\nError: {error_msg}\n\n🚨 For immediate medical emergencies, seek professional help directly.\n\nπŸ“ž Gaza Emergency Numbers:\n- Palestinian Red Crescent: 101\n- Civil Defense: 102",
            "confidence": 0.0,
            "sources": [],
            "search_results_count": 0,
            "safety_issues": ["System error occurred"],
            "safety_warnings": ["Unable to validate medical accuracy"],
            "response_time": 0.0,
            "timestamp": datetime.now().isoformat()[:19],
            "cached": False,
            "error": True
        }

# Global system instance
enhanced_rag_system = None

def initialize_enhanced_system():
    """Initialize enhanced system with better error handling"""
    global enhanced_rag_system
    if enhanced_rag_system is None:
        try:
            enhanced_rag_system = EnhancedGazaRAGSystem()
            enhanced_rag_system.initialize()
            logger.info("Enhanced Gaza RAG System initialized successfully")
        except Exception as e:
            logger.error(f"Failed to initialize enhanced system: {e}")
            raise
    return enhanced_rag_system

def process_medical_query_with_progress(query: str, progress=gr.Progress()) -> Tuple[str, str, str]:
    """Enhanced query processing with detailed progress tracking and status updates"""
    if not query.strip():
        return "Please enter a medical question.", "", "⚠️ No query provided"
    
    try:
        # Initialize system with progress
        progress(0.05, desc="πŸ”§ Initializing system...")
        system = initialize_enhanced_system()
        
        # Create async event loop for progress tracking
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        def progress_callback(value, desc):
            progress(value, desc=desc)
        
        try:
            # Run async generation with progress
            result = loop.run_until_complete(
                system.generate_response_async(query, progress_callback)
            )
        finally:
            loop.close()
        
        # Prepare response with enhanced metadata
        response = result["response"]
        
        # Prepare detailed metadata
        metadata_parts = [
            f"🎯 Confidence: {result['confidence']:.1%}",
            f"⏱️ Response: {result['response_time']}s",
            f"πŸ“š Sources: {result['search_results_count']} found"
        ]
        
        if result.get('cached'):
            metadata_parts.append("πŸ’Ύ Cached")
        
        if result.get('sources'):
            metadata_parts.append(f"πŸ“– Refs: {', '.join(result['sources'][:2])}")
        
        metadata = " | ".join(metadata_parts)
        
        # Prepare status with warnings/issues
        status_parts = []
        if result.get('safety_warnings'):
            status_parts.append(f"⚠️ {len(result['safety_warnings'])} warnings")
        if result.get('safety_issues'):
            status_parts.append(f"🚨 {len(result['safety_issues'])} issues")
        if not status_parts:
            status_parts.append("βœ… Safe response")
        
        status = " | ".join(status_parts)
        
        return response, metadata, status
        
    except Exception as e:
        logger.error(f"Error processing query: {e}")
        error_response = f"⚠️ Error processing your query: {str(e)}\n\n🚨 For medical emergencies, seek immediate professional help."
        error_metadata = f"❌ Error at {datetime.now().strftime('%H:%M:%S')}"
        error_status = "🚨 System error occurred"
        return error_response, error_metadata, error_status


def create_advanced_gradio_interface():
    """Create advanced Gradio interface with modern design and enhanced UX"""
    
    # Advanced CSS with medical theme and animations
    css = """
    @import url('https://fonts.googleapis.com/css2?family=Love+Ya+Like+A+Sister&display=swap');

    * {
        font-family: 'Love Ya Like A Sister', cursive !important;
    }

    .gradio-container {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
    }
    
    .main-container {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border-radius: 20px;
        padding: 30px;
        margin: 20px;
        box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        border: 1px solid rgba(255,255,255,0.2);
    }
    
    .header-section {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 15px;
        padding: 25px;
        margin-bottom: 25px;
        text-align: center;
        box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
    }
    
    .query-container {
        background: linear-gradient(135deg, #f8f9ff 0%, #e8f2ff 100%);
        border-radius: 15px;
        padding: 20px;
        margin: 15px 0;
        border: 2px solid #667eea;
        transition: all 0.3s ease;
    }
    
    .query-container:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 25px rgba(102, 126, 234, 0.2);
    }
    
    .query-input {
        border: none !important;
        background: white !important;
        border-radius: 12px !important;
        padding: 15px !important;
        font-size: 16px !important;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1) !important;
        transition: all 0.3s ease !important;
    }
    
    .query-input:focus {
        transform: scale(1.02) !important;
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3) !important;
    }
    
    .response-container {
        background: linear-gradient(135deg, #fff 0%, #f8f9ff 100%);
        border-radius: 15px;
        padding: 20px;
        margin: 15px 0;
        border: 2px solid #4CAF50;
        min-height: 300px;
    }
    
    .response-output {
        border: none !important;
        background: transparent !important;
        font-size: 15px !important;
        line-height: 1.7 !important;
        color: #2c3e50 !important;
    }
    
    .metadata-container {
        background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
        border-radius: 12px;
        padding: 15px;
        margin: 10px 0;
        border-left: 5px solid #2196F3;
    }
    
    .metadata-output {
        border: none !important;
        background: transparent !important;
        font-size: 13px !important;
        color: #1565c0 !important;
        font-weight: 500 !important;
    }
    
    .status-container {
        background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);
        border-radius: 12px;
        padding: 15px;
        margin: 10px 0;
        border-left: 5px solid #4CAF50;
    }
    
    .status-output {
        border: none !important;
        background: transparent !important;
        font-size: 13px !important;
        color: #2e7d32 !important;
        font-weight: 500 !important;
    }
    
    .submit-btn {
        background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%) !important;
        color: white !important;
        border: none !important;
        border-radius: 12px !important;
        padding: 15px 30px !important;
        font-size: 16px !important;
        font-weight: 600 !important;
        cursor: pointer !important;
        transition: all 0.3s ease !important;
        box-shadow: 0 6px 20px rgba(76, 175, 80, 0.3) !important;
    }
    
    .submit-btn:hover {
        transform: translateY(-3px) !important;
        box-shadow: 0 10px 30px rgba(76, 175, 80, 0.4) !important;
    }
    
    .clear-btn {
        background: linear-gradient(135deg, #ff7043 0%, #ff5722 100%) !important;
        color: white !important;
        border: none !important;
        border-radius: 12px !important;
        padding: 15px 25px !important;
        font-size: 14px !important;
        font-weight: 500 !important;
        transition: all 0.3s ease !important;
    }
    
    .clear-btn:hover {
        transform: translateY(-2px) !important;
        box-shadow: 0 8px 20px rgba(255, 87, 34, 0.3) !important;
    }
    
    .emergency-notice {
        background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
        border: 2px solid #f44336;
        border-radius: 15px;
        padding: 20px;
        margin: 20px 0;
        color: #c62828;
        font-weight: 600;
        animation: pulse 2s infinite;
    }
    
    @keyframes pulse {
        0% { box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.4); }
        70% { box-shadow: 0 0 0 10px rgba(244, 67, 54, 0); }
        100% { box-shadow: 0 0 0 0 rgba(244, 67, 54, 0); }
    }
    
    .gaza-context {
        background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);
        border: 2px solid #4caf50;
        border-radius: 15px;
        padding: 20px;
        margin: 20px 0;
        color: #2e7d32;
        font-weight: 500;
    }
    
    .sidebar-container {
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        border-radius: 15px;
        padding: 20px;
        margin: 10px 0;
        border: 1px solid rgba(0,0,0,0.1);
    }
    
    .example-container {
        background: white;
        border-radius: 12px;
        padding: 20px;
        margin: 15px 0;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    }
    
    .progress-container {
        margin: 15px 0;
        padding: 10px;
        background: rgba(255,255,255,0.8);
        border-radius: 10px;
    }
    
    .footer-section {
        background: linear-gradient(135deg, #37474f 0%, #263238 100%);
        color: white;
        border-radius: 15px;
        padding: 20px;
        margin-top: 30px;
        text-align: center;
    }

    /* GLOBAL TEXT FIXES */
.gradio-container, 
.query-container, 
.response-container, 
.metadata-container, 
.status-container {
    color: white !important;
}

.query-input, 
.response-output, 
.metadata-output, 
.status-output {
    color: white !important;
    background-color: rgba(0, 0, 0, 0.2) !important;
}

/* BANNER-INSPIRED PANEL BACKGROUNDS */
.query-container,
.response-container,
.metadata-container,
.status-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    border: 2px solid #ffffff22 !important;
    border-radius: 15px !important;
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
}

/* EXAMPLE SECTION BUTTON STYLING */
.example-container .example {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    color: white !important;
    font-weight: 600 !important;
    border-radius: 12px !important;
    padding: 15px !important;
    margin: 10px !important;
    text-align: center !important;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    cursor: pointer;
}

.example-container .example:hover {
    transform: scale(1.03);
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
}

/* MAKE HEADER + EXAMPLES MORE PROMINENT */
.header-section {
    color: white !important;
    text-shadow: 0px 0px 6px rgba(0,0,0,0.4);
}

.example-container {
    margin-top: -20px !important;
}
    """

    with gr.Blocks(
        css=css, 
        title="πŸ₯ Advanced Gaza First Aid Assistant",
        theme=gr.themes.Soft(
            primary_hue="blue",
            secondary_hue="green",
            neutral_hue="slate"
        )
    ) as interface:
        
        # Header Section
        with gr.Row(elem_classes=["main-container"]):
            gr.HTML("""
            <div class="header-section">
                <h1 style="margin: 0; font-size: 2.5em; font-weight: 700;">
                    πŸ₯ Advanced Gaza First Aid Assistant
                </h1>
                <h2 style="margin: 10px 0 0 0; font-size: 1.2em; font-weight: 400; opacity: 0.9;">
                    AI-Powered Medical Guidance for Gaza Healthcare Workers
                </h2>
                <p style="margin: 15px 0 0 0; font-size: 1em; opacity: 0.8;">
                    Enhanced with 768-dimensional medical embeddings β€’ Advanced FAISS indexing β€’ Real-time safety validation
                </p>
            </div>
            """)
        
        # Main Interface
        with gr.Row(elem_classes=["main-container"]):
            with gr.Column(scale=2):
                # Query Input Section
                with gr.Group(elem_classes=["query-container"]):
                    gr.Markdown("### 🩺 Medical Query Input")
                    query_input = gr.Textbox(
                        label="Describe your medical situation",
                        placeholder="Enter your first aid question or describe the medical emergency...",
                        lines=4,
                        elem_classes=["query-input"]
                    )
                    
                    with gr.Row():
                        submit_btn = gr.Button(
                            "πŸ” Get Medical Guidance", 
                            variant="primary",
                            elem_classes=["submit-btn"],
                            scale=3
                        )
                        clear_btn = gr.Button(
                            "πŸ—‘οΈ Clear", 
                            variant="secondary",
                            elem_classes=["clear-btn"],
                            scale=1
                        )
            
            with gr.Column(scale=1):
                # Sidebar with Quick Access
                with gr.Group(elem_classes=["sidebar-container"]):
                    gr.Markdown("""
                    ### 🎯 Quick Access Guide
                    
                    **🚨 Emergency Priorities:**
                    - Severe bleeding control
                    - Burn treatment protocols  
                    - Airway management
                    - Trauma stabilization
                    - Shock prevention
                    
                    **πŸ₯ Gaza-Specific Scenarios:**
                    - Limited water situations
                    - Power outage medical care
                    - Supply shortage alternatives
                    - Mass casualty protocols
                    - Improvised medical tools
                    
                    **πŸ“Š System Status:**
                    - βœ… Enhanced embeddings active
                    - βœ… Advanced indexing enabled
                    - βœ… Safety validation online
                    - βœ… Gaza context aware
                    """)
        
        # Response Section
        with gr.Row(elem_classes=["main-container"]):
            with gr.Column():
                # Main Response
                with gr.Group(elem_classes=["response-container"]):
                    gr.Markdown("### 🩹 Medical Guidance Response")
                    response_output = gr.Textbox(
                        label="AI Medical Guidance",
                        lines=15,
                        elem_classes=["response-output"],
                        interactive=False,
                        placeholder="Your medical guidance will appear here..."
                    )
                
                # Metadata and Status
                with gr.Row():
                    with gr.Column(scale=1):
                        with gr.Group(elem_classes=["metadata-container"]):
                            metadata_output = gr.Textbox(
                                label="πŸ“Š Response Metadata",
                                lines=2,
                                elem_classes=["metadata-output"],
                                interactive=False,
                                placeholder="Response metadata will appear here..."
                            )
                    
                    with gr.Column(scale=1):
                        with gr.Group(elem_classes=["status-container"]):
                            status_output = gr.Textbox(
                                label="πŸ›‘οΈ Safety Status",
                                lines=2,
                                elem_classes=["status-output"],
                                interactive=False,
                                placeholder="Safety validation status will appear here..."
                            )
        
        # Important Notices
        with gr.Row(elem_classes=["main-container"]):
            gr.HTML("""
            <div class="emergency-notice">
                <h3 style="margin: 0 0 10px 0;">🚨 CRITICAL EMERGENCY DISCLAIMER</h3>
                <p style="margin: 0; font-size: 1.1em;">
                    For life-threatening emergencies, seek immediate professional medical attention.<br>
                    πŸ“ž <strong>Gaza Emergency Contacts:</strong> Palestinian Red Crescent (101) | Civil Defense (102)
                </p>
            </div>
            """)
        
        with gr.Row(elem_classes=["main-container"]):
            gr.HTML("""
            <div class="gaza-context">
                <h3 style="margin: 0 0 10px 0;">πŸ“ Gaza Context Awareness</h3>
                <p style="margin: 0; font-size: 1em;">
                    This advanced AI system is specifically designed for Gaza's challenging conditions including 
                    limited resources, frequent power outages, and restricted medical supply access. All guidance 
                    considers these constraints and provides practical alternatives when standard treatments are unavailable.
                </p>
            </div>
            """)
        
        # Examples Section
        with gr.Row(elem_classes=["main-container"]):
            with gr.Group(elem_classes=["example-container"]):
                gr.Markdown("### πŸ’‘ Example Medical Scenarios")
                
                example_queries = [
                    "How to treat severe burns when clean water is extremely limited?",
                    "Managing gunshot wounds with only basic household supplies",
                    "Recognizing and treating infection in wounds without antibiotics",
                    "Emergency care for children during extended power outages",
                    "Treating compound fractures without proper medical equipment",
                    "Managing diabetic emergencies when insulin is unavailable",
                    "Stopping arterial bleeding with improvised tourniquets",
                    "Recognizing and treating shock in mass casualty situations",
                    "Airway management for unconscious patients without equipment",
                    "Preventing infection in surgical wounds during siege conditions"
                ]
                
                gr.Examples(
                    examples=example_queries,
                    inputs=query_input,
                    label="Click any example to try it:",
                    examples_per_page=5
                )
        
        # Event Handlers
        submit_btn.click(
            process_medical_query_with_progress,
            inputs=query_input,
            outputs=[response_output, metadata_output, status_output],
            show_progress=True
        )
        
        query_input.submit(
            process_medical_query_with_progress,
            inputs=query_input,
            outputs=[response_output, metadata_output, status_output],
            show_progress=True
        )
        
        clear_btn.click(
            lambda: ("", "", "", ""),
            outputs=[query_input, response_output, metadata_output, status_output]
        )
        
        # Footer
        with gr.Row(elem_classes=["main-container"]):
            gr.HTML("""
            <div class="footer-section">
                <h3 style="margin: 0 0 15px 0;">πŸ”¬ Advanced Technical Features</h3>
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; margin-bottom: 20px;">
                    <div>
                        <strong>🧠 Enhanced AI:</strong><br>
                        768-dimensional medical embeddings<br>
                        Advanced FAISS IVF indexing<br>
                        Optimized LLM quantization
                    </div>
                    <div>
                        <strong>πŸ›‘οΈ Safety Systems:</strong><br>
                        Real-time medical validation<br>
                        Contraindication detection<br>
                        Gaza-specific risk assessment
                    </div>
                    <div>
                        <strong>⚑ Performance:</strong><br>
                        Async processing pipeline<br>
                        Intelligent response caching<br>
                        Progressive loading indicators
                    </div>
                </div>
                <hr style="border: 1px solid rgba(255,255,255,0.2); margin: 20px 0;">
                <p style="margin: 0; opacity: 0.8;">
                    <strong>βš•οΈ Medical Disclaimer:</strong> This AI assistant provides educational guidance based on established medical protocols. 
                    It is designed to support, not replace, medical professionals. Always consult qualified healthcare providers for definitive care.
                </p>
            </div>
            """)
    
    return interface

def main():
    """Enhanced main function with comprehensive error handling and system monitoring"""
    logger.info("πŸš€ Starting Advanced Gaza First Aid Assistant")
    
    try:
        # System initialization with detailed logging
        logger.info("πŸ”§ Pre-initializing enhanced RAG system...")
        system = initialize_enhanced_system()
        
        # Verify system components
        logger.info("βœ… Knowledge base initialized")
        logger.info("βœ… Medical fact checker ready")
        logger.info("βœ… Enhanced embeddings loaded")
        logger.info("βœ… Advanced FAISS indexing active")
        
        # Create and launch advanced interface
        logger.info("🎨 Creating advanced Gradio interface...")
        interface = create_advanced_gradio_interface()
        
        logger.info("🌐 Launching advanced interface...")
        interface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            max_threads=6,  # Increased for better async performance
            show_error=True,
            quiet=False,
            favicon_path=None,
            ssl_verify=False
        )
        
    except Exception as e:
        logger.error(f"❌ Failed to start Advanced Gaza First Aid Assistant: {e}")
        print(f"\n🚨 STARTUP ERROR: {e}")
        print("\nπŸ”§ Troubleshooting Steps:")
        print("1. Check if all dependencies are installed: pip install -r requirements.txt")
        print("2. Ensure sufficient memory is available (minimum 4GB RAM recommended)")
        print("3. Verify data directory exists and contains medical documents")
        print("4. Check system logs for detailed error information")
        print("\nπŸ“ž For technical support, check the application logs above.")
        sys.exit(1)

if __name__ == "__main__":
    main()