Spaces:
Running
Running
Fix: Runtime error and voice cloning buttons
Browse files
app.py
CHANGED
|
@@ -8,9 +8,122 @@ import sys
|
|
| 8 |
import os
|
| 9 |
sys.path.append(os.path.dirname(__file__))
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from supertonic_voice_module import create_supertonic_interface
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Override the demo with voice cloning integrated
|
| 15 |
demo_with_voice = gr.Blocks(title="ProVerBs Ultimate Legal AI Brain", css=custom_css)
|
| 16 |
|
|
|
|
| 8 |
import os
|
| 9 |
sys.path.append(os.path.dirname(__file__))
|
| 10 |
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from huggingface_hub import InferenceClient
|
| 13 |
+
import json
|
| 14 |
+
import os
|
| 15 |
+
from datetime import datetime
|
| 16 |
+
from typing import Dict, List, Optional
|
| 17 |
+
import requests
|
| 18 |
+
|
| 19 |
+
# Import Unified Brain
|
| 20 |
+
from unified_brain import UnifiedBrain, ReasoningContext
|
| 21 |
+
|
| 22 |
+
# Import Performance & Analytics
|
| 23 |
+
from performance_optimizer import performance_cache, performance_monitor, with_caching
|
| 24 |
+
from analytics_seo import analytics_tracker, SEOOptimizer
|
| 25 |
+
|
| 26 |
+
# Import Voice Cloning
|
| 27 |
from supertonic_voice_module import create_supertonic_interface
|
| 28 |
|
| 29 |
+
# Initialize
|
| 30 |
+
ultimate_brain = UltimateLegalBrain()
|
| 31 |
+
|
| 32 |
+
# Copy the respond function and other necessary code from app_ultimate_brain
|
| 33 |
+
class UltimateLegalBrain:
|
| 34 |
+
def __init__(self):
|
| 35 |
+
self.brain = UnifiedBrain()
|
| 36 |
+
self.legal_modes = {
|
| 37 |
+
"navigation": "π Navigation Guide",
|
| 38 |
+
"general": "π¬ General Legal",
|
| 39 |
+
"document_validation": "π Document Validator",
|
| 40 |
+
"legal_research": "π Legal Research",
|
| 41 |
+
"etymology": "π Etymology Expert",
|
| 42 |
+
"case_management": "πΌ Case Management",
|
| 43 |
+
"regulatory_updates": "π Regulatory Updates"
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
async def process_legal_query(self, query: str, mode: str, ai_provider: str = "huggingface", use_reasoning_protocols: bool = True, **kwargs) -> Dict:
|
| 47 |
+
reasoning_result = None
|
| 48 |
+
if use_reasoning_protocols:
|
| 49 |
+
preferences = {'use_reflection': mode in ['document_validation', 'legal_research'], 'multi_agent': False}
|
| 50 |
+
reasoning_result = await self.brain.process(query=query, preferences=preferences, execution_mode='sequential')
|
| 51 |
+
|
| 52 |
+
legal_prompt = self.get_legal_system_prompt(mode)
|
| 53 |
+
if reasoning_result and reasoning_result['success']:
|
| 54 |
+
reasoning_trace = "\n".join([f"π§ {r['protocol']}: {', '.join(r['trace'][:2])}" for r in reasoning_result['results']])
|
| 55 |
+
enhanced_query = f"{legal_prompt}\n\nReasoning Analysis:\n{reasoning_trace}\n\nUser Query: {query}"
|
| 56 |
+
else:
|
| 57 |
+
enhanced_query = f"{legal_prompt}\n\nUser Query: {query}"
|
| 58 |
+
|
| 59 |
+
return {"enhanced_query": enhanced_query, "reasoning_result": reasoning_result, "mode": mode, "ai_provider": ai_provider}
|
| 60 |
+
|
| 61 |
+
def get_legal_system_prompt(self, mode: str) -> str:
|
| 62 |
+
prompts = {
|
| 63 |
+
"navigation": "You are a ProVerBs Legal AI Navigation Guide with advanced reasoning capabilities.",
|
| 64 |
+
"general": "You are a General Legal Assistant powered by ADAPPT-Iβ’ reasoning technology.",
|
| 65 |
+
"document_validation": "You are a Document Validator using Chain-of-Thought and Self-Consistency protocols.",
|
| 66 |
+
"legal_research": "You are a Legal Research Assistant with RAG and Tree-of-Thoughts capabilities.",
|
| 67 |
+
"etymology": "You are a Legal Etymology Expert with multi-step reasoning.",
|
| 68 |
+
"case_management": "You are a Case Management Helper with ReAct protocol integration.",
|
| 69 |
+
"regulatory_updates": "You are a Regulatory Monitor with real-time analysis capabilities."
|
| 70 |
+
}
|
| 71 |
+
return prompts.get(mode, prompts["general"])
|
| 72 |
+
|
| 73 |
+
async def respond_with_ultimate_brain(message, history: list, mode: str, ai_provider: str, use_reasoning: bool, max_tokens: int, temperature: float, top_p: float, hf_token = None):
|
| 74 |
+
import time
|
| 75 |
+
start_time = time.time()
|
| 76 |
+
|
| 77 |
+
brain_result = await ultimate_brain.process_legal_query(query=message, mode=mode, ai_provider=ai_provider, use_reasoning_protocols=use_reasoning)
|
| 78 |
+
|
| 79 |
+
if use_reasoning and brain_result['reasoning_result']:
|
| 80 |
+
reasoning_info = "π§ **Reasoning Protocols Applied:**\n"
|
| 81 |
+
for r in brain_result['reasoning_result']['results']:
|
| 82 |
+
reasoning_info += f"- {r['protocol']}: β
{r['status']}\n"
|
| 83 |
+
yield reasoning_info + "\n\n"
|
| 84 |
+
|
| 85 |
+
if ai_provider == "huggingface":
|
| 86 |
+
token = hf_token.token if hf_token else None
|
| 87 |
+
client = InferenceClient(token=token, model="meta-llama/Llama-3.3-70B-Instruct")
|
| 88 |
+
|
| 89 |
+
messages = [{"role": "system", "content": brain_result['enhanced_query']}]
|
| 90 |
+
for user_msg, assistant_msg in history:
|
| 91 |
+
if user_msg:
|
| 92 |
+
messages.append({"role": "user", "content": user_msg})
|
| 93 |
+
if assistant_msg:
|
| 94 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 95 |
+
|
| 96 |
+
messages.append({"role": "user", "content": message})
|
| 97 |
+
|
| 98 |
+
response = reasoning_info if use_reasoning and brain_result['reasoning_result'] else ""
|
| 99 |
+
try:
|
| 100 |
+
for chunk in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
|
| 101 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 102 |
+
response += chunk.choices[0].delta.content
|
| 103 |
+
yield response
|
| 104 |
+
except Exception as e:
|
| 105 |
+
yield f"{response}\n\nβ Error: {str(e)}"
|
| 106 |
+
|
| 107 |
+
# Custom CSS
|
| 108 |
+
custom_css = """
|
| 109 |
+
.gradio-container { max-width: 1400px !important; }
|
| 110 |
+
.header-section {
|
| 111 |
+
text-align: center; padding: 40px 20px;
|
| 112 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 113 |
+
color: white; border-radius: 12px; margin-bottom: 30px;
|
| 114 |
+
}
|
| 115 |
+
.header-section h1 { font-size: 3rem; margin-bottom: 10px; font-weight: 700; }
|
| 116 |
+
.brain-badge {
|
| 117 |
+
display: inline-block; background: #ff6b6b; color: white;
|
| 118 |
+
padding: 8px 16px; border-radius: 20px; font-weight: bold;
|
| 119 |
+
margin: 10px 5px;
|
| 120 |
+
}
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
# SEO
|
| 124 |
+
seo_meta = SEOOptimizer.get_meta_tags()
|
| 125 |
+
seo_structured = SEOOptimizer.get_structured_data()
|
| 126 |
+
|
| 127 |
# Override the demo with voice cloning integrated
|
| 128 |
demo_with_voice = gr.Blocks(title="ProVerBs Ultimate Legal AI Brain", css=custom_css)
|
| 129 |
|