Spaces:
Running
Running
Update app.py
Browse filesFixed Update app.py
app.py
CHANGED
|
@@ -1,369 +1,576 @@
|
|
| 1 |
-
|
| 2 |
-
ProVerBs Ultimate Brain with Complete Voice Cloning
|
| 3 |
-
Integrates Supertonic voice cloning with all controls
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
from
|
| 17 |
-
import
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
from
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
"""
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
</
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
βββ
|
| 2 |
+
ProVerBs Ultimate Brain with Complete Voice Cloning - FIXED VERSION
|
| 3 |
+
Integrates Supertonic voice cloning with all controls
|
| 4 |
+
βββ
|
| 5 |
+
|
| 6 |
+
import sys
|
| 7 |
+
import os
|
| 8 |
+
import asyncio
|
| 9 |
+
from typing import Dict, List, Optional
|
| 10 |
+
|
| 11 |
+
# Add current directory to path
|
| 12 |
+
|
| 13 |
+
sys.path.append(os.path.dirname(**file**))
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
from huggingface_hub import InferenceClient
|
| 17 |
+
import json
|
| 18 |
+
from datetime import datetime
|
| 19 |
+
|
| 20 |
+
# Try importing optional modules with fallbacks
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from unified_brain import UnifiedBrain, ReasoningContext
|
| 24 |
+
UNIFIED_BRAIN_AVAILABLE = True
|
| 25 |
+
except ImportError:
|
| 26 |
+
UNIFIED_BRAIN_AVAILABLE = False
|
| 27 |
+
print(βWarning: unified_brain module not available - using fallbackβ)
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
from performance_optimizer import performance_cache, performance_monitor, with_caching
|
| 31 |
+
PERFORMANCE_AVAILABLE = True
|
| 32 |
+
except ImportError:
|
| 33 |
+
PERFORMANCE_AVAILABLE = False
|
| 34 |
+
print(βWarning: performance_optimizer module not availableβ)
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
from analytics_seo import analytics_tracker, SEOOptimizer
|
| 38 |
+
ANALYTICS_AVAILABLE = True
|
| 39 |
+
except ImportError:
|
| 40 |
+
ANALYTICS_AVAILABLE = False
|
| 41 |
+
print(βWarning: analytics_seo module not availableβ)
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
from supertonic_voice_module import create_supertonic_interface
|
| 45 |
+
VOICE_AVAILABLE = True
|
| 46 |
+
except ImportError:
|
| 47 |
+
VOICE_AVAILABLE = False
|
| 48 |
+
print(βWarning: supertonic_voice_module not availableβ)
|
| 49 |
+
|
| 50 |
+
# ============================================================================
|
| 51 |
+
|
| 52 |
+
# MOCK CLASSES FOR MISSING DEPENDENCIES (Fallback)
|
| 53 |
+
|
| 54 |
+
# ============================================================================
|
| 55 |
+
|
| 56 |
+
class MockUnifiedBrain:
|
| 57 |
+
βββFallback when unified_brain is not availableβββ
|
| 58 |
+
async def process(self, query: str, preferences: dict, execution_mode: str):
|
| 59 |
+
return {
|
| 60 |
+
βsuccessβ: False,
|
| 61 |
+
βresultsβ: [],
|
| 62 |
+
βmessageβ: βUnified brain module not availableβ
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
class MockPerformanceCache:
|
| 66 |
+
def get_stats(self):
|
| 67 |
+
return {βstatusβ: βCache module not availableβ}
|
| 68 |
+
def clear(self):
|
| 69 |
+
return {βstatusβ: βclearedβ}
|
| 70 |
+
|
| 71 |
+
class MockPerformanceMonitor:
|
| 72 |
+
def get_metrics(self):
|
| 73 |
+
return {βstatusβ: βMonitor module not availableβ}
|
| 74 |
+
|
| 75 |
+
class MockAnalyticsTracker:
|
| 76 |
+
def get_analytics(self):
|
| 77 |
+
return {βstatusβ: βAnalytics module not availableβ}
|
| 78 |
+
|
| 79 |
+
class MockSEOOptimizer:
|
| 80 |
+
@staticmethod
|
| 81 |
+
def get_meta_tags():
|
| 82 |
+
return β<meta name="description" content="ProVerBs Legal AI">β
|
| 83 |
+
|
| 84 |
+
```
|
| 85 |
+
@staticmethod
|
| 86 |
+
def get_structured_data():
|
| 87 |
+
return '<script type="application/ld+json">{}</script>'
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
# Initialize mocks if needed
|
| 91 |
+
|
| 92 |
+
if not UNIFIED_BRAIN_AVAILABLE:
|
| 93 |
+
UnifiedBrain = MockUnifiedBrain
|
| 94 |
+
|
| 95 |
+
if not PERFORMANCE_AVAILABLE:
|
| 96 |
+
performance_cache = MockPerformanceCache()
|
| 97 |
+
performance_monitor = MockPerformanceMonitor()
|
| 98 |
+
|
| 99 |
+
if not ANALYTICS_AVAILABLE:
|
| 100 |
+
analytics_tracker = MockAnalyticsTracker()
|
| 101 |
+
SEOOptimizer = MockSEOOptimizer()
|
| 102 |
+
|
| 103 |
+
# ============================================================================
|
| 104 |
+
|
| 105 |
+
# MAIN CLASS DEFINITION (Must come BEFORE instantiation)
|
| 106 |
+
|
| 107 |
+
# ============================================================================
|
| 108 |
+
|
| 109 |
+
class UltimateLegalBrain:
|
| 110 |
+
βββMain brain class for legal AI processingβββ
|
| 111 |
+
|
| 112 |
+
```
|
| 113 |
+
def __init__(self):
|
| 114 |
+
self.brain = UnifiedBrain()
|
| 115 |
+
self.legal_modes = {
|
| 116 |
+
"navigation": "π Navigation Guide",
|
| 117 |
+
"general": "π¬ General Legal",
|
| 118 |
+
"document_validation": "π Document Validator",
|
| 119 |
+
"legal_research": "π Legal Research",
|
| 120 |
+
"etymology": "π Etymology Expert",
|
| 121 |
+
"case_management": "πΌ Case Management",
|
| 122 |
+
"regulatory_updates": "π Regulatory Updates"
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
async def process_legal_query(
|
| 126 |
+
self,
|
| 127 |
+
query: str,
|
| 128 |
+
mode: str,
|
| 129 |
+
ai_provider: str = "huggingface",
|
| 130 |
+
use_reasoning_protocols: bool = True,
|
| 131 |
+
**kwargs
|
| 132 |
+
) -> Dict:
|
| 133 |
+
"""Process legal query with reasoning protocols"""
|
| 134 |
+
reasoning_result = None
|
| 135 |
+
|
| 136 |
+
if use_reasoning_protocols and UNIFIED_BRAIN_AVAILABLE:
|
| 137 |
+
preferences = {
|
| 138 |
+
'use_reflection': mode in ['document_validation', 'legal_research'],
|
| 139 |
+
'multi_agent': False
|
| 140 |
+
}
|
| 141 |
+
try:
|
| 142 |
+
reasoning_result = await self.brain.process(
|
| 143 |
+
query=query,
|
| 144 |
+
preferences=preferences,
|
| 145 |
+
execution_mode='sequential'
|
| 146 |
+
)
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Reasoning error: {e}")
|
| 149 |
+
reasoning_result = None
|
| 150 |
+
|
| 151 |
+
legal_prompt = self.get_legal_system_prompt(mode)
|
| 152 |
+
|
| 153 |
+
if reasoning_result and reasoning_result.get('success'):
|
| 154 |
+
reasoning_trace = "\n".join([
|
| 155 |
+
f"π§ {r['protocol']}: {', '.join(r.get('trace', [])[:2])}"
|
| 156 |
+
for r in reasoning_result.get('results', [])
|
| 157 |
+
])
|
| 158 |
+
enhanced_query = f"{legal_prompt}\n\nReasoning Analysis:\n{reasoning_trace}\n\nUser Query: {query}"
|
| 159 |
+
else:
|
| 160 |
+
enhanced_query = f"{legal_prompt}\n\nUser Query: {query}"
|
| 161 |
+
|
| 162 |
+
return {
|
| 163 |
+
"enhanced_query": enhanced_query,
|
| 164 |
+
"reasoning_result": reasoning_result,
|
| 165 |
+
"mode": mode,
|
| 166 |
+
"ai_provider": ai_provider
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
def get_legal_system_prompt(self, mode: str) -> str:
|
| 170 |
+
"""Get system prompt for specific legal mode"""
|
| 171 |
+
prompts = {
|
| 172 |
+
"navigation": "You are a ProVerBs Legal AI Navigation Guide with advanced reasoning capabilities.",
|
| 173 |
+
"general": "You are a General Legal Assistant powered by ADAPPT-Iβ’ reasoning technology.",
|
| 174 |
+
"document_validation": "You are a Document Validator using Chain-of-Thought and Self-Consistency protocols.",
|
| 175 |
+
"legal_research": "You are a Legal Research Assistant with RAG and Tree-of-Thoughts capabilities.",
|
| 176 |
+
"etymology": "You are a Legal Etymology Expert with multi-step reasoning.",
|
| 177 |
+
"case_management": "You are a Case Management Helper with ReAct protocol integration.",
|
| 178 |
+
"regulatory_updates": "You are a Regulatory Monitor with real-time analysis capabilities."
|
| 179 |
+
}
|
| 180 |
+
return prompts.get(mode, prompts["general"])
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
# ============================================================================
|
| 184 |
+
|
| 185 |
+
# INITIALIZE BRAIN (After class definition)
|
| 186 |
+
|
| 187 |
+
# ============================================================================
|
| 188 |
+
|
| 189 |
+
ultimate_brain = UltimateLegalBrain()
|
| 190 |
+
|
| 191 |
+
# ============================================================================
|
| 192 |
+
|
| 193 |
+
# RESPONSE HANDLER
|
| 194 |
+
|
| 195 |
+
# ============================================================================
|
| 196 |
+
|
| 197 |
+
def respond_with_ultimate_brain(
|
| 198 |
+
message: str,
|
| 199 |
+
history: list,
|
| 200 |
+
mode: str,
|
| 201 |
+
ai_provider: str,
|
| 202 |
+
use_reasoning: bool,
|
| 203 |
+
max_tokens: int,
|
| 204 |
+
temperature: float,
|
| 205 |
+
top_p: float,
|
| 206 |
+
request: gr.Request = None
|
| 207 |
+
):
|
| 208 |
+
βββMain response handler - synchronous wrapper for async processingβββ
|
| 209 |
+
|
| 210 |
+
```
|
| 211 |
+
# Handle async processing synchronously for Gradio
|
| 212 |
+
try:
|
| 213 |
+
# Create new event loop for async call
|
| 214 |
+
loop = asyncio.new_event_loop()
|
| 215 |
+
asyncio.set_event_loop(loop)
|
| 216 |
+
brain_result = loop.run_until_complete(
|
| 217 |
+
ultimate_brain.process_legal_query(
|
| 218 |
+
query=message,
|
| 219 |
+
mode=mode,
|
| 220 |
+
ai_provider=ai_provider,
|
| 221 |
+
use_reasoning_protocols=use_reasoning
|
| 222 |
+
)
|
| 223 |
+
)
|
| 224 |
+
loop.close()
|
| 225 |
+
except Exception as e:
|
| 226 |
+
yield f"β οΈ Reasoning processing error: {str(e)}\n\nContinuing with standard processing...\n\n"
|
| 227 |
+
brain_result = {
|
| 228 |
+
'enhanced_query': ultimate_brain.get_legal_system_prompt(mode) + f"\n\nUser Query: {message}",
|
| 229 |
+
'reasoning_result': None,
|
| 230 |
+
'mode': mode,
|
| 231 |
+
'ai_provider': ai_provider
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
# Show reasoning info if available
|
| 235 |
+
reasoning_info = ""
|
| 236 |
+
if use_reasoning and brain_result.get('reasoning_result'):
|
| 237 |
+
reasoning_info = "π§ **Reasoning Protocols Applied:**\n"
|
| 238 |
+
for r in brain_result['reasoning_result'].get('results', []):
|
| 239 |
+
reasoning_info += f"- {r.get('protocol', 'Unknown')}: β
{r.get('status', 'completed')}\n"
|
| 240 |
+
reasoning_info += "\n\n"
|
| 241 |
+
yield reasoning_info
|
| 242 |
+
|
| 243 |
+
# Handle different AI providers
|
| 244 |
+
if ai_provider == "huggingface":
|
| 245 |
+
try:
|
| 246 |
+
# Get token from environment or request
|
| 247 |
+
hf_token = os.environ.get("HF_TOKEN") or (request.headers.get("authorization", "").replace("Bearer ", "") if request else None)
|
| 248 |
+
|
| 249 |
+
client = InferenceClient(
|
| 250 |
+
token=hf_token,
|
| 251 |
+
model="meta-llama/Llama-3.3-70B-Instruct"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Build message history
|
| 255 |
+
messages = [{"role": "system", "content": brain_result['enhanced_query']}]
|
| 256 |
+
|
| 257 |
+
for user_msg, assistant_msg in history:
|
| 258 |
+
if user_msg:
|
| 259 |
+
messages.append({"role": "user", "content": user_msg})
|
| 260 |
+
if assistant_msg:
|
| 261 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 262 |
+
|
| 263 |
+
messages.append({"role": "user", "content": message})
|
| 264 |
+
|
| 265 |
+
# Stream response
|
| 266 |
+
response = reasoning_info
|
| 267 |
+
for chunk in client.chat_completion(
|
| 268 |
+
messages,
|
| 269 |
+
max_tokens=max_tokens,
|
| 270 |
+
stream=True,
|
| 271 |
+
temperature=temperature,
|
| 272 |
+
top_p=top_p
|
| 273 |
+
):
|
| 274 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 275 |
+
response += chunk.choices[0].delta.content
|
| 276 |
+
yield response
|
| 277 |
+
|
| 278 |
+
except Exception as e:
|
| 279 |
+
yield f"{reasoning_info}\n\nβ HuggingFace API Error: {str(e)}\n\nPlease check your API token or try another provider."
|
| 280 |
+
|
| 281 |
+
else:
|
| 282 |
+
# Other providers not yet implemented
|
| 283 |
+
yield f"{reasoning_info}\n\nβ οΈ Provider '{ai_provider}' is not yet implemented. Using HuggingFace as fallback or implement your own provider logic."
|
| 284 |
+
```
|
| 285 |
+
|
| 286 |
+
# ============================================================================
|
| 287 |
+
|
| 288 |
+
# MOCK VOICE INTERFACE (if supertonic not available)
|
| 289 |
+
|
| 290 |
+
# ============================================================================
|
| 291 |
+
|
| 292 |
+
def create_mock_voice_interface():
|
| 293 |
+
βββFallback voice interface when supertonic module is unavailableβββ
|
| 294 |
+
gr.Markdown(βββ
|
| 295 |
+
## ποΈ Voice Cloning Module
|
| 296 |
+
|
| 297 |
+
```
|
| 298 |
+
β οΈ **Supertonic voice module not found.**
|
| 299 |
+
|
| 300 |
+
To enable voice cloning:
|
| 301 |
+
1. Install required dependencies
|
| 302 |
+
2. Add `supertonic_voice_module.py` to your project
|
| 303 |
+
3. Restart the application
|
| 304 |
+
|
| 305 |
+
### Expected Features:
|
| 306 |
+
- Voice recording with professional controls
|
| 307 |
+
- Text-to-speech with voice cloning
|
| 308 |
+
- Audio playback and export
|
| 309 |
+
- Voice profile management
|
| 310 |
+
""")
|
| 311 |
+
|
| 312 |
+
with gr.Row():
|
| 313 |
+
gr.Button("π€ Record (Not Available)", interactive=False)
|
| 314 |
+
gr.Button("βΈοΈ Pause (Not Available)", interactive=False)
|
| 315 |
+
gr.Button("βΉοΈ Stop (Not Available)", interactive=False)
|
| 316 |
+
```
|
| 317 |
+
|
| 318 |
+
# ============================================================================
|
| 319 |
+
|
| 320 |
+
# CUSTOM CSS
|
| 321 |
+
|
| 322 |
+
# ============================================================================
|
| 323 |
+
|
| 324 |
+
custom_css = βββ
|
| 325 |
+
.gradio-container { max-width: 1400px !important; }
|
| 326 |
+
.header-section {
|
| 327 |
+
text-align: center; padding: 40px 20px;
|
| 328 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 329 |
+
color: white; border-radius: 12px; margin-bottom: 30px;
|
| 330 |
+
}
|
| 331 |
+
.header-section h1 { font-size: 3rem; margin-bottom: 10px; font-weight: 700; }
|
| 332 |
+
.brain-badge {
|
| 333 |
+
display: inline-block; background: #ff6b6b; color: white;
|
| 334 |
+
padding: 8px 16px; border-radius: 20px; font-weight: bold;
|
| 335 |
+
margin: 10px 5px;
|
| 336 |
+
}
|
| 337 |
+
βββ
|
| 338 |
+
|
| 339 |
+
# SEO
|
| 340 |
+
|
| 341 |
+
seo_meta = SEOOptimizer.get_meta_tags()
|
| 342 |
+
seo_structured = SEOOptimizer.get_structured_data()
|
| 343 |
+
|
| 344 |
+
# ============================================================================
|
| 345 |
+
|
| 346 |
+
# GRADIO INTERFACE
|
| 347 |
+
|
| 348 |
+
# ============================================================================
|
| 349 |
+
|
| 350 |
+
demo = gr.Blocks(title=βProVerBs Ultimate Legal AI Brainβ, css=custom_css)
|
| 351 |
+
|
| 352 |
+
with demo:
|
| 353 |
+
# Add SEO tags
|
| 354 |
+
gr.HTML(seo_meta + seo_structured)
|
| 355 |
+
|
| 356 |
+
```
|
| 357 |
+
# Header
|
| 358 |
+
gr.HTML("""
|
| 359 |
+
<div class="header-section">
|
| 360 |
+
<h1>βοΈ ProVerBs Ultimate Legal AI Brain</h1>
|
| 361 |
+
<p style="font-size: 1.3rem;">Powered by Pro'VerBsβ’ & ADAPPT-Iβ’ Technology</p>
|
| 362 |
+
<div>
|
| 363 |
+
<span class="brain-badge">π§ 100+ Reasoning Protocols</span>
|
| 364 |
+
<span class="brain-badge">π€ 6 AI Models</span>
|
| 365 |
+
<span class="brain-badge">βοΈ 7 Legal Modes</span>
|
| 366 |
+
<span class="brain-badge">ποΈ Voice Cloning</span>
|
| 367 |
+
</div>
|
| 368 |
+
<p style="font-size: 0.9rem; margin-top: 15px; opacity: 0.9;">
|
| 369 |
+
Chain-of-Thought β’ Self-Consistency β’ Tree-of-Thoughts β’ ReAct β’ Reflexion β’ RAG<br>
|
| 370 |
+
Quantum Reasoning β’ Multi-Agent β’ Voice Cloning β’ Audio Processing
|
| 371 |
+
</p>
|
| 372 |
+
</div>
|
| 373 |
+
""")
|
| 374 |
+
|
| 375 |
+
with gr.Tabs():
|
| 376 |
+
# Welcome Tab
|
| 377 |
+
with gr.Tab("π Welcome"):
|
| 378 |
+
gr.Markdown("""
|
| 379 |
+
## Welcome to the Ultimate ProVerBs Legal AI Brain
|
| 380 |
+
|
| 381 |
+
### π§ Unified Reasoning Brain (100+ Protocols)
|
| 382 |
+
|
| 383 |
+
**Core Reasoning Protocols:**
|
| 384 |
+
- Chain-of-Thought (CoT) - Step-by-step reasoning
|
| 385 |
+
- Self-Consistency - Multiple reasoning paths
|
| 386 |
+
- Tree-of-Thoughts (ToT) - Branching exploration
|
| 387 |
+
- ReAct - Reason + Act cycles
|
| 388 |
+
- Reflexion - Self-reflection with memory
|
| 389 |
+
- RAG - Retrieval-Augmented Generation
|
| 390 |
+
|
| 391 |
+
### π€ 6 AI Model Options:
|
| 392 |
+
- π€ HuggingFace Llama-3.3-70B (Free, always available)
|
| 393 |
+
- π§ GPT-4 Turbo (OpenAI) - Coming Soon
|
| 394 |
+
- β¨ Gemini 3.0 (Google) - Coming Soon
|
| 395 |
+
- π Perplexity AI (Research) - Coming Soon
|
| 396 |
+
- π₯· Ninja AI - Coming Soon
|
| 397 |
+
- π» LM Studio (Local) - Coming Soon
|
| 398 |
+
|
| 399 |
+
### βοΈ 7 Specialized Legal Modes:
|
| 400 |
+
- Navigation | General Legal | Document Validation
|
| 401 |
+
- Legal Research | Etymology | Case Management | Regulatory Updates
|
| 402 |
+
|
| 403 |
+
### ποΈ Voice Cloning:
|
| 404 |
+
- Record voice samples
|
| 405 |
+
- Clone voices with text-to-speech
|
| 406 |
+
- Professional audio processing
|
| 407 |
+
- Voice profile management
|
| 408 |
+
|
| 409 |
+
**Get Started:** Click "π€ AI Legal Chatbot" tab!
|
| 410 |
+
""")
|
| 411 |
+
|
| 412 |
+
# AI Chatbot Tab
|
| 413 |
+
with gr.Tab("π€ AI Legal Chatbot"):
|
| 414 |
+
gr.Markdown("""
|
| 415 |
+
## Multi-AI Legal Chatbot
|
| 416 |
+
Select your AI model and legal assistant mode below!
|
| 417 |
+
""")
|
| 418 |
+
|
| 419 |
+
with gr.Row():
|
| 420 |
+
ai_provider_selector = gr.Dropdown(
|
| 421 |
+
choices=[
|
| 422 |
+
("π€ Llama-3.3-70B (Free)", "huggingface"),
|
| 423 |
+
("π§ GPT-4 Turbo", "gpt4"),
|
| 424 |
+
("β¨ Gemini 3.0", "gemini"),
|
| 425 |
+
("π Perplexity AI", "perplexity"),
|
| 426 |
+
("π₯· Ninja AI", "ninjaai"),
|
| 427 |
+
("π» LM Studio", "lmstudio")
|
| 428 |
+
],
|
| 429 |
+
value="huggingface",
|
| 430 |
+
label="π€ AI Model"
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
mode_selector = gr.Dropdown(
|
| 434 |
+
choices=[
|
| 435 |
+
("π Navigation", "navigation"),
|
| 436 |
+
("π¬ General Legal", "general"),
|
| 437 |
+
("π Document Validator", "document_validation"),
|
| 438 |
+
("π Legal Research", "legal_research"),
|
| 439 |
+
("π Etymology", "etymology"),
|
| 440 |
+
("πΌ Case Management", "case_management"),
|
| 441 |
+
("π Regulatory Updates", "regulatory_updates")
|
| 442 |
+
],
|
| 443 |
+
value="general",
|
| 444 |
+
label="βοΈ Legal Mode"
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
use_reasoning_toggle = gr.Checkbox(
|
| 448 |
+
label="π§ Enable Reasoning Protocols",
|
| 449 |
+
value=True,
|
| 450 |
+
info="Use 100+ reasoning protocols for enhanced analysis"
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
chatbot_interface = gr.ChatInterface(
|
| 454 |
+
respond_with_ultimate_brain,
|
| 455 |
+
chatbot=gr.Chatbot(
|
| 456 |
+
height=550,
|
| 457 |
+
placeholder="π¬ Ultimate Legal AI ready! Ask anything...",
|
| 458 |
+
show_label=False
|
| 459 |
+
),
|
| 460 |
+
textbox=gr.Textbox(
|
| 461 |
+
placeholder="Ask your legal question here...",
|
| 462 |
+
container=False,
|
| 463 |
+
scale=7
|
| 464 |
+
),
|
| 465 |
+
additional_inputs=[
|
| 466 |
+
mode_selector,
|
| 467 |
+
ai_provider_selector,
|
| 468 |
+
use_reasoning_toggle,
|
| 469 |
+
gr.Slider(128, 4096, value=2048, step=128, label="Max Tokens"),
|
| 470 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 471 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 472 |
+
],
|
| 473 |
+
examples=[
|
| 474 |
+
["What reasoning protocols are available?"],
|
| 475 |
+
["Analyze this contract using Chain-of-Thought reasoning"],
|
| 476 |
+
["Research case law with Tree-of-Thoughts exploration"],
|
| 477 |
+
["Explain legal etymology of 'habeas corpus'"]
|
| 478 |
+
],
|
| 479 |
+
cache_examples=False
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
# Voice Cloning Tab
|
| 483 |
+
with gr.Tab("ποΈ Voice Cloning"):
|
| 484 |
+
if VOICE_AVAILABLE:
|
| 485 |
+
create_supertonic_interface()
|
| 486 |
+
else:
|
| 487 |
+
create_mock_voice_interface()
|
| 488 |
+
|
| 489 |
+
# Analytics Tab
|
| 490 |
+
with gr.Tab("π Analytics"):
|
| 491 |
+
gr.Markdown("""
|
| 492 |
+
## Analytics & Performance Dashboard
|
| 493 |
+
View real-time analytics and performance metrics for the Ultimate Brain.
|
| 494 |
+
""")
|
| 495 |
+
|
| 496 |
+
with gr.Row():
|
| 497 |
+
analytics_btn = gr.Button("π Refresh Analytics", variant="primary")
|
| 498 |
+
clear_cache_btn = gr.Button("ποΈ Clear Cache", variant="secondary")
|
| 499 |
+
|
| 500 |
+
analytics_output = gr.JSON(label="Analytics Data")
|
| 501 |
+
performance_output = gr.JSON(label="Performance Metrics")
|
| 502 |
+
cache_stats_output = gr.JSON(label="Cache Statistics")
|
| 503 |
+
|
| 504 |
+
analytics_btn.click(
|
| 505 |
+
fn=lambda: (
|
| 506 |
+
analytics_tracker.get_analytics(),
|
| 507 |
+
performance_monitor.get_metrics(),
|
| 508 |
+
performance_cache.get_stats()
|
| 509 |
+
),
|
| 510 |
+
outputs=[analytics_output, performance_output, cache_stats_output]
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
clear_cache_btn.click(
|
| 514 |
+
fn=lambda: performance_cache.clear(),
|
| 515 |
+
outputs=[cache_stats_output]
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
+
# About Tab
|
| 519 |
+
with gr.Tab("βΉοΈ About"):
|
| 520 |
+
status_text = "β
All modules loaded" if all([
|
| 521 |
+
UNIFIED_BRAIN_AVAILABLE,
|
| 522 |
+
PERFORMANCE_AVAILABLE,
|
| 523 |
+
ANALYTICS_AVAILABLE,
|
| 524 |
+
VOICE_AVAILABLE
|
| 525 |
+
]) else "β οΈ Some modules unavailable (using fallbacks)"
|
| 526 |
+
|
| 527 |
+
gr.Markdown(f"""
|
| 528 |
+
## About ProVerBs Ultimate Legal AI Brain
|
| 529 |
+
|
| 530 |
+
### Status: {status_text}
|
| 531 |
+
|
| 532 |
+
**Module Status:**
|
| 533 |
+
- Unified Brain: {"β
" if UNIFIED_BRAIN_AVAILABLE else "β οΈ Fallback"}
|
| 534 |
+
- Performance: {"β
" if PERFORMANCE_AVAILABLE else "β οΈ Fallback"}
|
| 535 |
+
- Analytics: {"β
" if ANALYTICS_AVAILABLE else "β οΈ Fallback"}
|
| 536 |
+
- Voice Cloning: {"β
" if VOICE_AVAILABLE else "β οΈ Not Available"}
|
| 537 |
+
|
| 538 |
+
### π Revolutionary Features:
|
| 539 |
+
- **100+ Reasoning Protocols** - Most advanced reasoning system
|
| 540 |
+
- **6 AI Models** - Choose the best for your needs
|
| 541 |
+
- **7 Legal Modes** - Specialized for different legal tasks
|
| 542 |
+
- **Voice Cloning** - Professional Supertonic integration (when available)
|
| 543 |
+
|
| 544 |
+
### β οΈ Disclaimer:
|
| 545 |
+
This platform provides general legal information only. Consult with a licensed attorney for specific legal matters.
|
| 546 |
+
|
| 547 |
+
---
|
| 548 |
+
**Version 3.0.1 FIXED** | Built by Solomon7890
|
| 549 |
+
""")
|
| 550 |
+
|
| 551 |
+
# Footer
|
| 552 |
+
gr.Markdown("""
|
| 553 |
+
---
|
| 554 |
+
<div style="text-align: center; padding: 20px;">
|
| 555 |
+
<p><strong>βοΈ ProVerBs Ultimate Legal AI Brain v3.0.1</strong></p>
|
| 556 |
+
<p>Powered by Pro'VerBsβ’ & ADAPPT-Iβ’ | 100+ Protocols | 6 AI Models</p>
|
| 557 |
+
<p style="font-size: 0.85rem; color: #666;">
|
| 558 |
+
Β© 2025 Solomon 8888 | Built with β€οΈ for legal professionals worldwide
|
| 559 |
+
</p>
|
| 560 |
+
</div>
|
| 561 |
+
""")
|
| 562 |
+
```
|
| 563 |
+
|
| 564 |
+
# ============================================================================
|
| 565 |
+
|
| 566 |
+
# LAUNCH
|
| 567 |
+
|
| 568 |
+
# ============================================================================
|
| 569 |
+
|
| 570 |
+
if **name** == β**main**β:
|
| 571 |
+
demo.queue(max_size=20)
|
| 572 |
+
demo.launch(
|
| 573 |
+
server_name=β0.0.0.0β,
|
| 574 |
+
server_port=7860,
|
| 575 |
+
share=False
|
| 576 |
+
)
|