#!/usr/bin/env python3 # proverbs_ultimate_brain.py """ ProVerBs Ultimate Brain with Complete Voice Cloning - FIXED VERSION Integrates Supertonic voice cloning with all controls (mocked if unavailable). """ import sys import os import asyncio from typing import Dict, List, Optional, Any, Tuple # Add current directory to path sys.path.append(os.path.dirname(__file__)) import gradio as gr # Optional external client import (may fail; handled below) try: from huggingface_hub import InferenceClient HUGGINGFACE_AVAILABLE = True except Exception: HUGGINGFACE_AVAILABLE = False InferenceClient = None import json from datetime import datetime # Try importing optional modules with fallbacks try: from unified_brain import UnifiedBrain, ReasoningContext # type: ignore UNIFIED_BRAIN_AVAILABLE = True except Exception: UNIFIED_BRAIN_AVAILABLE = False UnifiedBrain = None print("Warning: unified_brain module not available - using fallback") try: from performance_optimizer import performance_cache, performance_monitor, with_caching # type: ignore PERFORMANCE_AVAILABLE = True except Exception: PERFORMANCE_AVAILABLE = False performance_cache = None performance_monitor = None print("Warning: performance_optimizer module not available - using fallback") try: from analytics_seo import analytics_tracker, SEOOptimizer # type: ignore ANALYTICS_AVAILABLE = True except Exception: ANALYTICS_AVAILABLE = False analytics_tracker = None SEOOptimizer = None print("Warning: analytics_seo module not available - using fallback") try: from supertonic_voice_module import create_supertonic_interface # type: ignore VOICE_AVAILABLE = True except Exception: VOICE_AVAILABLE = False create_supertonic_interface = None print("Warning: supertonic_voice_module not available - using fallback") # ===================================================================== # MOCK CLASSES / FALLBACKS # ===================================================================== class MockUnifiedBrain: """Fallback when unified_brain is not available""" async def process(self, query: str, preferences: dict, execution_mode: str): return { "success": False, "results": [], "message": "Unified brain module not available" } class MockPerformanceCache: def get_stats(self): return {"status": "Cache module not available"} def clear(self): return {"status": "cleared"} class MockPerformanceMonitor: def get_metrics(self): return {"status": "Monitor module not available"} class MockAnalyticsTracker: def get_analytics(self): return {"status": "Analytics module not available"} class MockSEOOptimizer: @staticmethod def get_meta_tags() -> str: return '' @staticmethod def get_structured_data() -> str: return '' # Initialize mocks if needed if not UNIFIED_BRAIN_AVAILABLE: UnifiedBrain = MockUnifiedBrain if not PERFORMANCE_AVAILABLE: performance_cache = MockPerformanceCache() performance_monitor = MockPerformanceMonitor() if not ANALYTICS_AVAILABLE: analytics_tracker = MockAnalyticsTracker() SEOOptimizer = MockSEOOptimizer() # ===================================================================== # MAIN CLASS DEFINITION # ===================================================================== class UltimateLegalBrain: """Main brain class for legal AI processing""" def __init__(self): # if UnifiedBrain is a class or a factory, instantiate; if it's a mock, it is class too try: self.brain = UnifiedBrain() except Exception: # Last-resort: use mock instance self.brain = MockUnifiedBrain() self.legal_modes = { "navigation": "π Navigation Guide", "general": "π¬ General Legal", "document_validation": "π Document Validator", "legal_research": "π Legal Research", "etymology": "π Etymology Expert", "case_management": "πΌ Case Management", "regulatory_updates": "π Regulatory Updates" } async def process_legal_query( self, query: str, mode: str, ai_provider: str = "huggingface", use_reasoning_protocols: bool = True, **kwargs ) -> Dict[str, Any]: """Process legal query with reasoning protocols""" reasoning_result = None if use_reasoning_protocols and UNIFIED_BRAIN_AVAILABLE: preferences = { "use_reflection": mode in ["document_validation", "legal_research"], "multi_agent": False } try: reasoning_result = await self.brain.process( query=query, preferences=preferences, execution_mode="sequential" ) except Exception as e: print(f"Reasoning error: {e}") reasoning_result = None legal_prompt = self.get_legal_system_prompt(mode) if reasoning_result and reasoning_result.get("success"): # produce a short trace if present reasoning_trace = "\n".join([ f"π§ {r.get('protocol','unknown')}: {', '.join(r.get('trace', [])[:2])}" for r in reasoning_result.get("results", []) ]) enhanced_query = f"{legal_prompt}\n\nReasoning Analysis:\n{reasoning_trace}\n\nUser Query: {query}" else: enhanced_query = f"{legal_prompt}\n\nUser Query: {query}" return { "enhanced_query": enhanced_query, "reasoning_result": reasoning_result, "mode": mode, "ai_provider": ai_provider } def get_legal_system_prompt(self, mode: str) -> str: """Get system prompt for specific legal mode""" prompts = { "navigation": "You are a ProVerBs Legal AI Navigation Guide with advanced reasoning capabilities.", "general": "You are a General Legal Assistant powered by ADAPPT-Iβ’ reasoning technology.", "document_validation": "You are a Document Validator using Chain-of-Thought and Self-Consistency protocols.", "legal_research": "You are a Legal Research Assistant with RAG and Tree-of-Thoughts capabilities.", "etymology": "You are a Legal Etymology Expert with multi-step reasoning.", "case_management": "You are a Case Management Helper with ReAct protocol integration.", "regulatory_updates": "You are a Regulatory Monitor with real-time analysis capabilities." } return prompts.get(mode, prompts["general"]) # ===================================================================== # INITIALIZE BRAIN # ===================================================================== ultimate_brain = UltimateLegalBrain() # ===================================================================== # RESPONSE HANDLER # ===================================================================== def respond_with_ultimate_brain( message: str, history: List[Tuple[Optional[str], Optional[str]]], mode: str, ai_provider: str, use_reasoning: bool, max_tokens: int, temperature: float, top_p: float, request: Optional[gr.Request] = None ): """ Main response handler - synchronous wrapper for async processing. This is a generator that yields partial or complete responses for streaming. """ # Run the async processing in a fresh event loop (to avoid "already running loop" errors) brain_result = None try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) brain_result = loop.run_until_complete( ultimate_brain.process_legal_query( query=message, mode=mode, ai_provider=ai_provider, use_reasoning_protocols=use_reasoning ) ) except Exception as e: # If something fails, yield a warning and continue with fallback prompt yield f"β οΈ Reasoning processing error: {str(e)}\n\nContinuing with standard processing...\n\n" brain_result = { "enhanced_query": ultimate_brain.get_legal_system_prompt(mode) + f"\n\nUser Query: {message}", "reasoning_result": None, "mode": mode, "ai_provider": ai_provider } finally: try: loop.close() except Exception: pass # Build reasoning info if available reasoning_info = "" if use_reasoning and brain_result.get("reasoning_result"): reasoning_info = "π§ **Reasoning Protocols Applied:**\n" for r in brain_result["reasoning_result"].get("results", []): reasoning_info += f"- {r.get('protocol', 'Unknown')}: β {r.get('status', 'completed')}\n" reasoning_info += "\n\n" yield reasoning_info # Handle the AI provider; currently only huggingface is implemented if ai_provider == "huggingface": # Build HF client if available, otherwise return the enhanced query as text if not HUGGINGFACE_AVAILABLE or InferenceClient is None: # Return enhanced query so user can see what would be asked yield reasoning_info + brain_result["enhanced_query"] return try: hf_token = os.environ.get("HF_TOKEN") # If running in Gradio request context, try to pull token from headers if not hf_token and request is not None: try: hf_token = request.headers.get("authorization", "").replace("Bearer ", "") except Exception: hf_token = None if not hf_token: yield reasoning_info + "β HuggingFace token not found. Set HF_TOKEN environment variable or pass token in request." return client = InferenceClient(token=hf_token, model="meta-llama/Llama-3.3-70B-Instruct") # Build message history messages = [{"role": "system", "content": brain_result["enhanced_query"]}] for user_msg, assistant_msg in history or []: if user_msg: messages.append({"role": "user", "content": user_msg}) if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) # Stream response using client.chat_completion if available response_text = reasoning_info # Defensive: check if client has chat_completion attribute if hasattr(client, "chat_completion"): for chunk in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p ): # chunk shape may vary; attempt to extract text try: delta = chunk.choices[0].delta content = delta.content if hasattr(delta, "content") else delta.get("content", "") except Exception: content = chunk.get("text", "") if isinstance(chunk, dict) else "" response_text += content yield response_text else: # If no streaming API, perform a single request (defensive) res = client.chat(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p) text = "" try: text = res.choices[0].message.content except Exception: text = str(res) response_text += text yield response_text except Exception as e: yield f"{reasoning_info}\n\nβ HuggingFace API Error: {str(e)}\n\nPlease check your API token or try another provider." else: # Other providers not yet implemented yield f"{reasoning_info}\n\nβ οΈ Provider '{ai_provider}' is not yet implemented. Using HuggingFace as fallback or implement your own provider logic." # ===================================================================== # MOCK VOICE INTERFACE (if supertonic not available) # ===================================================================== def create_mock_voice_interface(): """Fallback voice interface when supertonic module is unavailable""" # The function must place Gradio components in the current Blocks context. gr.Markdown(""" ## ποΈ Voice Cloning Module β οΈ **Supertonic voice module not found.** To enable voice cloning: 1. Install required dependencies 2. Add `supertonic_voice_module.py` to your project 3. Restart the application ### Expected Features: - Voice recording with professional controls - Text-to-speech with voice cloning - Audio playback and export - Voice profile management """) with gr.Row(): gr.Button("π€ Record (Not Available)", interactive=False) gr.Button("βΈοΈ Pause (Not Available)", interactive=False) gr.Button("βΉοΈ Stop (Not Available)", interactive=False) # ===================================================================== # CUSTOM CSS & SEO # ===================================================================== custom_css = """ .gradio-container { max-width: 1400px !important; } .header-section { text-align: center; padding: 40px 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 12px; margin-bottom: 30px; } .header-section h1 { font-size: 3rem; margin-bottom: 10px; font-weight: 700; } .brain-badge { display: inline-block; background: #ff6b6b; color: white; padding: 8px 16px; border-radius: 20px; font-weight: bold; margin: 10px 5px; } """ seo_meta = SEOOptimizer.get_meta_tags() if SEOOptimizer is not None else "" seo_structured = SEOOptimizer.get_structured_data() if SEOOptimizer is not None else "" # ===================================================================== # GRADIO INTERFACE # ===================================================================== demo = gr.Blocks(title="ProVerBs Ultimate Legal AI Brain", css=custom_css) with demo: # Add SEO tags (as HTML) if seo_meta or seo_structured: gr.HTML(seo_meta + seo_structured) # Header gr.HTML("""
Powered by Pro'VerBsβ’ & ADAPPT-Iβ’ Technology
Chain-of-Thought β’ Self-Consistency β’ Tree-of-Thoughts β’ ReAct β’ Reflexion β’ RAG
Quantum Reasoning β’ Multi-Agent β’ Voice Cloning β’ Audio Processing
βοΈ ProVerBs Ultimate Legal AI Brain v3.0.1
Powered by Pro'VerBsβ’ & ADAPPT-Iβ’ | 100+ Protocols | 6 AI Models
Β© 2025 Solomon 8888 | Built with β€οΈ for legal professionals worldwide