File size: 65,250 Bytes
1535ec7
 
f67f570
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d793fdd
f67f570
d793fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d793fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
d793fdd
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
f67f570
1535ec7
 
 
 
 
 
 
 
 
f67f570
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
f67f570
1535ec7
f67f570
1535ec7
f67f570
 
1535ec7
f67f570
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
 
f67f570
 
 
d793fdd
f67f570
d793fdd
 
 
 
 
 
f67f570
 
 
 
d793fdd
 
 
f67f570
d793fdd
f67f570
d793fdd
f67f570
d793fdd
f67f570
d793fdd
f67f570
d793fdd
f67f570
d793fdd
 
 
 
f67f570
d793fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
1535ec7
f67f570
 
 
7aad614
 
 
 
 
f67f570
7aad614
 
f67f570
 
 
7aad614
 
f67f570
 
 
 
 
 
 
 
 
 
7aad614
f67f570
 
7aad614
 
f67f570
1535ec7
f67f570
7aad614
f67f570
 
 
 
 
 
 
 
 
 
7aad614
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
1535ec7
f67f570
 
 
7aad614
f67f570
 
 
 
 
 
 
1535ec7
7aad614
1535ec7
 
 
f67f570
 
1535ec7
 
f67f570
 
1535ec7
 
 
 
 
 
7aad614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d793fdd
1535ec7
d793fdd
 
 
 
 
 
1535ec7
f67f570
1535ec7
f67f570
 
1535ec7
d793fdd
 
 
 
 
 
1535ec7
f67f570
d793fdd
f67f570
 
d793fdd
 
 
f67f570
d793fdd
 
f67f570
d793fdd
 
 
f67f570
 
d793fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
d793fdd
f67f570
 
 
 
1535ec7
d793fdd
 
 
 
 
1535ec7
 
 
 
f67f570
1535ec7
 
d793fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
 
 
f67f570
 
 
1535ec7
 
 
 
 
 
 
 
f67f570
 
 
 
 
1535ec7
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
 
 
f67f570
1535ec7
f67f570
 
 
1535ec7
f67f570
1535ec7
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
f67f570
1535ec7
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
1535ec7
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
f67f570
1535ec7
f67f570
 
1535ec7
 
 
 
f67f570
 
1535ec7
f67f570
 
1535ec7
f67f570
 
 
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
1535ec7
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
 
 
 
 
 
 
 
 
 
 
 
f67f570
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
 
 
f67f570
 
 
 
 
 
 
1535ec7
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
 
 
f67f570
1535ec7
 
 
 
 
 
 
 
 
7aad614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535ec7
7aad614
1535ec7
 
7aad614
 
 
 
 
 
 
f67f570
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
#!/usr/bin/env python3
"""

Enhanced Production-Ready Mamba Encoder Swarm Demo

Integrates pretrained Mamba weights from HuggingFace with swarm architecture

"""

import gradio as gr
import torch
import numpy as np
import time
import json
import logging
import os
import psutil
from typing import Optional, Dict, Any, Tuple
from datetime import datetime
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
from huggingface_hub import snapshot_download, hf_hub_download

# Setup comprehensive logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('mamba_swarm_demo.log'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

class MambaWeightLoader:
    """Dynamic loader for pretrained Mamba weights"""
    
    def __init__(self, model_name="state-spaces/mamba-130m"):
        self.model_name = model_name
        self.cache_dir = "/tmp/mamba_cache" if os.path.exists("/tmp") else "./mamba_cache"
        self.model = None
        self.tokenizer = None
        self.config = None
    
    def download_and_load(self):
        """Download and load Mamba weights in HuggingFace Spaces"""
        try:
            logger.info(f"πŸ”„ Loading pretrained model: {self.model_name}")
            
            # Create cache directory
            os.makedirs(self.cache_dir, exist_ok=True)
            
            # Load tokenizer with better error handling
            logger.info("πŸ“ Loading tokenizer...")
            try:
                # Try loading the specific tokenizer first
                self.tokenizer = AutoTokenizer.from_pretrained(
                    self.model_name,
                    cache_dir=self.cache_dir,
                    trust_remote_code=True,
                    use_fast=False  # Use slow tokenizer to avoid conversion issues
                )
            except Exception as tokenizer_error:
                logger.warning(f"Primary tokenizer loading failed: {tokenizer_error}")
                # Fallback to GPT2 tokenizer which is compatible with most models
                logger.info("Using GPT2 tokenizer as fallback...")
                from transformers import GPT2Tokenizer
                self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
            
            # Handle tokenizer padding
            if self.tokenizer.pad_token is None:
                if self.tokenizer.eos_token is not None:
                    self.tokenizer.pad_token = self.tokenizer.eos_token
                else:
                    self.tokenizer.add_special_tokens({'pad_token': '[PAD]'})
            
            # Load configuration
            logger.info("βš™οΈ Loading model configuration...")
            self.config = AutoConfig.from_pretrained(
                self.model_name,
                cache_dir=self.cache_dir,
                trust_remote_code=True
            )
            
            # Load model with optimizations for Spaces
            logger.info("🧠 Loading model weights...")
            
            # Determine optimal dtype and device settings
            device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
            dtype = torch.float16 if device.type == "cuda" else torch.float32
            
            try:
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.model_name,
                    config=self.config,
                    cache_dir=self.cache_dir,
                    trust_remote_code=True,
                    torch_dtype=dtype,
                    device_map="auto" if torch.cuda.is_available() else None,
                    low_cpu_mem_usage=True
                )
            except Exception as model_error:
                logger.error(f"Model loading failed: {model_error}")
                # Try with basic settings
                logger.info("Retrying with basic model loading settings...")
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.model_name,
                    trust_remote_code=True,
                    torch_dtype=dtype
                )
            
            # Move to device if not using device_map
            if not torch.cuda.is_available() or not hasattr(self.model, 'hf_device_map'):
                self.model.to(device)
            
            self.model.eval()
            
            # Log model info
            num_params = sum(p.numel() for p in self.model.parameters())
            logger.info(f"βœ… Model loaded successfully!")
            logger.info(f"πŸ“Š Parameters: {num_params:,} ({num_params/1e6:.1f}M)")
            logger.info(f"πŸ”§ Device: {device}, dtype: {dtype}")
            
            return True
            
        except Exception as e:
            logger.error(f"❌ Error loading pretrained model: {e}")
            return False
    
    def get_model_info(self):
        """Get model information"""
        if self.model:
            try:
                num_params = sum(p.numel() for p in self.model.parameters())
                device = next(self.model.parameters()).device
                dtype = next(self.model.parameters()).dtype
                
                return {
                    "name": self.model_name,
                    "parameters": f"{num_params:,}",
                    "parameters_millions": f"{num_params/1e6:.1f}M",
                    "device": str(device),
                    "dtype": str(dtype),
                    "vocab_size": getattr(self.config, 'vocab_size', 'Unknown'),
                    "hidden_size": getattr(self.config, 'd_model', getattr(self.config, 'hidden_size', 'Unknown'))
                }
            except Exception as e:
                logger.error(f"Error getting model info: {e}")
                return {"error": str(e)}
        return None

class MambaSwarmDemo:
    """Enhanced Production-ready Mamba Swarm Demo with dynamic pretrained weight loading"""
    
    def __init__(self, model_path: str = "./", fallback_mode: bool = False):
        self.model = None
        self.tokenizer = None
        self.config = None
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model_path = model_path
        self.fallback_mode = fallback_mode
        self.model_loaded = False
        self.pretrained_loader = None
        self.using_pretrained = False
        
        # Performance tracking
        self.stats = {
            'total_requests': 0,
            'successful_generations': 0,
            'failed_generations': 0,
            'avg_generation_time': 0.0,
            'total_tokens_generated': 0
        }
        
        # Domain mappings for intelligent routing
        self.domain_keywords = {
            'medical': ['medical', 'health', 'doctor', 'patient', 'disease', 'treatment', 'symptom', 'diagnosis'],
            'legal': ['legal', 'law', 'court', 'judge', 'contract', 'patent', 'lawsuit', 'attorney'],
            'code': ['code', 'python', 'programming', 'function', 'algorithm', 'software', 'debug', 'api'],
            'science': ['science', 'research', 'experiment', 'theory', 'physics', 'chemistry', 'biology'],
            'creative': ['story', 'creative', 'write', 'novel', 'poem', 'character', 'plot', 'narrative'],
            'business': ['business', 'marketing', 'strategy', 'finance', 'management', 'sales', 'revenue'],
            'general': ['explain', 'what', 'how', 'why', 'describe', 'tell', 'information']
        }
        
        self._initialize_model()
        logger.info(f"Demo initialized - Model loaded: {self.model_loaded}, Using pretrained: {self.using_pretrained}, Fallback mode: {self.fallback_mode}")
    
    def _initialize_model(self):
        """Initialize model with pretrained weights or fallback"""
        try:
            logger.info("πŸš€ Attempting to load model with priority: Pretrained -> Custom -> Fallback")
            
            # Try to load pretrained model first (highest priority)
            success = self._load_pretrained_model()
            
            if not success:
                logger.info("Pretrained loading failed, trying custom swarm model...")
                success = self._load_custom_swarm_model()
            
            if not success:
                logger.info("All model loading attempts failed, enabling fallback mode")
                self.fallback_mode = True
                self._initialize_fallback_mode()
                
        except Exception as e:
            logger.error(f"Model initialization failed: {e}")
            logger.info("Falling back to simulation mode")
            self.fallback_mode = True
            self._initialize_fallback_mode()
    
    def _load_pretrained_model(self):
        """Load pretrained Mamba model from HuggingFace with automatic model selection"""
        try:
            # Choose model based on available resources - using more compatible models
            MODEL_OPTIONS = {
                "small": "gpt2",                         # Known working model for testing
                "medium": "microsoft/DialoGPT-medium",   # Alternative medium model
                "mamba-small": "state-spaces/mamba-130m",      # Original Mamba small
                "mamba-medium": "state-spaces/mamba-790m",     # Original Mamba medium  
                "mamba-large": "state-spaces/mamba-1.4b",      # Original Mamba large
                "mamba-xl": "state-spaces/mamba-2.8b",         # Original Mamba XL
            }
            
            # Auto-select model based on available memory
            memory_gb = psutil.virtual_memory().total / (1024**3)
            
            # Try Mamba models first, fallback to GPT-2 based models if they fail
            model_priority = []
            if memory_gb >= 32 and torch.cuda.is_available():
                model_priority = ["mamba-xl", "mamba-large", "mamba-medium", "medium", "small"]
            elif memory_gb >= 16 and torch.cuda.is_available():
                model_priority = ["mamba-large", "mamba-medium", "medium", "small"]
            elif memory_gb >= 8:
                model_priority = ["mamba-medium", "mamba-small", "medium", "small"]
            else:
                model_priority = ["mamba-small", "small"]
            
            logger.info(f"🎯 Model priority order: {model_priority} (Available memory: {memory_gb:.1f}GB)")
            
            # Try models in priority order
            for model_key in model_priority:
                selected_model = MODEL_OPTIONS[model_key]
                logger.info(f"πŸ”„ Trying model: {selected_model}")
                
                try:
                    # Initialize loader
                    self.pretrained_loader = MambaWeightLoader(selected_model)
                    
                    # Download and load
                    if self.pretrained_loader.download_and_load():
                        self.model = self.pretrained_loader.model
                        self.tokenizer = self.pretrained_loader.tokenizer
                        self.config = self.pretrained_loader.config
                        self.model_loaded = True
                        self.using_pretrained = True
                        
                        logger.info(f"βœ… Successfully loaded pretrained model: {selected_model}")
                        return True
                    else:
                        logger.warning(f"❌ Failed to load {selected_model}, trying next...")
                        continue
                        
                except Exception as model_error:
                    logger.warning(f"❌ Error with {selected_model}: {model_error}")
                    continue
            
            logger.warning("❌ All pretrained models failed to load")
            return False
                
        except Exception as e:
            logger.error(f"Pretrained model loading error: {e}")
            return False
    
    def _load_custom_swarm_model(self):
        """Try to load custom swarm model implementation"""
        try:
            logger.info("Attempting to load custom Mamba Swarm model...")
            
            # Try multiple import paths for the custom model
            model_class = None
            
            try:
                from modeling_mamba_swarm import MambaSwarmForCausalLM
                model_class = MambaSwarmForCausalLM
                logger.info("Found MambaSwarmForCausalLM")
            except ImportError:
                try:
                    from core.mamba_swarm_integration import MambaEncoderSwarmModel
                    model_class = MambaEncoderSwarmModel
                    logger.info("Found MambaEncoderSwarmModel")
                except ImportError:
                    try:
                        from system.mambaSwarm import UnifiedMambaSwarm
                        # Use the unified swarm in native mode
                        swarm = UnifiedMambaSwarm(use_pretrained=False)
                        if hasattr(swarm, 'native_swarm_model') and swarm.native_swarm_model:
                            self.model = swarm.native_swarm_model
                            self.model_loaded = True
                            logger.info("Loaded native swarm model from UnifiedMambaSwarm")
                            return True
                        else:
                            raise ImportError("No native swarm model available")
                    except ImportError:
                        logger.warning("No custom swarm model found")
                        return False
            
            if model_class is None:
                return False
            
            # Create configuration for custom model
            try:
                from modeling_mamba_swarm import MambaSwarmConfig
                self.config = MambaSwarmConfig(
                    num_encoders=8,
                    max_mamba_encoders=100,
                    d_model=768,
                    vocab_size=50257,
                    max_sequence_length=2048
                )
            except ImportError:
                # Fallback config
                try:
                    from core.config import MambaConfig
                    self.config = MambaConfig()
                    self.config.num_encoders = 8
                    self.config.max_mamba_encoders = 100
                except ImportError:
                    # Create minimal config
                    self.config = type('Config', (), {
                        'num_encoders': 8,
                        'max_mamba_encoders': 100,
                        'd_model': 768,
                        'vocab_size': 50257,
                        'max_sequence_length': 2048
                    })()
            
            # Initialize custom model
            if model_class.__name__ == 'MambaEncoderSwarmModel':
                self.model = model_class(self.config, num_encoders=8)
            else:
                self.model = model_class(self.config)
            
            # Create tokenizer
            from transformers import GPT2Tokenizer
            self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
            
            self.model.to(self.device)
            self.model.eval()
            self.model_loaded = True
            
            logger.info("βœ… Custom swarm model loaded successfully!")
            return True
            
        except Exception as e:
            logger.error(f"Custom model loading error: {e}")
            return False
    
    def _initialize_fallback_mode(self):
        """Initialize fallback/simulation mode"""
        logger.info("Initializing fallback simulation mode")
        
        # Create mock config
        try:
            from modeling_mamba_swarm import MambaSwarmConfig
            self.config = MambaSwarmConfig(
                num_encoders=8,
                max_mamba_encoders=100,
                d_model=768,
                vocab_size=50257,
                max_sequence_length=2048
            )
        except ImportError:
            # Fallback mock config
            self.config = type('MockConfig', (), {
                'max_mamba_encoders': 100,
                'num_encoders': 8,
                'd_model': 768,
                'vocab_size': 50257,
                'max_sequence_length': 2048
            })()
        
        # Create mock tokenizer
        class MockTokenizer:
            def __init__(self):
                self.pad_token_id = 0
                self.eos_token_id = 1
                self.pad_token = "[PAD]"
                self.eos_token = "[EOS]"
            
            def encode(self, text, return_tensors=None):
                tokens = text.split()
                token_ids = [hash(token) % 1000 for token in tokens]
                if return_tensors == "pt":
                    return torch.tensor([token_ids])
                return token_ids
            
            def decode(self, token_ids, skip_special_tokens=True):
                return f"Generated response for {len(token_ids)} tokens"
        
        self.tokenizer = MockTokenizer()
        
        # Create mock model
        class MockModel:
            def __init__(self, config):
                self.config = config
                self.num_active_encoders = 5
            
            def set_active_encoders(self, num):
                self.num_active_encoders = min(num, self.config.max_mamba_encoders)
            
            def eval(self):
                pass
        
        self.model = MockModel(self.config)
        logger.info("Fallback mode initialized successfully")
    
    def _detect_domain(self, prompt: str) -> Tuple[str, float]:
        """Detect the domain of the prompt for intelligent routing"""
        prompt_lower = prompt.lower()
        domain_scores = {}
        
        for domain, keywords in self.domain_keywords.items():
            score = sum(1 for keyword in keywords if keyword in prompt_lower)
            if score > 0:
                domain_scores[domain] = score / len(keywords)
        
        if domain_scores:
            best_domain = max(domain_scores, key=domain_scores.get)
            confidence = domain_scores[best_domain]
            return best_domain, confidence
        
        return 'general', 0.5
    
    def _simulate_encoder_selection(self, prompt: str, num_encoders: int) -> Dict[str, Any]:
        """Simulate intelligent encoder selection based on domain"""
        domain, confidence = self._detect_domain(prompt)
        
        # Domain-specific encoder ranges (simulated)
        domain_ranges = {
            'medical': (1, 20),
            'legal': (21, 40),
            'code': (41, 60),
            'science': (61, 80),
            'creative': (81, 95),
            'business': (96, 100),
            'general': (1, 100)
        }
        
        start, end = domain_ranges.get(domain, (1, 100))
        available_encoders = list(range(start, min(end + 1, 101)))
        
        # Select encoders based on prompt complexity and domain
        prompt_complexity = min(len(prompt.split()) / 10, 3.0)
        optimal_count = min(max(int(num_encoders * (1 + prompt_complexity)), 3), 25)
        
        if len(available_encoders) >= optimal_count:
            selected = np.random.choice(available_encoders, size=optimal_count, replace=False)
        else:
            selected = available_encoders
        
        selected_encoders = sorted(selected.tolist())
        
        # Generate confidence scores
        base_confidence = max(0.6, confidence)
        confidence_scores = np.random.normal(base_confidence, 0.1, len(selected_encoders))
        confidence_scores = np.clip(confidence_scores, 0.5, 0.98).tolist()
        
        return {
            'selected_encoders': selected_encoders,
            'confidence_scores': confidence_scores,
            'detected_domain': domain,
            'domain_confidence': confidence,
            'total_active': len(selected_encoders)
        }
    
    def generate_text(self, prompt: str, max_length: int = 100, temperature: float = 0.7, 

                     top_p: float = 0.9, num_encoders: int = 5, show_routing: bool = True) -> Tuple[str, str]:
        """Generate text with comprehensive error handling and routing information"""
        start_time = time.time()
        
        # Update statistics
        self.stats['total_requests'] += 1
        
        try:
            if not prompt.strip():
                return "Please enter a prompt.", ""
            
            # Simulate routing decision
            routing_info = self._simulate_encoder_selection(prompt, num_encoders)
            
            if self.model_loaded and not self.fallback_mode:
                # Real model generation
                response = self._generate_real(prompt, max_length, temperature, top_p, num_encoders)
            else:
                # Simulated generation
                response = self._simulate_generation(prompt, routing_info, max_length)
            
            # Calculate performance metrics
            generation_time = time.time() - start_time
            estimated_tokens = len(response.split())
            
            # Update statistics
            self.stats['successful_generations'] += 1
            self.stats['total_tokens_generated'] += estimated_tokens
            
            # Update average generation time
            total_successful = self.stats['successful_generations']
            prev_avg = self.stats['avg_generation_time']
            self.stats['avg_generation_time'] = (prev_avg * (total_successful - 1) + generation_time) / total_successful
            
            # Generate routing display
            routing_display = ""
            if show_routing:
                routing_display = self._create_routing_display(routing_info, generation_time, estimated_tokens)
            
            logger.info(f"Generated {estimated_tokens} tokens in {generation_time:.2f}s")
            return response, routing_display
            
        except Exception as e:
            self.stats['failed_generations'] += 1
            error_msg = f"Error generating response: {str(e)}"
            logger.error(error_msg)
            return error_msg, ""
    
    def _generate_real(self, prompt: str, max_length: int, temperature: float, 

                      top_p: float, num_encoders: int) -> str:
        """Generate using real pretrained or custom model"""
        try:
            # Encode input with proper error handling
            try:
                inputs = self.tokenizer.encode(prompt, return_tensors="pt").to(self.device)
            except Exception as tokenize_error:
                logger.error(f"Tokenization error: {tokenize_error}")
                return f"Tokenization error: {str(tokenize_error)}"
            
            # Adjust number of active encoders (if supported)
            if hasattr(self.model, 'set_active_encoders'):
                max_encoders = getattr(self.config, 'max_mamba_encoders', 100)
                self.model.set_active_encoders(min(num_encoders, max_encoders))
            
            # Check if model has generate method
            if not hasattr(self.model, 'generate'):
                logger.warning("Model doesn't have generate method, using forward pass")
                return self._generate_with_forward_pass(inputs, prompt, max_length, temperature)
            
            # Generate with memory optimization and better error handling
            with torch.no_grad():
                try:
                    # Try full generation with parameters
                    outputs = self.model.generate(
                        inputs,
                        max_new_tokens=min(max_length, 512),
                        temperature=max(temperature, 0.1),  # Ensure minimum temperature
                        top_p=max(top_p, 0.1),  # Ensure minimum top_p
                        do_sample=True,
                        pad_token_id=getattr(self.tokenizer, 'pad_token_id', 0),
                        eos_token_id=getattr(self.tokenizer, 'eos_token_id', 1),
                        use_cache=True,
                        attention_mask=torch.ones_like(inputs),
                        repetition_penalty=1.1,  # Prevent repetition
                        no_repeat_ngram_size=3  # Prevent n-gram repetition
                    )
                except Exception as gen_error:
                    logger.warning(f"Full generation failed: {gen_error}")
                    # Try simpler generation
                    try:
                        outputs = self.model.generate(
                            inputs,
                            max_new_tokens=min(max_length, 256),
                            temperature=0.7,
                            do_sample=True,
                            pad_token_id=getattr(self.tokenizer, 'pad_token_id', 0),
                            eos_token_id=getattr(self.tokenizer, 'eos_token_id', 1)
                        )
                    except Exception as simple_gen_error:
                        logger.warning(f"Simple generation failed: {simple_gen_error}")
                        # Try greedy decoding
                        outputs = self.model.generate(
                            inputs,
                            max_new_tokens=min(max_length, 128),
                            do_sample=False,
                            pad_token_id=getattr(self.tokenizer, 'pad_token_id', 0),
                            eos_token_id=getattr(self.tokenizer, 'eos_token_id', 1)
                        )
            
            # Decode output with error handling
            try:
                generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            except Exception as decode_error:
                logger.error(f"Decoding error: {decode_error}")
                return f"Decoding error: {str(decode_error)}"
            
            # Clean up the response
            if generated_text.startswith(prompt):
                response = generated_text[len(prompt):].strip()
            else:
                response = generated_text.strip()
            
            # Additional cleanup for mock swarm outputs
            if not response or len(response) < 10 or response.count(' ') < 3:
                logger.warning("Generated response seems too short or invalid, using enhanced simulation")
                return self._generate_enhanced_simulation(prompt, max_length)
            
            return response if response else "Generated response was empty."
            
        except torch.cuda.OutOfMemoryError:
            logger.error("CUDA out of memory during generation")
            return "Error: GPU memory insufficient. Try reducing max_length or switching to CPU mode."
        except Exception as e:
            logger.error(f"Real generation error: {e}")
            return self._generate_enhanced_simulation(prompt, max_length)
    
    def _generate_with_forward_pass(self, inputs: torch.Tensor, prompt: str, max_length: int, temperature: float) -> str:
        """Generate using forward pass when generate method is not available"""
        try:
            logger.info("Using forward pass generation")
            
            generated_tokens = inputs.clone()
            max_gen_length = min(max_length, 200)
            
            for _ in range(max_gen_length):
                with torch.no_grad():
                    outputs = self.model(generated_tokens)
                    
                    if hasattr(outputs, 'logits'):
                        logits = outputs.logits
                    else:
                        logits = outputs
                    
                    # Get next token probabilities
                    next_token_logits = logits[:, -1, :] / max(temperature, 0.1)
                    next_token_probs = torch.softmax(next_token_logits, dim=-1)
                    
                    # Sample next token
                    next_token = torch.multinomial(next_token_probs, num_samples=1)
                    
                    # Check for EOS token
                    if next_token.item() == getattr(self.tokenizer, 'eos_token_id', 1):
                        break
                    
                    # Append to sequence
                    generated_tokens = torch.cat([generated_tokens, next_token], dim=1)
            
            # Decode the generated sequence
            generated_text = self.tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
            response = generated_text[len(prompt):].strip()
            
            return response if response else self._generate_enhanced_simulation(prompt, max_length)
            
        except Exception as e:
            logger.error(f"Forward pass generation error: {e}")
            return self._generate_enhanced_simulation(prompt, max_length)
    
    def _generate_enhanced_simulation(self, prompt: str, max_length: int) -> str:
        """Enhanced simulation for when real generation fails"""
        logger.info("Using enhanced simulation mode")
        
        domain, confidence = self._detect_domain(prompt)
        
        # More sophisticated domain-specific responses
        if domain == 'code':
            return f"""Here's a solution for your programming request:



```python

def main():

    \"\"\"

    Implementation based on your requirements: {prompt[:100]}...

    \"\"\"

    try:

        # Input processing

        data = process_input()

        

        # Core logic implementation

        result = perform_operation(data)

        

        # Output formatting

        return format_result(result)

        

    except Exception as e:

        print(f"Error occurred: {{e}}")

        return None



def process_input():

    # Process user input here

    return processed_data



def perform_operation(data):

    # Main operation logic

    return operation_result



def format_result(result):

    # Format and return result

    return formatted_result



if __name__ == "__main__":

    main()

```



This implementation includes proper error handling, modular structure, and follows Python best practices."""

        elif domain == 'medical':
            return f"""Regarding your medical inquiry about: {prompt[:100]}...



**Medical Overview:**

This topic relates to important health considerations that require professional medical evaluation. 



**Key Medical Points:**

β€’ Symptoms can vary significantly between individuals

β€’ Proper medical history and examination are essential

β€’ Diagnostic tests may be required for accurate assessment

β€’ Treatment plans should be individualized based on specific circumstances

β€’ Regular follow-up and monitoring may be necessary



**Risk Factors to Consider:**

β€’ Age, gender, and genetic predisposition

β€’ Existing medical conditions and medications

β€’ Lifestyle factors and environmental exposures

β€’ Previous medical history and family history



**When to Seek Medical Attention:**

β€’ If symptoms persist or worsen

β€’ If new concerning symptoms develop

β€’ For routine screening and prevention

β€’ When questions about treatment arise



**Important Disclaimer:** This information is for educational purposes only and should not replace professional medical advice. Please consult with qualified healthcare providers for proper diagnosis, treatment, and medical care specific to your situation."""

        elif domain == 'science':
            return f"""Scientific Analysis of: {prompt[:100]}...



**Scientific Overview:**

This topic involves complex scientific principles that can be understood through systematic analysis and evidence-based reasoning.



**Theoretical Framework:**

The underlying mechanisms involve interactions between multiple variables, governed by well-established scientific laws and emerging research findings.



**Key Scientific Principles:**

β€’ Fundamental forces and interactions at play

β€’ Thermodynamic and kinetic considerations

β€’ Molecular and atomic-level processes

β€’ Energy transfer and conservation laws

β€’ Equilibrium states and dynamic systems



**Current Research Status:**

Recent peer-reviewed studies have advanced our understanding of these phenomena, with several breakthrough discoveries providing new insights into the mechanisms involved.



**Practical Applications:**

β€’ Industrial and technological implementations

β€’ Medical and pharmaceutical applications

β€’ Environmental and sustainability implications

β€’ Future research directions and potential developments



**Methodology Considerations:**

Scientific investigation of this topic requires controlled experimental conditions, precise measurement techniques, and statistical analysis to ensure reliable and reproducible results."""

        elif domain == 'legal':
            return f"""Legal Analysis regarding: {prompt[:100]}...



**Legal Framework:**

This matter involves various legal considerations that depend on jurisdiction, applicable statutes, and case law precedent.



**Key Legal Aspects:**

β€’ Statutory requirements and regulatory compliance

β€’ Common law principles and judicial precedent

β€’ Constitutional considerations where applicable

β€’ Procedural requirements and deadlines

β€’ Rights and obligations of involved parties



**Jurisdictional Considerations:**

β€’ Federal vs. state/provincial law applications

β€’ International treaty obligations where relevant

β€’ Cross-border enforcement mechanisms

β€’ Conflict of laws principles



**Risk Assessment:**

β€’ Potential legal exposure and liability

β€’ Compliance requirements and penalties

β€’ Litigation risks and dispute resolution options

β€’ Insurance and indemnification considerations



**Recommended Actions:**

β€’ Consult with qualified legal counsel

β€’ Review relevant documentation and contracts

β€’ Assess compliance with applicable regulations

β€’ Consider alternative dispute resolution methods



**Legal Disclaimer:** This information is for general informational purposes only and does not constitute legal advice. Specific legal situations require consultation with qualified attorneys familiar with applicable law and jurisdiction."""

        elif domain == 'business':
            return f"""Business Strategy Analysis for: {prompt[:100]}...



**Executive Summary:**

This business challenge presents opportunities for strategic growth and operational optimization through data-driven decision making and market-focused initiatives.



**Market Analysis:**

β€’ Current market size and growth trajectory

β€’ Competitive landscape and positioning

β€’ Customer segmentation and value propositions

β€’ Industry trends and disruption factors

β€’ Regulatory environment and compliance requirements



**Strategic Recommendations:**



*Short-term (0-6 months):*

β€’ Immediate market positioning adjustments

β€’ Resource allocation optimization

β€’ Quick-win revenue opportunities

β€’ Risk mitigation implementation



*Medium-term (6-18 months):*

β€’ Strategic partnership development

β€’ Product/service portfolio expansion

β€’ Market penetration strategies

β€’ Operational efficiency improvements



*Long-term (18+ months):*

β€’ Innovation and R&D investments

β€’ Market leadership positioning

β€’ Scalability infrastructure development

β€’ Sustainable competitive advantage building



**Financial Projections:**

Based on market analysis and conservative growth assumptions, implementing these strategies could result in significant ROI improvements and market share expansion.



**Implementation Roadmap:**

Phased approach with clear milestones, KPIs, and accountability measures to ensure successful execution and measurable results."""

        elif domain == 'creative':
            return f"""Creative Response to: {prompt[:50]}...



**The Story Unfolds**



In the realm where imagination meets reality, your creative vision takes shape. The narrative begins with a single moment of inspiration, growing into something far greater than the sum of its parts.



*Setting the Scene:*

The world around us shifts and transforms, revealing hidden layers of meaning and possibility. Each detail contributes to a larger tapestry of human experience, woven together by threads of emotion, memory, and hope.



*Character Development:*

Our protagonist faces the eternal question that defines all great stories: How do we find meaning in the midst of uncertainty? The journey ahead is fraught with challenges, but also filled with moments of profound discovery.



*The Central Conflict:*

Like all meaningful narratives, this story explores the tension between what is and what could be. The characters must navigate between their deepest fears and their highest aspirations, finding courage in unexpected places.



*Resolution and Growth:*

Through struggle and perseverance, the story reveals its deeper truth: that creativity itself is an act of courage, a willingness to venture into the unknown and bring back something meaningful for others to share.



*Themes Explored:*

β€’ The power of imagination to transform reality

β€’ The courage required to pursue creative vision

β€’ The connection between individual expression and universal truth

β€’ The role of art in making sense of human experience



The story continues to unfold, limited only by the boundaries of imagination itself."""

        else:  # general
            return f"""Comprehensive Analysis of: {prompt[:100]}...



**Overview:**

Your inquiry touches on several important aspects that warrant careful consideration and analysis from multiple perspectives.



**Key Considerations:**

β€’ Historical context and background information

β€’ Current state of knowledge and understanding

β€’ Multiple viewpoints and interpretations

β€’ Practical implications and applications

β€’ Future trends and potential developments



**Detailed Analysis:**

The topic involves complex interactions between various factors, each contributing to a nuanced understanding of the subject matter. Evidence-based reasoning suggests that successful approaches typically involve:



1. **Systematic Assessment** - Thorough evaluation of available information

2. **Critical Analysis** - Examination of assumptions and underlying principles  

3. **Stakeholder Consideration** - Understanding impact on all affected parties

4. **Risk Evaluation** - Assessment of potential challenges and mitigation strategies

5. **Implementation Planning** - Practical steps for moving forward effectively



**Best Practices:**

β€’ Maintain objectivity and evidence-based reasoning

β€’ Consider multiple perspectives and potential outcomes

β€’ Regular review and adjustment of approaches as needed

β€’ Clear communication with all stakeholders involved

β€’ Documentation of decisions and rationale for future reference



**Conclusion:**

This analysis provides a framework for understanding the key elements involved. Success typically requires combining theoretical knowledge with practical experience, while remaining adaptable to changing circumstances and new information."""

        return response
    
    def _simulate_generation(self, prompt: str, routing_info: Dict, max_length: int) -> str:
        """Generate sophisticated simulated responses"""
        domain = routing_info['detected_domain']
        
        # Enhanced domain-specific responses
        if domain == 'code':
            return f"""Here's a comprehensive solution for your request:



```python

def solution(input_data):

    \"\"\"

    Optimized implementation based on your requirements

    \"\"\"

    try:

        # Input validation

        if not input_data:

            raise ValueError("Input cannot be empty")

        

        # Process the data

        result = process_input(input_data)

        

        return result

    except Exception as e:

        print(f"Error: {{e}}")

        return None



def process_input(data):

    # Implementation here

    return processed_data

```



This solution includes error handling, input validation, and follows best practices for production code."""
        
        elif domain == 'medical':
            return f"""Based on current medical knowledge regarding your query:



**Overview:**

This topic involves several important medical considerations that should be evaluated by healthcare professionals.



**Key Points:**

β€’ Symptoms and presentation can vary significantly between individuals

β€’ Early detection and proper diagnosis are crucial

β€’ Treatment approaches should be personalized

β€’ Regular monitoring may be recommended



**Important Note:** This information is for educational purposes only. Please consult with qualified healthcare professionals for personalized medical advice, diagnosis, and treatment recommendations."""
        
        else:
            return f"""**Response to: "{prompt[:50]}..."**



Based on analysis from {routing_info['total_active']} specialized encoders in the {domain} domain:



This is a comprehensive response that addresses your query with relevant information and insights. The analysis considers multiple perspectives and provides a balanced view of the topic.



**Key insights:**

β€’ The topic involves several interconnected factors

β€’ Current understanding is based on established principles

β€’ Practical applications may vary depending on context

β€’ Further exploration could yield additional insights



**Domain expertise applied:** {domain.title()} specialization with {routing_info['domain_confidence']:.1%} confidence."""
    
    def _create_routing_display(self, routing_info: Dict, generation_time: float, 

                              estimated_tokens: int) -> str:
        """Create rich routing information display"""
        model_type = "Real Pretrained Model" if (self.model_loaded and not self.fallback_mode and self.using_pretrained) else "Custom Swarm Model" if (self.model_loaded and not self.fallback_mode) else "Simulation Mode"
        model_name = getattr(self.pretrained_loader, 'model_name', 'Custom/Simulation') if self.pretrained_loader else 'Custom/Simulation'
        
        return f"""

## 🧠 Intelligent Routing Analysis



**🎯 Domain Detection:**

- **Primary Domain**: {routing_info['detected_domain'].title()}

- **Confidence**: {routing_info['domain_confidence']:.1%}

- **Specialization Level**: {'High' if routing_info['domain_confidence'] > 0.7 else 'Medium' if routing_info['domain_confidence'] > 0.4 else 'General'}



**⚑ Model Information:**

- **Model Type**: {model_type}

- **Base Model**: {model_name}

- **Active Encoders**: {routing_info['total_active']}/{getattr(self.config, 'max_mamba_encoders', 100)}

- **Device**: {self.device}



**πŸ”’ Selected Encoder IDs:**

{', '.join(map(str, routing_info['selected_encoders'][:15]))}{'...' if len(routing_info['selected_encoders']) > 15 else ''}



**πŸ“Š Performance Metrics:**

- **Generation Time**: {generation_time:.2f}s

- **Estimated Tokens**: {estimated_tokens}

- **Tokens/Second**: {estimated_tokens/generation_time:.1f}

- **Success Rate**: {(self.stats['successful_generations'] / max(self.stats['total_requests'], 1) * 100):.1f}%



**🎚️ Confidence Scores (Top 5):**

{', '.join([f'{score:.3f}' for score in routing_info['confidence_scores'][:5]])}{'...' if len(routing_info['confidence_scores']) > 5 else ''}



**πŸ’‘ Optimization Notes:**

- Encoder selection optimized for domain: {routing_info['detected_domain']}

- {'Pretrained weights from HuggingFace' if self.using_pretrained else 'Custom swarm implementation' if self.model_loaded and not self.fallback_mode else 'Simulation mode active'}

- Dynamic load balancing across {routing_info['total_active']} active encoders

"""
    
    def get_model_info(self) -> str:
        """Get comprehensive model information"""
        if not self.model:
            return "Model not initialized"
        
        # Get system information
        memory_info = psutil.virtual_memory()
        gpu_info = "N/A"
        if torch.cuda.is_available():
            gpu_info = f"{torch.cuda.get_device_name(0)} ({torch.cuda.get_device_properties(0).total_memory // 1024**3}GB)"
        
        # Get pretrained model info if available
        pretrained_info = ""
        if self.pretrained_loader:
            model_info = self.pretrained_loader.get_model_info()
            if model_info and 'error' not in model_info:
                pretrained_info = f"""

**πŸ€— Pretrained Model Details:**

- **Model Name**: {model_info['name']}

- **Parameters**: {model_info['parameters']} ({model_info['parameters_millions']})

- **Vocabulary Size**: {model_info['vocab_size']:,}

- **Hidden Size**: {model_info['hidden_size']}

- **Model Device**: {model_info['device']}

- **Data Type**: {model_info['dtype']}

"""
        
        status_emoji = "βœ…" if self.model_loaded and not self.fallback_mode else "⚠️" 
        status_text = f"Loaded {'with Pretrained Weights' if self.using_pretrained else 'with Custom Swarm'}" if self.model_loaded and not self.fallback_mode else "Simulation Mode"
        
        return f"""

**πŸ€– Mamba Encoder Swarm Model Information**



**Model Configuration:**

- **Status**: {status_emoji} {status_text}

- **Active Encoders**: {getattr(self.model, 'num_active_encoders', 'N/A')}

- **Max Encoders**: {getattr(self.config, 'max_mamba_encoders', 100)}

- **Model Dimension**: {getattr(self.config, 'd_model', getattr(self.config, 'hidden_size', 768))}

- **Vocabulary Size**: {getattr(self.config, 'vocab_size', 50257):,}

- **Max Sequence Length**: {getattr(self.config, 'max_sequence_length', 'N/A')}

{pretrained_info}

**System Information:**

- **Device**: {self.device} {f'({gpu_info})' if gpu_info != 'N/A' else ''}

- **RAM Usage**: {memory_info.percent:.1f}% ({memory_info.used // 1024**3}GB / {memory_info.total // 1024**3}GB)

- **PyTorch Version**: {torch.__version__}



**Performance Statistics:**

- **Total Requests**: {self.stats['total_requests']}

- **Successful**: {self.stats['successful_generations']}

- **Failed**: {self.stats['failed_generations']}

- **Success Rate**: {(self.stats['successful_generations'] / max(self.stats['total_requests'], 1) * 100):.1f}%

- **Avg Generation Time**: {self.stats['avg_generation_time']:.2f}s

- **Total Tokens Generated**: {self.stats['total_tokens_generated']:,}



**Mode**: {'🟒 Pretrained Model Active' if self.using_pretrained else 'πŸ”΅ Custom Swarm Active' if self.model_loaded and not self.fallback_mode else '🟑 Simulation Mode'}

"""
    
    def get_system_status(self) -> Dict[str, Any]:
        """Get system status for monitoring"""
        return {
            'model_loaded': self.model_loaded,
            'using_pretrained': self.using_pretrained,
            'fallback_mode': self.fallback_mode,
            'device': str(self.device),
            'stats': self.stats.copy(),
            'timestamp': datetime.now().isoformat()
        }
    
    def switch_model(self, model_size: str = "auto") -> str:
        """Switch between different pretrained model sizes"""
        if not self.using_pretrained:
            return "❌ Model switching only available when using pretrained models"
        
        try:
            MODEL_OPTIONS = {
                "small": "state-spaces/mamba-130m",
                "medium": "state-spaces/mamba-790m", 
                "large": "state-spaces/mamba-1.4b",
                "xl": "state-spaces/mamba-2.8b"
            }
            
            if model_size == "auto":
                # Auto-select based on memory
                memory_gb = psutil.virtual_memory().total / (1024**3)
                if memory_gb >= 32 and torch.cuda.is_available():
                    model_size = "xl"
                elif memory_gb >= 16 and torch.cuda.is_available():
                    model_size = "large"
                elif memory_gb >= 8:
                    model_size = "medium"
                else:
                    model_size = "small"
            
            if model_size not in MODEL_OPTIONS:
                return f"❌ Invalid model size. Choose from: {list(MODEL_OPTIONS.keys())}"
            
            selected_model = MODEL_OPTIONS[model_size]
            
            # Check if already using this model
            if self.pretrained_loader and self.pretrained_loader.model_name == selected_model:
                return f"βœ… Already using {selected_model}"
            
            logger.info(f"πŸ”„ Switching to model: {selected_model}")
            
            # Clear current model
            if self.model:
                del self.model
                torch.cuda.empty_cache() if torch.cuda.is_available() else None
            
            # Load new model
            self.pretrained_loader = MambaWeightLoader(selected_model)
            
            if self.pretrained_loader.download_and_load():
                self.model = self.pretrained_loader.model
                self.tokenizer = self.pretrained_loader.tokenizer
                self.config = self.pretrained_loader.config
                
                logger.info(f"βœ… Successfully switched to {selected_model}")
                return f"βœ… Successfully switched to {selected_model}"
            else:
                logger.error(f"❌ Failed to switch to {selected_model}")
                return f"❌ Failed to switch to {selected_model}"
                
        except Exception as e:
            logger.error(f"Error switching model: {e}")
            return f"❌ Error switching model: {str(e)}"

def create_production_demo() -> gr.Blocks:
    """Create production-ready Gradio interface with pretrained model support"""
    
    # Initialize demo with pretrained model capability
    try:
        demo_instance = MambaSwarmDemo(model_path="./", fallback_mode=False)
    except Exception as e:
        logger.warning(f"Primary initialization failed: {e}")
        demo_instance = MambaSwarmDemo(model_path="./", fallback_mode=True)
    
    def generate_response(prompt, max_length, temperature, top_p, num_encoders, show_routing):
        return demo_instance.generate_text(prompt, max_length, temperature, top_p, num_encoders, show_routing)
    
    def show_model_info():
        return demo_instance.get_model_info()
    
    def refresh_model_info():
        return demo_instance.get_model_info()
    
    def switch_model_size(model_size):
        result = demo_instance.switch_model(model_size)
        return result, demo_instance.get_model_info()
    
    # Create interface
    with gr.Blocks(
        title="Mamba Encoder Swarm - Production Demo with Pretrained Weights",
        theme=gr.themes.Soft(),
        css="""

        .gradio-container {

            max-width: 1200px;

            margin: auto;

        }

        .model-info {

            background-color: #f8f9fa;

            border-radius: 8px;

            padding: 15px;

            margin: 10px 0;

        }

        .routing-info {

            background-color: #e8f4fd;

            border-radius: 8px;

            padding: 15px;

            margin: 10px 0;

        }

        .status-indicator {

            background-color: #d4edda;

            border: 1px solid #c3e6cb;

            border-radius: 8px;

            padding: 10px;

            margin: 10px 0;

        }

        """
    ) as demo:
        
        # Header
        gr.Markdown("""

        # 🐍 Mamba Encoder Swarm - Production Demo

        

        **Advanced Language Model with Pretrained Weights & Dynamic Routing**

        

        Now featuring **automatic pretrained weight loading** from HuggingFace's state-spaces Mamba models, 

        with intelligent domain-aware routing across up to 100 specialized encoders.

        """)
        
        # Status indicator
        with gr.Row():
            with gr.Column(scale=3):
                status_text = f"🟒 Real Pretrained Model" if demo_instance.using_pretrained else f"πŸ”΅ Custom Swarm Model" if demo_instance.model_loaded and not demo_instance.fallback_mode else "🟑 Simulation Mode"
                status_indicator = gr.Markdown(
                    f"**Status**: {status_text}",
                    elem_classes=["status-indicator"]
                )
            with gr.Column(scale=1):
                if demo_instance.using_pretrained:
                    model_switch = gr.Dropdown(
                        choices=["auto", "small", "medium", "large", "xl"],
                        value="auto",
                        label="πŸ”„ Switch Model",
                        info="Change pretrained model size"
                    )
                    switch_btn = gr.Button("Switch Model", variant="secondary", size="sm")
        
        with gr.Row():
            # Left column - Input and controls
            with gr.Column(scale=2):
                prompt_input = gr.Textbox(
                    label="πŸ“ Input Prompt",
                    placeholder="Enter your prompt here... (e.g., 'Explain quantum computing', 'Write a Python function', 'Analyze market trends')",
                    lines=4,
                    max_lines=8
                )
                
                with gr.Accordion("βš™οΈ Generation Parameters", open=False):
                    with gr.Row():
                        max_length = gr.Slider(
                            label="Max Length",
                            minimum=50,
                            maximum=1000,
                            value=200,
                            step=25,
                            info="Maximum number of tokens to generate"
                        )
                        temperature = gr.Slider(
                            label="Temperature",
                            minimum=0.1,
                            maximum=2.0,
                            value=0.7,
                            step=0.1,
                            info="Controls randomness (lower = more focused)"
                        )
                    
                    with gr.Row():
                        top_p = gr.Slider(
                            label="Top-p (Nucleus Sampling)",
                            minimum=0.1,
                            maximum=1.0,
                            value=0.9,
                            step=0.05,
                            info="Probability mass for nucleus sampling"
                        )
                        num_encoders = gr.Slider(
                            label="Target Active Encoders",
                            minimum=1,
                            maximum=25,
                            value=8,
                            step=1,
                            info="Preferred number of encoders to activate"
                        )
                    
                    show_routing = gr.Checkbox(
                        label="Show Routing Information",
                        value=True,
                        info="Display detailed routing and performance metrics"
                    )
                
                generate_btn = gr.Button("πŸš€ Generate Response", variant="primary", size="lg")
                
            # Right column - Output and information
            with gr.Column(scale=3):
                response_output = gr.Textbox(
                    label="πŸ“„ Generated Response",
                    lines=12,
                    max_lines=20,
                    interactive=False,
                    show_copy_button=True
                )
                
                routing_output = gr.Markdown(
                    label="πŸ” Routing & Performance Analysis",
                    visible=True,
                    elem_classes=["routing-info"]
                )
        
        # Model information section
        with gr.Accordion("πŸ€– Model Information & Statistics", open=False):
            with gr.Row():
                model_info_display = gr.Markdown(
                    value=show_model_info(),
                    elem_classes=["model-info"]
                )
                with gr.Column(scale=1):
                    refresh_info_btn = gr.Button("πŸ”„ Refresh Info", size="sm")
                    if demo_instance.using_pretrained:
                        model_status = gr.Textbox(
                            label="Model Switch Status",
                            interactive=False,
                            lines=2
                        )
        
        # Examples section
        with gr.Accordion("πŸ’‘ Example Prompts", open=True):
            gr.Markdown("### Try these examples to see domain-specific routing in action:")
            
            examples = [
                ["Explain the process of photosynthesis in detail", 300, 0.7, 0.9, 10, True],
                ["Write a Python function to implement binary search with error handling", 250, 0.5, 0.8, 8, True],
                ["What are the early symptoms of Type 2 diabetes?", 200, 0.6, 0.9, 12, True],
                ["Analyze the legal implications of AI-generated content", 350, 0.7, 0.9, 15, True],
                ["Write a creative short story about a time-traveling scientist", 400, 0.9, 0.95, 12, True],
                ["Develop a marketing strategy for a sustainable fashion startup", 300, 0.8, 0.9, 10, True],
                ["How does quantum entanglement work and what are its applications?", 350, 0.6, 0.9, 15, True],
                ["Explain the economic impact of renewable energy adoption", 300, 0.7, 0.9, 12, True]
            ]
            
            gr.Examples(
                examples=examples,
                inputs=[prompt_input, max_length, temperature, top_p, num_encoders, show_routing],
                outputs=[response_output, routing_output],
                fn=generate_response,
                cache_examples=False,
                label="Click any example to load it"
            )
        
        # Advanced features section
        with gr.Accordion("πŸ”¬ Advanced Features", open=False):
            gr.Markdown("""

            ### πŸš€ Pretrained Model Features

            - **Automatic Model Selection**: Chooses optimal model size based on available memory

            - **Dynamic Model Switching**: Switch between different Mamba model sizes

            - **HuggingFace Integration**: Direct loading from state-spaces repository

            - **Memory Optimization**: Efficient loading with half-precision and device mapping

            

            ### 🧠 Intelligent Routing System

            - **Domain Detection**: Automatic classification of prompt domains

            - **Specialized Encoders**: 100+ domain-specific encoder pools

            - **Load Balancing**: Dynamic distribution across active encoders

            - **Confidence Scoring**: Weighted aggregation based on encoder confidence

            

            ### πŸ“Š Model Sizes Available

            - **Small (130M)**: ~500MB, good for basic tasks

            - **Medium (790M)**: ~3GB, balanced performance

            - **Large (1.4B)**: ~5GB, high-quality responses  

            - **XL (2.8B)**: ~10GB, best performance (requires 16GB+ RAM)

            """)
        
        # Event handlers
        generate_btn.click(
            fn=generate_response,
            inputs=[prompt_input, max_length, temperature, top_p, num_encoders, show_routing],
            outputs=[response_output, routing_output],
            api_name="generate"
        )
        
        refresh_info_btn.click(
            fn=refresh_model_info,
            outputs=model_info_display
        )
        
        # Model switching event handler (only if using pretrained)
        if demo_instance.using_pretrained:
            switch_btn.click(
                fn=switch_model_size,
                inputs=[model_switch],
                outputs=[model_status, model_info_display]
            )
        
        # Auto-refresh status on page load
        demo.load(
            fn=lambda: (demo_instance.get_model_info(), f"**Status**: {'🟒 Real Pretrained Model' if demo_instance.using_pretrained else 'πŸ”΅ Custom Swarm Model' if demo_instance.model_loaded and not demo_instance.fallback_mode else '🟑 Simulation Mode'}"),
            outputs=[model_info_display, status_indicator]
        )
        
        # Footer
        gr.Markdown("""

        ---

        ### πŸ—οΈ Enhanced Architecture Overview

        

        **πŸ€— Pretrained Integration**

        - Direct loading from HuggingFace state-spaces Mamba models

        - Automatic model size selection based on system resources

        - Seamless fallback to custom swarm implementation

        - Dynamic model switching without restart

        

        **🧠 Intelligent Routing System**

        - Domain detection based on prompt analysis

        - Dynamic encoder selection optimized for content type

        - Load balancing across specialized encoder pools

        - Confidence-weighted response aggregation

        

        **πŸ”§ Production Features**

        - Comprehensive error handling and fallback modes

        - Real-time performance monitoring and statistics

        - Memory optimization and CUDA support

        - Detailed logging and debugging capabilities

        

        **πŸ“Š Specialized Domains**

        - **Medical & Healthcare** β€’ **Legal & Regulatory** β€’ **Code & Technical**

        - **Science & Research** β€’ **Creative Writing** β€’ **Business & Finance**

        

        Built with ❀️ using Gradio, PyTorch, HuggingFace Transformers, and the Mamba architecture

        """)
    
    return demo

if __name__ == "__main__":
    # Create and launch production demo
    try:
        demo = create_production_demo()
        
        # Launch with production settings - compatible with different Gradio versions
        launch_kwargs = {
            "server_name": "0.0.0.0",
            "server_port": 7860,
            "share": False,  # Set to True for public sharing
            "debug": False,
            "show_error": True,
            "quiet": False,
        }
        
        # Add optional parameters if supported
        try:
            # Test if these parameters are supported in this Gradio version
            import gradio as gr
            import inspect
            launch_signature = inspect.signature(gr.Blocks.launch)
            
            # Add parameters if supported
            if 'favicon_path' in launch_signature.parameters:
                launch_kwargs['favicon_path'] = None
            if 'ssl_verify' in launch_signature.parameters:
                launch_kwargs['ssl_verify'] = False
            if 'show_tips' in launch_signature.parameters:
                launch_kwargs['show_tips'] = True
            if 'enable_queue' in launch_signature.parameters:
                launch_kwargs['enable_queue'] = True
            if 'max_threads' in launch_signature.parameters:
                launch_kwargs['max_threads'] = 10
                
        except Exception as e:
            logger.warning(f"Could not detect Gradio parameters: {e}")
        
        # Launch with detected parameters
        logger.info(f"Launching with parameters: {list(launch_kwargs.keys())}")
        demo.launch(**launch_kwargs)
        
    except Exception as e:
        logger.error(f"Failed to launch demo: {e}")
        print(f"❌ Demo launch failed: {e}")
        print("Please check the logs for more details.")
        
        # Try minimal launch as last resort
        try:
            logger.info("Attempting minimal launch...")
            demo.launch(share=False, debug=False)
        except Exception as e2:
            logger.error(f"Minimal launch also failed: {e2}")
            print(f"❌ All launch attempts failed. Error: {e2}")