File size: 99,490 Bytes
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c26e67
 
 
 
 
 
 
15bb146
 
5c26e67
 
 
 
 
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1492aa
15bb146
 
 
 
 
c7cc357
 
 
 
 
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7cc357
15bb146
c7cc357
15bb146
c7cc357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15bb146
 
c7cc357
 
 
 
15bb146
c7cc357
15bb146
 
c7cc357
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26eff0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1492aa
15bb146
 
 
 
 
 
 
a1492aa
 
 
 
 
 
 
 
 
 
 
 
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1492aa
 
15bb146
a1492aa
 
 
 
 
 
15bb146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
#!/usr/bin/env python3
"""
🚀 GAIA Multi-Agent System - UNIVERSAL MULTIMODAL AI AGENT
Enhanced with comprehensive multimodal capabilities for ANY type of question:
- 🎥 Video Processing & Analysis
- 🎵 Audio Processing & Speech Recognition  
- 🎨 Image Generation & Advanced Computer Vision
- 📊 Data Visualization & Chart Generation
- 🎙️ Speech Synthesis & Voice Generation
- 🎬 Video Generation & Editing
- 🧬 Scientific Computing & Analysis
- 📈 Advanced Analytics & Modeling
"""

import os
import sys
import re
import json
import time
import random
import logging
import requests
import tempfile
import base64
import hashlib
import subprocess
from typing import Dict, List, Any, Optional, Tuple, Union
from dataclasses import dataclass
from enum import Enum
from urllib.parse import urlparse, urljoin
import math
import statistics

# Core AI and Web Libraries
try:
    from huggingface_hub import InferenceClient
    HF_AVAILABLE = True
except ImportError:
    HF_AVAILABLE = False
    print("⚠️ huggingface_hub not available. AI features limited.")

try:
    import openai
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False
    print("⚠️ OpenAI not available. GPT models unavailable.")

# Web Scraping
try:
    from bs4 import BeautifulSoup
    BS4_AVAILABLE = True
except ImportError:
    BS4_AVAILABLE = False
    print("⚠️ BeautifulSoup not available. Web scraping limited.")

# Image Processing
try:
    from PIL import Image, ImageDraw, ImageFont
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False
    print("⚠️ Pillow not available. Image processing limited.")

# Video Processing
try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False
    print("⚠️ OpenCV not available. Video processing unavailable.")

# Audio Processing
try:
    import librosa
    import soundfile as sf
    AUDIO_AVAILABLE = True
except ImportError:
    AUDIO_AVAILABLE = False
    print("⚠️ Audio libraries not available. Audio processing unavailable.")

# Speech Recognition
try:
    import speech_recognition as sr
    SPEECH_AVAILABLE = True
except ImportError:
    SPEECH_AVAILABLE = False
    print("⚠️ Speech recognition not available.")

# Text-to-Speech
try:
    import pyttsx3
    TTS_AVAILABLE = True
except ImportError:
    TTS_AVAILABLE = False
    print("⚠️ Text-to-speech not available.")

# Data Visualization
try:
    import matplotlib.pyplot as plt
    import plotly.graph_objects as go
    import plotly.express as px
    VIZ_AVAILABLE = True
    # Optional: seaborn
    try:
        import seaborn as sns
        SEABORN_AVAILABLE = True
    except ImportError:
        SEABORN_AVAILABLE = False
        sns = None
except ImportError:
    VIZ_AVAILABLE = False
    SEABORN_AVAILABLE = False
    plt = None
    go = None
    px = None
    sns = None
    print("⚠️ Visualization libraries not available.")

# Scientific Computing
try:
    import numpy as np
    import pandas as pd
    import scipy.stats as stats
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import KMeans
    SCIENCE_AVAILABLE = True
except ImportError:
    SCIENCE_AVAILABLE = False
    print("⚠️ Scientific computing libraries not available.")

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class ToolType(Enum):
    """🛠️ Universal tool types for any content type"""
    # Original tools
    WEB_SEARCH = "web_search"
    BROWSE_URL = "browse_url"
    DOWNLOAD_FILE = "download_file"
    READ_PDF = "read_pdf"
    ANALYZE_IMAGE = "analyze_image"
    CALCULATOR = "calculator"
    
    # New multimodal tools
    PROCESS_VIDEO = "process_video"
    ANALYZE_AUDIO = "analyze_audio"
    GENERATE_IMAGE = "generate_image"
    SYNTHESIZE_SPEECH = "synthesize_speech"
    CREATE_VISUALIZATION = "create_visualization"
    ANALYZE_DATA = "analyze_data"
    GENERATE_VIDEO = "generate_video"
    EXTRACT_AUDIO = "extract_audio"
    TRANSCRIBE_SPEECH = "transcribe_speech"
    DETECT_OBJECTS = "detect_objects"
    FACE_RECOGNITION = "face_recognition"
    SCIENTIFIC_COMPUTE = "scientific_compute"

@dataclass
class ToolCall:
    tool: ToolType
    parameters: Dict[str, Any]

class UniversalMultimodalToolkit:
    """🌟 Universal toolkit for processing ANY type of content"""
    
    def __init__(self, hf_token: str = None, openai_key: str = None):
        self.hf_token = hf_token
        self.openai_key = openai_key
        self.temp_dir = tempfile.mkdtemp()
        
        # Initialize specialized clients
        self._init_multimodal_clients()
        
    def _init_multimodal_clients(self):
        """Initialize all multimodal AI clients"""
        self.clients = {}
        
        if self.hf_token and HF_AVAILABLE:
            # Vision models
            self.clients['vision'] = InferenceClient(model="Salesforce/blip-image-captioning-large", token=self.hf_token)
            self.clients['image_gen'] = InferenceClient(model="stabilityai/stable-diffusion-xl-base-1.0", token=self.hf_token)
            self.clients['object_detection'] = InferenceClient(model="facebook/detr-resnet-50", token=self.hf_token)
            
            # Audio models - Updated to use provider pattern for speech recognition
            self.clients['speech_to_text'] = InferenceClient(
                provider="hf-inference",
                api_key=self.hf_token,
            )
            self.clients['audio_classification'] = InferenceClient(model="facebook/wav2vec2-base-960h", token=self.hf_token)
            
            # Text generation for multimodal
            self.clients['text_gen'] = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct", token=self.hf_token)

    # === VIDEO PROCESSING ===
    def process_video(self, video_path: str, task: str = "analyze") -> str:
        """🎥 Process and analyze video content"""
        if not CV2_AVAILABLE:
            return "❌ Video processing unavailable. Install opencv-python."
            
        try:
            logger.info(f"🎥 Processing video: {video_path} | Task: {task}")
            
            cap = cv2.VideoCapture(video_path)
            if not cap.isOpened():
                return f"❌ Could not open video: {video_path}"
                
            # Get video properties
            fps = cap.get(cv2.CAP_PROP_FPS)
            frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            duration = frame_count / fps if fps > 0 else 0
            width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            
            video_info = f"Video: {width}x{height}, {fps:.1f} FPS, {duration:.1f}s, {frame_count} frames"
            
            if task == "extract_frames":
                # Extract key frames for analysis
                frames_extracted = []
                frame_interval = max(1, frame_count // 10)  # Extract 10 frames max
                
                for i in range(0, frame_count, frame_interval):
                    cap.set(cv2.CAP_PROP_POS_FRAMES, i)
                    ret, frame = cap.read()
                    if ret:
                        frame_path = os.path.join(self.temp_dir, f"frame_{i}.jpg")
                        cv2.imwrite(frame_path, frame)
                        frames_extracted.append(frame_path)
                        
                cap.release()
                
                # Analyze extracted frames
                frame_analyses = []
                for frame_path in frames_extracted[:3]:  # Analyze first 3 frames
                    analysis = self.analyze_image(frame_path, "Describe what you see in this video frame")
                    frame_analyses.append(analysis)
                
                return f"{video_info}. Frame analysis: {'; '.join(frame_analyses)}"
                
            elif task == "motion_detection":
                # Simple motion detection
                ret, frame1 = cap.read()
                if not ret:
                    cap.release()
                    return f"{video_info}. Motion detection failed."
                    
                frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
                motion_detected = 0
                
                while True:
                    ret, frame2 = cap.read()
                    if not ret:
                        break
                        
                    frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
                    diff = cv2.absdiff(frame1_gray, frame2_gray)
                    
                    if cv2.countNonZero(diff) > 5000:  # Threshold for motion
                        motion_detected += 1
                        
                    frame1_gray = frame2_gray
                    
                cap.release()
                motion_percentage = (motion_detected / frame_count) * 100
                
                return f"{video_info}. Motion detected in {motion_percentage:.1f}% of frames."
                
            else:
                cap.release()
                return f"{video_info}. Basic video analysis complete."
                
        except Exception as e:
            logger.error(f"❌ Video processing error: {e}")
            return f"❌ Video processing failed: {e}"

    # === AUDIO PROCESSING ===
    def analyze_audio(self, audio_path: str, task: str = "analyze") -> str:
        """🎵 Analyze audio content"""
        if not AUDIO_AVAILABLE:
            return "❌ Audio processing unavailable. Install librosa and soundfile."
            
        try:
            logger.info(f"🎵 Analyzing audio: {audio_path} | Task: {task}")
            
            # Load audio
            y, sr = librosa.load(audio_path, sr=None)
            duration = len(y) / sr
            
            audio_info = f"Audio: {duration:.1f}s, {sr} Hz, {len(y)} samples"
            
            if task == "transcribe":
                return self.transcribe_speech(audio_path)
            elif task == "features":
                # Extract audio features
                tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
                spectral_centroids = librosa.feature.spectral_centroid(y=y, sr=sr)[0]
                spectral_rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)[0]
                zero_crossing_rate = librosa.feature.zero_crossing_rate(y)[0]
                
                features = {
                    "tempo": float(tempo),
                    "avg_spectral_centroid": float(np.mean(spectral_centroids)),
                    "avg_spectral_rolloff": float(np.mean(spectral_rolloff)),
                    "avg_zero_crossing_rate": float(np.mean(zero_crossing_rate))
                }
                
                return f"{audio_info}. Features: {json.dumps(features, indent=2)}"
            else:
                return f"{audio_info}. Basic audio analysis complete."
                
        except Exception as e:
            logger.error(f"❌ Audio analysis error: {e}")
            return f"❌ Audio analysis failed: {e}"

    def transcribe_speech(self, audio_path: str) -> str:
        """🎙️ Convert speech to text using Whisper via HuggingFace Inference API"""
        try:
            logger.info(f"🎙️ Transcribing speech from: {audio_path}")
            
            if self.hf_token and HF_AVAILABLE and 'speech_to_text' in self.clients:
                # Use Whisper via HuggingFace Inference API with provider pattern
                try:
                    result = self.clients['speech_to_text'].automatic_speech_recognition(
                        audio_path, 
                        model="openai/whisper-large-v3"
                    )
                    
                    if isinstance(result, dict) and 'text' in result:
                        transcription = result['text'].strip()
                    elif isinstance(result, str):
                        transcription = result.strip()
                    else:
                        transcription = str(result).strip()
                    
                    if transcription:
                        return f"Transcription: {transcription}"
                    else:
                        return "❌ No transcription available"
                        
                except Exception as hf_error:
                    logger.warning(f"⚠️ HuggingFace speech recognition failed: {hf_error}")
                    # Fall through to local recognition
            
            # Fallback to local speech recognition if available
            if SPEECH_AVAILABLE:
                try:
                    r = sr.Recognizer()
                    with sr.AudioFile(audio_path) as source:
                        audio = r.record(source)
                        text = r.recognize_google(audio)
                        return f"Transcription: {text}"
                except sr.UnknownValueError:
                    return "❌ Could not understand audio"
                except sr.RequestError as e:
                    return f"❌ Speech recognition error: {e}"
            else:
                return "❌ Speech recognition unavailable. Need HuggingFace token or speech_recognition library."
                
        except Exception as e:
            logger.error(f"❌ Transcription error: {e}")
            return f"❌ Transcription failed: {e}"

    # === IMAGE GENERATION ===
    def generate_image(self, prompt: str, style: str = "realistic") -> str:
        """🎨 Generate images from text descriptions"""
        try:
            logger.info(f"🎨 Generating image: {prompt} | Style: {style}")
            
            if self.hf_token and 'image_gen' in self.clients:
                # Use Stable Diffusion via HuggingFace
                enhanced_prompt = f"{prompt}, {style} style, high quality, detailed"
                
                image = self.clients['image_gen'].text_to_image(enhanced_prompt)
                
                # Save generated image
                image_path = os.path.join(self.temp_dir, f"generated_{int(time.time())}.png")
                image.save(image_path)
                
                return f"✅ Image generated and saved to: {image_path}"
            
            elif self.openai_key and OPENAI_AVAILABLE:
                # Use DALL-E via OpenAI
                client = openai.OpenAI(api_key=self.openai_key)
                response = client.images.generate(
                    model="dall-e-3",
                    prompt=f"{prompt}, {style} style",
                    size="1024x1024",
                    quality="standard",
                    n=1,
                )
                
                image_url = response.data[0].url
                
                # Download and save image
                img_response = requests.get(image_url)
                image_path = os.path.join(self.temp_dir, f"dalle_generated_{int(time.time())}.png")
                with open(image_path, 'wb') as f:
                    f.write(img_response.content)
                
                return f"✅ DALL-E image generated and saved to: {image_path}"
            else:
                return "❌ Image generation unavailable. Need HuggingFace token or OpenAI key."
                
        except Exception as e:
            logger.error(f"❌ Image generation error: {e}")
            return f"❌ Image generation failed: {e}"

    # === SPEECH SYNTHESIS ===
    def synthesize_speech(self, text: str, voice: str = "default") -> str:
        """🎙️ Convert text to speech"""
        try:
            logger.info(f"🎙️ Synthesizing speech: {text[:50]}... | Voice: {voice}")
            
            if TTS_AVAILABLE:
                engine = pyttsx3.init()
                
                # Set voice properties
                voices = engine.getProperty('voices')
                if voices and len(voices) > 0:
                    if voice == "female" and len(voices) > 1:
                        engine.setProperty('voice', voices[1].id)
                    else:
                        engine.setProperty('voice', voices[0].id)
                
                # Set speech rate and volume
                engine.setProperty('rate', 150)
                engine.setProperty('volume', 0.9)
                
                # Generate speech file
                speech_path = os.path.join(self.temp_dir, f"speech_{int(time.time())}.wav")
                engine.save_to_file(text, speech_path)
                engine.runAndWait()
                
                return f"✅ Speech synthesized and saved to: {speech_path}"
            else:
                return "❌ Text-to-speech unavailable. Install pyttsx3."
                
        except Exception as e:
            logger.error(f"❌ Speech synthesis error: {e}")
            return f"❌ Speech synthesis failed: {e}"

    # === DATA VISUALIZATION ===
    def create_visualization(self, data: Dict[str, Any], chart_type: str = "bar") -> str:
        """📊 Create data visualizations and charts"""
        try:
            logger.info(f"📊 Creating {chart_type} chart")
            
            if not VIZ_AVAILABLE:
                return "❌ Visualization unavailable. Install matplotlib, seaborn, and plotly."
            
            # Prepare data
            if isinstance(data, dict) and 'x' in data and 'y' in data:
                x_data = data['x']
                y_data = data['y']
                title = data.get('title', 'Data Visualization')
            else:
                return "❌ Invalid data format. Need dict with 'x' and 'y' keys."
            
            # Create visualization
            plt.figure(figsize=(10, 6))
            
            if chart_type == "bar":
                plt.bar(x_data, y_data)
            elif chart_type == "line":
                plt.plot(x_data, y_data, marker='o')
            elif chart_type == "scatter":
                plt.scatter(x_data, y_data)
            elif chart_type == "pie":
                plt.pie(y_data, labels=x_data, autopct='%1.1f%%')
            else:
                plt.plot(x_data, y_data)
            
            plt.title(title)
            plt.xlabel(data.get('xlabel', 'X'))
            plt.ylabel(data.get('ylabel', 'Y'))
            plt.grid(True, alpha=0.3)
            
            # Save chart
            chart_path = os.path.join(self.temp_dir, f"chart_{int(time.time())}.png")
            plt.savefig(chart_path, dpi=300, bbox_inches='tight')
            plt.close()
            
            return f"✅ {chart_type.title()} chart created and saved to: {chart_path}"
            
        except Exception as e:
            logger.error(f"❌ Visualization error: {e}")
            return f"❌ Visualization failed: {e}"

    # === SCIENTIFIC COMPUTING ===
    def scientific_compute(self, operation: str, data: Dict[str, Any]) -> str:
        """🧬 Perform scientific computations and analysis"""
        try:
            if not SCIENCE_AVAILABLE:
                return "❌ Scientific computing unavailable. Install numpy, pandas, scipy, sklearn."
            
            logger.info(f"🧬 Scientific computation: {operation}")
            
            if operation == "statistics":
                values = data.get('values', [])
                if not values:
                    return "❌ No values provided for statistics"
                
                result = {
                    "mean": float(np.mean(values)),
                    "median": float(np.median(values)),
                    "std": float(np.std(values)),
                    "min": float(np.min(values)),
                    "max": float(np.max(values)),
                    "variance": float(np.var(values)),
                    "skewness": float(stats.skew(values)),
                    "kurtosis": float(stats.kurtosis(values))
                }
                
                return f"Statistics: {json.dumps(result, indent=2)}"
            
            elif operation == "correlation":
                x = data.get('x', [])
                y = data.get('y', [])
                if not x or not y or len(x) != len(y):
                    return "❌ Need equal length x and y arrays for correlation"
                
                correlation = float(np.corrcoef(x, y)[0, 1])
                p_value = float(stats.pearsonr(x, y)[1])
                
                return f"Correlation: {correlation:.4f}, P-value: {p_value:.4f}"
            
            elif operation == "clustering":
                data_points = data.get('data', [])
                n_clusters = data.get('clusters', 3)
                
                if not data_points:
                    return "❌ No data points provided for clustering"
                
                # Perform K-means clustering
                scaler = StandardScaler()
                scaled_data = scaler.fit_transform(data_points)
                
                kmeans = KMeans(n_clusters=n_clusters, random_state=42)
                labels = kmeans.fit_predict(scaled_data)
                
                return f"Clustering complete. Labels: {labels.tolist()}"
            
            else:
                return f"❌ Unknown scientific operation: {operation}"
                
        except Exception as e:
            logger.error(f"❌ Scientific computation error: {e}")
            return f"❌ Scientific computation failed: {e}"

    # === OBJECT DETECTION ===
    def detect_objects(self, image_path: str) -> str:
        """🔍 Detect and identify objects in images"""
        try:
            logger.info(f"🔍 Detecting objects in: {image_path}")
            
            if self.hf_token and 'object_detection' in self.clients:
                with open(image_path, 'rb') as img_file:
                    result = self.clients['object_detection'].object_detection(img_file.read())
                
                if result:
                    objects = []
                    for detection in result:
                        label = detection.get('label', 'unknown')
                        score = detection.get('score', 0)
                        objects.append(f"{label} ({score:.2f})")
                    
                    return f"Objects detected: {', '.join(objects)}"
                else:
                    return "No objects detected"
            else:
                return "❌ Object detection unavailable. Need HuggingFace token."
                
        except Exception as e:
            logger.error(f"❌ Object detection error: {e}")
            return f"❌ Object detection failed: {e}"

    # Enhanced existing methods
    def web_search(self, query: str, num_results: int = 5) -> str:
        """🔍 Enhanced web search with comprehensive crawling and browsing"""
        try:
            logger.info(f"🔍 Web search: {query}")
            
            # Enhanced DuckDuckGo search with better result extraction
            search_url = f"https://duckduckgo.com/html/?q={requests.utils.quote(query)}"
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            
            response = requests.get(search_url, headers=headers, timeout=15)
            response.raise_for_status()
            
            if not BS4_AVAILABLE:
                return f"⚠️ Search completed but parsing limited. Raw response length: {len(response.text)}"
            
            soup = BeautifulSoup(response.text, 'html.parser')
            results = []
            
            # Enhanced result extraction with multiple patterns
            result_selectors = [
                'div.result',
                'div[data-result-index]',
                'article',
                'li.result'
            ]
            
            for selector in result_selectors:
                search_results = soup.select(selector)[:num_results]
                if search_results:
                    break
            else:
                search_results = []
            
            for result in search_results:
                # Extract title
                title_elem = (result.find('a', class_='result__a') or 
                             result.find('h2') or 
                             result.find('h3') or 
                             result.find('a'))
                
                # Extract snippet
                snippet_elem = (result.find('a', class_='result__snippet') or
                              result.find('span', class_='result__snippet') or
                              result.find('p'))
                
                if title_elem:
                    title = title_elem.get_text(strip=True)
                    url = title_elem.get('href', '')
                    snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""
                    
                    # Clean and format URL
                    if url and not url.startswith('http'):
                        if url.startswith('//'):
                            url = 'https:' + url
                        elif url.startswith('/'):
                            url = 'https://duckduckgo.com' + url
                    
                    results.append({
                        'title': title,
                        'url': url,
                        'snippet': snippet
                    })
            
            if results:
                # Format results for AI consumption
                formatted_results = []
                for i, result in enumerate(results, 1):
                    formatted_results.append(
                        f"{i}. {result['title']}\n"
                        f"   {result['snippet']}\n"
                        f"   URL: {result['url']}"
                    )
                
                return "\n\n".join(formatted_results)
            else:
                # Fallback: Try alternative search approach
                try:
                    alt_url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(query)}"
                    alt_response = requests.get(alt_url, headers=headers, timeout=10)
                    if alt_response.status_code == 200:
                        return f"Search completed for '{query}' - found {len(alt_response.text)} characters of content"
                except:
                    pass
                
                return f"🔍 No results found for '{query}'"
                
        except Exception as e:
            logger.error(f"❌ Web search error: {e}")
            return f"❌ Web search failed: {e}"

    def browse_url(self, url: str) -> str:
        """🌐 Enhanced web browsing with content extraction"""
        try:
            logger.info(f"🌐 Browsing URL: {url}")
            
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language': 'en-US,en;q=0.5',
                'Accept-Encoding': 'gzip, deflate',
                'Connection': 'keep-alive'
            }
            
            response = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
            response.raise_for_status()
            
            if not BS4_AVAILABLE:
                return f"⚠️ URL accessed but content parsing limited. Content length: {len(response.text)}"
            
            soup = BeautifulSoup(response.text, 'html.parser')
            
            # Remove script and style elements
            for script in soup(["script", "style", "nav", "footer", "header"]):
                script.decompose()
            
            # Extract main content
            content_selectors = [
                'main',
                'article', 
                'div[role="main"]',
                'div.content',
                'div.main-content',
                'div.post-content',
                'div.entry-content',
                'div.article-body',
                'section'
            ]
            
            main_content = None
            for selector in content_selectors:
                main_content = soup.select_one(selector)
                if main_content:
                    break
            
            if not main_content:
                main_content = soup.find('body') or soup
            
            # Extract text content
            text_content = main_content.get_text(separator=' ', strip=True)
            
            # Clean up the text
            lines = text_content.split('\n')
            cleaned_lines = []
            for line in lines:
                line = line.strip()
                if line and len(line) > 3:  # Filter out very short lines
                    cleaned_lines.append(line)
            
            content = '\n'.join(cleaned_lines)
            
            # Truncate if too long (keep first 3000 characters)
            if len(content) > 3000:
                content = content[:3000] + "... [content truncated]"
            
            return f"📄 Content from {url}:\n\n{content}"
            
        except Exception as e:
            logger.error(f"❌ URL browsing error: {e}")
            return f"❌ Failed to browse {url}: {e}"

    def download_file(self, url: str, task_id: str = None) -> str:
        """📥 Download files from URLs or GAIA API"""
        try:
            logger.info(f"📥 Downloading file from: {url}")
            
            # Handle GAIA API task file downloads
            if task_id and not url:
                gaia_url = f"https://huggingface.co/datasets/gaia-benchmark/GAIA/raw/main/2023/validation/{task_id}"
                url = gaia_url
            
            # Set up headers
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            # Download the file
            response = requests.get(url, headers=headers, timeout=30, stream=True)
            response.raise_for_status()
            
            # Determine file extension
            content_type = response.headers.get('content-type', '').lower()
            if 'pdf' in content_type:
                extension = '.pdf'
            elif 'image' in content_type:
                if 'jpeg' in content_type or 'jpg' in content_type:
                    extension = '.jpg'
                elif 'png' in content_type:
                    extension = '.png'
                else:
                    extension = '.img'
            elif 'text' in content_type:
                extension = '.txt'
            else:
                # Try to extract from URL
                parsed_url = urlparse(url)
                path = parsed_url.path
                if '.' in path:
                    extension = '.' + path.split('.')[-1]
                else:
                    extension = '.bin'
            
            # Save to temp directory
            filename = f"downloaded_file_{task_id or 'temp'}{extension}"
            filepath = os.path.join(self.temp_dir, filename)
            
            with open(filepath, 'wb') as f:
                for chunk in response.iter_content(chunk_size=8192):
                    f.write(chunk)
            
            logger.info(f"📥 File downloaded to: {filepath}")
            return filepath
            
        except Exception as e:
            logger.error(f"❌ File download error: {e}")
            return f"❌ Download failed: {e}"

    def read_pdf(self, file_path: str) -> str:
        """📄 Read and extract text from PDF files"""
        try:
            logger.info(f"📄 Reading PDF: {file_path}")
            
            # Try importing PyPDF2
            try:
                import PyPDF2
                PDF_AVAILABLE = True
            except ImportError:
                PDF_AVAILABLE = False
            
            if not PDF_AVAILABLE:
                return "❌ PDF reading unavailable. Install PyPDF2."
            
            with open(file_path, 'rb') as file:
                pdf_reader = PyPDF2.PdfReader(file)
                text_content = []
                
                for page_num, page in enumerate(pdf_reader.pages):
                    try:
                        text = page.extract_text()
                        if text.strip():
                            text_content.append(f"[Page {page_num + 1}]\n{text}")
                    except Exception as page_error:
                        logger.warning(f"⚠️ Error reading page {page_num + 1}: {page_error}")
                        text_content.append(f"[Page {page_num + 1}] - Error reading page")
                
                full_text = "\n\n".join(text_content)
                
                # Truncate if too long
                if len(full_text) > 5000:
                    full_text = full_text[:5000] + "... [content truncated]"
                
                return full_text
                
        except Exception as e:
            logger.error(f"❌ PDF reading error: {e}")
            return f"❌ Failed to read PDF: {e}"

    def calculator(self, expression: str) -> str:
        """🧮 Enhanced mathematical calculator with scientific functions"""
        try:
            logger.info(f"🧮 Calculating: {expression}")
            
            # Import required math modules
            import math
            import statistics
            
            # Clean the expression
            expression = expression.strip()
            
            # Allow common mathematical functions
            safe_dict = {
                "__builtins__": {},
                "abs": abs,
                "round": round,
                "min": min,
                "max": max,
                "sum": sum,
                "len": len,
                "pow": pow,
                "sqrt": math.sqrt,
                "sin": math.sin,
                "cos": math.cos,
                "tan": math.tan,
                "log": math.log,
                "log10": math.log10,
                "exp": math.exp,
                "pi": math.pi,
                "e": math.e,
                "factorial": math.factorial,
                "mean": statistics.mean,
                "median": statistics.median,
                "mode": statistics.mode,
                "stdev": statistics.stdev,
            }
            
            # Evaluate the expression safely
            result = eval(expression, safe_dict, {})
            
            # Format the result appropriately
            if isinstance(result, float):
                if result.is_integer():
                    return str(int(result))
                else:
                    return f"{result:.6f}".rstrip('0').rstrip('.')
            else:
                return str(result)
                
        except Exception as e:
            logger.error(f"❌ Calculation error: {e}")
            return f"❌ Calculation failed: {e}"

    def analyze_image(self, image_path: str, question: str = "") -> str:
        """🖼️ Enhanced image analysis with multiple AI models"""
        if not PIL_AVAILABLE:
            return "❌ Image analysis unavailable. Install Pillow."
        
        try:
            logger.info(f"🖼️ Analyzing image: {image_path} | Question: {question}")
            
            # Get basic image info
            with Image.open(image_path) as img:
                basic_info = f"Image: {img.size[0]}x{img.size[1]} pixels, format: {img.format}, mode: {img.mode}"
                
                # Multi-model analysis
                analyses = []
                
                # 1. OpenAI GPT-4V (if available)
                if self.openai_key and question:
                    try:
                        with open(image_path, 'rb') as img_file:
                            img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
                        
                        client = openai.OpenAI(api_key=self.openai_key)
                        response = client.chat.completions.create(
                            model="gpt-4o",
                            messages=[
                                {
                                    "role": "user",
                                    "content": [
                                        {"type": "text", "text": f"Analyze this image and answer: {question}. Provide only the direct answer, no explanations."},
                                        {
                                            "type": "image_url",
                                            "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}
                                        }
                                    ]
                                }
                            ],
                            max_tokens=300
                        )
                        
                        gpt4v_result = response.choices[0].message.content.strip()
                        analyses.append(f"GPT-4V: {gpt4v_result}")
                        
                    except Exception as vision_error:
                        logger.warning(f"⚠️ GPT-4V analysis failed: {vision_error}")
                
                # 2. HuggingFace Vision Models (if available)
                if self.hf_token and 'vision' in self.clients:
                    try:
                        with open(image_path, 'rb') as img_file:
                            caption = self.clients['vision'].image_to_text(img_file.read())
                            if caption:
                                analyses.append(f"BLIP: {caption[0].get('generated_text', 'No caption')}")
                    except Exception as hf_error:
                        logger.warning(f"⚠️ HuggingFace vision analysis failed: {hf_error}")
                
                # 3. Object Detection
                if question and "object" in question.lower():
                    obj_result = self.detect_objects(image_path)
                    if not obj_result.startswith("❌"):
                        analyses.append(f"Objects: {obj_result}")
                
                # Combine all analyses
                if analyses:
                    combined_analysis = "; ".join(analyses)
                    return f"{basic_info}. Analysis: {combined_analysis}"
                else:
                    return f"{basic_info}. Advanced vision analysis requires API keys."
                
        except Exception as e:
            logger.error(f"❌ Image analysis error: {e}")
            return f"❌ Image analysis failed: {e}"

    # === ENHANCED DOCUMENT PROCESSING ===
    def read_docx(self, file_path: str) -> str:
        """📄 Read Microsoft Word documents"""
        try:
            import docx2txt
            text = docx2txt.process(file_path)
            logger.info(f"📄 DOCX read: {len(text)} characters")
            return text
        except ImportError:
            logger.warning("⚠️ docx2txt not available. Install python-docx.")
            return "❌ DOCX reading unavailable. Install python-docx."
        except Exception as e:
            logger.error(f"❌ DOCX reading error: {e}")
            return f"❌ DOCX reading failed: {e}"

    def read_excel(self, file_path: str, sheet_name: str = None) -> str:
        """📊 Read Excel spreadsheets"""
        try:
            import pandas as pd
            if sheet_name:
                df = pd.read_excel(file_path, sheet_name=sheet_name)
            else:
                df = pd.read_excel(file_path)
            
            # Convert to readable format
            result = f"Excel data ({df.shape[0]} rows, {df.shape[1]} columns):\n"
            result += df.to_string(max_rows=50, max_cols=10)
            
            logger.info(f"📊 Excel read: {df.shape}")
            return result
        except ImportError:
            logger.warning("⚠️ pandas not available for Excel reading.")
            return "❌ Excel reading unavailable. Install pandas and openpyxl."
        except Exception as e:
            logger.error(f"❌ Excel reading error: {e}")
            return f"❌ Excel reading failed: {e}"

    def read_csv(self, file_path: str) -> str:
        """📋 Read CSV files"""
        try:
            import pandas as pd
            df = pd.read_csv(file_path)
            
            # Convert to readable format
            result = f"CSV data ({df.shape[0]} rows, {df.shape[1]} columns):\n"
            result += df.head(20).to_string()
            
            if df.shape[0] > 20:
                result += f"\n... (showing first 20 of {df.shape[0]} rows)"
            
            logger.info(f"📋 CSV read: {df.shape}")
            return result
        except ImportError:
            logger.warning("⚠️ pandas not available for CSV reading.")
            return "❌ CSV reading unavailable. Install pandas."
        except Exception as e:
            logger.error(f"❌ CSV reading error: {e}")
            return f"❌ CSV reading failed: {e}"

    def read_text_file(self, file_path: str, encoding: str = 'utf-8') -> str:
        """📝 Read plain text files with encoding detection"""
        try:
            # Try UTF-8 first
            try:
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
            except UnicodeDecodeError:
                # Try other common encodings
                encodings = ['latin-1', 'cp1252', 'ascii']
                content = None
                for enc in encodings:
                    try:
                        with open(file_path, 'r', encoding=enc) as f:
                            content = f.read()
                        break
                    except UnicodeDecodeError:
                        continue
                
                if content is None:
                    return "❌ Unable to decode text file with common encodings"
            
            logger.info(f"📝 Text file read: {len(content)} characters")
            return content[:10000] + ("..." if len(content) > 10000 else "")
        except Exception as e:
            logger.error(f"❌ Text file reading error: {e}")
            return f"❌ Text file reading failed: {e}"

    def extract_archive(self, file_path: str) -> str:
        """📦 Extract and list archive contents (ZIP, RAR, etc.)"""
        try:
            import zipfile
            import os
            
            if file_path.endswith('.zip'):
                with zipfile.ZipFile(file_path, 'r') as zip_ref:
                    file_list = zip_ref.namelist()
                    extract_dir = os.path.join(os.path.dirname(file_path), 'extracted')
                    os.makedirs(extract_dir, exist_ok=True)
                    zip_ref.extractall(extract_dir)
                    
                    result = f"📦 ZIP archive extracted to {extract_dir}\n"
                    result += f"Contents ({len(file_list)} files):\n"
                    result += "\n".join(file_list[:20])
                    
                    if len(file_list) > 20:
                        result += f"\n... (showing first 20 of {len(file_list)} files)"
                    
                    logger.info(f"📦 ZIP extracted: {len(file_list)} files")
                    return result
            else:
                return f"❌ Unsupported archive format: {file_path}"
        except Exception as e:
            logger.error(f"❌ Archive extraction error: {e}")
            return f"❌ Archive extraction failed: {e}"

    # === ENHANCED WEB BROWSING ===
    def browse_with_js(self, url: str) -> str:
        """🌐 Enhanced web browsing with JavaScript support (when available)"""
        try:
            # Try playwright for dynamic content
            from playwright.sync_api import sync_playwright
            
            with sync_playwright() as p:
                browser = p.chromium.launch(headless=True)
                page = browser.new_page()
                page.goto(url, timeout=15000)
                page.wait_for_timeout(2000)  # Wait for JS to load
                content = page.content()
                browser.close()
                
                # Parse content
                from bs4 import BeautifulSoup
                soup = BeautifulSoup(content, 'html.parser')
                
                # Remove scripts and styles
                for script in soup(["script", "style"]):
                    script.decompose()
                
                text = soup.get_text()
                # Clean up whitespace
                lines = (line.strip() for line in text.splitlines())
                chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
                clean_text = ' '.join(chunk for chunk in chunks if chunk)
                
                logger.info(f"🌐 JS-enabled browsing: {url} - {len(clean_text)} chars")
                return clean_text[:5000] + ("..." if len(clean_text) > 5000 else "")
                
        except ImportError:
            logger.info("⚠️ Playwright not available, falling back to requests")
            return self.browse_url(url)
        except Exception as e:
            logger.warning(f"⚠️ JS browsing failed: {e}, falling back to basic")
            return self.browse_url(url)

    # === ENHANCED GAIA FILE HANDLING ===
    def download_gaia_file(self, task_id: str, file_name: str = None) -> str:
        """📥 Enhanced GAIA file download with comprehensive format support"""
        try:
            # GAIA API endpoint for file downloads
            api_base = "https://agents-course-unit4-scoring.hf.space"
            file_url = f"{api_base}/files/{task_id}"
            
            logger.info(f"📥 Downloading GAIA file for task: {task_id}")
            
            headers = {
                'User-Agent': 'GAIA-Agent/1.0 (Enhanced)',
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate',
            }
            
            response = requests.get(file_url, headers=headers, timeout=30, stream=True)
            
            if response.status_code == 200:
                # Determine file extension from headers or filename
                content_type = response.headers.get('content-type', '')
                content_disposition = response.headers.get('content-disposition', '')
                
                # Extract filename from Content-Disposition header
                if file_name:
                    filename = file_name
                elif 'filename=' in content_disposition:
                    filename = content_disposition.split('filename=')[1].strip('"\'')
                else:
                    # Guess extension from content type
                    extension_map = {
                        'image/jpeg': '.jpg',
                        'image/png': '.png',
                        'image/gif': '.gif',
                        'application/pdf': '.pdf',
                        'text/plain': '.txt',
                        'application/json': '.json',
                        'text/csv': '.csv',
                        'application/vnd.ms-excel': '.xlsx',
                        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
                        'application/msword': '.docx',
                        'video/mp4': '.mp4',
                        'audio/mpeg': '.mp3',
                        'audio/wav': '.wav',
                        'application/zip': '.zip',
                    }
                    extension = extension_map.get(content_type, '.tmp')
                    filename = f"gaia_file_{task_id}{extension}"
                
                # Save file
                import tempfile
                import os
                
                temp_dir = tempfile.gettempdir()
                filepath = os.path.join(temp_dir, filename)
                
                with open(filepath, 'wb') as f:
                    for chunk in response.iter_content(chunk_size=8192):
                        f.write(chunk)
                
                file_size = os.path.getsize(filepath)
                logger.info(f"📥 GAIA file downloaded: {filepath} ({file_size} bytes)")
                
                # Automatically process based on file type
                return self.process_downloaded_file(filepath, task_id)
                
            else:
                error_msg = f"❌ GAIA file download failed: HTTP {response.status_code}"
                logger.error(error_msg)
                return error_msg
                
        except Exception as e:
            error_msg = f"❌ GAIA file download error: {e}"
            logger.error(error_msg)
            return error_msg

    def process_downloaded_file(self, filepath: str, task_id: str) -> str:
        """📋 Process downloaded GAIA files based on their type"""
        try:
            import os
            filename = os.path.basename(filepath)
            file_ext = os.path.splitext(filename)[1].lower()
            
            logger.info(f"📋 Processing GAIA file: {filename} (type: {file_ext})")
            
            result = f"📁 GAIA File: {filename} (Task: {task_id})\n\n"
            
            # Process based on file type
            if file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']:
                # Image file
                image_result = self.analyze_image(filepath, "Describe this image in detail")
                result += f"🖼️ Image Analysis:\n{image_result}\n"
                
            elif file_ext == '.pdf':
                # PDF document
                pdf_content = self.read_pdf(filepath)
                result += f"📄 PDF Content:\n{pdf_content}\n"
                
            elif file_ext in ['.txt', '.md', '.py', '.js', '.html', '.css']:
                # Text files
                text_content = self.read_text_file(filepath)
                result += f"📝 Text Content:\n{text_content}\n"
                
            elif file_ext in ['.csv']:
                # CSV files
                csv_content = self.read_csv(filepath)
                result += f"📊 CSV Data:\n{csv_content}\n"
                
            elif file_ext in ['.xlsx', '.xls']:
                # Excel files
                excel_content = self.read_excel(filepath)
                result += f"📈 Excel Data:\n{excel_content}\n"
                
            elif file_ext in ['.docx']:
                # Word documents
                docx_content = self.read_docx(filepath)
                result += f"📄 Word Document:\n{docx_content}\n"
                
            elif file_ext in ['.mp4', '.avi', '.mov', '.wmv']:
                # Video files
                video_result = self.process_video(filepath, "analyze")
                result += f"🎥 Video Analysis:\n{video_result}\n"
                
            elif file_ext in ['.mp3', '.wav', '.m4a', '.flac']:
                # Audio files
                audio_result = self.analyze_audio(filepath, "transcribe")
                result += f"🎵 Audio Analysis:\n{audio_result}\n"
                
            elif file_ext in ['.zip', '.rar']:
                # Archive files
                archive_result = self.extract_archive(filepath)
                result += f"📦 Archive Contents:\n{archive_result}\n"
                
            elif file_ext in ['.json']:
                # JSON files
                try:
                    import json
                    with open(filepath, 'r') as f:
                        json_data = json.load(f)
                    result += f"📋 JSON Data:\n{json.dumps(json_data, indent=2)[:2000]}\n"
                except Exception as e:
                    result += f"❌ JSON parsing error: {e}\n"
                    
            else:
                # Unknown file type - try as text
                try:
                    text_content = self.read_text_file(filepath)
                    result += f"📄 Raw Content:\n{text_content}\n"
                except:
                    result += f"❌ Unsupported file type: {file_ext}\n"
            
            # Add file metadata
            file_size = os.path.getsize(filepath)
            result += f"\n📊 File Info: {file_size} bytes, Path: {filepath}"
            
            return result
            
        except Exception as e:
            error_msg = f"❌ File processing error: {e}"
            logger.error(error_msg)
            return error_msg

    # === ENHANCED REASONING CHAIN ===
    def reasoning_chain(self, question: str, max_steps: int = 5) -> str:
        """🧠 Explicit step-by-step reasoning for complex GAIA questions"""
        try:
            logger.info(f"🧠 Starting reasoning chain for: {question[:50]}...")
            
            reasoning_steps = []
            current_context = question
            
            for step in range(1, max_steps + 1):
                logger.info(f"🧠 Reasoning step {step}/{max_steps}")
                
                # Analyze what we need to do next
                analysis_prompt = f"""Analyze this question step by step:

Question: {question}

Previous context: {current_context}

What is the next logical step to solve this question? Be specific about:
1. What information do we need?
2. What tool should we use?
3. What specific action to take?

Respond with just the next action needed."""
                
                # Get next step from our best model
                next_step = self.fast_qa_answer(analysis_prompt)
                reasoning_steps.append(f"Step {step}: {next_step}")
                
                # Execute the step if it mentions a specific tool
                if any(tool in next_step.lower() for tool in ['search', 'download', 'calculate', 'analyze', 'read']):
                    # Extract and execute tool call
                    if 'search' in next_step.lower():
                        search_query = self._extract_search_query(next_step, question)
                        if search_query:
                            search_result = self.web_search(search_query)
                            current_context += f"\n\nSearch result: {search_result[:500]}"
                            reasoning_steps.append(f"  → Executed search: {search_result[:100]}...")
                    
                    elif 'calculate' in next_step.lower():
                        calc_expr = self._extract_calculation(next_step, question)
                        if calc_expr:
                            calc_result = self.calculator(calc_expr)
                            current_context += f"\n\nCalculation: {calc_expr} = {calc_result}"
                            reasoning_steps.append(f"  → Calculated: {calc_expr} = {calc_result}")
                    
                # Check if we have enough information
                if self._has_sufficient_info(current_context, question):
                    reasoning_steps.append(f"Step {step + 1}: Sufficient information gathered")
                    break
            
            # Generate final answer
            final_prompt = f"""Based on this reasoning chain, provide the final answer:

Question: {question}

Reasoning steps:
{chr(10).join(reasoning_steps)}

Context: {current_context}

Provide ONLY the final answer - no explanation."""
            
            final_answer = self.fast_qa_answer(final_prompt)
            
            logger.info(f"🧠 Reasoning chain complete: {len(reasoning_steps)} steps")
            return final_answer
            
        except Exception as e:
            logger.error(f"❌ Reasoning chain error: {e}")
            return self.query_with_tools(question)  # Fallback to regular processing

    def _extract_search_query(self, step_text: str, question: str) -> str:
        """Extract search query from reasoning step"""
        # Simple extraction logic
        if 'search for' in step_text.lower():
            parts = step_text.lower().split('search for')[1].split('.')[0]
            return parts.strip(' "\'')
        return None

    def _extract_calculation(self, step_text: str, question: str) -> str:
        """Extract calculation from reasoning step"""
        import re
        # Look for mathematical expressions
        math_patterns = [
            r'[\d+\-*/().\s]+',
            r'\d+\s*[+\-*/]\s*\d+',
        ]
        for pattern in math_patterns:
            matches = re.findall(pattern, step_text)
            if matches:
                return matches[0].strip()
        return None

    def _has_sufficient_info(self, context: str, question: str) -> bool:
        """Check if we have sufficient information to answer"""
        # Simple heuristic - check if context is substantially longer than question
        return len(context) > len(question) * 3 and len(context) > 200

    # === ENHANCED TOOL ENUMERATION ===

# === MAIN SYSTEM CLASSES ===

class EnhancedMultiModelGAIASystem:
    """🚀 Complete GAIA system with advanced tool calling and multi-modal capabilities"""
    
    def __init__(self, hf_token: str = None, openai_key: str = None):
        # Initialize enhanced toolkit
        self.toolkit = UniversalMultimodalToolkit(hf_token, openai_key)
        
        # Initialize AI clients
        self.hf_token = hf_token or os.getenv('HF_TOKEN')
        self.openai_key = openai_key or os.getenv('OPENAI_API_KEY')
        
        # 🚀 SPEED OPTIMIZATION: Response cache for instant answers
        self.response_cache = {}
        self.qa_cache = {}
        
        # Initialize clients with comprehensive model support
        self.clients = self._initialize_clients()
        # 🎯 PRIORITY ORDER: Qwen3-235B-A22B as TOP model for best performance
        available_models = list(self.clients.keys())
        
        # Preferred order (only include models that are actually available)
        preferred_order = [
            "fireworks_qwen3_235b",  # 🥇 PRIORITY 1: Qwen3-235B-A22B (Best reasoning)
            "together_deepseek_r1",   # 🥈 PRIORITY 2: DeepSeek-R1 (Strong reasoning)
            "openai_gpt4o",          # 🥉 PRIORITY 3: GPT-4o (Vision capabilities)
            "together_llama",        # PRIORITY 4: Llama-3.3-70B (Large context)
            "novita_minimax",        # PRIORITY 5: MiniMax (Extended context)
            "featherless_kimi",      # PRIORITY 6: Moonshot (Specialized tasks)
            "fallback_basic"         # PRIORITY 7: Local fallback
        ]
        
        # Only include available models in priority list
        self.model_priority = [model for model in preferred_order if model in available_models]
        
        if not self.model_priority:
            logger.error("❌ No models available for processing")
        else:
            logger.info(f"🎯 Model priority: {self.model_priority[0]} (top priority)")
        
        logger.info("🚀 Enhanced Multi-Model GAIA System initialized")
        
    def _initialize_clients(self) -> Dict[str, Any]:
        """Initialize all AI model clients with SPEED OPTIMIZATION for 100% GAIA performance"""
        clients = {}
        
        if self.hf_token and HF_AVAILABLE:
            # 🚀 ULTRA-FAST QA MODEL (Priority 0 - for instant answers)
            clients["ultra_fast_qa"] = {
                "client": InferenceClient(
                    provider="hf-inference",
                    api_key=self.hf_token,
                ),
                "model": "deepset/roberta-base-squad2",
                "priority": 0,
                "provider": "HuggingFace QA",
                "type": "question_answering",
                "speed": "ultra_fast",
                "use_for": ["factual", "simple", "direct"]
            }
            
            # ⚡ FAST BERT QA (Priority 0.5)
            clients["fast_bert_qa"] = {
                "client": InferenceClient(
                    provider="hf-inference",
                    api_key=self.hf_token,
                ),
                "model": "deepset/bert-base-cased-squad2",
                "priority": 0.5,
                "provider": "HuggingFace QA",
                "type": "question_answering", 
                "speed": "very_fast",
                "use_for": ["reading_comprehension", "context_based"]
            }
            
            # 🔥 Together AI models (Priority: DeepSeek-R1)
            clients["together_deepseek_r1"] = {
                "client": InferenceClient(model="deepseek-ai/DeepSeek-R1", token=self.hf_token),
                "priority": 1,
                "provider": "Together AI",
                "type": "chat",
                "speed": "fast"
            }
            clients["together_llama"] = {
                "client": InferenceClient(model="meta-llama/Llama-3.3-70B-Instruct", token=self.hf_token),
                "priority": 2,
                "provider": "Together AI",
                "type": "chat",
                "speed": "medium"
            }
            
            # 🌟 Novita AI models (Enhanced Speed)
            clients["novita_minimax"] = {
                "client": InferenceClient(model="MiniMax/MiniMax-M1-80k", token=self.hf_token),
                "priority": 3,
                "provider": "Novita AI",
                "type": "chat",
                "speed": "fast"
            }
            clients["novita_deepseek_chat"] = {
                "client": InferenceClient(model="deepseek-ai/deepseek-chat", token=self.hf_token),
                "priority": 4,
                "provider": "Novita AI",
                "type": "chat",
                "speed": "fast"
            }
            
            # 🪶 Featherless AI models 
            clients["featherless_kimi"] = {
                "client": InferenceClient(model="moonshot-ai/moonshot-v1-8k", token=self.hf_token),
                "priority": 5,
                "provider": "Featherless AI",
                "type": "chat",
                "speed": "medium"
            }
            clients["featherless_jan"] = {
                "client": InferenceClient(model="janhq/jan-nano", token=self.hf_token),
                "priority": 6,
                "provider": "Featherless AI", 
                "type": "chat",
                "speed": "very_fast"
            }
            
            # 🚀 Fireworks AI models - TOP PRIORITY MODEL
            clients["fireworks_qwen3_235b"] = {
                "client": InferenceClient(
                    provider="fireworks-ai",
                    api_key=self.hf_token,
                ),
                "model": "Qwen/Qwen3-235B-A22B",
                "priority": 0.1,  # 🥇 HIGHEST PRIORITY - Best reasoning model
                "provider": "Fireworks AI",
                "type": "chat",
                "speed": "fast"
            }
            clients["fireworks_llama"] = {
                "client": InferenceClient(model="accounts/fireworks/models/llama-v3p1-8b-instruct", token=self.hf_token),
                "priority": 7,
                "provider": "Fireworks AI",
                "type": "chat",
                "speed": "very_fast"
            }
            
            # 🤗 HuggingFace Inference models (Specialized)
            clients["hf_mistral"] = {
                "client": InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1", token=self.hf_token),
                "priority": 8,
                "provider": "HuggingFace",
                "type": "chat",
                "speed": "fast"
            }
            clients["hf_phi"] = {
                "client": InferenceClient(model="microsoft/Phi-3-mini-4k-instruct", token=self.hf_token),
                "priority": 9,
                "provider": "HuggingFace",
                "type": "chat",
                "speed": "ultra_fast"
            }
            
        # 🤖 OpenAI models (if API key available)
        if self.openai_key and OPENAI_AVAILABLE:
            clients["openai_gpt4o"] = {
                "client": "openai_gpt4o",
                "model": "gpt-4o",
                "priority": 1.5,
                "provider": "OpenAI",
                "type": "chat",
                "speed": "medium"
            }
            clients["openai_gpt35"] = {
                "client": "openai_gpt35",
                "model": "gpt-3.5-turbo",
                "priority": 10,
                "provider": "OpenAI",
                "type": "chat",
                "speed": "fast"
            }

        # 🛡️ Fallback client for when external services are unavailable
        if not clients:
            clients["fallback_basic"] = {
                "client": "fallback",
                "model": "basic",
                "priority": 999,
                "provider": "Local Fallback",
                "type": "fallback",
                "speed": "instant"
            }
            logger.warning("⚠️ No external AI services available, using fallback mode")
            
        logger.info(f"✅ Initialized {len(clients)} AI clients with speed optimization")
        return clients
    
    def parse_tool_calls(self, response: str) -> List[ToolCall]:
        """🔧 Parse advanced tool calls from AI response"""
        tool_calls = []
        
        # Enhanced patterns for tool calls
        patterns = [
            r'TOOL_CALL:\s*(\w+)\((.*?)\)',  # TOOL_CALL: web_search(query="...")
            r'<tool>(\w+)</tool>\s*<params>(.*?)</params>',  # XML-style
            r'```(\w+)\n(.*?)\n```',  # Code block style
        ]
        
        for pattern in patterns:
            matches = re.findall(pattern, response, re.DOTALL | re.IGNORECASE)
            for tool_name, params_str in matches:
                try:
                    params = self._parse_parameters(params_str)
                    tool_type = ToolType(tool_name.lower())
                    tool_calls.append(ToolCall(tool=tool_type, parameters=params))
                    logger.info(f"🔧 Parsed tool call: {tool_name} with params: {params}")
                except (ValueError, Exception) as e:
                    logger.warning(f"⚠️ Failed to parse tool call {tool_name}: {e}")
        
        return tool_calls
    
    def _parse_parameters(self, params_str: str) -> Dict[str, Any]:
        """Parse parameters from various formats"""
        params = {}
        if not params_str.strip():
            return params
            
        # Try JSON parsing first
        try:
            return json.loads(params_str)
        except:
            pass
            
        # Try key=value parsing
        param_matches = re.findall(r'(\w+)=(["\'])(.*?)\2', params_str)
        for param_name, quote, param_value in param_matches:
            params[param_name] = param_value
            
        # Try simple text for single parameter
        if not params and params_str.strip():
            # Remove quotes if present
            clean_param = params_str.strip().strip('"\'')
            params['query'] = clean_param  # Default to query parameter
            
        return params
    
    def execute_tool_call(self, tool_call: ToolCall) -> str:
        """⚡ Execute a single tool call with comprehensive error handling"""
        try:
            logger.info(f"⚡ Executing {tool_call.tool.value} with params: {tool_call.parameters}")
            
            if tool_call.tool == ToolType.WEB_SEARCH:
                query = tool_call.parameters.get('query', '')
                results = self.toolkit.web_search(query)
                return f"🔍 Web search results:\n{results}"
            
            elif tool_call.tool == ToolType.BROWSE_URL:
                url = tool_call.parameters.get('url', '')
                result = self.toolkit.browse_url(url)
                return result
            
            elif tool_call.tool == ToolType.DOWNLOAD_FILE:
                task_id = tool_call.parameters.get('task_id', '')
                url = tool_call.parameters.get('url', '')
                filepath = self.toolkit.download_file(url, task_id)
                return f"📥 Downloaded file to: {filepath}"
            
            elif tool_call.tool == ToolType.READ_PDF:
                file_path = tool_call.parameters.get('file_path', '')
                text = self.toolkit.read_pdf(file_path)
                return f"📄 PDF content:\n{text}"
                
            elif tool_call.tool == ToolType.ANALYZE_IMAGE:
                image_path = tool_call.parameters.get('image_path', '')
                question = tool_call.parameters.get('question', '')
                result = self.toolkit.analyze_image(image_path, question)
                return f"🖼️ Image analysis: {result}"
                
            elif tool_call.tool == ToolType.CALCULATOR:
                expression = tool_call.parameters.get('expression', '')
                result = self.toolkit.calculator(expression)
                return f"🧮 Calculation result: {result}"
            
            elif tool_call.tool == ToolType.PROCESS_VIDEO:
                video_path = tool_call.parameters.get('video_path', '')
                task = tool_call.parameters.get('task', 'analyze')
                result = self.toolkit.process_video(video_path, task)
                return f"🎥 Video analysis: {result}"
            
            elif tool_call.tool == ToolType.ANALYZE_AUDIO:
                audio_path = tool_call.parameters.get('audio_path', '')
                task = tool_call.parameters.get('task', 'analyze')
                result = self.toolkit.analyze_audio(audio_path, task)
                return f"🎵 Audio analysis: {result}"
            
            elif tool_call.tool == ToolType.GENERATE_IMAGE:
                prompt = tool_call.parameters.get('prompt', '')
                style = tool_call.parameters.get('style', 'realistic')
                result = self.toolkit.generate_image(prompt, style)
                return f"🎨 Image generation: {result}"
            
            elif tool_call.tool == ToolType.SYNTHESIZE_SPEECH:
                text = tool_call.parameters.get('text', '')
                voice = tool_call.parameters.get('voice', 'default')
                result = self.toolkit.synthesize_speech(text, voice)
                return f"🎙️ Speech synthesis: {result}"
            
            elif tool_call.tool == ToolType.CREATE_VISUALIZATION:
                data = tool_call.parameters.get('data', {})
                chart_type = tool_call.parameters.get('chart_type', 'bar')
                result = self.toolkit.create_visualization(data, chart_type)
                return f"📊 Data visualization: {result}"
            
            elif tool_call.tool == ToolType.ANALYZE_DATA:
                data = tool_call.parameters.get('data', {})
                operation = tool_call.parameters.get('operation', 'statistics')
                result = self.toolkit.scientific_compute(operation, data)
                return f"🧬 Scientific computation: {result}"
            
            elif tool_call.tool == ToolType.GENERATE_VIDEO:
                video_path = tool_call.parameters.get('video_path', '')
                result = self.toolkit.process_video(video_path, 'generate')
                return f"🎬 Video generation: {result}"
            
            elif tool_call.tool == ToolType.EXTRACT_AUDIO:
                audio_path = tool_call.parameters.get('audio_path', '')
                result = self.toolkit.analyze_audio(audio_path, 'extract')
                return f"🎵 Audio extraction: {result}"
            
            elif tool_call.tool == ToolType.TRANSCRIBE_SPEECH:
                audio_path = tool_call.parameters.get('audio_path', '')
                result = self.toolkit.transcribe_speech(audio_path)
                return f"🎙️ Speech transcription: {result}"
            
            elif tool_call.tool == ToolType.DETECT_OBJECTS:
                image_path = tool_call.parameters.get('image_path', '')
                result = self.toolkit.detect_objects(image_path)
                return f"🔍 Object detection: {result}"
            
            elif tool_call.tool == ToolType.FACE_RECOGNITION:
                image_path = tool_call.parameters.get('image_path', '')
                result = self.toolkit.analyze_image(image_path, "Identify the person in this image")
                return f"👤 Face recognition: {result}"
            
            elif tool_call.tool == ToolType.SCIENTIFIC_COMPUTE:
                operation = tool_call.parameters.get('operation', 'statistics')
                data = tool_call.parameters.get('data', {})
                result = self.toolkit.scientific_compute(operation, data)
                return f"🧬 Scientific computation: {result}"
            
            else:
                return f"❌ Unknown tool: {tool_call.tool}"
                
        except Exception as e:
            error_msg = f"❌ Tool execution failed: {str(e)}"
            logger.error(error_msg)
            return error_msg
    
    def fast_qa_answer(self, question: str, context: str = "") -> str:
        """🚀 Ultra-fast question answering using optimized models"""
        try:
            # Check cache first
            cache_key = hashlib.md5(f"{question}:{context}".encode()).hexdigest()
            if cache_key in self.qa_cache:
                logger.info("🚀 Cache hit - instant answer!")
                return self.qa_cache[cache_key]
            
            # Try ultra-fast QA model first
            if "ultra_fast_qa" in self.clients:
                try:
                    client_info = self.clients["ultra_fast_qa"]
                    client = client_info["client"]
                    
                    # Use question-answering endpoint with correct model parameter
                    if context:
                        result = client.question_answering(
                            question=question, 
                            context=context,
                            model=client_info["model"]
                        )
                        answer = result.get("answer", "").strip()
                    else:
                        # For questions without context, use web search for context
                        search_result = self.toolkit.web_search(question, num_results=2)
                        result = client.question_answering(
                            question=question, 
                            context=search_result[:500],
                            model=client_info["model"]
                        )
                        answer = result.get("answer", "").strip()
                    
                    if answer:
                        # Cache the result
                        self.qa_cache[cache_key] = answer
                        return answer
                        
                except Exception as e:
                    logger.warning(f"⚠️ Fast QA failed: {e}")
            
            # Fallback to regular processing
            return None
            
        except Exception as e:
            logger.error(f"❌ Fast QA error: {e}")
            return None

    def query_with_tools(self, question: str, model_name: str = None, max_iterations: int = 3) -> str:
        """🧠 Enhanced query processing with SPEED-OPTIMIZED capabilities for 100% GAIA performance"""
        
        # 🚀 FIRST: Try ultra-fast QA for instant answers  
        fast_answer = self.fast_qa_answer(question)
        if fast_answer:
            logger.info("⚡ Ultra-fast QA answer found!")
            return self._clean_final_answer(fast_answer)
        
        # Check response cache
        cache_key = hashlib.md5(question.encode()).hexdigest()
        if cache_key in self.response_cache:
            logger.info("🚀 Cache hit - instant answer!")
            return self.response_cache[cache_key]
            
        if not model_name:
            model_name = self.model_priority[0]
        
        logger.info(f"🧠 Processing question with {model_name}: {question[:100]}...")
        
        # Ultra-enhanced system prompt for GAIA benchmark
        system_prompt = f"""You are an advanced AI agent optimized for the GAIA benchmark with access to powerful tools.

🛠️ AVAILABLE TOOLS:
- TOOL_CALL: web_search(query="search term") - Search the web for current information
- TOOL_CALL: browse_url(url="https://example.com") - Browse and extract content from specific URLs
- TOOL_CALL: download_file(task_id="123") - Download files from GAIA tasks or URLs
- TOOL_CALL: read_pdf(file_path="document.pdf") - Read and extract text from PDF files
- TOOL_CALL: analyze_image(image_path="image.jpg", question="what to analyze") - Analyze images with vision AI
- TOOL_CALL: calculator(expression="2+2*3") - Perform mathematical calculations and scientific functions
- TOOL_CALL: process_video(video_path="video.mp4", task="analyze") - Analyze video content
- TOOL_CALL: analyze_audio(audio_path="audio.wav", task="analyze") - Analyze audio content
- TOOL_CALL: generate_image(prompt="description", style="realistic") - Generate images from text descriptions
- TOOL_CALL: synthesize_speech(text="Hello, world!", voice="default") - Convert text to speech
- TOOL_CALL: create_visualization(data="chart_data", chart_type="bar") - Create data visualizations and charts
- TOOL_CALL: analyze_data(data="statistical_data") - Perform scientific computations and analysis
- TOOL_CALL: generate_video(video_path="output.mp4") - Generate videos from video content
- TOOL_CALL: extract_audio(audio_path="audio.wav") - Extract audio from video content
- TOOL_CALL: transcribe_speech(audio_path="audio.wav") - Convert speech to text
- TOOL_CALL: detect_objects(image_path="image.jpg") - Detect and identify objects in images
- TOOL_CALL: face_recognition(image_path="image.jpg") - Identify the person in images
- TOOL_CALL: scientific_compute(operation="statistics", data="numerical_data") - Perform scientific computations and analysis

🎯 GAIA BENCHMARK INSTRUCTIONS:
1. For research questions, ALWAYS use web_search first to get current information
2. If files are mentioned or task IDs given, use download_file then read_pdf/analyze_image
3. For multi-step problems, break down systematically and use tools in logical order
4. For image questions, use analyze_image with specific question about what to find
5. CRITICAL: Provide DIRECT, CONCISE answers ONLY - no explanations or reasoning
6. Format response as just the final answer - nothing else

Question: {question}

Think step by step about what tools you need, use them, then provide ONLY the final answer."""

        conversation_history = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": question}
        ]
        
        # Iterative tool calling loop
        for iteration in range(max_iterations):
            try:
                client_info = self.clients.get(model_name)
                if not client_info:
                    logger.warning(f"⚠️ Model {model_name} unavailable, using fallback")
                    return self._fallback_response(question)
                
                # Handle fallback client
                if model_name == "fallback_basic":
                    logger.info("🛡️ Using local fallback processing")
                    return self._fallback_response(question)
                
                # Get AI response
                if "openai" in model_name:
                    response = client_info["client"].chat.completions.create(
                        model=client_info["model"],
                        messages=conversation_history,
                        max_tokens=1500,
                        temperature=0.0
                    )
                    ai_response = response.choices[0].message.content
                elif model_name == "fireworks_qwen3_235b":
                    # Use the specific Qwen model implementation
                    response = client_info["client"].chat.completions.create(
                        model=client_info["model"],
                        messages=conversation_history,
                        max_tokens=1500,
                        temperature=0.0
                    )
                    ai_response = response.choices[0].message.content
                else:
                    response = client_info["client"].chat_completion(
                        messages=conversation_history,
                        max_tokens=1500,
                        temperature=0.0
                    )
                    ai_response = response.choices[0].message.content
                
                # Clean thinking process from response (critical for GAIA compliance)
                ai_response = self._remove_thinking_process(ai_response)
                
                logger.info(f"🤖 AI Response (iteration {iteration + 1}): {ai_response[:200]}...")
                
                # Check for tool calls
                tool_calls = self.parse_tool_calls(ai_response)
                
                if tool_calls:
                    # Execute tools and collect results
                    tool_results = []
                    for tool_call in tool_calls:
                        result = self.execute_tool_call(tool_call)
                        tool_results.append(f"Tool {tool_call.tool.value}: {result}")
                    
                    # Add tool results to conversation
                    conversation_history.append({"role": "assistant", "content": ai_response})
                    
                    tool_context = f"TOOL RESULTS:\n" + "\n\n".join(tool_results)
                    tool_context += f"\n\nBased on these tool results, provide the final answer to: {question}\nProvide ONLY the direct answer - no explanations:"
                    
                    conversation_history.append({"role": "user", "content": tool_context})
                    
                    logger.info(f"🔧 Executed {len(tool_calls)} tools, continuing to iteration {iteration + 2}")
                    
                else:
                    # No tools needed, extract final answer
                    final_answer = self._extract_final_answer(ai_response)
                    logger.info(f"✅ Final answer extracted: {final_answer}")
                    return final_answer
                    
            except Exception as e:
                logger.error(f"❌ Query iteration {iteration + 1} failed for {model_name}: {e}")
                
                # Try next model in priority list
                current_index = self.model_priority.index(model_name) if model_name in self.model_priority else 0
                if current_index + 1 < len(self.model_priority):
                    model_name = self.model_priority[current_index + 1]
                    logger.info(f"🔄 Switching to model: {model_name}")
                else:
                    break
        
        # Final attempt with tool results if we have them
        if len(conversation_history) > 2:
            try:
                client_info = self.clients.get(model_name)
                if client_info:
                    if "openai" in model_name:
                        final_response = client_info["client"].chat.completions.create(
                            model=client_info["model"],
                            messages=conversation_history,
                            max_tokens=300,
                            temperature=0.0
                        )
                        final_answer = final_response.choices[0].message.content
                    else:
                        final_response = client_info["client"].chat_completion(
                            messages=conversation_history,
                            max_tokens=300,
                            temperature=0.0
                        )
                        final_answer = final_response.choices[0].message.content
                    
                    return self._extract_final_answer(final_answer)
            except Exception as e:
                logger.error(f"❌ Final answer extraction failed: {e}")
        
        # Ultimate fallback
        logger.warning(f"⚠️ Using fallback response for: {question}")
        return self._fallback_response(question)
    
    def _extract_final_answer(self, response: str) -> str:
        """✨ Ultra-aggressive answer extraction for perfect GAIA compliance"""
        if not response:
            return "Unknown"
        
        logger.info(f"✨ Extracting final answer from: {response[:100]}...")
        
        # Remove tool calls completely
        response = re.sub(r'TOOL_CALL:.*?\n', '', response, flags=re.DOTALL)
        response = re.sub(r'<tool>.*?</tool>', '', response, flags=re.DOTALL | re.IGNORECASE)
        response = re.sub(r'<params>.*?</params>', '', response, flags=re.DOTALL | re.IGNORECASE)
        
        # Remove thinking blocks aggressively
        response = re.sub(r'<think>.*?</think>', '', response, flags=re.DOTALL | re.IGNORECASE)
        response = re.sub(r'\*\*Think\*\*.*?\*\*Answer\*\*', '', response, flags=re.DOTALL | re.IGNORECASE)
        
        # Remove reasoning phrases more comprehensively
        reasoning_patterns = [
            r'let me.*?[.!?]\s*',
            r'i need to.*?[.!?]\s*',
            r'first,?\s*i.*?[.!?]\s*',
            r'to solve this.*?[.!?]\s*',
            r'based on.*?[,.]?\s*',
            r'the answer is[:\s]*',
            r'therefore[,:\s]*',
            r'so[,:\s]*the answer[,:\s]*',
            r'thus[,:\s]*',
            r'in conclusion[,:\s]*',
            r'after.*?analysis[,:\s]*',
            r'from.*?search[,:\s]*'
        ]
        
        for pattern in reasoning_patterns:
            response = re.sub(pattern, '', response, flags=re.IGNORECASE)
        
        # Extract core answer patterns
        answer_patterns = [
            r'(?:answer|result)[:\s]*([^\n.!?]+)',
            r'(?:final|conclusion)[:\s]*([^\n.!?]+)',
            r'^([A-Z][^.!?]*)',  # First capitalized sentence
            r'(\d+(?:\.\d+)?)',   # Numbers
            r'([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)'  # Proper nouns
        ]
        
        for pattern in answer_patterns:
            match = re.search(pattern, response, re.IGNORECASE)
            if match:
                answer = match.group(1).strip()
                if len(answer) > 2:  # Avoid single characters
                    return self._clean_final_answer(answer)
        
        # Take the last substantial line
        lines = [line.strip() for line in response.split('\n') if line.strip()]
        if lines:
            # Filter out obvious non-answers
            for line in reversed(lines):
                if len(line) > 2 and not any(word in line.lower() for word in ['tool', 'search', 'analysis', 'extract']):
                    return self._clean_final_answer(line)
        
        # Final cleanup of the entire response
        return self._clean_final_answer(response.strip())
    
    def _remove_thinking_process(self, response: str) -> str:
        """🧠 Remove thinking process from responses to ensure only final answers"""
        try:
            # Remove common thinking indicators
            thinking_patterns = [
                r'<thinking>.*?</thinking>',
                r'<reasoning>.*?</reasoning>',
                r'<analysis>.*?</analysis>',
                r'Let me think.*?(?=\n\n|\.|$)',
                r'I need to.*?(?=\n\n|\.|$)',
                r'First, I.*?(?=\n\n|\.|$)',
                r'Step \d+:.*?(?=\n|\.|$)',
                r'Thinking step by step.*?(?=\n\n|\.|$)',
                r'^.*?Let me analyze.*?(?=\n\n)',
                r'^.*?I should.*?(?=\n\n)',
                r'To solve this.*?(?=\n\n)',
            ]
            
            cleaned = response
            for pattern in thinking_patterns:
                cleaned = re.sub(pattern, '', cleaned, flags=re.DOTALL | re.IGNORECASE)
            
            # Remove multiple newlines and clean up
            cleaned = re.sub(r'\n\s*\n', '\n', cleaned).strip()
            
            # If response starts with reasoning words, extract the final answer
            if any(cleaned.lower().startswith(word) for word in ['let me', 'first', 'i need to', 'to solve', 'thinking']):
                # Look for final answer patterns
                final_patterns = [
                    r'(?:the answer is|answer:|final answer:|therefore|so|thus|hence)[:\s]*(.+?)(?:\.|$)',
                    r'(?:^|\n)([^.\n]+?)(?:\.|$)'  # Last sentence
                ]
                
                for pattern in final_patterns:
                    match = re.search(pattern, cleaned, re.IGNORECASE | re.MULTILINE)
                    if match:
                        potential_answer = match.group(1).strip()
                        if potential_answer and len(potential_answer) < 200:  # Reasonable answer length
                            return potential_answer
            
            return cleaned
            
        except Exception as e:
            logger.warning(f"⚠️ Error removing thinking process: {e}")
            return response

    def _clean_final_answer(self, answer: str) -> str:
        """🧹 Enhanced answer cleaning that preserves meaning and completeness"""
        if not answer:
            return "Unable to determine answer"
            
        # Quality validation - reject broken/incomplete responses
        answer = answer.strip()
        
        # Reject clearly broken responses but allow valid short answers
        broken_patterns = [
            r'^s,?\s*$',      # Just "s," or "s"
            r'^s\s+\w+$',     # "s something"
            r'^(think|right|Unable to)$',  # Single incomplete words
            r'^Jagged$',      # Random single words
        ]
        
        # Don't reject numbers or valid single words
        if answer.isdigit() or answer.replace('.', '').replace('-', '').isdigit():
            # Valid number - keep it
            pass
        elif len(answer) == 1 and answer.isalpha():
            # Single letter might be valid (like "A", "B" for multiple choice)
            pass
        else:
            # Apply broken pattern checks for other cases
            for pattern in broken_patterns:
                if re.match(pattern, answer, re.IGNORECASE):
                    return "Unable to provide complete answer"
        
        # Remove common prefixes but preserve content
        prefixes = ['answer:', 'result:', 'final:', 'conclusion:', 'the answer is', 'it is', 'this is']
        for prefix in prefixes:
            if answer.lower().startswith(prefix):
                answer = answer[len(prefix):].strip()
        
        # Remove tool call artifacts
        answer = re.sub(r'^TOOL_CALL:.*$', '', answer, flags=re.MULTILINE)
        answer = re.sub(r'from \d+ tool calls?', '', answer)
        
        # Clean whitespace but preserve structure
        answer = re.sub(r'\s+', ' ', answer).strip()
        
        # Remove quotes if they wrap the entire answer
        if (answer.startswith('"') and answer.endswith('"')) or (answer.startswith("'") and answer.endswith("'")):
            answer = answer[1:-1]
        
        # Final validation - but allow valid single character answers
        if len(answer) < 1:
            return "Unable to provide complete answer"
        elif len(answer) == 1:
            # Single character is OK if it's a digit or capital letter
            if answer.isdigit() or answer.isupper():
                return answer.strip()
            else:
                return "Unable to provide complete answer"
            
        return answer.strip()
    
    def _fallback_response(self, question: str) -> str:
        """🛡️ Enhanced fallback responses optimized for GAIA benchmark"""
        question_lower = question.lower()
        logger.info(f"🛡️ Using enhanced fallback for: {question[:50]}...")
        
        # Enhanced mathematical operations
        if any(word in question_lower for word in ['calculate', 'compute', 'math', '+', '-', '*', '/', 'sum', 'product']):
            numbers = re.findall(r'-?\d+(?:\.\d+)?', question)
            if len(numbers) >= 2:
                try:
                    a, b = float(numbers[0]), float(numbers[1])
                    if '+' in question or 'add' in question_lower or 'sum' in question_lower:
                        return str(int(a + b) if (a + b).is_integer() else a + b)
                    elif '-' in question or 'subtract' in question_lower or 'minus' in question_lower:
                        return str(int(a - b) if (a - b).is_integer() else a - b)
                    elif '*' in question or 'multiply' in question_lower or 'times' in question_lower or 'product' in question_lower:
                        return str(int(a * b) if (a * b).is_integer() else a * b)
                    elif '/' in question or 'divide' in question_lower:
                        return str(int(a / b) if (a / b).is_integer() else round(a / b, 6))
                except:
                    pass
        
        # Enhanced geography and capitals
        if any(word in question_lower for word in ['capital', 'country', 'city']):
            capitals = {
                'france': 'Paris', 'germany': 'Berlin', 'italy': 'Rome', 'spain': 'Madrid',
                'japan': 'Tokyo', 'china': 'Beijing', 'usa': 'Washington D.C.', 'united states': 'Washington D.C.',
                'uk': 'London', 'united kingdom': 'London', 'canada': 'Ottawa', 'australia': 'Canberra',
                'brazil': 'Brasília', 'india': 'New Delhi', 'russia': 'Moscow', 'mexico': 'Mexico City'
            }
            for country, capital in capitals.items():
                if country in question_lower:
                    return capital
        
        # Enhanced political and current affairs
        if 'president' in question_lower:
            if any(country in question_lower for country in ['united states', 'usa', 'america']):
                return 'Joe Biden'
            elif 'france' in question_lower:
                return 'Emmanuel Macron'
            elif 'russia' in question_lower:
                return 'Vladimir Putin'
        
        # Enhanced counting questions
        if 'how many' in question_lower:
            counting_map = {
                'planets': '8', 'continents': '7', 'days in year': '365', 'days in week': '7',
                'months': '12', 'seasons': '4', 'oceans': '5', 'great lakes': '5'
            }
            for item, count in counting_map.items():
                if item in question_lower:
                    return count
        
        # Enhanced scientific formulas
        if 'chemical formula' in question_lower or 'formula' in question_lower:
            formulas = {
                'water': 'H2O', 'carbon dioxide': 'CO2', 'methane': 'CH4', 'ammonia': 'NH3',
                'salt': 'NaCl', 'sugar': 'C12H22O11', 'alcohol': 'C2H5OH', 'oxygen': 'O2'
            }
            for compound, formula in formulas.items():
                if compound in question_lower:
                    return formula
        
        # Enhanced units and conversions
        if any(word in question_lower for word in ['meter', 'kilogram', 'second', 'celsius', 'fahrenheit']):
            if 'freezing point' in question_lower and 'water' in question_lower:
                if 'celsius' in question_lower:
                    return '0'
                elif 'fahrenheit' in question_lower:
                    return '32'
        
        # Enhanced colors and basic facts
        if 'color' in question_lower or 'colour' in question_lower:
            if 'sun' in question_lower:
                return 'yellow'
            elif 'grass' in question_lower:
                return 'green'
            elif 'sky' in question_lower:
                return 'blue'
        
        # GAIA-specific fallback for research questions
        if any(word in question_lower for word in ['when', 'where', 'who', 'what', 'which', 'how']):
            return "Information not available without web search"
        
        # Default fallback with instruction
        return "Unable to determine answer without additional tools"
    
    def cleanup(self):
        """🧹 Cleanup temporary resources"""
        pass

# Backward compatibility aliases
class MultiModelGAIASystem(EnhancedMultiModelGAIASystem):
    """Alias for backward compatibility"""
    pass

def create_gaia_system(hf_token: str = None, openai_key: str = None) -> EnhancedMultiModelGAIASystem:
    """🚀 Create an enhanced GAIA system with all advanced capabilities"""
    return EnhancedMultiModelGAIASystem(hf_token=hf_token, openai_key=openai_key)

class BasicAgent:
    """🤖 GAIA-compatible agent interface with comprehensive tool calling"""
    
    def __init__(self, hf_token: str = None, openai_key: str = None):
        self.system = create_gaia_system(hf_token, openai_key)
        logger.info("🤖 BasicAgent with enhanced GAIA capabilities initialized")
    
    def query(self, question: str) -> str:
        """Process GAIA question with full tool calling support"""
        try:
            result = self.system.query_with_tools(question)
            return result
        except Exception as e:
            logger.error(f"❌ Agent query failed: {e}")
            return self.system._fallback_response(question)
    
    def clean_for_api_submission(self, response: str) -> str:
        """Clean response for GAIA API submission"""
        return self.system._extract_final_answer(response)
    
    def __call__(self, question: str) -> str:
        """Callable interface for backward compatibility"""
        return self.query(question)
    
    def cleanup(self):
        """Cleanup resources"""
        self.system.cleanup()

# Test function for comprehensive validation
def test_enhanced_gaia_system():
    """🧪 Test the enhanced GAIA system with tool calling"""
    print("🧪 Testing Enhanced GAIA System with Tool Calling")
    
    # Initialize the system
    agent = BasicAgent()
    
    # Test questions requiring different tools
    test_questions = [
        "What is 15 + 27?",  # Calculator
        "What is the capital of France?",  # Fallback knowledge
        "Search for the current weather in Paris",  # Web search
        "How many planets are in our solar system?",  # Fallback knowledge
        "What is 2 * 3 + 4?",  # Calculator
    ]
    
    print("\n" + "="*50)
    print("🎯 ENHANCED GAIA COMPLIANCE TEST")
    print("="*50)
    
    for question in test_questions:
        print(f"\nQ: {question}")
        response = agent.query(question)
        print(f"A: {response}")  # Should be clean, direct answers with tool usage
    
    # Cleanup
    agent.cleanup()
    print("\n✅ Enhanced GAIA system test complete!")

if __name__ == "__main__":
    test_enhanced_gaia_system()