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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
#!/usr/bin/env python3
"""

renamed from app_real.py - Production-Ready Mamba Encoder Swarm Demo

Combines real model functionality with rich UI and comprehensive error handling

"""

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

# 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 MambaSwarmDemo:
    """Production-ready Mamba Swarm Demo with fallback capabilities"""
    
    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
        
        # 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}, Fallback mode: {self.fallback_mode}")
    
    def _initialize_model(self):
        """Initialize model with comprehensive error handling and fallback"""
        try:
            logger.info("Attempting to load Mamba Swarm model...")
            
            # Check if model files exist
            config_path = os.path.join(self.model_path, "config.json")
            if not os.path.exists(config_path) and not self.fallback_mode:
                logger.warning(f"Config file not found at {config_path}, enabling fallback mode")
                self.fallback_mode = True
            
            if not self.fallback_mode:
                # Try to load real model
                self._load_real_model()
            else:
                # Initialize in fallback mode
                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_real_model(self):
        """Load the actual Mamba Swarm model"""
        try:
            # Try multiple import paths for the model
            model_class = None
            
            # Try importing from different locations
            try:
                from modeling_mamba_swarm import MambaSwarmForCausalLM
                model_class = MambaSwarmForCausalLM
                logger.info("Loaded MambaSwarmForCausalLM from modeling_mamba_swarm")
            except ImportError:
                try:
                    from upload_to_hf import MambaSwarmForCausalLM
                    model_class = MambaSwarmForCausalLM
                    logger.info("Loaded MambaSwarmForCausalLM from upload_to_hf")
                except ImportError:
                    try:
                        from core.mamba_swarm_integration import MambaEncoderSwarmModel
                        model_class = MambaEncoderSwarmModel
                        logger.info("Loaded MambaEncoderSwarmModel from core.mamba_swarm_integration")
                    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
                            else:
                                raise ImportError("No native swarm model available")
                        except ImportError as e:
                            logger.error(f"All model imports failed: {e}")
                            raise ImportError("No compatible Mamba Swarm model found")
            
            if model_class is None:
                raise ImportError("No model class available")
            
            # Load configuration
            try:
                self.config = AutoConfig.from_pretrained(self.model_path, trust_remote_code=True)
                logger.info(f"Loaded config: {self.config.__class__.__name__}")
            except Exception as e:
                logger.warning(f"Could not load config from {self.model_path}: {e}")
                # Create a default config using our MambaSwarmConfig
                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
                    )
                    logger.info("Using default MambaSwarmConfig")
                except ImportError:
                    # Final fallback to basic config
                    from core.config import MambaConfig
                    self.config = MambaConfig()
                    # Add swarm-specific attributes
                    self.config.num_encoders = 8
                    self.config.max_mamba_encoders = 100
                    self.config.max_sequence_length = 2048
                    logger.info("Using default MambaConfig with swarm attributes")
            
            # Load tokenizer
            try:
                self.tokenizer = AutoTokenizer.from_pretrained(self.model_path)
                if self.tokenizer.pad_token is None:
                    self.tokenizer.pad_token = self.tokenizer.eos_token
                logger.info("Tokenizer loaded successfully")
            except Exception as e:
                logger.warning(f"Could not load tokenizer: {e}")
                # Use a simple fallback 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
                logger.info("Using fallback GPT2 tokenizer")
            
            # Load model with memory optimization
            dtype = torch.float16 if self.device.type == "cuda" else torch.float32
            
            if model_class == MambaEncoderSwarmModel:
                # Native integration model - create with MambaConfig
                from core.config import MambaConfig
                if not hasattr(self, 'config') or not isinstance(self.config, MambaConfig):
                    mamba_config = MambaConfig(
                        d_model=getattr(self.config, 'd_model', 768),
                        vocab_size=getattr(self.config, 'vocab_size', 50257),
                        n_layers=8,
                        d_state=16,
                        d_conv=4,
                        bias=False
                    )
                    self.model = model_class(mamba_config, num_encoders=getattr(self.config, 'num_encoders', 8))
                else:
                    self.model = model_class(self.config, num_encoders=getattr(self.config, 'num_encoders', 8))
            else:
                # HuggingFace-style model or our new MambaSwarmForCausalLM
                if hasattr(model_class, 'from_pretrained') and os.path.exists(self.model_path):
                    self.model = model_class.from_pretrained(
                        self.model_path,
                        config=self.config,
                        torch_dtype=dtype,
                        trust_remote_code=True,
                        low_cpu_mem_usage=True
                    )
                else:
                    # Create with config only
                    self.model = model_class(self.config)
            
            self.model.to(self.device)
            self.model.eval()
            self.model_loaded = True
            
            # Log model info
            num_params = sum(p.numel() for p in self.model.parameters())
            logger.info(f"Model loaded successfully on {self.device}")
            logger.info(f"Model parameters: {num_params:,} ({num_params/1e6:.1f}M)")
            
        except Exception as e:
            logger.error(f"Real model loading failed: {e}")
            raise
    
    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):
                # Simple word-based tokenization for simulation
                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):
                # Mock decoding
                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)  # Complexity factor
        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 _simulate_generation(self, prompt: str, routing_info: Dict, max_length: int) -> str:
        """Generate sophisticated simulated responses based on domain"""
        domain = routing_info['detected_domain']
        
        domain_responses = {
            'medical': f"""Based on medical literature and current research, regarding "{prompt[:50]}...":



This condition/topic involves multiple factors including genetic predisposition, environmental influences, and lifestyle factors. Key considerations include:



β€’ Proper medical evaluation is essential

β€’ Individual symptoms may vary significantly  

β€’ Treatment approaches should be personalized

β€’ Regular monitoring is typically recommended



**Important**: This information is for educational purposes only. Please consult with qualified healthcare professionals for personalized medical advice and treatment recommendations.""",
            
            'legal': f"""From a legal perspective on "{prompt[:50]}...":



The legal framework surrounding this matter involves several key considerations:



β€’ Jurisdictional requirements and applicable statutes

β€’ Precedent cases and regulatory guidelines

β€’ Compliance obligations and reporting requirements

β€’ Risk assessment and mitigation strategies



**Disclaimer**: This information is for general informational purposes only and does not constitute legal advice. Consult with qualified legal professionals for specific legal matters.""",
            
            'code': f"""Here's a comprehensive solution for "{prompt[:50]}...":



```python

def optimized_solution(input_data):

    \"\"\"

    Efficient implementation with error handling

    Time complexity: O(n log n)

    Space complexity: O(n)

    \"\"\"

    try:

        # Input validation

        if not input_data:

            raise ValueError("Input data cannot be empty")

        

        # Core algorithm implementation

        result = process_data(input_data)

        

        # Additional optimizations

        result = optimize_output(result)

        

        return result

    

    except Exception as e:

        logger.error(f"Processing error: {{e}}")

        return None



def process_data(data):

    # Implementation details here

    return processed_data



def optimize_output(data):

    # Performance optimizations

    return optimized_data

```



**Key Features:**

β€’ Error handling and input validation

β€’ Optimized performance characteristics

β€’ Comprehensive documentation

β€’ Production-ready implementation""",
            
            'science': f"""Scientific Analysis of "{prompt[:50]}...":



Based on current scientific understanding and peer-reviewed research:



**Theoretical Framework:**

The underlying principles involve complex interactions between multiple variables, governed by established scientific laws and emerging theories.



**Methodology:**

β€’ Systematic observation and data collection

β€’ Controlled experimental design

β€’ Statistical analysis and validation

β€’ Peer review and reproducibility testing



**Current Research:**

Recent studies indicate significant progress in understanding the mechanisms involved, with several promising avenues for future investigation.



**Implications:**

These findings have broad applications across multiple disciplines and may lead to significant advances in the field.""",
            
            'creative': f"""**{prompt[:30]}...**



The story unfolds in a world where imagination meets reality, where every character carries the weight of their dreams and the burden of their choices.



*Chapter 1: The Beginning*



In the quiet moments before dawn, when the world holds its breath between night and day, our tale begins. The protagonist stands at the threshold of an adventure that will challenge everything they thought they knew about themselves and the world around them.



The narrative weaves through layers of meaning, exploring themes of identity, purpose, and the delicate balance between hope and reality. Each scene is crafted with careful attention to emotional resonance and character development.



*As the story progresses, we discover that the true journey is not external, but internalβ€”a transformation of the soul that mirrors the changing landscape of the world itself.*



**Themes Explored:**

β€’ Personal growth and self-discovery

β€’ The power of resilience and determination

β€’ The complexity of human relationships

β€’ The intersection of dreams and reality""",
            
            'business': f"""**Strategic Analysis: {prompt[:50]}...**



**Executive Summary:**

This comprehensive analysis examines the strategic implications and market opportunities related to the identified business challenge.



**Market Assessment:**

β€’ Current market size and growth projections

β€’ Competitive landscape analysis

β€’ Key trends and disruption factors

β€’ Customer segment identification



**Strategic Recommendations:**

1. **Short-term actions** (0-6 months)

   - Immediate market positioning

   - Resource allocation optimization

   - Risk mitigation strategies



2. **Medium-term initiatives** (6-18 months)

   - Strategic partnerships and alliances

   - Product/service development

   - Market expansion opportunities



3. **Long-term vision** (18+ months)

   - Innovation and R&D investment

   - Scalability and sustainability

   - Market leadership positioning



**Financial Projections:**

Based on conservative estimates, implementation of these strategies could result in significant ROI and market share growth.""",
            
            'general': f"""**Comprehensive Response to: "{prompt[:50]}..."**



Thank you for your inquiry. Based on available knowledge and expertise from {routing_info['total_active']} specialized domains, here's a comprehensive analysis:



**Key Points:**

β€’ The topic involves multiple interconnected factors that require careful consideration

β€’ Current understanding is based on established principles and ongoing research

β€’ Practical applications vary depending on specific context and requirements

β€’ Best practices emphasize a balanced, evidence-based approach



**Detailed Analysis:**

The subject matter encompasses several important dimensions that merit thorough examination. Each aspect contributes to a deeper understanding of the overall concept and its implications.



**Practical Considerations:**

Implementation requires careful planning, adequate resources, and ongoing monitoring to ensure optimal outcomes. Success factors include stakeholder engagement, clear communication, and adaptive management strategies.



**Conclusion:**

This analysis provides a foundation for informed decision-making while acknowledging the complexity and nuanced nature of the topic."""
        }
        
        return domain_responses.get(domain, domain_responses['general'])
    
    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

        

        Returns:

            Tuple of (generated_text, routing_info_display)

        """
        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 with sophisticated responses
                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 model"""
        try:
            # Encode input
            inputs = self.tokenizer.encode(prompt, return_tensors="pt").to(self.device)
            
            # Adjust number of active encoders
            if hasattr(self.model, 'set_active_encoders'):
                self.model.set_active_encoders(min(num_encoders, self.config.max_mamba_encoders))
            
            # Generate with memory optimization
            with torch.no_grad():
                outputs = self.model.generate(
                    inputs,
                    max_length=min(max_length, getattr(self.config, 'max_sequence_length', 2048)),
                    temperature=temperature,
                    top_p=top_p,
                    do_sample=True,
                    pad_token_id=self.tokenizer.pad_token_id,
                    eos_token_id=self.tokenizer.eos_token_id,
                    use_cache=True
                )
            
            # Decode output
            generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            
            # Remove input prompt from output
            response = generated_text[len(prompt):].strip()
            
            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 num_encoders."
        except Exception as e:
            logger.error(f"Real generation error: {e}")
            return f"Generation error: {str(e)}"
    
    def _create_routing_display(self, routing_info: Dict, generation_time: float, 

                              estimated_tokens: int) -> str:
        """Create rich routing information display"""
        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'}



**⚑ Encoder Activation:**

- **Active Encoders**: {routing_info['total_active']}/{self.config.max_mamba_encoders}

- **Selection Strategy**: Domain-optimized routing

- **Load Distribution**: Balanced across specialized encoders



**πŸ”’ 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}

- **Model Mode**: {'Real Model' if self.model_loaded and not self.fallback_mode else 'Simulation'}



**🎚️ 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']}

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

- Confidence-weighted aggregation applied

"""
    
    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)"
        
        return f"""

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



**Model Configuration:**

- **Status**: {'βœ… Loaded' if self.model_loaded else '⚠️ Simulation Mode'}

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

- **Max Encoders**: {self.config.max_mamba_encoders}

- **Model Dimension**: {self.config.d_model}

- **Vocabulary Size**: {self.config.vocab_size:,}

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



**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)

- **Python/PyTorch**: {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']:,}



**Fallback Mode**: {'⚠️ Active' if self.fallback_mode else 'βœ… Disabled'}

"""
    
    def get_system_status(self) -> Dict[str, Any]:
        """Get system status for monitoring"""
        return {
            'model_loaded': self.model_loaded,
            'fallback_mode': self.fallback_mode,
            'device': str(self.device),
            'stats': self.stats.copy(),
            'timestamp': datetime.now().isoformat()
        }

def create_production_demo() -> gr.Blocks:
    """Create production-ready Gradio interface"""
    
    # Initialize demo with fallback 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()
    
    # Create interface
    with gr.Blocks(
        title="Mamba Encoder Swarm - Production Demo",
        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;

        }

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

        # 🐍 Mamba Encoder Swarm - Production Demo

        

        **Advanced Language Model with Dynamic Routing & Intelligent Encoder Selection**

        

        Experience the power of up to 100 specialized Mamba encoders with intelligent domain-aware routing, 

        comprehensive error handling, and production-ready performance monitoring.

        """)
        
        # Status indicator
        with gr.Row():
            with gr.Column(scale=1):
                status_indicator = gr.Markdown(
                    f"**Status**: {'🟒 Real Model' if demo_instance.model_loaded and not demo_instance.fallback_mode else '🟑 Simulation Mode'}"
                )
        
        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"]
                )
                refresh_info_btn = gr.Button("πŸ”„ Refresh Info", size="sm")
        
        # 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]
            ]
            
            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"
            )
        
        # 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
        )
        
        # Footer
        gr.Markdown("""

        ---

        ### πŸ—οΈ Architecture Overview

        

        **🧠 Intelligent Routing System**

        - Domain detection based on prompt analysis

        - Dynamic encoder selection optimized for content type

        - Load balancing across specialized encoder pools

        

        **πŸ”§ 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, 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}")