File size: 26,686 Bytes
d96041f
d7261e5
 
d96041f
 
d7261e5
 
 
 
 
d96041f
 
 
 
 
 
d7261e5
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
d96041f
 
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
d96041f
 
 
d7261e5
 
 
d96041f
d7261e5
d96041f
d7261e5
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
88c88a0
d96041f
88c88a0
 
 
 
d96041f
d7261e5
88c88a0
 
 
 
 
 
 
 
 
 
 
 
 
 
62bcb89
 
 
 
d96041f
d7261e5
88c88a0
62bcb89
93c73f4
 
d7261e5
62bcb89
 
d96041f
d7261e5
 
62bcb89
 
d7261e5
d96041f
88c88a0
 
62bcb89
 
 
 
 
 
 
88c88a0
d7261e5
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
d96041f
d7261e5
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
59969cc
d7261e5
b2aa74a
d7261e5
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
d96041f
 
 
 
d7261e5
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
 
d7261e5
 
d96041f
d7261e5
 
d96041f
 
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
d96041f
 
d7261e5
d96041f
 
 
 
 
d7261e5
 
d96041f
 
d7261e5
d96041f
 
d7261e5
 
 
d96041f
d7261e5
d96041f
 
d7261e5
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
d96041f
 
d7261e5
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
d96041f
 
d7261e5
 
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
d7261e5
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
d96041f
d7261e5
 
d96041f
d7261e5
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
d96041f
d7261e5
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
d96041f
d7261e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96041f
 
 
 
 
d7261e5
 
 
 
 
d96041f
 
 
 
d7261e5
 
 
d96041f
 
d7261e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
"""

ProVerBs Ultimate Brain with Complete Voice Cloning

Integrates Supertonic voice cloning with all controls

"""

# Import everything from app_ultimate_brain
import sys
import os
sys.path.append(os.path.dirname(__file__))

import gradio as gr
from huggingface_hub import InferenceClient
import json
import os
from datetime import datetime
from typing import Dict, List, Optional
import requests

# Import Unified Brain
from unified_brain import UnifiedBrain, ReasoningContext

# Import Performance & Analytics
from performance_optimizer import performance_cache, performance_monitor, with_caching
from analytics_seo import analytics_tracker, SEOOptimizer

# Import Voice Cloning
from supertonic_voice_module import create_supertonic_interface

# Import Document Processor
from document_processor import DocumentProcessor

# Define class FIRST
class UltimateLegalBrain:
    def __init__(self):
        self.brain = UnifiedBrain()
        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:
        reasoning_result = None
        if use_reasoning_protocols:
            preferences = {'use_reflection': mode in ['document_validation', 'legal_research'], 'multi_agent': False}
            reasoning_result = await self.brain.process(query=query, preferences=preferences, execution_mode='sequential')
        
        legal_prompt = self.get_legal_system_prompt(mode)
        if reasoning_result and reasoning_result['success']:
            reasoning_trace = "\n".join([f"🧠 {r['protocol']}: {', '.join(r['trace'][:2])}" for r in reasoning_result['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:
        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"])

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):
    import time
    start_time = time.time()
    
    brain_result = await ultimate_brain.process_legal_query(query=message, mode=mode, ai_provider=ai_provider, use_reasoning_protocols=use_reasoning)
    
    if use_reasoning and brain_result['reasoning_result']:
        reasoning_info = "🧠 **Reasoning Protocols Applied:**\n"
        for r in brain_result['reasoning_result']['results']:
            reasoning_info += f"- {r['protocol']}: βœ… {r['status']}\n"
        yield reasoning_info + "\n\n"
    
    if ai_provider == "huggingface":
        token = hf_token.token if hf_token else None
        client = InferenceClient(token=token, model="meta-llama/Llama-3.3-70B-Instruct")
        
        messages = [{"role": "system", "content": brain_result['enhanced_query']}]
        for user_msg, assistant_msg in history:
            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})
        
        response = reasoning_info if use_reasoning and brain_result['reasoning_result'] else ""
        try:
            for chunk in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
                if chunk.choices and chunk.choices[0].delta.content:
                    response += chunk.choices[0].delta.content
                    yield response
        except Exception as e:
            yield f"{response}\n\n❌ Error: {str(e)}"

# Custom CSS - Simplified with logo styling
# Custom CSS - Futuristic Gold and Blue Theme
custom_css = """

.gradio-container { 

    max-width: 1400px !important; 

    background-color: #0a0e17 !important; 

}

.header-section {

    text-align: center; padding: 40px 20px;

    background: linear-gradient(135deg, #001529 0%, #003366 50%, #8a6e14 100%);

    color: #ffffff; 

    border-radius: 12px; margin-bottom: 20px;

    border: 1px solid #D4AF37;

    box-shadow: 0 4px 20px rgba(212, 175, 55, 0.2);

}

.header-section h1 { 

    font-size: 3rem; margin-bottom: 10px; font-weight: 700; 

    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);

    background: linear-gradient(to right, #ffffff, #FFD700);

    -webkit-background-clip: text;

    -webkit-text-fill-color: transparent;

}

.header-section p {

    color: #FFD700;

    font-weight: 700;

    font-size: 1.2rem;

    text-shadow: 1px 1px 1px #000000;

}

.brain-badge {

    display: inline-block; 

    background: linear-gradient(45deg, #FFD700, #FDB931);

    color: #000000;

    padding: 8px 16px; border-radius: 20px; font-weight: 800;

    margin: 10px 5px;

    border: 2px solid #FFFFFF;

    box-shadow: 0 0 15px rgba(255, 215, 0, 0.6);

}

.logo-circle img {

    border-radius: 50% !important;

    box-shadow: 0 0 35px rgba(255, 215, 0, 0.8) !important;

    border: 5px solid #FFD700 !important;

    object-fit: cover !important;

}

/* Tab Styling */

.tabs button.selected {

    color: #FFD700 !important;

    border_bottom: 2px solid #FFD700 !important;

    font-weight: bold;

}

/* Global Text Color Override for visibility */

body, .gradio-container, .prose {

    color: #FFD700 !important;

}

"""

# SEO
seo_meta = SEOOptimizer.get_meta_tags()
seo_structured = SEOOptimizer.get_structured_data()

# Initialize AFTER class definition
ultimate_brain = UltimateLegalBrain()
document_processor = DocumentProcessor()

# Override the demo with voice cloning integrated
demo_with_voice = gr.Blocks(title="ProVerBs Ultimate Legal AI Brain")

with demo_with_voice:
    gr.HTML(f"<style>{custom_css}</style>")
    # Add SEO tags
    gr.HTML(seo_meta + seo_structured)
    
    # Header with Rotating Logos - Using Gradio Image components (Method 3 - WORKS!)
    assets_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets")
    logo_paths = [
        os.path.join(assets_dir, 'logo_1.jpg'),
        os.path.join(assets_dir, 'logo_2.jpg'),
        os.path.join(assets_dir, 'logo_3.jpg')
    ]
    
    gr.HTML("""

    <div class="header-section">

        <h1>βš–οΈ ProVerBs Ultimate Legal AI Brain</h1>

        <p style="font-size: 1.3rem;">Powered by Pro'VerBsβ„’ & ADAPPT-Iβ„’ Technology</p>

    </div>

    """)
    
    # Single rotating logo display
    gr.HTML('<div style="text-align: center; margin: 20px 0;"><h3 style="color: #667eea;">Our Brand Identity</h3></div>')
    with gr.Row():
        rotating_logo = gr.Image(
            value=logo_paths[0],
            label="",
            show_label=False,
            height=180,
            width=180,
            container=False,
            elem_classes="logo-circle",
            elem_id="main_rotating_logo"
        )
    
    # Hidden state to track current logo index
    logo_index_state = gr.State(0)
    
    # Function to rotate logo
    def rotate_logo(current_index):
        import time
        time.sleep(60)  # Wait 60 seconds
        next_index = (current_index + 1) % 3
        return logo_paths[next_index], next_index
    
    # Set up automatic rotation on load
    demo_with_voice.load(
        fn=lambda: (logo_paths[0], 0),
        outputs=[rotating_logo, logo_index_state]
    )
    
    gr.HTML("""

    <div style="text-align: center; margin-top: 10px;">

        <div>

            <span class="brain-badge">🧠 100+ Reasoning Protocols</span>

            <span class="brain-badge">πŸ€– 6 AI Models</span>

            <span class="brain-badge">βš–οΈ 7 Legal Modes</span>

            <span class="brain-badge">πŸŽ™οΈ Voice Cloning</span>

        </div>

        <p style="font-size: 0.9rem; margin-top: 15px; opacity: 0.9;">

            Chain-of-Thought β€’ Self-Consistency β€’ Tree-of-Thoughts β€’ ReAct β€’ Reflexion β€’ RAG<br>

            Quantum Reasoning β€’ Multi-Agent β€’ Voice Cloning β€’ Audio Processing

        </p>

    </div>

    """)
    
    with gr.Tabs():
        # Welcome Tab
        with gr.Tab("🏠 Welcome"):
            gr.Markdown("""

            ## Welcome to the Ultimate ProVerBs Legal AI Brain

            

            ### 🧠 Unified Reasoning Brain (100+ Protocols)

            

            **Core Reasoning Protocols:**

            - Chain-of-Thought (CoT) - Step-by-step reasoning

            - Self-Consistency - Multiple reasoning paths

            - Tree-of-Thoughts (ToT) - Branching exploration

            - ReAct - Reason + Act cycles

            - Reflexion - Self-reflection with memory

            - RAG - Retrieval-Augmented Generation

            

            ### πŸ€– 6 AI Model Options:

            - πŸ€— HuggingFace Llama-3.3-70B (Free, always available)

            - 🧠 GPT-4 Turbo (OpenAI)

            - ✨ Gemini 3.0 (Google)

            - πŸ” Perplexity AI (Research)

            - πŸ₯· Ninja AI

            - πŸ’» LM Studio (Local)

            

            ### βš–οΈ 7 Specialized Legal Modes:

            - Navigation | General Legal | Document Validation

            - Legal Research | Etymology | Case Management | Regulatory Updates

            

            ### πŸŽ™οΈ **NEW! Supertonic Voice Cloning:**

            - Record voice samples

            - Clone voices with text-to-speech

            - Professional audio processing

            - Voice profile management

            - **Full controls**: Play, Record, Pause, Rewind, etc.

            

            **Get Started:** Click "πŸ€– AI Legal Chatbot" or "πŸŽ™οΈ Voice Cloning" tab!

            """)
        
        # AI Chatbot Tab (copy from original)
        with gr.Tab("πŸ€– AI Legal Chatbot"):
            gr.Markdown("""

            ## Multi-AI Legal Chatbot

            Select your AI model and legal assistant mode below!

            """)
            
            with gr.Row():
                ai_provider_selector = gr.Dropdown(
                    choices=[
                        ("πŸ€— Llama-3.3-70B (Free)", "huggingface"),
                        ("🧠 GPT-4 Turbo", "gpt4"),
                        ("✨ Gemini 3.0", "gemini"),
                        ("πŸ” Perplexity AI", "perplexity"),
                        ("πŸ₯· Ninja AI", "ninjaai"),
                        ("πŸ’» LM Studio", "lmstudio")
                    ],
                    value="huggingface",
                    label="πŸ€– AI Model"
                )
                
                mode_selector = gr.Dropdown(
                    choices=[
                        ("πŸ“ Navigation", "navigation"),
                        ("πŸ’¬ General Legal", "general"),
                        ("πŸ“„ Document Validator", "document_validation"),
                        ("πŸ” Legal Research", "legal_research"),
                        ("πŸ“š Etymology", "etymology"),
                        ("πŸ’Ό Case Management", "case_management"),
                        ("πŸ“‹ Regulatory Updates", "regulatory_updates")
                    ],
                    value="general",
                    label="βš–οΈ Legal Mode"
                )
                
                use_reasoning_toggle = gr.Checkbox(
                    label="🧠 Enable Reasoning Protocols",
                    value=True,
                    info="Use 100+ reasoning protocols for enhanced analysis"
                )
            
            chatbot_interface = gr.ChatInterface(
                respond_with_ultimate_brain,
                chatbot=gr.Chatbot(
                    height=550,
                    placeholder="πŸ’¬ Ultimate Legal AI ready! Ask anything...",
                    show_label=False
                ),
                textbox=gr.Textbox(
                    placeholder="Ask your legal question here...",
                    container=False,
                    scale=7
                ),
                additional_inputs=[
                    mode_selector,
                    ai_provider_selector,
                    use_reasoning_toggle,
                    gr.Slider(128, 4096, value=2048, step=128, label="Max Tokens"),
                    gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
                    gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
                ],
                examples=[
                    ["What reasoning protocols are available?"],
                    ["Analyze this contract using Chain-of-Thought reasoning"],
                    ["Research case law with Tree-of-Thoughts exploration"]
                ],
                cache_examples=False
            )
        
        # Voice Cloning Tab - FULL SUPERTONIC INTERFACE
        with gr.Tab("πŸŽ™οΈ Voice Cloning"):
            create_supertonic_interface()
        
        # Document Processing Tab - NEW!
        with gr.Tab("πŸ“„ Document Processing"):
            gr.Markdown("""

            ## πŸ“„ Document Processing & OCR

            Upload documents, images, or URLs to extract text and analyze with AI.

            

            **Supported formats:**

            - πŸ“„ PDF documents

            - πŸ–ΌοΈ Images (PNG, JPG, JPEG) with OCR

            - ✍️ Handwriting recognition

            - πŸ“ Text files (.txt)

            - πŸ“„ Word documents (.docx)

            - 🌐 Web URLs

            """)
            
            with gr.Tabs():
                # File Upload Tab
                with gr.Tab("πŸ“ Upload File"):
                    file_input = gr.File(
                        label="Upload Document",
                        file_types=[".pdf", ".png", ".jpg", ".jpeg", ".txt", ".docx"],
                        type="filepath"
                    )
                    process_file_btn = gr.Button("πŸ” Process File", variant="primary")
                    
                # URL Input Tab
                with gr.Tab("🌐 From URL"):
                    url_input = gr.Textbox(
                        label="Enter URL",
                        placeholder="https://example.com/document.pdf",
                        lines=1
                    )
                    process_url_btn = gr.Button("πŸ” Process URL", variant="primary")
                
                # Direct Text Tab
                with gr.Tab("✍️ Direct Text"):
                    direct_text_input = gr.Textbox(
                        label="Enter or Paste Text",
                        placeholder="Paste your text here...",
                        lines=10
                    )
                    process_text_btn = gr.Button("πŸ” Process Text", variant="primary")
            
            # Output Section
            gr.Markdown("### πŸ“Š Extracted Content")
            
            with gr.Row():
                with gr.Column(scale=1):
                    doc_info_output = gr.JSON(
                        label="Document Info",
                        visible=True
                    )
                
                with gr.Column(scale=2):
                    extracted_text_output = gr.Textbox(
                        label="Extracted Text",
                        lines=15,
                        max_lines=20,
                        show_copy_button=True
                    )
            
            # AI Analysis Section
            gr.Markdown("### πŸ€– AI Analysis")
            analyze_btn = gr.Button("🧠 Analyze with AI", variant="secondary")
            analysis_output = gr.Textbox(
                label="AI Analysis",
                lines=10,
                show_copy_button=True
            )
            
            # Processing functions
            def process_file_wrapper(file_path):
                if not file_path:
                    return None, "Please upload a file first."
                
                result, error = document_processor.process_file(file_path)
                
                if error:
                    return {"error": error}, ""
                
                if result:
                    info = {
                        "filename": result['filename'],
                        "file_type": result['file_type'],
                        "upload_date": result['upload_date'],
                        "source": result['source_type'],
                        "content_length": len(result['content'])
                    }
                    return info, result['content']
                
                return {"error": "Failed to process file"}, ""
            
            def process_url_wrapper(url):
                if not url:
                    return None, "Please enter a URL first."
                
                result, error = document_processor.process_url(url)
                
                if error:
                    return {"error": error}, ""
                
                if result:
                    info = {
                        "source": result['source_url'],
                        "filename": result['filename'],
                        "file_type": result['file_type'],
                        "upload_date": result['upload_date'],
                        "content_length": len(result['content'])
                    }
                    return info, result['content']
                
                return {"error": "Failed to process URL"}, ""
            
            def process_text_wrapper(text):
                if not text:
                    return None, "Please enter some text first."
                
                result, error = document_processor.process_text(text)
                
                if error:
                    return {"error": error}, ""
                
                if result:
                    info = {
                        "source": "Direct Input",
                        "upload_date": result['upload_date'],
                        "content_length": len(result['content'])
                    }
                    return info, result['content']
                
                return {"error": "Failed to process text"}, ""
            
            async def analyze_with_ai(extracted_text):
                if not extracted_text:
                    return "No text to analyze. Please process a document first."
                
                # Use the ultimate brain to analyze
                query = f"Please analyze this document and provide key insights:\n\n{extracted_text[:3000]}"
                
                result = await ultimate_brain.process_legal_query(
                    query=query,
                    mode="document_validation",
                    ai_provider="huggingface",
                    use_reasoning_protocols=True
                )
                
                # Get AI response
                token = None
                client = InferenceClient(token=token, model="meta-llama/Llama-3.3-70B-Instruct")
                
                messages = [
                    {"role": "system", "content": result['enhanced_query']},
                    {"role": "user", "content": query}
                ]
                
                response = ""
                try:
                    for chunk in client.chat_completion(messages, max_tokens=1024, stream=True, temperature=0.7, top_p=0.95):
                        if chunk.choices and chunk.choices[0].delta.content:
                            response += chunk.choices[0].delta.content
                except Exception as e:
                    response = f"Error: {str(e)}"
                
                return response
            
            # Connect buttons
            process_file_btn.click(
                fn=process_file_wrapper,
                inputs=[file_input],
                outputs=[doc_info_output, extracted_text_output]
            )
            
            process_url_btn.click(
                fn=process_url_wrapper,
                inputs=[url_input],
                outputs=[doc_info_output, extracted_text_output]
            )
            
            process_text_btn.click(
                fn=process_text_wrapper,
                inputs=[direct_text_input],
                outputs=[doc_info_output, extracted_text_output]
            )
            
            analyze_btn.click(
                fn=analyze_with_ai,
                inputs=[extracted_text_output],
                outputs=[analysis_output]
            )
        
        # Analytics Tab
        with gr.Tab("πŸ“Š Analytics"):
            gr.Markdown("""

            ## Analytics & Performance Dashboard

            View real-time analytics and performance metrics for the Ultimate Brain.

            """)
            
            with gr.Row():
                analytics_btn = gr.Button("πŸ“Š Refresh Analytics", variant="primary")
                clear_cache_btn = gr.Button("πŸ—‘οΈ Clear Cache", variant="secondary")
            
            analytics_output = gr.JSON(label="Analytics Data")
            performance_output = gr.JSON(label="Performance Metrics")
            cache_stats_output = gr.JSON(label="Cache Statistics")
            
            def get_analytics():
                return analytics_tracker.get_analytics()
            
            def get_performance():
                return performance_monitor.get_metrics()
            
            def get_cache_stats():
                return performance_cache.get_stats()
            
            def clear_cache_action():
                performance_cache.clear()
                return {"status": "Cache cleared successfully"}
            
            analytics_btn.click(
                fn=lambda: (get_analytics(), get_performance(), get_cache_stats()),
                outputs=[analytics_output, performance_output, cache_stats_output]
            )
            
            clear_cache_btn.click(
                fn=clear_cache_action,
                outputs=[cache_stats_output]
            )
        
        # Reasoning Brain Tab
        with gr.Tab("🧠 Reasoning Brain"):
            gr.Markdown("""

            ## Unified AI Reasoning Brain

            

            ### πŸ“Š Protocol Categories:

            

            #### Core Reasoning (Protocols 1-50)

            - Chain-of-Thought, Self-Consistency, Tree-of-Thoughts

            - ReAct, Reflexion, RAG, and more

            

            #### Quantum-Specific (Protocols 51-100)

            - Quantum Job Orchestration, VQE, QAOA

            - Circuit Transpilation, Error Mitigation

            

            #### Multi-Agent (Protocols 73-100)

            - Multi-Agent Coordination

            - Contract Net Protocol

            """)
        
        # About Tab
        with gr.Tab("ℹ️ About"):
            gr.Markdown("""

            ## About ProVerBs Ultimate Legal AI Brain

            

            ### πŸš€ Revolutionary Features:

            - **100+ Reasoning Protocols** - Most advanced reasoning system

            - **6 AI Models** - Choose the best for your needs

            - **7 Legal Modes** - Specialized for different legal tasks

            - **Voice Cloning** - Professional Supertonic integration

            - **Audio Processing** - Complete recording and playback controls

            

            ### πŸŽ™οΈ Voice Cloning Features:

            - Record voice samples with full controls

            - Clone any voice with text-to-speech

            - Professional audio processing

            - Export voice profiles

            - Play, Pause, Record, Rewind, Stop controls

            

            ### πŸ“š Resources:

            - **Main Space**: https://huggingface.co/spaces/Solomon7890/ProVerbS_LaW_mAiN_PAgE

            - **Supertonic**: https://github.com/supertone-inc/supertonic

            - **Models**: https://huggingface.co/Supertone/supertonic

            

            ### ⚠️ Disclaimer:

            This platform provides general legal information only. Consult with a licensed attorney for specific legal matters.

            

            ---

            **Version 3.0.0 + Voice Cloning** | Built by Solomon7890

            """)
    
    # Footer
    gr.Markdown("""

    ---

    <div style="text-align: center; padding: 20px;">

        <p><strong>βš–οΈ ProVerBs Ultimate Legal AI Brain v3.0 + Voice Cloning</strong></p>

        <p>Powered by Pro'VerBsβ„’ & ADAPPT-Iβ„’ | 100+ Protocols | 6 AI Models | Voice Cloning</p>

        <p style="font-size: 0.85rem; color: #666;">

            © 2025 Solomon 8888 | Built with ❀️ for legal professionals worldwide

        </p>

    </div>

    """)

# Use the new demo with voice cloning
demo = demo_with_voice

if __name__ == "__main__":
    demo.queue(max_size=20)
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)