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