File size: 27,149 Bytes
b71326c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8f167
 
b71326c
 
 
 
 
5a8f167
 
 
 
 
 
 
 
b71326c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1017cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02229b
a1017cc
 
e02229b
a1017cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02229b
a1017cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02229b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1017cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8f167
b71326c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8f167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b71326c
 
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# app.py - DeepSeek Hexa-Agent Discussion Platform (Free Version)
import gradio as gr
import requests
import threading
import time
import numpy as np
import faiss
import os
import pickle
from datetime import datetime
import re
import json
import matplotlib.pyplot as plt
import networkx as nx
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from functools import lru_cache
from sentence_transformers import SentenceTransformer

# === CONFIG ===
EMBEDDING_MODEL = "all-MiniLM-L6-v2"  # Local embedding model
CHAT_MODEL = "HuggingFaceH4/zephyr-7b-beta"  # Free model via Hugging Face API
MEMORY_FILE = "memory.pkl"
INDEX_FILE = "memory.index"
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")  # Optional for higher rate limits

# Initialize local embedding model
embedding_model = SentenceTransformer(EMBEDDING_MODEL)

# === EMBEDDING UTILS ===
@lru_cache(maxsize=500)
def get_embedding(text):
    """Local embedding function"""
    return embedding_model.encode(text)

def cosine_similarity(vec1, vec2):
    vec1 = np.array(vec1)
    vec2 = np.array(vec2)
    return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))

# === MEMORY INITIALIZATION ===
memory_data = []
try:
    memory_index = faiss.read_index(INDEX_FILE)
    with open(MEMORY_FILE, "rb") as f:
        memory_data = pickle.load(f)
except:
    memory_index = faiss.IndexFlatL2(384)  # 384 dimensions for MiniLM

# === AGENT SYSTEM PROMPTS ===
AGENT_A_PROMPT = """You are the Discussion Initiator. Your role:
1. Introduce complex topics requiring multidisciplinary perspectives
2. Frame debates exploring tensions between values, ethics, and progress
3. Challenge assumptions while maintaining intellectual humility
4. Connect concepts across domains (science, ethics, policy, technology)
5. Elevate discussions beyond surface-level analysis"""

AGENT_B_PROMPT = """You are the Critical Responder. Your role:
1. Provide counterpoints with evidence-based reasoning
2. Identify logical fallacies and cognitive biases in arguments
3. Analyze implications at different scales (individual, societal, global)
4. Consider second and third-order consequences
5. Balance idealism with practical constraints"""

OVERSEER_PROMPT = """You are the Depth Guardian. Your role:
1. Ensure discussions maintain intellectual rigor
2. Intervene when conversations become superficial or repetitive
3. Highlight unexamined assumptions and blind spots
4. Introduce relevant frameworks (systems thinking, ethical paradigms)
5. Prompt consideration of marginalized perspectives
6. Synthesize key tensions and paradoxes"""

OUTSIDER_PROMPT = """You are the Cross-Disciplinary Provocateur. Your role:
1. Introduce radical perspectives from unrelated fields
2. Challenge conventional wisdom with contrarian viewpoints
3. Surface historical precedents and analogies
4. Propose unconventional solutions to complex problems
5. Highlight overlooked connections and systemic relationships
6. Question the framing of the discussion itself"""

CULTURAL_LENS_PROMPT = """You are the Cultural Perspective. Your role:
1. Provide viewpoints from diverse global cultures (Eastern, Western, Indigenous, African, etc.)
2. Highlight how cultural values shape perspectives on the topic
3. Identify cultural biases in arguments and assumptions
4. Share traditions and practices relevant to the discussion
5. Suggest culturally inclusive approaches to solutions
6. Bridge cultural divides through nuanced understanding
7. Consider post-colonial and decolonial perspectives"""

JUDGE_PROMPT = """You are the Impartial Judge. Your role:
1. Periodically review the discussion and provide balanced rulings
2. Identify areas of agreement and unresolved tensions
3. Evaluate the strength of arguments from different perspectives
4. Highlight the most compelling insights and critical flaws
5. Suggest pathways toward resolution or further inquiry
6. Deliver rulings with clear justification and constructive guidance
7. Maintain objectivity while acknowledging valid points from all sides
8. Consider ethical implications and practical feasibility"""

# === GLOBAL STATE ===
conversation = []
turn_count = 0
auto_mode = False
current_topic = ""
last_ruling_turn = 0
agent_params = {
    "Initiator": {"creativity": 0.7, "criticality": 0.5},
    "Responder": {"creativity": 0.5, "criticality": 0.8},
    "Guardian": {"creativity": 0.6, "criticality": 0.9},
    "Provocateur": {"creativity": 0.9, "criticality": 0.7},
    "Cultural": {"creativity": 0.7, "criticality": 0.6},
    "Judge": {"creativity": 0.4, "criticality": 0.9}
}
# === FREE CHAT COMPLETION API ===
def safe_chat_completion(system, messages, temperature=0.7):
    """Use free Hugging Face Inference API"""
    try:
        # Format messages for Hugging Face API
        formatted = [{"role": "system", "content": system}]
        formatted.extend(messages)
        
        # Prepare payload
        payload = {
            "inputs": formatted,
            "parameters": {
                "max_new_tokens": 300,
                "temperature": temperature,
                "return_full_text": False
            }
        }
        
        headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}
        
        response = requests.post(
            f"https://api-inference.huggingface.co/models/{CHAT_MODEL}",
            json=payload,
            headers=headers,
            timeout=60
        )
        
        if response.status_code == 200:
            return response.json()[0]['generated_text'].strip()
        elif response.status_code == 503:  # Model loading
            time.sleep(15)
            return safe_chat_completion(system, messages, temperature)
        else:
            return f"⚠️ API Error: {response.text}"
            
    except Exception as e:
        return f"⚠️ System Error: {str(e)}"

# === MEMORY MANAGEMENT ===
def embed_and_store(text, agent=None):
    try:
        vec = get_embedding(text)
        memory_index.add(np.array([vec], dtype='float32'))
        memory_data.append({
            "text": text,
            "timestamp": datetime.now().isoformat(),
            "agent": agent or "system",
            "topic": current_topic
        })
        if len(memory_data) % 5 == 0:
            with open(MEMORY_FILE, "wb") as f:
                pickle.dump(memory_data, f)
            faiss.write_index(memory_index, INDEX_FILE)
    except Exception as e:
        print(f"Memory Error: {str(e)}")

def retrieve_relevant_memory(query, k=3):
    """Retrieve relevant past discussions"""
    try:
        query_embedding = get_embedding(query)
        distances, indices = memory_index.search(np.array([query_embedding], dtype='float32'), k)
        
        relevant = []
        for i, idx in enumerate(indices[0]):
            if idx < len(memory_data) and idx >= 0:
                relevant.append({
                    "text": memory_data[idx]['text'][:200] + "...",
                    "topic": memory_data[idx].get('topic', 'Unknown'),
                    "agent": memory_data[idx].get('agent', 'Unknown'),
                    "similarity": 1 - distances[0][i]  # Convert distance to similarity
                })
        return relevant
    except Exception as e:
        print(f"Memory retrieval error: {str(e)}")
        return []

# ... [Rest of the functions remain the same as previous implementation] ...
# Keep all the functions from the previous implementation except:
# - safe_chat_completion (already replaced above)
# - get_embedding (already replaced above)

# ... [Keep all imports, config, and function definitions above] ...

# === GRADIO UI ===
with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Discussion Platform") as demo:
    gr.Markdown("# 🧠 Hexa-Agent Discussion System (Free Version)")
    gr.Markdown("### Powered by Open-Source Models")
    
    # State variables
    conversation_state = gr.State([])
    turn_count_state = gr.State(0)
    current_topic_state = gr.State("")
    last_ruling_turn_state = gr.State(0)
    auto_mode_state = gr.State(False)
    agent_params_state = gr.State(agent_params)
    
    # Status panel
    with gr.Row():
        turn_counter = gr.Number(label="Turn Count", value=0, interactive=False)
        topic_display = gr.Textbox(label="Current Topic", interactive=False, lines=2)
        agent_status = gr.Textbox(label="Active Agents", value="πŸ’‘ Initiator, πŸ” Responder", interactive=False)
    
    # Tabbed interface
    with gr.Tab("Live Discussion"):
        convo_display = gr.HTML(
            value="<div class='convo-container'>Discussion will appear here</div>", 
            elem_id="convo-display"
        )
        
        with gr.Row():
            step_btn = gr.Button("▢️ Next Turn", variant="primary")
            auto_btn = gr.Button("πŸ”΄ Auto: OFF", variant="secondary")
            clear_btn = gr.Button("πŸ”„ New Discussion", variant="stop")
            topic_btn = gr.Button("🎲 Random Topic", variant="secondary")
            ruling_btn = gr.Button("βš–οΈ Request Ruling", variant="primary")
        
        with gr.Accordion("πŸ’¬ Guide the Discussion", open=False):
            topic_input = gr.Textbox(label="Set Custom Topic", placeholder="e.g., Ethics of AGI in cultural contexts...")
            with gr.Row():
                qbox = gr.Textbox(label="Ask the Depth Guardian", placeholder="What perspectives are missing?")
                ruling_qbox = gr.Textbox(label="Specific Question for Judge", placeholder="What should be our guiding principle?")
            with gr.Row():
                overseer_out = gr.Textbox(label="Depth Guardian Response", interactive=False)
                judge_out = gr.Textbox(label="Judge's Response", interactive=False)
    
# === COMPLETE IMPLEMENTATION ===

def overseer_respond(question, conversation, current_topic):
    """Get response from Depth Guardian"""
    context = f"Current Topic: {current_topic}\n\n" if current_topic else ""
    context += "Conversation History:\n"
    for msg in conversation[-5:]:
        context += f"- {msg['agent']}: {msg['text']}\n"
    
    response = safe_chat_completion(
        system=OVERSEER_PROMPT,
        messages=[{"role": "user", "content": f"{context}\nQuestion: {question}"}],
        temperature=0.8
    )
    embed_and_store(response, "Guardian")
    return response

def ask_judge(question, conversation, current_topic):
    """Get ruling from Judge"""
    context = f"Topic: {current_topic}\n\n" if current_topic else ""
    context += "Recent Discussion:\n"
    for msg in conversation[-5:]:
        context += f"- {msg['agent']}: {msg['text']}\n"
    
    response = safe_chat_completion(
        system=JUDGE_PROMPT,
        messages=[{"role": "user", "content": f"{context}\nSpecific Question: {question}"}],
        temperature=0.6
    )
    
def step(topic_input, conversation, turn_count, current_topic, last_ruling_turn, agent_params):
    """Advance the discussion by one turn"""
    # Remove global declarations - we'll use the parameters directly
    # Set topic on first turn
    if turn_count == 0:
        if topic_input.strip():
            current_topic = topic_input.strip()
        else:
            current_topic = "Ethical Implications of Advanced AI Systems"
    
    # Determine which agent speaks
    agent_sequence = ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural"]
    agent_index = turn_count % len(agent_sequence)
    agent_name = agent_sequence[agent_index]
    
    # Special handling for Judge
    judge_interval = 5
    if turn_count - last_ruling_turn >= judge_interval and turn_count > 0:
        agent_name = "Judge"
    
    # Get system prompt and temperature
    prompts = {
        "Initiator": AGENT_A_PROMPT,
        "Responder": AGENT_B_PROMPT,
        "Guardian": OVERSEER_PROMPT,
        "Provocateur": OUTSIDER_PROMPT,
        "Cultural": CULTURAL_LENS_PROMPT,
        "Judge": JUDGE_PROMPT
    }
    temperature = agent_params[agent_name]["creativity"]
    
    # Prepare context
    context = f"Current Topic: {current_topic}\n\nDiscussion History:\n"
    for msg in conversation[-5:]:
        context += f"{msg['agent']}: {msg['text']}\n\n"
    
    # Generate response
    response = safe_chat_completion(
        system=prompts[agent_name],
        messages=[{"role": "user", "content": context}],
        temperature=temperature
    )
    
    # Create message entry
    new_entry = {
        "agent": agent_name,
        "text": response,
        "turn": turn_count + 1
    }
    
    # Update state
    updated_conversation = conversation + [new_entry]
    new_turn_count = turn_count + 1
    new_last_ruling_turn = new_turn_count if agent_name == "Judge" else last_ruling_turn
    
    # Update memory
    embed_and_store(response, agent_name, current_topic)  # Pass current_topic here
    
    # Format HTML output
    html_output = format_conversation_html(updated_conversation)
    
    # Get agent-specific displays
    intervention = get_last_by_agent(updated_conversation, "Guardian")
    outsider = get_last_by_agent(updated_conversation, "Provocateur")
    cultural = get_last_by_agent(updated_conversation, "Cultural")
    judge = get_last_by_agent(updated_conversation, "Judge")
    
    # Prepare agent status
    active_agents = " | ".join([f"{agent}: {entry['text'][:30]}..." for agent, entry in zip(
        ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural", "Judge"],
        [new_entry] * 6  # Simplified for demo
    )])
    
    return (
        html_output,
        intervention,
        outsider,
        cultural,
        judge,
        current_topic,
        new_turn_count,
        active_agents,
        updated_conversation,
        new_turn_count,
        current_topic,
        new_last_ruling_turn,
        agent_params
    )

# Update embed_and_store to accept topic as parameter
def embed_and_store(text, agent=None, topic=""):
    """Store text with associated topic"""
    try:
        vec = get_embedding(text)
        memory_index.add(np.array([vec], dtype='float32'))
        memory_data.append({
            "text": text,
            "timestamp": datetime.now().isoformat(),
            "agent": agent or "system",
            "topic": topic
        })
        if len(memory_data) % 5 == 0:
            with open(MEMORY_FILE, "wb") as f:
                pickle.dump(memory_data, f)
            faiss.write_index(memory_index, INDEX_FILE)
    except Exception as e:
        print(f"Memory Error: {str(e)}")

# ... [Rest of the functions remain unchanged] ...
def get_last_by_agent(conversation, agent_name):
    """Get last message from specific agent"""
    for msg in reversed(conversation):
        if msg["agent"] == agent_name:
            return msg["text"]
    return "No message yet"

def format_conversation_html(conversation):
    """Format conversation as HTML"""
    html = "<div class='convo-container'>"
    for msg in conversation:
        agent = msg["agent"]
        color_map = {
            "Initiator": "#e6f7ff",
            "Responder": "#f6ffed",
            "Guardian": "#fff7e6",
            "Provocateur": "#f9e6ff",
            "Cultural": "#e6ffed",
            "Judge": "#f0f0f0",
            "User": "#f0f0f0"
        }
        color = color_map.get(agent, "#ffffff")
        html += f"""
        <div style='background:{color}; padding:10px; margin:10px; border-radius:5px;'>
            <b>{agent}:</b> {msg['text']}
        </div>
        """
    html += "</div>"
    return html

def toggle_auto(auto_mode):
    """Toggle auto-advance mode"""
    new_mode = not auto_mode
    return ("🟒 Auto: ON" if new_mode else "πŸ”΄ Auto: OFF", new_mode)

def clear_convo():
    """Reset conversation"""
    global conversation, turn_count, current_topic, last_ruling_turn
    conversation = []
    turn_count = 0
    current_topic = ""
    last_ruling_turn = 0
    return (
        format_conversation_html([]),
        "",
        "",
        "",
        "",
        "",
        0,
        "πŸ’‘ Initiator, πŸ” Responder",
        [],
        0,
        "",
        0,
        "",
        ""
    )

def new_topic(conversation, turn_count, current_topic):
    """Generate a new discussion topic"""
    # In a real implementation, this would call an LLM to generate a topic
    topics = [
        "The Ethics of Genetic Engineering in Humans",
        "Universal Basic Income in the Age of Automation",
        "Cultural Impacts of Global AI Deployment",
        "Privacy vs Security in Digital Societies",
        "The Future of Human-AI Collaboration"
    ]
    new_topic = np.random.choice(topics)
    return (
        format_conversation_html([]),
        new_topic,
        0,
        [],
        0,
        new_topic
    )

def request_ruling(conversation, current_topic, turn_count, last_ruling_turn):
    """Request a ruling from the Judge"""
    context = f"Topic: {current_topic}\n\nDiscussion Summary:\n"
    for msg in conversation[-5:]:
        context += f"- {msg['agent']}: {msg['text']}\n"
    
    response = safe_chat_completion(
        system=JUDGE_PROMPT,
        messages=[{"role": "user", "content": f"{context}\nPlease provide a comprehensive ruling."}],
        temperature=0.5
    )
    
    new_entry = {
        "agent": "Judge",
        "text": response,
        "turn": turn_count
    }
    updated_conversation = conversation + [new_entry]
    return response, updated_conversation, turn_count

def run_analysis(conversation):
    """Run basic analysis (simplified for free version)"""
    # Sentiment analysis placeholder
    sentiments = ["Positive", "Neutral", "Negative"]
    sentiment_result = np.random.choice(sentiments, p=[0.4, 0.4, 0.2])
    
    # Topic extraction placeholder
    topics = ["AI Ethics", "Policy", "Cultural Impact", "Technology", "Future Scenarios"]
    topic_result = ", ".join(np.random.choice(topics, 3, replace=False))
    
    # Agent participation plot
    agents = [msg["agent"] for msg in conversation]
    if agents:
        agent_counts = {agent: agents.count(agent) for agent in set(agents)}
        plt.figure(figsize=(8, 4))
        plt.bar(agent_counts.keys(), agent_counts.values())
        plt.title("Agent Participation")
        plt.ylabel("Number of Messages")
        plt.tight_layout()
        plt.savefig("agent_plot.png")
        plot_path = "agent_plot.png"
    else:
        plot_path = None
    
    return (
        f"Overall Sentiment: {sentiment_result}",
        f"Key Topics: {topic_result}",
        plot_path
    )

def generate_knowledge_graph(conversation):
    """Generate a simple knowledge graph (placeholder)"""
    G = nx.DiGraph()
    entities = ["AI", "Ethics", "Society", "Technology", "Future"]
    for i, e1 in enumerate(entities):
        for j, e2 in enumerate(entities):
            if i != j and np.random.random() > 0.7:
                G.add_edge(e1, e2, weight=np.random.random())
    
    plt.figure(figsize=(10, 8))
    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, node_size=2000, 
            node_color="skyblue", font_size=10, 
            edge_color="gray", width=1.5)
    plt.title("Knowledge Graph")
    plt.savefig("knowledge_graph.png")
    return "knowledge_graph.png"

def export_handler(format_radio, conversation, current_topic, turn_count):
    """Export conversation in various formats"""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
    if format_radio == "txt":
        filename = f"discussion_{timestamp}.txt"
        with open(filename, "w") as f:
            f.write(f"Topic: {current_topic}\nTurns: {turn_count}\n\n")
            for msg in conversation:
                f.write(f"{msg['agent']} (Turn {msg.get('turn', 'N/A')}):\n{msg['text']}\n\n")
        return filename
    
    elif format_radio == "pdf":
        filename = f"discussion_{timestamp}.pdf"
        doc = SimpleDocTemplate(filename, pagesize=letter)
        styles = getSampleStyleSheet()
        story = []
        
        story.append(Paragraph(f"Discussion: {current_topic}", styles["Title"]))
        story.append(Paragraph(f"Turns: {turn_count}", styles["Normal"]))
        story.append(Spacer(1, 12))
        
        for msg in conversation:
            agent_text = f"<b>{msg['agent']}</b> (Turn {msg.get('turn', 'N/A')}):"
            story.append(Paragraph(agent_text, styles["Normal"]))
            story.append(Paragraph(msg["text"], styles["BodyText"]))
            story.append(Spacer(1, 12))
        
        doc.build(story)
        return filename
    
    elif format_radio == "json":
        filename = f"discussion_{timestamp}.json"
        data = {
            "topic": current_topic,
            "turns": turn_count,
            "conversation": conversation
        }
        with open(filename, "w") as f:
            json.dump(data, f, indent=2)
        return filename
    
    return "export_error.txt"

def send_to_webhook(webhook_url, conversation, current_topic, turn_count):
    """Send conversation to webhook"""
    if not webhook_url.startswith("http"):
        return "⚠️ Invalid URL"
    
    payload = {
        "topic": current_topic,
        "turns": turn_count,
        "conversation": conversation
    }
    
    try:
        response = requests.post(webhook_url, json=payload, timeout=10)
        if response.status_code == 200:
            return "βœ… Sent successfully!"
        return f"⚠️ Error: {response.status_code} - {response.text}"
    except Exception as e:
        return f"⚠️ Connection error: {str(e)}"

def add_user_contribution(user_input, conversation):
    """Add user contribution to conversation"""
    if not user_input.strip():
        return format_conversation_html(conversation), "Please enter a message", conversation
    
    new_entry = {
        "agent": "User",
        "text": user_input,
        "turn": len(conversation) + 1
    }
    updated_conversation = conversation + [new_entry]
    embed_and_store(user_input, "User")
    return format_conversation_html(updated_conversation), "βœ… Added your contribution!", updated_conversation

def update_agent_params(*args):
    """Update agent parameters from sliders"""
    # Last argument is the current params state
    current_params = args[-1]
    sliders = args[:-1]
    
    # Map sliders to agent parameters
    agents = ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural", "Judge"]
    params = ["creativity", "criticality"]
    
    updated_params = {}
    slider_index = 0
    for agent in agents:
        updated_params[agent] = {}
        for param in params:
            updated_params[agent][param] = sliders[slider_index]
            slider_index += 1
    
    return updated_params

    # Custom CSS
    demo.css = """
    .convo-container {
        max-height: 400px;
        overflow-y: auto;
        padding: 15px;
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        background-color: #f9f9f9;
        line-height: 1.6;
    }
    .convo-container p {
        margin-bottom: 10px;
    }
    #topic-display {
        font-weight: bold;
        font-size: 1.1em;
    }
    .free-model-notice {
        background-color: #e6f7ff;
        padding: 10px;
        border-radius: 5px;
        margin-bottom: 15px;
        border-left: 4px solid #1890ff;
    }
    """
    
    # Free model notice
    gr.Markdown("""
    <div class="free-model-notice">
        <b>Using Free Models:</b> This version uses open-source models from Hugging Face.
        Responses may be slower and less refined than commercial APIs.
        Consider using local GPU for better performance.
    </div>
    """)
    
    # Event handlers with proper state management
    qbox.submit(
        overseer_respond, 
        inputs=[qbox, conversation_state, current_topic_state],
        outputs=[overseer_out]
    )
    
    ruling_qbox.submit(
        ask_judge, 
        inputs=[ruling_qbox, conversation_state, current_topic_state],
        outputs=[judge_out]
    )
    
    step_btn.click(
        step, 
        inputs=[topic_input, conversation_state, turn_count_state, current_topic_state, last_ruling_turn_state, agent_params_state],
        outputs=[
            convo_display, intervention_display, outsider_display, 
            cultural_display, judge_display, topic_display, turn_counter, 
            agent_status, conversation_state, turn_count_state, current_topic_state,
            last_ruling_turn_state
        ]
    )
    
    auto_btn.click(
        toggle_auto, 
        inputs=[auto_mode_state],
        outputs=[auto_btn, auto_mode_state]
    )
    
    clear_btn.click(
        clear_convo, 
        outputs=[
            convo_display, intervention_display, outsider_display, 
            cultural_display, judge_display, topic_display, turn_counter,
            agent_status, conversation_state, turn_count_state, current_topic_state,
            last_ruling_turn_state, overseer_out, judge_out
        ]
    )
    
    topic_btn.click(
        new_topic,
        inputs=[conversation_state, turn_count_state, current_topic_state],
        outputs=[
            convo_display, topic_display, turn_counter, conversation_state, 
            turn_count_state, current_topic_state
        ]
    )
    
    ruling_btn.click(
        request_ruling,
        inputs=[conversation_state, current_topic_state, turn_count_state, last_ruling_turn_state],
        outputs=[judge_display, conversation_state, last_ruling_turn_state]
    )
    
    analysis_btn.click(
        run_analysis,
        inputs=[conversation_state],
        outputs=[sentiment_display, topics_display, agent_plot]
    )
    
    graph_btn.click(
        generate_knowledge_graph,
        inputs=[conversation_state],
        outputs=[graph_display]
    )
    
    export_btn.click(
        export_handler,
        inputs=[format_radio, conversation_state, current_topic_state, turn_count_state],
        outputs=[export_result]
    )
    
    integrate_btn.click(
        send_to_webhook,
        inputs=[webhook_url, conversation_state, current_topic_state, turn_count_state],
        outputs=[integration_status]
    )
    
    submit_btn.click(
        add_user_contribution,
        inputs=[user_input, conversation_state],
        outputs=[convo_display, user_feedback, conversation_state]
    )
    
    voting_btn.click(
        lambda: "βœ… Your vote has been recorded!",
        outputs=[user_feedback]
    )
    
    flag_btn.click(
        lambda: "🚩 Issue flagged for moderator review",
        outputs=[user_feedback]
    )
    
    # Create input list for slider change events
    slider_inputs = [agent_sliders[f"{agent}_{param}"] 
                    for agent in ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural", "Judge"]
                    for param in ["creativity", "critical"]]
    
    for slider in slider_inputs:
        slider.change(
            update_agent_params,
            inputs=slider_inputs + [agent_params_state],
            outputs=[agent_params_state]
        )

demo.launch()