Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Mamba Encoder Swarm Demo - Ultimate Production Version
|
| 4 |
-
Combines the best features from all versions with advanced optimization
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -17,6 +18,13 @@ import warnings
|
|
| 17 |
from typing import Optional, Dict, Any, Tuple, List
|
| 18 |
from datetime import datetime
|
| 19 |
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, GPT2Tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Suppress warnings for cleaner output
|
| 22 |
warnings.filterwarnings("ignore")
|
|
@@ -719,12 +727,370 @@ class AdvancedPerformanceMonitor:
|
|
| 719 |
}
|
| 720 |
|
| 721 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
class UltimateMambaSwarm:
|
| 723 |
-
"""Ultimate Mamba Swarm combining
|
| 724 |
|
| 725 |
def __init__(self):
|
| 726 |
self.model_loader = UltimateModelLoader()
|
| 727 |
self.performance_monitor = AdvancedPerformanceMonitor()
|
|
|
|
| 728 |
self.model_loaded = False
|
| 729 |
self.current_model_size = "auto"
|
| 730 |
|
|
@@ -1067,8 +1433,8 @@ class UltimateMambaSwarm:
|
|
| 1067 |
|
| 1068 |
def generate_text_ultimate(self, prompt: str, max_length: int = 200, temperature: float = 0.7,
|
| 1069 |
top_p: float = 0.9, num_encoders: int = 12, model_size: str = "auto",
|
| 1070 |
-
show_routing: bool = True) -> Tuple[str, str]:
|
| 1071 |
-
"""
|
| 1072 |
|
| 1073 |
start_time = time.time()
|
| 1074 |
|
|
@@ -1084,25 +1450,41 @@ class UltimateMambaSwarm:
|
|
| 1084 |
# Advanced domain detection
|
| 1085 |
domain, confidence = self.detect_domain_advanced(prompt)
|
| 1086 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1087 |
# Advanced encoder routing
|
| 1088 |
routing_info = self.simulate_advanced_encoder_routing(
|
| 1089 |
domain, confidence, num_encoders, self.current_model_size
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
-
#
|
| 1093 |
if self.model_loaded:
|
| 1094 |
-
print(f"π§ Using
|
| 1095 |
-
response = self.
|
|
|
|
|
|
|
| 1096 |
else:
|
| 1097 |
-
print(f"π Using fallback
|
| 1098 |
-
response = self.
|
| 1099 |
|
| 1100 |
# Quality validation
|
| 1101 |
is_gibberish = self.model_loader._is_gibberish_advanced(response) if self.model_loaded else False
|
| 1102 |
|
| 1103 |
if is_gibberish:
|
| 1104 |
-
logger.warning("π« Gibberish detected, using enhanced fallback")
|
| 1105 |
-
response = self.
|
| 1106 |
is_gibberish = True # Mark for monitoring
|
| 1107 |
|
| 1108 |
# Performance logging
|
|
@@ -1113,19 +1495,158 @@ class UltimateMambaSwarm:
|
|
| 1113 |
generation_time, token_count, True, domain, is_gibberish
|
| 1114 |
)
|
| 1115 |
|
| 1116 |
-
# Create
|
| 1117 |
routing_display = ""
|
| 1118 |
if show_routing:
|
| 1119 |
-
routing_display = self.
|
| 1120 |
-
routing_info, generation_time, token_count
|
| 1121 |
)
|
| 1122 |
|
| 1123 |
return response, routing_display
|
| 1124 |
|
| 1125 |
except Exception as e:
|
| 1126 |
-
logger.error(f"
|
| 1127 |
self.performance_monitor.log_generation(0, 0, False)
|
| 1128 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1129 |
|
| 1130 |
def _generate_with_ultimate_model(self, prompt: str, max_length: int, temperature: float, top_p: float, domain: str = 'general') -> str:
|
| 1131 |
"""Generate using loaded model with ultimate optimization and content safety"""
|
|
@@ -1721,6 +2242,92 @@ Secondary: {', '.join(map(str, routing_info['selected_encoders'][8:16]))}{'...'
|
|
| 1721 |
- **Context History**: {len(self.domain_context_history)} entries
|
| 1722 |
- **Learning Domains**: {', '.join(self.learned_patterns.keys()) if self.learned_patterns else 'Initializing'}
|
| 1723 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1724 |
**π Mamba Status**: Ready for GPU activation (mamba_ssm commented out)
|
| 1725 |
"""
|
| 1726 |
|
|
@@ -1736,7 +2343,7 @@ Secondary: {', '.join(map(str, routing_info['selected_encoders'][8:16]))}{'...'
|
|
| 1736 |
return success
|
| 1737 |
|
| 1738 |
def get_ultimate_system_info(self) -> str:
|
| 1739 |
-
"""Get
|
| 1740 |
memory_info = psutil.virtual_memory()
|
| 1741 |
gpu_info = "CPU Only"
|
| 1742 |
if torch.cuda.is_available():
|
|
@@ -1745,25 +2352,32 @@ Secondary: {', '.join(map(str, routing_info['selected_encoders'][8:16]))}{'...'
|
|
| 1745 |
gpu_info += f" ({gpu_memory:.1f}GB)"
|
| 1746 |
|
| 1747 |
perf_stats = self.performance_monitor.get_comprehensive_stats()
|
|
|
|
| 1748 |
model_info = self.model_loader.get_model_info()
|
| 1749 |
|
| 1750 |
return f"""
|
| 1751 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1752 |
|
| 1753 |
-
|
| 1754 |
-
- **
|
| 1755 |
-
- **
|
| 1756 |
-
- **
|
| 1757 |
-
- **
|
| 1758 |
|
| 1759 |
**π» Hardware Configuration:**
|
| 1760 |
- **Processing Unit**: {gpu_info}
|
| 1761 |
- **System RAM**: {memory_info.total / (1024**3):.1f}GB ({memory_info.percent:.1f}% used)
|
| 1762 |
- **Available RAM**: {memory_info.available / (1024**3):.1f}GB
|
| 1763 |
-
- **
|
| 1764 |
- **Mamba Readiness**: {"π’ GPU Ready for Mamba Activation" if torch.cuda.is_available() else "π‘ CPU Mode - GPU Needed for Mamba"}
|
| 1765 |
|
| 1766 |
-
**π
|
| 1767 |
- **Total Requests**: {perf_stats.get('total_requests', 0)}
|
| 1768 |
- **Success Rate**: {perf_stats.get('success_rate', 'N/A')}
|
| 1769 |
- **Quality Rate**: {perf_stats.get('quality_rate', 'N/A')}
|
|
@@ -1771,17 +2385,30 @@ Secondary: {', '.join(map(str, routing_info['selected_encoders'][8:16]))}{'...'
|
|
| 1771 |
- **Model Adaptations**: {perf_stats.get('model_switches', 0)}
|
| 1772 |
- **Quality Filters Activated**: {perf_stats.get('gibberish_prevented', 0)}
|
| 1773 |
|
| 1774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1775 |
- **Supported Domains**: {len(self.base_domain_patterns)} specialized domains with adaptive learning
|
| 1776 |
- **Encoder Pool**: 100 virtual encoders with dynamic routing
|
| 1777 |
-
- **Quality Protection**: Multi-layer intelligence validation
|
| 1778 |
-
- **
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
- **
|
| 1782 |
-
- **
|
| 1783 |
-
- **
|
| 1784 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1785 |
"""
|
| 1786 |
|
| 1787 |
|
|
@@ -1791,7 +2418,7 @@ def create_ultimate_interface():
|
|
| 1791 |
swarm = UltimateMambaSwarm()
|
| 1792 |
|
| 1793 |
with gr.Blocks(
|
| 1794 |
-
title="Mamba Encoder Swarm",
|
| 1795 |
theme=gr.themes.Soft(),
|
| 1796 |
css="""
|
| 1797 |
.gradio-container { max-width: 1600px; margin: auto; }
|
|
@@ -1818,9 +2445,9 @@ def create_ultimate_interface():
|
|
| 1818 |
) as demo:
|
| 1819 |
|
| 1820 |
gr.Markdown("""
|
| 1821 |
-
#
|
| 1822 |
|
| 1823 |
-
|
| 1824 |
|
| 1825 |
Features intelligent Mamba encoder swarm architecture with advanced domain routing, comprehensive performance analytics, and multi-tier quality protection. *Currently optimized for CPU with GPU Mamba encoders ready for activation.*
|
| 1826 |
|
|
@@ -1860,6 +2487,13 @@ def create_ultimate_interface():
|
|
| 1860 |
label="π€ Model Size Selection"
|
| 1861 |
)
|
| 1862 |
show_routing = gr.Checkbox(label="π Show Intelligence Analysis", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1863 |
|
| 1864 |
generate_btn = gr.Button("π Generate Response", variant="primary", size="lg")
|
| 1865 |
|
|
@@ -1912,7 +2546,7 @@ def create_ultimate_interface():
|
|
| 1912 |
# Event handlers
|
| 1913 |
generate_btn.click(
|
| 1914 |
fn=swarm.generate_text_ultimate,
|
| 1915 |
-
inputs=[prompt_input, max_length, temperature, top_p, num_encoders, model_size, show_routing],
|
| 1916 |
outputs=[response_output, routing_output]
|
| 1917 |
)
|
| 1918 |
|
|
@@ -1921,18 +2555,22 @@ def create_ultimate_interface():
|
|
| 1921 |
outputs=system_info
|
| 1922 |
)
|
| 1923 |
|
| 1924 |
-
#
|
| 1925 |
gr.Markdown("""
|
| 1926 |
---
|
| 1927 |
-
###
|
| 1928 |
-
-
|
|
|
|
| 1929 |
- **π― Elite Domain Routing** - 7 specialized domains with confidence-based encoder selection
|
| 1930 |
-
- **β‘ Advanced State-Space Processing** - Intelligent encoder swarm architecture
|
| 1931 |
-
- **π‘οΈ
|
| 1932 |
-
- **π
|
| 1933 |
-
- **π
|
| 1934 |
-
- **ποΈ
|
| 1935 |
-
- **π
|
|
|
|
|
|
|
|
|
|
| 1936 |
|
| 1937 |
**Current Status**: π₯οΈ CPU Mode Active | π Mamba Encoders Ready for GPU Activation | β‘ Instant Hardware Detection
|
| 1938 |
""")
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Mamba Encoder Swarm Demo - Ultimate Production Version with Hybrid Intelligence
|
| 4 |
+
Combines the best features from all versions with advanced optimization, adaptive learning,
|
| 5 |
+
and smart internet search capabilities for real-time information access
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
|
|
| 18 |
from typing import Optional, Dict, Any, Tuple, List
|
| 19 |
from datetime import datetime
|
| 20 |
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, GPT2Tokenizer
|
| 21 |
+
import requests
|
| 22 |
+
from urllib.parse import quote_plus
|
| 23 |
+
import re
|
| 24 |
+
from bs4 import BeautifulSoup
|
| 25 |
+
import wikipedia
|
| 26 |
+
import threading
|
| 27 |
+
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
| 28 |
|
| 29 |
# Suppress warnings for cleaner output
|
| 30 |
warnings.filterwarnings("ignore")
|
|
|
|
| 727 |
}
|
| 728 |
|
| 729 |
|
| 730 |
+
class HybridIntelligenceSearchEngine:
|
| 731 |
+
"""Advanced web search and information retrieval system for hybrid AI intelligence"""
|
| 732 |
+
|
| 733 |
+
def __init__(self):
|
| 734 |
+
self.search_history = []
|
| 735 |
+
self.cached_results = {}
|
| 736 |
+
self.search_count = 0
|
| 737 |
+
self.timeout = 10 # seconds
|
| 738 |
+
|
| 739 |
+
# User-Agent for web requests
|
| 740 |
+
self.headers = {
|
| 741 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 742 |
+
}
|
| 743 |
+
|
| 744 |
+
print("π Hybrid Intelligence Search Engine initialized")
|
| 745 |
+
|
| 746 |
+
def needs_current_info(self, prompt: str, domain: str) -> bool:
|
| 747 |
+
"""Intelligent detection of queries requiring current/real-time information"""
|
| 748 |
+
prompt_lower = prompt.lower()
|
| 749 |
+
|
| 750 |
+
# Time-sensitive indicators
|
| 751 |
+
time_indicators = [
|
| 752 |
+
'today', 'yesterday', 'this year', 'current', 'latest', 'recent', 'now', 'nowadays',
|
| 753 |
+
'what\'s happening', 'breaking news', 'trending', 'update', 'new', '2024', '2025'
|
| 754 |
+
]
|
| 755 |
+
|
| 756 |
+
# Factual query indicators
|
| 757 |
+
factual_indicators = [
|
| 758 |
+
'what is', 'who is', 'when did', 'where is', 'how much', 'population of',
|
| 759 |
+
'capital of', 'price of', 'stock', 'weather', 'news about', 'facts about'
|
| 760 |
+
]
|
| 761 |
+
|
| 762 |
+
# Domain-specific search triggers
|
| 763 |
+
domain_search_triggers = {
|
| 764 |
+
'science': ['research shows', 'studies indicate', 'scientific evidence', 'peer reviewed'],
|
| 765 |
+
'medical': ['clinical trials', 'medical studies', 'treatment options', 'side effects'],
|
| 766 |
+
'business': ['market data', 'stock price', 'company news', 'financial report'],
|
| 767 |
+
'legal': ['court case', 'legal precedent', 'law changes', 'statute'],
|
| 768 |
+
'general': ['statistics', 'data on', 'information about', 'facts on']
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
# Check for time-sensitive content
|
| 772 |
+
if any(indicator in prompt_lower for indicator in time_indicators):
|
| 773 |
+
print(f"π Time-sensitive query detected: {prompt[:50]}...")
|
| 774 |
+
return True
|
| 775 |
+
|
| 776 |
+
# Check for factual queries
|
| 777 |
+
if any(indicator in prompt_lower for indicator in factual_indicators):
|
| 778 |
+
print(f"π Factual query detected: {prompt[:50]}...")
|
| 779 |
+
return True
|
| 780 |
+
|
| 781 |
+
# Check domain-specific triggers
|
| 782 |
+
domain_triggers = domain_search_triggers.get(domain, [])
|
| 783 |
+
if any(trigger in prompt_lower for trigger in domain_triggers):
|
| 784 |
+
print(f"π― Domain-specific search needed for {domain}: {prompt[:50]}...")
|
| 785 |
+
return True
|
| 786 |
+
|
| 787 |
+
# Questions that likely need verification
|
| 788 |
+
verification_patterns = [
|
| 789 |
+
'is it true', 'verify', 'confirm', 'check if', 'find out'
|
| 790 |
+
]
|
| 791 |
+
if any(pattern in prompt_lower for pattern in verification_patterns):
|
| 792 |
+
print(f"β
Verification request detected: {prompt[:50]}...")
|
| 793 |
+
return True
|
| 794 |
+
|
| 795 |
+
return False
|
| 796 |
+
|
| 797 |
+
def generate_smart_search_queries(self, prompt: str, domain: str) -> List[str]:
|
| 798 |
+
"""Generate optimized search queries based on prompt and domain"""
|
| 799 |
+
queries = []
|
| 800 |
+
prompt_clean = prompt.strip()
|
| 801 |
+
|
| 802 |
+
# Base query
|
| 803 |
+
queries.append(prompt_clean)
|
| 804 |
+
|
| 805 |
+
# Domain-enhanced queries
|
| 806 |
+
if domain == 'medical':
|
| 807 |
+
queries.extend([
|
| 808 |
+
f"{prompt_clean} medical research",
|
| 809 |
+
f"{prompt_clean} clinical studies",
|
| 810 |
+
f"{prompt_clean} healthcare guidelines"
|
| 811 |
+
])
|
| 812 |
+
elif domain == 'science':
|
| 813 |
+
queries.extend([
|
| 814 |
+
f"{prompt_clean} scientific research",
|
| 815 |
+
f"{prompt_clean} peer reviewed studies",
|
| 816 |
+
f"{prompt_clean} scientific evidence"
|
| 817 |
+
])
|
| 818 |
+
elif domain == 'business':
|
| 819 |
+
queries.extend([
|
| 820 |
+
f"{prompt_clean} market analysis",
|
| 821 |
+
f"{prompt_clean} business data",
|
| 822 |
+
f"{prompt_clean} industry report"
|
| 823 |
+
])
|
| 824 |
+
elif domain == 'legal':
|
| 825 |
+
queries.extend([
|
| 826 |
+
f"{prompt_clean} legal analysis",
|
| 827 |
+
f"{prompt_clean} court case",
|
| 828 |
+
f"{prompt_clean} law statute"
|
| 829 |
+
])
|
| 830 |
+
elif domain == 'code':
|
| 831 |
+
queries.extend([
|
| 832 |
+
f"{prompt_clean} programming tutorial",
|
| 833 |
+
f"{prompt_clean} code example",
|
| 834 |
+
f"{prompt_clean} documentation"
|
| 835 |
+
])
|
| 836 |
+
|
| 837 |
+
# Extract key terms for focused search
|
| 838 |
+
key_terms = self._extract_key_terms(prompt_clean)
|
| 839 |
+
if key_terms:
|
| 840 |
+
queries.append(' '.join(key_terms[:5])) # Top 5 key terms
|
| 841 |
+
|
| 842 |
+
return queries[:4] # Limit to 4 queries to avoid spam
|
| 843 |
+
|
| 844 |
+
def _extract_key_terms(self, text: str) -> List[str]:
|
| 845 |
+
"""Extract key terms from text for focused searching"""
|
| 846 |
+
# Remove common stop words
|
| 847 |
+
stop_words = {
|
| 848 |
+
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with',
|
| 849 |
+
'by', 'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'do', 'does',
|
| 850 |
+
'did', 'will', 'would', 'could', 'should', 'may', 'might', 'can', 'what', 'how',
|
| 851 |
+
'when', 'where', 'why', 'who', 'which', 'this', 'that', 'these', 'those'
|
| 852 |
+
}
|
| 853 |
+
|
| 854 |
+
# Extract words, filter stop words, and prioritize longer terms
|
| 855 |
+
words = re.findall(r'\b[a-zA-Z]{3,}\b', text.lower())
|
| 856 |
+
key_terms = [word for word in words if word not in stop_words]
|
| 857 |
+
|
| 858 |
+
# Sort by length (longer terms usually more specific)
|
| 859 |
+
return sorted(set(key_terms), key=len, reverse=True)
|
| 860 |
+
|
| 861 |
+
def search_duckduckgo(self, query: str, max_results: int = 5) -> List[Dict[str, str]]:
|
| 862 |
+
"""Search using DuckDuckGo Instant Answer API (privacy-focused)"""
|
| 863 |
+
try:
|
| 864 |
+
# DuckDuckGo Instant Answer API
|
| 865 |
+
url = "https://api.duckduckgo.com/"
|
| 866 |
+
params = {
|
| 867 |
+
'q': query,
|
| 868 |
+
'format': 'json',
|
| 869 |
+
'no_redirect': '1',
|
| 870 |
+
'no_html': '1',
|
| 871 |
+
'skip_disambig': '1'
|
| 872 |
+
}
|
| 873 |
+
|
| 874 |
+
response = requests.get(url, params=params, headers=self.headers, timeout=self.timeout)
|
| 875 |
+
response.raise_for_status()
|
| 876 |
+
data = response.json()
|
| 877 |
+
|
| 878 |
+
results = []
|
| 879 |
+
|
| 880 |
+
# Extract instant answer
|
| 881 |
+
if data.get('Abstract'):
|
| 882 |
+
results.append({
|
| 883 |
+
'title': data.get('Heading', 'DuckDuckGo Instant Answer'),
|
| 884 |
+
'snippet': data['Abstract'][:500],
|
| 885 |
+
'url': data.get('AbstractURL', ''),
|
| 886 |
+
'source': 'DuckDuckGo Instant Answer'
|
| 887 |
+
})
|
| 888 |
+
|
| 889 |
+
# Extract related topics
|
| 890 |
+
for topic in data.get('RelatedTopics', [])[:3]:
|
| 891 |
+
if isinstance(topic, dict) and topic.get('Text'):
|
| 892 |
+
results.append({
|
| 893 |
+
'title': topic.get('Text', '')[:100],
|
| 894 |
+
'snippet': topic.get('Text', '')[:400],
|
| 895 |
+
'url': topic.get('FirstURL', ''),
|
| 896 |
+
'source': 'DuckDuckGo Related'
|
| 897 |
+
})
|
| 898 |
+
|
| 899 |
+
return results[:max_results]
|
| 900 |
+
|
| 901 |
+
except Exception as e:
|
| 902 |
+
print(f"π DuckDuckGo search error: {e}")
|
| 903 |
+
return []
|
| 904 |
+
|
| 905 |
+
def search_wikipedia(self, query: str, max_results: int = 3) -> List[Dict[str, str]]:
|
| 906 |
+
"""Search Wikipedia for factual information"""
|
| 907 |
+
try:
|
| 908 |
+
# Simple Wikipedia search without the wikipedia library
|
| 909 |
+
search_url = "https://en.wikipedia.org/api/rest_v1/page/summary/"
|
| 910 |
+
|
| 911 |
+
# Try direct page lookup first
|
| 912 |
+
safe_query = quote_plus(query.replace(' ', '_'))
|
| 913 |
+
response = requests.get(
|
| 914 |
+
f"{search_url}{safe_query}",
|
| 915 |
+
headers=self.headers,
|
| 916 |
+
timeout=self.timeout
|
| 917 |
+
)
|
| 918 |
+
|
| 919 |
+
results = []
|
| 920 |
+
if response.status_code == 200:
|
| 921 |
+
data = response.json()
|
| 922 |
+
if not data.get('type') == 'disambiguation':
|
| 923 |
+
results.append({
|
| 924 |
+
'title': data.get('title', query),
|
| 925 |
+
'snippet': data.get('extract', '')[:500],
|
| 926 |
+
'url': data.get('content_urls', {}).get('desktop', {}).get('page', ''),
|
| 927 |
+
'source': 'Wikipedia'
|
| 928 |
+
})
|
| 929 |
+
|
| 930 |
+
# If no direct match, try search API
|
| 931 |
+
if not results:
|
| 932 |
+
search_api = "https://en.wikipedia.org/api/rest_v1/page/search/"
|
| 933 |
+
search_response = requests.get(
|
| 934 |
+
f"{search_api}{quote_plus(query)}",
|
| 935 |
+
headers=self.headers,
|
| 936 |
+
timeout=self.timeout
|
| 937 |
+
)
|
| 938 |
+
|
| 939 |
+
if search_response.status_code == 200:
|
| 940 |
+
search_data = search_response.json()
|
| 941 |
+
for page in search_data.get('pages', [])[:max_results]:
|
| 942 |
+
results.append({
|
| 943 |
+
'title': page.get('title', ''),
|
| 944 |
+
'snippet': page.get('description', '')[:400],
|
| 945 |
+
'url': f"https://en.wikipedia.org/wiki/{quote_plus(page.get('key', ''))}",
|
| 946 |
+
'source': 'Wikipedia Search'
|
| 947 |
+
})
|
| 948 |
+
|
| 949 |
+
return results
|
| 950 |
+
|
| 951 |
+
except Exception as e:
|
| 952 |
+
print(f"π Wikipedia search error: {e}")
|
| 953 |
+
return []
|
| 954 |
+
|
| 955 |
+
def search_web_comprehensive(self, prompt: str, domain: str) -> Dict[str, Any]:
|
| 956 |
+
"""Comprehensive web search combining multiple sources"""
|
| 957 |
+
self.search_count += 1
|
| 958 |
+
search_start_time = time.time()
|
| 959 |
+
|
| 960 |
+
# Check cache first
|
| 961 |
+
cache_key = f"{prompt}_{domain}"
|
| 962 |
+
if cache_key in self.cached_results:
|
| 963 |
+
cached_result = self.cached_results[cache_key]
|
| 964 |
+
if time.time() - cached_result['timestamp'] < 3600: # 1 hour cache
|
| 965 |
+
print(f"πΎ Using cached search results for: {prompt[:50]}...")
|
| 966 |
+
return cached_result['data']
|
| 967 |
+
|
| 968 |
+
print(f"π Hybrid Search #{self.search_count}: '{prompt[:50]}...' (Domain: {domain})")
|
| 969 |
+
|
| 970 |
+
# Generate smart search queries
|
| 971 |
+
search_queries = self.generate_smart_search_queries(prompt, domain)
|
| 972 |
+
|
| 973 |
+
all_results = []
|
| 974 |
+
search_sources = []
|
| 975 |
+
|
| 976 |
+
# Use ThreadPoolExecutor for concurrent searches
|
| 977 |
+
with ThreadPoolExecutor(max_workers=3) as executor:
|
| 978 |
+
futures = []
|
| 979 |
+
|
| 980 |
+
# Submit search tasks
|
| 981 |
+
for query in search_queries[:2]: # Limit to 2 queries for speed
|
| 982 |
+
futures.append(executor.submit(self.search_duckduckgo, query, 3))
|
| 983 |
+
futures.append(executor.submit(self.search_wikipedia, query, 2))
|
| 984 |
+
|
| 985 |
+
# Collect results with timeout
|
| 986 |
+
for future in futures:
|
| 987 |
+
try:
|
| 988 |
+
results = future.result(timeout=self.timeout)
|
| 989 |
+
all_results.extend(results)
|
| 990 |
+
if results:
|
| 991 |
+
search_sources.append(results[0]['source'])
|
| 992 |
+
except TimeoutError:
|
| 993 |
+
print("β° Search timeout occurred")
|
| 994 |
+
except Exception as e:
|
| 995 |
+
print(f"β Search error: {e}")
|
| 996 |
+
|
| 997 |
+
# Remove duplicates and rank results
|
| 998 |
+
unique_results = []
|
| 999 |
+
seen_snippets = set()
|
| 1000 |
+
|
| 1001 |
+
for result in all_results:
|
| 1002 |
+
snippet_key = result['snippet'][:100].lower()
|
| 1003 |
+
if snippet_key not in seen_snippets and len(result['snippet']) > 50:
|
| 1004 |
+
seen_snippets.add(snippet_key)
|
| 1005 |
+
unique_results.append(result)
|
| 1006 |
+
|
| 1007 |
+
search_time = time.time() - search_start_time
|
| 1008 |
+
|
| 1009 |
+
# Create comprehensive search result
|
| 1010 |
+
search_result = {
|
| 1011 |
+
'results': unique_results[:6], # Top 6 results
|
| 1012 |
+
'search_queries': search_queries,
|
| 1013 |
+
'search_time': search_time,
|
| 1014 |
+
'sources_used': list(set(search_sources)),
|
| 1015 |
+
'total_results': len(unique_results),
|
| 1016 |
+
'search_successful': len(unique_results) > 0,
|
| 1017 |
+
'domain': domain,
|
| 1018 |
+
'timestamp': time.time()
|
| 1019 |
+
}
|
| 1020 |
+
|
| 1021 |
+
# Cache the result
|
| 1022 |
+
self.cached_results[cache_key] = {
|
| 1023 |
+
'data': search_result,
|
| 1024 |
+
'timestamp': time.time()
|
| 1025 |
+
}
|
| 1026 |
+
|
| 1027 |
+
# Store in search history
|
| 1028 |
+
self.search_history.append({
|
| 1029 |
+
'prompt': prompt[:100],
|
| 1030 |
+
'domain': domain,
|
| 1031 |
+
'results_count': len(unique_results),
|
| 1032 |
+
'search_time': search_time,
|
| 1033 |
+
'timestamp': time.time()
|
| 1034 |
+
})
|
| 1035 |
+
|
| 1036 |
+
# Keep only recent history
|
| 1037 |
+
if len(self.search_history) > 50:
|
| 1038 |
+
self.search_history = self.search_history[-50:]
|
| 1039 |
+
|
| 1040 |
+
print(f"β
Search completed: {len(unique_results)} results in {search_time:.2f}s")
|
| 1041 |
+
return search_result
|
| 1042 |
+
|
| 1043 |
+
def format_search_results_for_ai(self, search_data: Dict[str, Any]) -> str:
|
| 1044 |
+
"""Format search results for AI processing"""
|
| 1045 |
+
if not search_data['search_successful']:
|
| 1046 |
+
return "No relevant web search results found."
|
| 1047 |
+
|
| 1048 |
+
formatted_results = []
|
| 1049 |
+
formatted_results.append(f"**π Web Search Results ({search_data['total_results']} sources found in {search_data['search_time']:.1f}s):**\n")
|
| 1050 |
+
|
| 1051 |
+
for i, result in enumerate(search_data['results'], 1):
|
| 1052 |
+
formatted_results.append(f"**Source {i} ({result['source']}):**")
|
| 1053 |
+
formatted_results.append(f"Title: {result['title']}")
|
| 1054 |
+
formatted_results.append(f"Content: {result['snippet']}")
|
| 1055 |
+
if result['url']:
|
| 1056 |
+
formatted_results.append(f"URL: {result['url']}")
|
| 1057 |
+
formatted_results.append("") # Empty line for separation
|
| 1058 |
+
|
| 1059 |
+
formatted_results.append(f"**Search Sources:** {', '.join(search_data['sources_used'])}")
|
| 1060 |
+
|
| 1061 |
+
return "\n".join(formatted_results)
|
| 1062 |
+
|
| 1063 |
+
def get_search_stats(self) -> Dict[str, Any]:
|
| 1064 |
+
"""Get search engine statistics"""
|
| 1065 |
+
if not self.search_history:
|
| 1066 |
+
return {"status": "No searches performed"}
|
| 1067 |
+
|
| 1068 |
+
recent_searches = self.search_history[-10:]
|
| 1069 |
+
avg_search_time = sum(s['search_time'] for s in recent_searches) / len(recent_searches)
|
| 1070 |
+
avg_results = sum(s['results_count'] for s in recent_searches) / len(recent_searches)
|
| 1071 |
+
|
| 1072 |
+
domain_counts = {}
|
| 1073 |
+
for search in recent_searches:
|
| 1074 |
+
domain = search['domain']
|
| 1075 |
+
domain_counts[domain] = domain_counts.get(domain, 0) + 1
|
| 1076 |
+
|
| 1077 |
+
return {
|
| 1078 |
+
'total_searches': self.search_count,
|
| 1079 |
+
'avg_search_time': f"{avg_search_time:.2f}s",
|
| 1080 |
+
'avg_results_per_search': f"{avg_results:.1f}",
|
| 1081 |
+
'cache_size': len(self.cached_results),
|
| 1082 |
+
'popular_domains': domain_counts,
|
| 1083 |
+
'recent_searches': len(recent_searches)
|
| 1084 |
+
}
|
| 1085 |
+
|
| 1086 |
+
|
| 1087 |
class UltimateMambaSwarm:
|
| 1088 |
+
"""Ultimate Mamba Swarm with Hybrid Intelligence combining local AI with web search"""
|
| 1089 |
|
| 1090 |
def __init__(self):
|
| 1091 |
self.model_loader = UltimateModelLoader()
|
| 1092 |
self.performance_monitor = AdvancedPerformanceMonitor()
|
| 1093 |
+
self.search_engine = HybridIntelligenceSearchEngine() # New hybrid intelligence
|
| 1094 |
self.model_loaded = False
|
| 1095 |
self.current_model_size = "auto"
|
| 1096 |
|
|
|
|
| 1433 |
|
| 1434 |
def generate_text_ultimate(self, prompt: str, max_length: int = 200, temperature: float = 0.7,
|
| 1435 |
top_p: float = 0.9, num_encoders: int = 12, model_size: str = "auto",
|
| 1436 |
+
show_routing: bool = True, enable_search: bool = True) -> Tuple[str, str]:
|
| 1437 |
+
"""π Hybrid Intelligence Generation: Combines local AI with real-time web search"""
|
| 1438 |
|
| 1439 |
start_time = time.time()
|
| 1440 |
|
|
|
|
| 1450 |
# Advanced domain detection
|
| 1451 |
domain, confidence = self.detect_domain_advanced(prompt)
|
| 1452 |
|
| 1453 |
+
# π HYBRID INTELLIGENCE: Check if web search is needed
|
| 1454 |
+
search_data = None
|
| 1455 |
+
web_context = ""
|
| 1456 |
+
|
| 1457 |
+
if enable_search and self.search_engine.needs_current_info(prompt, domain):
|
| 1458 |
+
print(f"π Hybrid Intelligence activated - searching web for current information...")
|
| 1459 |
+
search_data = self.search_engine.search_web_comprehensive(prompt, domain)
|
| 1460 |
+
|
| 1461 |
+
if search_data['search_successful']:
|
| 1462 |
+
web_context = self.search_engine.format_search_results_for_ai(search_data)
|
| 1463 |
+
print(f"β
Web search successful: {search_data['total_results']} sources integrated")
|
| 1464 |
+
else:
|
| 1465 |
+
print(f"β οΈ Web search returned no results")
|
| 1466 |
+
|
| 1467 |
# Advanced encoder routing
|
| 1468 |
routing_info = self.simulate_advanced_encoder_routing(
|
| 1469 |
domain, confidence, num_encoders, self.current_model_size
|
| 1470 |
)
|
| 1471 |
|
| 1472 |
+
# π§ ENHANCED GENERATION: Local AI + Web Intelligence
|
| 1473 |
if self.model_loaded:
|
| 1474 |
+
print(f"π§ Using hybrid model inference: {self.model_loader.model_name} + Web Intelligence")
|
| 1475 |
+
response = self._generate_with_hybrid_intelligence(
|
| 1476 |
+
prompt, max_length, temperature, top_p, domain, web_context
|
| 1477 |
+
)
|
| 1478 |
else:
|
| 1479 |
+
print(f"π Using hybrid fallback system (enhanced with web data)")
|
| 1480 |
+
response = self._generate_hybrid_fallback(prompt, domain, web_context)
|
| 1481 |
|
| 1482 |
# Quality validation
|
| 1483 |
is_gibberish = self.model_loader._is_gibberish_advanced(response) if self.model_loaded else False
|
| 1484 |
|
| 1485 |
if is_gibberish:
|
| 1486 |
+
logger.warning("π« Gibberish detected, using enhanced hybrid fallback")
|
| 1487 |
+
response = self._generate_hybrid_fallback(prompt, domain, web_context)
|
| 1488 |
is_gibberish = True # Mark for monitoring
|
| 1489 |
|
| 1490 |
# Performance logging
|
|
|
|
| 1495 |
generation_time, token_count, True, domain, is_gibberish
|
| 1496 |
)
|
| 1497 |
|
| 1498 |
+
# Create enhanced routing display with search info
|
| 1499 |
routing_display = ""
|
| 1500 |
if show_routing:
|
| 1501 |
+
routing_display = self._create_hybrid_routing_display(
|
| 1502 |
+
routing_info, generation_time, token_count, search_data
|
| 1503 |
)
|
| 1504 |
|
| 1505 |
return response, routing_display
|
| 1506 |
|
| 1507 |
except Exception as e:
|
| 1508 |
+
logger.error(f"Hybrid generation error: {e}")
|
| 1509 |
self.performance_monitor.log_generation(0, 0, False)
|
| 1510 |
+
return f"Hybrid generation error occurred. Using enhanced fallback response.", ""
|
| 1511 |
+
|
| 1512 |
+
def _generate_with_hybrid_intelligence(self, prompt: str, max_length: int, temperature: float,
|
| 1513 |
+
top_p: float, domain: str, web_context: str) -> str:
|
| 1514 |
+
"""π Generate using loaded model enhanced with web intelligence"""
|
| 1515 |
+
try:
|
| 1516 |
+
print(f"π― Hybrid Generation for domain: {domain}")
|
| 1517 |
+
|
| 1518 |
+
# Get optimal parameters
|
| 1519 |
+
gen_params = self.model_loader.get_optimal_generation_params(temperature, top_p, max_length)
|
| 1520 |
+
|
| 1521 |
+
# Create hybrid prompt with web context
|
| 1522 |
+
if web_context:
|
| 1523 |
+
hybrid_prompt = f"""Based on the following current web information and your knowledge, provide a comprehensive response:
|
| 1524 |
+
|
| 1525 |
+
WEB CONTEXT:
|
| 1526 |
+
{web_context[:1500]}
|
| 1527 |
+
|
| 1528 |
+
USER QUESTION: {prompt}
|
| 1529 |
+
|
| 1530 |
+
COMPREHENSIVE RESPONSE:"""
|
| 1531 |
+
print(f"π Using hybrid prompt with web context ({len(web_context)} chars)")
|
| 1532 |
+
else:
|
| 1533 |
+
# Fall back to regular generation if no web context
|
| 1534 |
+
return self._generate_with_ultimate_model(prompt, max_length, temperature, top_p, domain)
|
| 1535 |
+
|
| 1536 |
+
# Domain-specific parameter adjustments for hybrid generation
|
| 1537 |
+
if domain == 'code':
|
| 1538 |
+
gen_params.update({
|
| 1539 |
+
"temperature": min(gen_params.get("temperature", 0.4), 0.5),
|
| 1540 |
+
"top_p": min(gen_params.get("top_p", 0.85), 0.9),
|
| 1541 |
+
"repetition_penalty": 1.1
|
| 1542 |
+
})
|
| 1543 |
+
elif domain in ['medical', 'legal', 'science']:
|
| 1544 |
+
# More conservative for factual domains with web data
|
| 1545 |
+
gen_params.update({
|
| 1546 |
+
"temperature": min(gen_params.get("temperature", 0.5), 0.6),
|
| 1547 |
+
"top_p": min(gen_params.get("top_p", 0.8), 0.85),
|
| 1548 |
+
"repetition_penalty": 1.2
|
| 1549 |
+
})
|
| 1550 |
+
else:
|
| 1551 |
+
# Balanced approach for other domains
|
| 1552 |
+
gen_params.update({
|
| 1553 |
+
"temperature": min(gen_params.get("temperature", 0.7), 0.8),
|
| 1554 |
+
"repetition_penalty": 1.15
|
| 1555 |
+
})
|
| 1556 |
+
|
| 1557 |
+
print(f"π Hybrid params: temp={gen_params['temperature']:.2f}, top_p={gen_params['top_p']:.2f}")
|
| 1558 |
+
|
| 1559 |
+
# Tokenize hybrid prompt
|
| 1560 |
+
inputs = self.model_loader.tokenizer.encode(
|
| 1561 |
+
hybrid_prompt,
|
| 1562 |
+
return_tensors="pt",
|
| 1563 |
+
truncation=True,
|
| 1564 |
+
max_length=700 # Larger context for web data
|
| 1565 |
+
)
|
| 1566 |
+
inputs = inputs.to(self.model_loader.device)
|
| 1567 |
+
|
| 1568 |
+
# Generate with hybrid intelligence
|
| 1569 |
+
with torch.no_grad():
|
| 1570 |
+
outputs = self.model_loader.model.generate(inputs, **gen_params)
|
| 1571 |
+
|
| 1572 |
+
# Decode and validate
|
| 1573 |
+
generated_text = self.model_loader.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 1574 |
+
|
| 1575 |
+
# Extract response safely
|
| 1576 |
+
if "COMPREHENSIVE RESPONSE:" in generated_text:
|
| 1577 |
+
response = generated_text.split("COMPREHENSIVE RESPONSE:")[-1].strip()
|
| 1578 |
+
elif generated_text.startswith(hybrid_prompt):
|
| 1579 |
+
response = generated_text[len(hybrid_prompt):].strip()
|
| 1580 |
+
else:
|
| 1581 |
+
response = generated_text.strip()
|
| 1582 |
+
|
| 1583 |
+
# Enhanced validation for hybrid responses
|
| 1584 |
+
if self._is_inappropriate_content(response):
|
| 1585 |
+
logger.warning("π‘οΈ Inappropriate hybrid content detected, using fallback")
|
| 1586 |
+
return self._generate_hybrid_fallback(prompt, domain, web_context)
|
| 1587 |
+
|
| 1588 |
+
if self._is_response_too_generic(response, prompt, domain):
|
| 1589 |
+
logger.warning("π Generic hybrid response detected, using enhanced fallback")
|
| 1590 |
+
return self._generate_hybrid_fallback(prompt, domain, web_context)
|
| 1591 |
+
|
| 1592 |
+
# Add web source attribution if response uses web data
|
| 1593 |
+
if web_context and len(response) > 100:
|
| 1594 |
+
response += "\n\n*Response enhanced with current web information*"
|
| 1595 |
+
|
| 1596 |
+
return response if response else "I'm processing your hybrid request..."
|
| 1597 |
+
|
| 1598 |
+
except Exception as e:
|
| 1599 |
+
logger.error(f"Hybrid model generation error: {e}")
|
| 1600 |
+
return self._generate_hybrid_fallback(prompt, domain, web_context)
|
| 1601 |
+
|
| 1602 |
+
def _generate_hybrid_fallback(self, prompt: str, domain: str, web_context: str = "") -> str:
|
| 1603 |
+
"""π Enhanced fallback responses with web intelligence integration"""
|
| 1604 |
+
|
| 1605 |
+
# If we have web context, create an enhanced response
|
| 1606 |
+
if web_context:
|
| 1607 |
+
web_summary = self._extract_web_summary(web_context)
|
| 1608 |
+
base_response = self._generate_ultimate_fallback(prompt, domain)
|
| 1609 |
+
|
| 1610 |
+
# Enhance with web information
|
| 1611 |
+
enhanced_response = f"""{base_response}
|
| 1612 |
+
|
| 1613 |
+
**π Current Web Information:**
|
| 1614 |
+
{web_summary}
|
| 1615 |
+
|
| 1616 |
+
*This response combines domain expertise with current web information for enhanced accuracy.*"""
|
| 1617 |
+
|
| 1618 |
+
return enhanced_response
|
| 1619 |
+
else:
|
| 1620 |
+
# Fall back to standard ultimate fallback
|
| 1621 |
+
return self._generate_ultimate_fallback(prompt, domain)
|
| 1622 |
+
|
| 1623 |
+
def _extract_web_summary(self, web_context: str) -> str:
|
| 1624 |
+
"""Extract key information from web context for integration"""
|
| 1625 |
+
if not web_context:
|
| 1626 |
+
return ""
|
| 1627 |
+
|
| 1628 |
+
# Extract key sentences from web results
|
| 1629 |
+
sentences = re.split(r'[.!?]+', web_context)
|
| 1630 |
+
key_sentences = []
|
| 1631 |
+
|
| 1632 |
+
for sentence in sentences:
|
| 1633 |
+
sentence = sentence.strip()
|
| 1634 |
+
if (len(sentence) > 50 and
|
| 1635 |
+
any(word in sentence.lower() for word in ['research', 'study', 'analysis', 'data', 'evidence', 'findings', 'reports', 'according', 'statistics'])):
|
| 1636 |
+
key_sentences.append(sentence)
|
| 1637 |
+
if len(key_sentences) >= 3: # Limit to 3 key sentences
|
| 1638 |
+
break
|
| 1639 |
+
|
| 1640 |
+
if key_sentences:
|
| 1641 |
+
return "β’ " + "\nβ’ ".join(key_sentences)
|
| 1642 |
+
else:
|
| 1643 |
+
# If no key sentences found, return first substantial paragraph
|
| 1644 |
+
paragraphs = web_context.split('\n\n')
|
| 1645 |
+
for para in paragraphs:
|
| 1646 |
+
if len(para.strip()) > 100:
|
| 1647 |
+
return para.strip()[:400] + "..."
|
| 1648 |
+
|
| 1649 |
+
return "Current information from web sources integrated."
|
| 1650 |
|
| 1651 |
def _generate_with_ultimate_model(self, prompt: str, max_length: int, temperature: float, top_p: float, domain: str = 'general') -> str:
|
| 1652 |
"""Generate using loaded model with ultimate optimization and content safety"""
|
|
|
|
| 2242 |
- **Context History**: {len(self.domain_context_history)} entries
|
| 2243 |
- **Learning Domains**: {', '.join(self.learned_patterns.keys()) if self.learned_patterns else 'Initializing'}
|
| 2244 |
|
| 2245 |
+
**π Mamba Status**: Ready for GPU activation (mamba_ssm commented out)
|
| 2246 |
+
"""
|
| 2247 |
+
|
| 2248 |
+
def _create_hybrid_routing_display(self, routing_info: Dict, generation_time: float,
|
| 2249 |
+
token_count: int, search_data: Optional[Dict] = None) -> str:
|
| 2250 |
+
"""π Create hybrid intelligence routing display with web search metrics"""
|
| 2251 |
+
# Hide the actual model name and just show CPU Mode to keep Mamba branding
|
| 2252 |
+
model_info = "CPU Mode + Web Intelligence" if self.model_loaded else "Initializing Hybrid System"
|
| 2253 |
+
perf_stats = self.performance_monitor.get_comprehensive_stats()
|
| 2254 |
+
search_stats = self.search_engine.get_search_stats()
|
| 2255 |
+
|
| 2256 |
+
# Build search section
|
| 2257 |
+
search_section = ""
|
| 2258 |
+
if search_data:
|
| 2259 |
+
if search_data['search_successful']:
|
| 2260 |
+
search_section = f"""
|
| 2261 |
+
**π Hybrid Web Intelligence:**
|
| 2262 |
+
- **Search Status**: β
Active ({search_data['total_results']} sources found)
|
| 2263 |
+
- **Search Time**: {search_data['search_time']:.2f}s
|
| 2264 |
+
- **Sources Used**: {', '.join(search_data['sources_used'])}
|
| 2265 |
+
- **Search Queries**: {len(search_data['search_queries'])} optimized queries
|
| 2266 |
+
- **Intelligence Mode**: π Local AI + Real-time Web Data"""
|
| 2267 |
+
else:
|
| 2268 |
+
search_section = f"""
|
| 2269 |
+
**π Hybrid Web Intelligence:**
|
| 2270 |
+
- **Search Status**: β οΈ No current data needed
|
| 2271 |
+
- **Intelligence Mode**: π§ Local AI Knowledge Base"""
|
| 2272 |
+
else:
|
| 2273 |
+
search_section = f"""
|
| 2274 |
+
**π Hybrid Web Intelligence:**
|
| 2275 |
+
- **Search Status**: π€ Offline Mode (local knowledge only)
|
| 2276 |
+
- **Intelligence Mode**: π§ Pure Local AI Processing"""
|
| 2277 |
+
|
| 2278 |
+
return f"""
|
| 2279 |
+
## π Mamba Encoder Swarm - Hybrid Intelligence Analysis
|
| 2280 |
+
|
| 2281 |
+
**π― Advanced Domain Intelligence:**
|
| 2282 |
+
- **Primary Domain**: {routing_info['domain'].title()}
|
| 2283 |
+
- **Confidence Level**: {routing_info['domain_confidence']:.1%}
|
| 2284 |
+
- **Routing Precision**: {"π’ High" if routing_info['domain_confidence'] > 0.7 else "π‘ Medium" if routing_info['domain_confidence'] > 0.4 else "π΄ Low"}
|
| 2285 |
+
- **Efficiency Rating**: {routing_info['efficiency_rating']:.1%}
|
| 2286 |
+
{search_section}
|
| 2287 |
+
|
| 2288 |
+
**β‘ Mamba Swarm Performance:**
|
| 2289 |
+
- **Architecture**: Mamba Encoder Swarm (Hybrid Intelligence Mode)
|
| 2290 |
+
- **Model Size**: {routing_info['model_size'].title()}
|
| 2291 |
+
- **Selected Encoders**: {routing_info['total_active']}/100
|
| 2292 |
+
- **Hardware**: {self.model_loader.device}
|
| 2293 |
+
- **Quality Assurance**: β
Multi-layer Protection + Web Validation
|
| 2294 |
+
|
| 2295 |
+
**π Real-time Performance Analytics:**
|
| 2296 |
+
- **Generation Time**: {generation_time:.2f}s
|
| 2297 |
+
- **Token Output**: {token_count} tokens
|
| 2298 |
+
- **Processing Speed**: {token_count/generation_time:.1f} tok/s
|
| 2299 |
+
- **Success Rate**: {perf_stats.get('success_rate', 'N/A')}
|
| 2300 |
+
- **Quality Rate**: {perf_stats.get('quality_rate', 'N/A')}
|
| 2301 |
+
- **System Uptime**: {perf_stats.get('uptime', 'N/A')}
|
| 2302 |
+
|
| 2303 |
+
**π Search Engine Analytics:**
|
| 2304 |
+
- **Total Searches**: {search_stats.get('total_searches', 0)}
|
| 2305 |
+
- **Avg Search Time**: {search_stats.get('avg_search_time', 'N/A')}
|
| 2306 |
+
- **Avg Results/Search**: {search_stats.get('avg_results_per_search', 'N/A')}
|
| 2307 |
+
- **Cache Efficiency**: {search_stats.get('cache_size', 0)} cached results
|
| 2308 |
+
|
| 2309 |
+
**π’ Elite Encoder Distribution:**
|
| 2310 |
+
Primary: {', '.join(map(str, routing_info['selected_encoders'][:8]))}
|
| 2311 |
+
Secondary: {', '.join(map(str, routing_info['selected_encoders'][8:16]))}{'...' if len(routing_info['selected_encoders']) > 16 else ''}
|
| 2312 |
+
|
| 2313 |
+
**ποΈ Confidence Analytics:**
|
| 2314 |
+
- **Average**: {np.mean(routing_info['confidence_scores']):.3f}
|
| 2315 |
+
- **Range**: {min(routing_info['confidence_scores']):.3f} - {max(routing_info['confidence_scores']):.3f}
|
| 2316 |
+
- **Std Dev**: {np.std(routing_info['confidence_scores']):.3f}
|
| 2317 |
+
|
| 2318 |
+
**π‘οΈ Hybrid Quality Assurance:**
|
| 2319 |
+
- **Gibberish Prevention**: Active
|
| 2320 |
+
- **Parameter Optimization**: Dynamic + Context-Aware
|
| 2321 |
+
- **Fallback Protection**: Multi-layer + Web-Enhanced
|
| 2322 |
+
- **Source Validation**: Real-time fact checking
|
| 2323 |
+
|
| 2324 |
+
**π§ Adaptive Learning System:**
|
| 2325 |
+
- **Interactions Processed**: {self.interaction_count}
|
| 2326 |
+
- **Learned Patterns**: {sum(len(patterns.get('phrases', {})) for patterns in self.learned_patterns.values())}
|
| 2327 |
+
- **Context History**: {len(self.domain_context_history)} entries
|
| 2328 |
+
- **Learning Domains**: {', '.join(self.learned_patterns.keys()) if self.learned_patterns else 'Initializing'}
|
| 2329 |
+
|
| 2330 |
+
**π Hybrid Intelligence Status**: Local AI + Web Search Ready
|
| 2331 |
**π Mamba Status**: Ready for GPU activation (mamba_ssm commented out)
|
| 2332 |
"""
|
| 2333 |
|
|
|
|
| 2343 |
return success
|
| 2344 |
|
| 2345 |
def get_ultimate_system_info(self) -> str:
|
| 2346 |
+
"""Get hybrid intelligence system information display"""
|
| 2347 |
memory_info = psutil.virtual_memory()
|
| 2348 |
gpu_info = "CPU Only"
|
| 2349 |
if torch.cuda.is_available():
|
|
|
|
| 2352 |
gpu_info += f" ({gpu_memory:.1f}GB)"
|
| 2353 |
|
| 2354 |
perf_stats = self.performance_monitor.get_comprehensive_stats()
|
| 2355 |
+
search_stats = self.search_engine.get_search_stats()
|
| 2356 |
model_info = self.model_loader.get_model_info()
|
| 2357 |
|
| 2358 |
return f"""
|
| 2359 |
+
## οΏ½ Mamba Encoder Swarm - Hybrid Intelligence Dashboard
|
| 2360 |
+
|
| 2361 |
+
**π Hybrid Architecture Status**: β
Local AI + Web Intelligence Active
|
| 2362 |
+
- **Intelligence Level**: Revolutionary Hybrid Multi-Domain AI
|
| 2363 |
+
- **Processing Mode**: Mamba Encoder Swarm + Real-time Web Search
|
| 2364 |
+
- **Current Configuration**: CPU-Optimized AI + Internet-Connected Intelligence
|
| 2365 |
+
- **Activation Status**: Hybrid mode active, Mamba encoders ready for GPU
|
| 2366 |
|
| 2367 |
+
**π Hybrid Intelligence Features:**
|
| 2368 |
+
- **Web Search Engine**: β
DuckDuckGo + Wikipedia Integration
|
| 2369 |
+
- **Smart Query Detection**: β
Automatic current info detection
|
| 2370 |
+
- **Source Integration**: β
Real-time fact checking and validation
|
| 2371 |
+
- **Cache System**: οΏ½οΏ½οΏ½ Intelligent result caching for performance
|
| 2372 |
|
| 2373 |
**π» Hardware Configuration:**
|
| 2374 |
- **Processing Unit**: {gpu_info}
|
| 2375 |
- **System RAM**: {memory_info.total / (1024**3):.1f}GB ({memory_info.percent:.1f}% used)
|
| 2376 |
- **Available RAM**: {memory_info.available / (1024**3):.1f}GB
|
| 2377 |
+
- **Network**: β
Internet connectivity for hybrid intelligence
|
| 2378 |
- **Mamba Readiness**: {"π’ GPU Ready for Mamba Activation" if torch.cuda.is_available() else "π‘ CPU Mode - GPU Needed for Mamba"}
|
| 2379 |
|
| 2380 |
+
**π Hybrid Performance Analytics:**
|
| 2381 |
- **Total Requests**: {perf_stats.get('total_requests', 0)}
|
| 2382 |
- **Success Rate**: {perf_stats.get('success_rate', 'N/A')}
|
| 2383 |
- **Quality Rate**: {perf_stats.get('quality_rate', 'N/A')}
|
|
|
|
| 2385 |
- **Model Adaptations**: {perf_stats.get('model_switches', 0)}
|
| 2386 |
- **Quality Filters Activated**: {perf_stats.get('gibberish_prevented', 0)}
|
| 2387 |
|
| 2388 |
+
**π Web Intelligence Analytics:**
|
| 2389 |
+
- **Total Searches**: {search_stats.get('total_searches', 0)}
|
| 2390 |
+
- **Avg Search Time**: {search_stats.get('avg_search_time', 'N/A')}
|
| 2391 |
+
- **Search Success Rate**: {"High" if search_stats.get('total_searches', 0) > 0 else "Ready"}
|
| 2392 |
+
- **Cache Efficiency**: {search_stats.get('cache_size', 0)} results cached
|
| 2393 |
+
- **Popular Domains**: {', '.join(search_stats.get('popular_domains', {}).keys()) or 'Initializing'}
|
| 2394 |
+
|
| 2395 |
+
**π― Adaptive Domain Intelligence:**
|
| 2396 |
- **Supported Domains**: {len(self.base_domain_patterns)} specialized domains with adaptive learning
|
| 2397 |
- **Encoder Pool**: 100 virtual encoders with dynamic routing
|
| 2398 |
+
- **Quality Protection**: Multi-layer intelligence validation + web fact-checking
|
| 2399 |
+
- **Learning Systems**: Revolutionary 4-layer adaptive learning + web pattern recognition
|
| 2400 |
+
|
| 2401 |
+
**π Hybrid Capabilities:**
|
| 2402 |
+
- **Local AI Mode**: High-performance CPU processing with GPT-2 models
|
| 2403 |
+
- **Web Intelligence**: Real-time information retrieval and integration
|
| 2404 |
+
- **Smart Routing**: Automatic detection of queries needing current information
|
| 2405 |
+
- **Source Attribution**: Transparent web source integration and validation
|
| 2406 |
+
- **Hybrid Fallbacks**: Enhanced responses combining local knowledge + web data
|
| 2407 |
+
|
| 2408 |
+
**π Mamba Encoder Status:**
|
| 2409 |
+
- **Current Mode**: CPU Alternative with hybrid web intelligence
|
| 2410 |
+
- **GPU Readiness**: Ready for Mamba activation (requires uncommenting mamba_ssm)
|
| 2411 |
+
- **Architecture**: Full Mamba swarm intelligence preserved + web enhancement
|
| 2412 |
"""
|
| 2413 |
|
| 2414 |
|
|
|
|
| 2418 |
swarm = UltimateMambaSwarm()
|
| 2419 |
|
| 2420 |
with gr.Blocks(
|
| 2421 |
+
title="Mamba Encoder Swarm - Hybrid Intelligence",
|
| 2422 |
theme=gr.themes.Soft(),
|
| 2423 |
css="""
|
| 2424 |
.gradio-container { max-width: 1600px; margin: auto; }
|
|
|
|
| 2445 |
) as demo:
|
| 2446 |
|
| 2447 |
gr.Markdown("""
|
| 2448 |
+
# οΏ½ Mamba Encoder Swarm v2.0 - Hybrid Intelligence
|
| 2449 |
|
| 2450 |
+
**π Revolutionary AI combining Local Processing + Real-time Web Search**
|
| 2451 |
|
| 2452 |
Features intelligent Mamba encoder swarm architecture with advanced domain routing, comprehensive performance analytics, and multi-tier quality protection. *Currently optimized for CPU with GPU Mamba encoders ready for activation.*
|
| 2453 |
|
|
|
|
| 2487 |
label="π€ Model Size Selection"
|
| 2488 |
)
|
| 2489 |
show_routing = gr.Checkbox(label="π Show Intelligence Analysis", value=True)
|
| 2490 |
+
|
| 2491 |
+
with gr.Row():
|
| 2492 |
+
enable_search = gr.Checkbox(
|
| 2493 |
+
label="π Enable Hybrid Web Intelligence",
|
| 2494 |
+
value=True,
|
| 2495 |
+
info="Automatically search web for current information when needed"
|
| 2496 |
+
)
|
| 2497 |
|
| 2498 |
generate_btn = gr.Button("π Generate Response", variant="primary", size="lg")
|
| 2499 |
|
|
|
|
| 2546 |
# Event handlers
|
| 2547 |
generate_btn.click(
|
| 2548 |
fn=swarm.generate_text_ultimate,
|
| 2549 |
+
inputs=[prompt_input, max_length, temperature, top_p, num_encoders, model_size, show_routing, enable_search],
|
| 2550 |
outputs=[response_output, routing_output]
|
| 2551 |
)
|
| 2552 |
|
|
|
|
| 2555 |
outputs=system_info
|
| 2556 |
)
|
| 2557 |
|
| 2558 |
+
# Hybrid Intelligence Footer
|
| 2559 |
gr.Markdown("""
|
| 2560 |
---
|
| 2561 |
+
### π Hybrid Intelligence System Features
|
| 2562 |
+
- **π Revolutionary Web Integration** - Real-time search with DuckDuckGo + Wikipedia
|
| 2563 |
+
- **π§ Smart Query Detection** - Automatically identifies when current information is needed
|
| 2564 |
- **π― Elite Domain Routing** - 7 specialized domains with confidence-based encoder selection
|
| 2565 |
+
- **β‘ Advanced State-Space Processing** - Intelligent encoder swarm architecture + web intelligence
|
| 2566 |
+
- **π‘οΈ Enhanced Quality Assurance** - Multi-layer validation + web fact-checking
|
| 2567 |
+
- **π Comprehensive Analytics** - Real-time performance + search metrics monitoring
|
| 2568 |
+
- **π Hybrid Fallbacks** - Local knowledge enhanced with real-time web data
|
| 2569 |
+
- **ποΈ Intelligent Control** - Adaptive model switching + search optimization
|
| 2570 |
+
- **π Adaptive Learning** - 4-layer machine learning + web pattern recognition
|
| 2571 |
+
- **οΏ½ Mamba Ready** - Full architecture preserved, ready for GPU activation
|
| 2572 |
+
|
| 2573 |
+
**π Hybrid Intelligence Mode**: Combining the best of local AI processing with real-time web search capabilities for unprecedented accuracy and current information access.
|
| 2574 |
|
| 2575 |
**Current Status**: π₯οΈ CPU Mode Active | π Mamba Encoders Ready for GPU Activation | β‘ Instant Hardware Detection
|
| 2576 |
""")
|