Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py - Advanced Discussion Simulator with
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
import threading
|
@@ -44,7 +44,7 @@ except:
|
|
44 |
# === AGENT SYSTEM PROMPTS ===
|
45 |
AGENT_A_PROMPT = """You are the Discussion Initiator. Your role:
|
46 |
1. Introduce complex topics requiring multidisciplinary perspectives
|
47 |
-
2. Frame debates
|
48 |
3. Challenge assumptions while maintaining intellectual humility
|
49 |
4. Connect concepts across domains (science, ethics, policy, technology)
|
50 |
5. Elevate discussions beyond surface-level analysis"""
|
@@ -72,11 +72,31 @@ OUTSIDER_PROMPT = """You are the Cross-Disciplinary Provocateur. Your role:
|
|
72 |
5. Highlight overlooked connections and systemic relationships
|
73 |
6. Question the framing of the discussion itself"""
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# === GLOBAL STATE ===
|
76 |
conversation = []
|
77 |
turn_count = 0
|
78 |
auto_mode = False
|
79 |
current_topic = ""
|
|
|
80 |
|
81 |
# === CHAT COMPLETION ===
|
82 |
def chat_completion(system, messages, model=CHAT_MODEL):
|
@@ -172,12 +192,48 @@ def detect_repetition():
|
|
172 |
similarity = cosine_similarity(embeddings[-1], embeddings[-3])
|
173 |
return similarity > 0.82
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# === AGENT FUNCTIONS ===
|
176 |
def generate_topic():
|
177 |
"""Generate a complex discussion topic"""
|
178 |
topic = chat_completion(
|
179 |
-
"Generate a complex discussion topic requiring multidisciplinary analysis",
|
180 |
-
[{"role": "user", "content": "Create a topic addressing tensions between technological progress and
|
181 |
)
|
182 |
return topic.split(":")[-1].strip() if ":" in topic else topic
|
183 |
|
@@ -187,9 +243,41 @@ def outsider_comment():
|
|
187 |
prompt = f"Conversation Context:\n{context}\n\nProvide your cross-disciplinary perspective:"
|
188 |
return chat_completion(OUTSIDER_PROMPT, [{"role": "user", "content": prompt}])
|
189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
# === CORE CONVERSATION FLOW ===
|
191 |
def step(topic_input=""):
|
192 |
-
global conversation, turn_count, current_topic
|
193 |
|
194 |
# Initialize new discussion
|
195 |
if not conversation:
|
@@ -201,7 +289,8 @@ def step(topic_input=""):
|
|
201 |
conversation.append({"agent": "π‘ Initiator", "text": msg})
|
202 |
embed_and_store(msg, "Initiator")
|
203 |
turn_count = 1
|
204 |
-
|
|
|
205 |
|
206 |
# Critical Responder engages
|
207 |
last_msg = conversation[-1]['text']
|
@@ -236,8 +325,24 @@ def step(topic_input=""):
|
|
236 |
conversation.append({"agent": "π Provocateur", "text": outsider_msg})
|
237 |
embed_and_store(outsider_msg, "Outsider")
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
turn_count += 1
|
240 |
-
return format_convo(), intervention, outsider_msg, current_topic
|
241 |
|
242 |
# === OVERSEER QUERY HANDLER ===
|
243 |
def overseer_respond(query):
|
@@ -248,6 +353,16 @@ def overseer_respond(query):
|
|
248 |
except Exception as e:
|
249 |
return f"[Overseer Error: {str(e)}]"
|
250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
# === AUTO MODE HANDLER ===
|
252 |
def auto_loop():
|
253 |
global auto_mode
|
@@ -265,32 +380,60 @@ def toggle_auto():
|
|
265 |
# === GRADIO UI ===
|
266 |
with gr.Blocks(title="Advanced Discussion Simulator") as demo:
|
267 |
gr.Markdown("# π§ Advanced Discussion Simulator")
|
268 |
-
gr.Markdown("###
|
269 |
|
270 |
with gr.Row():
|
271 |
topic_display = gr.Textbox(label="Current Topic", interactive=False)
|
272 |
|
273 |
with gr.Row():
|
274 |
-
convo_display = gr.Markdown(
|
|
|
|
|
|
|
|
|
275 |
|
276 |
with gr.Row():
|
277 |
step_btn = gr.Button("βΆοΈ Next Turn", variant="primary")
|
278 |
auto_btn = gr.Button("π΄ Auto: OFF", variant="secondary")
|
279 |
clear_btn = gr.Button("π New Discussion", variant="stop")
|
280 |
topic_btn = gr.Button("π² Random Topic", variant="secondary")
|
|
|
281 |
|
282 |
with gr.Row():
|
283 |
with gr.Column(scale=1):
|
284 |
gr.Markdown("### βοΈ Depth Guardian")
|
285 |
-
intervention_display = gr.Textbox(label="
|
|
|
|
|
|
|
286 |
with gr.Column(scale=1):
|
287 |
-
gr.Markdown("###
|
288 |
-
|
|
|
|
|
|
|
|
|
|
|
289 |
|
290 |
with gr.Accordion("π¬ Guide the Discussion", open=False):
|
291 |
-
topic_input = gr.Textbox(label="Set Custom Topic", placeholder="e.g., Ethics of
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
# Event handlers
|
296 |
def clear_convo():
|
@@ -298,27 +441,40 @@ with gr.Blocks(title="Advanced Discussion Simulator") as demo:
|
|
298 |
conversation = []
|
299 |
turn_count = 0
|
300 |
current_topic = ""
|
301 |
-
return "**New discussion started**", "", "", "", ""
|
302 |
|
303 |
def new_topic():
|
304 |
clear_convo()
|
305 |
topic = generate_topic()
|
306 |
-
return "", "", "", topic, topic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
step_btn.click(
|
309 |
step,
|
310 |
inputs=[topic_input],
|
311 |
-
outputs=[convo_display, intervention_display, outsider_display, topic_display]
|
312 |
)
|
313 |
qbox.submit(overseer_respond, inputs=qbox, outputs=overseer_out)
|
|
|
314 |
auto_btn.click(toggle_auto, outputs=auto_btn)
|
315 |
clear_btn.click(
|
316 |
clear_convo,
|
317 |
-
outputs=[convo_display, intervention_display, outsider_display, topic_display, overseer_out]
|
318 |
)
|
319 |
topic_btn.click(
|
320 |
new_topic,
|
321 |
-
outputs=[convo_display, intervention_display, outsider_display, topic_display, overseer_out]
|
|
|
|
|
|
|
|
|
322 |
)
|
323 |
|
324 |
demo.launch()
|
|
|
1 |
+
# app.py - Advanced Discussion Simulator with Hexa-Agent System
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
import threading
|
|
|
44 |
# === AGENT SYSTEM PROMPTS ===
|
45 |
AGENT_A_PROMPT = """You are the Discussion Initiator. Your role:
|
46 |
1. Introduce complex topics requiring multidisciplinary perspectives
|
47 |
+
2. Frame debates exploring tensions between values, ethics, and progress
|
48 |
3. Challenge assumptions while maintaining intellectual humility
|
49 |
4. Connect concepts across domains (science, ethics, policy, technology)
|
50 |
5. Elevate discussions beyond surface-level analysis"""
|
|
|
72 |
5. Highlight overlooked connections and systemic relationships
|
73 |
6. Question the framing of the discussion itself"""
|
74 |
|
75 |
+
CULTURAL_LENS_PROMPT = """You are the Cultural Perspective. Your role:
|
76 |
+
1. Provide viewpoints from diverse global cultures (Eastern, Western, Indigenous, African, etc.)
|
77 |
+
2. Highlight how cultural values shape perspectives on the topic
|
78 |
+
3. Identify cultural biases in arguments and assumptions
|
79 |
+
4. Share traditions and practices relevant to the discussion
|
80 |
+
5. Suggest culturally inclusive approaches to solutions
|
81 |
+
6. Bridge cultural divides through nuanced understanding
|
82 |
+
7. Consider post-colonial and decolonial perspectives"""
|
83 |
+
|
84 |
+
JUDGE_PROMPT = """You are the Impartial Judge. Your role:
|
85 |
+
1. Periodically review the discussion and provide balanced rulings
|
86 |
+
2. Identify areas of agreement and unresolved tensions
|
87 |
+
3. Evaluate the strength of arguments from different perspectives
|
88 |
+
4. Highlight the most compelling insights and critical flaws
|
89 |
+
5. Suggest pathways toward resolution or further inquiry
|
90 |
+
6. Deliver rulings with clear justification and constructive guidance
|
91 |
+
7. Maintain objectivity while acknowledging valid points from all sides
|
92 |
+
8. Consider ethical implications and practical feasibility"""
|
93 |
+
|
94 |
# === GLOBAL STATE ===
|
95 |
conversation = []
|
96 |
turn_count = 0
|
97 |
auto_mode = False
|
98 |
current_topic = ""
|
99 |
+
last_ruling_turn = 0
|
100 |
|
101 |
# === CHAT COMPLETION ===
|
102 |
def chat_completion(system, messages, model=CHAT_MODEL):
|
|
|
192 |
similarity = cosine_similarity(embeddings[-1], embeddings[-3])
|
193 |
return similarity > 0.82
|
194 |
|
195 |
+
def detect_cultural_relevance():
|
196 |
+
"""Check if cultural perspectives are needed"""
|
197 |
+
if len(conversation) < 2:
|
198 |
+
return False
|
199 |
+
|
200 |
+
last_texts = " ".join([m['text'] for m in conversation[-2:]])
|
201 |
+
cultural_triggers = [
|
202 |
+
"society", "culture", "values", "tradition",
|
203 |
+
"global", "western", "eastern", "indigenous",
|
204 |
+
"community", "norms", "beliefs", "diversity",
|
205 |
+
"equity", "identity", "heritage", "colonial"
|
206 |
+
]
|
207 |
+
|
208 |
+
for trigger in cultural_triggers:
|
209 |
+
if trigger in last_texts.lower():
|
210 |
+
return True
|
211 |
+
return False
|
212 |
+
|
213 |
+
def detect_judgment_opportunity():
|
214 |
+
"""Identify when the discussion is ripe for judgment"""
|
215 |
+
if len(conversation) < 8:
|
216 |
+
return False
|
217 |
+
|
218 |
+
# Check for unresolved tensions
|
219 |
+
last_texts = " ".join([m['text'] for m in conversation[-4:]])
|
220 |
+
judgment_triggers = [
|
221 |
+
"tension", "dilemma", "paradox", "conflict",
|
222 |
+
"disagreement", "opposing views", "unresolved",
|
223 |
+
"contradiction", "impasse", "standoff"
|
224 |
+
]
|
225 |
+
|
226 |
+
for trigger in judgment_triggers:
|
227 |
+
if trigger in last_texts.lower():
|
228 |
+
return True
|
229 |
+
return False
|
230 |
+
|
231 |
# === AGENT FUNCTIONS ===
|
232 |
def generate_topic():
|
233 |
"""Generate a complex discussion topic"""
|
234 |
topic = chat_completion(
|
235 |
+
"Generate a complex discussion topic requiring multidisciplinary and multicultural analysis",
|
236 |
+
[{"role": "user", "content": "Create a topic addressing tensions between technological progress, ethics, and cultural values"}]
|
237 |
)
|
238 |
return topic.split(":")[-1].strip() if ":" in topic else topic
|
239 |
|
|
|
243 |
prompt = f"Conversation Context:\n{context}\n\nProvide your cross-disciplinary perspective:"
|
244 |
return chat_completion(OUTSIDER_PROMPT, [{"role": "user", "content": prompt}])
|
245 |
|
246 |
+
def cultural_perspective():
|
247 |
+
"""Generate cultural diversity perspective"""
|
248 |
+
context = "\n".join([f"{m['agent']}: {m['text']}" for m in conversation[-4:]])
|
249 |
+
prompt = f"Conversation Context:\n{context}\n\nProvide diverse cultural perspectives on this topic:"
|
250 |
+
return chat_completion(CULTURAL_LENS_PROMPT, [{"role": "user", "content": prompt}])
|
251 |
+
|
252 |
+
def judge_ruling():
|
253 |
+
"""Generate final judgment or ruling"""
|
254 |
+
global last_ruling_turn
|
255 |
+
|
256 |
+
# Create comprehensive context
|
257 |
+
context = "\n\n".join([
|
258 |
+
f"Discussion Topic: {current_topic}",
|
259 |
+
"Key Arguments:",
|
260 |
+
*[f"- {m['agent']}: {m['text']}" for m in conversation[-8:]]
|
261 |
+
])
|
262 |
+
|
263 |
+
prompt = f"""After reviewing this discussion, provide your impartial judgment:
|
264 |
+
{context}
|
265 |
+
|
266 |
+
Your ruling should:
|
267 |
+
1. Identify areas of agreement and unresolved tensions
|
268 |
+
2. Evaluate the strength of key arguments
|
269 |
+
3. Highlight the most compelling insights
|
270 |
+
4. Suggest pathways toward resolution
|
271 |
+
5. Consider ethical and practical implications
|
272 |
+
6. Provide constructive guidance for next steps"""
|
273 |
+
|
274 |
+
ruling = chat_completion(JUDGE_PROMPT, [{"role": "user", "content": prompt}])
|
275 |
+
last_ruling_turn = turn_count
|
276 |
+
return ruling
|
277 |
+
|
278 |
# === CORE CONVERSATION FLOW ===
|
279 |
def step(topic_input=""):
|
280 |
+
global conversation, turn_count, current_topic, last_ruling_turn
|
281 |
|
282 |
# Initialize new discussion
|
283 |
if not conversation:
|
|
|
289 |
conversation.append({"agent": "π‘ Initiator", "text": msg})
|
290 |
embed_and_store(msg, "Initiator")
|
291 |
turn_count = 1
|
292 |
+
last_ruling_turn = 0
|
293 |
+
return format_convo(), "", "", "", "", current_topic
|
294 |
|
295 |
# Critical Responder engages
|
296 |
last_msg = conversation[-1]['text']
|
|
|
325 |
conversation.append({"agent": "π Provocateur", "text": outsider_msg})
|
326 |
embed_and_store(outsider_msg, "Outsider")
|
327 |
|
328 |
+
# Cultural perspective
|
329 |
+
cultural_msg = ""
|
330 |
+
if turn_count % 5 == 0 or detect_cultural_relevance():
|
331 |
+
cultural_msg = cultural_perspective()
|
332 |
+
conversation.append({"agent": "π Cultural Lens", "text": cultural_msg})
|
333 |
+
embed_and_store(cultural_msg, "Cultural")
|
334 |
+
|
335 |
+
# Judge ruling
|
336 |
+
judge_msg = ""
|
337 |
+
ruling_interval = 6 # Turns between rulings
|
338 |
+
if (turn_count - last_ruling_turn >= ruling_interval and
|
339 |
+
(turn_count % ruling_interval == 0 or detect_judgment_opportunity())):
|
340 |
+
judge_msg = judge_ruling()
|
341 |
+
conversation.append({"agent": "βοΈ Judge", "text": judge_msg})
|
342 |
+
embed_and_store(judge_msg, "Judge")
|
343 |
+
|
344 |
turn_count += 1
|
345 |
+
return format_convo(), intervention, outsider_msg, cultural_msg, judge_msg, current_topic
|
346 |
|
347 |
# === OVERSEER QUERY HANDLER ===
|
348 |
def overseer_respond(query):
|
|
|
353 |
except Exception as e:
|
354 |
return f"[Overseer Error: {str(e)}]"
|
355 |
|
356 |
+
# === JUDGE RULING HANDLER ===
|
357 |
+
def request_ruling():
|
358 |
+
try:
|
359 |
+
ruling = judge_ruling()
|
360 |
+
conversation.append({"agent": "βοΈ Judge", "text": ruling})
|
361 |
+
embed_and_store(ruling, "Judge")
|
362 |
+
return ruling
|
363 |
+
except Exception as e:
|
364 |
+
return f"[Judge Error: {str(e)}]"
|
365 |
+
|
366 |
# === AUTO MODE HANDLER ===
|
367 |
def auto_loop():
|
368 |
global auto_mode
|
|
|
380 |
# === GRADIO UI ===
|
381 |
with gr.Blocks(title="Advanced Discussion Simulator") as demo:
|
382 |
gr.Markdown("# π§ Advanced Discussion Simulator")
|
383 |
+
gr.Markdown("### Hexa-Agent System for Complex Discourse")
|
384 |
|
385 |
with gr.Row():
|
386 |
topic_display = gr.Textbox(label="Current Topic", interactive=False)
|
387 |
|
388 |
with gr.Row():
|
389 |
+
convo_display = gr.Markdown(
|
390 |
+
value="**Discussion will appear here**",
|
391 |
+
elem_id="convo-display",
|
392 |
+
elem_classes="convo-scroll"
|
393 |
+
)
|
394 |
|
395 |
with gr.Row():
|
396 |
step_btn = gr.Button("βΆοΈ Next Turn", variant="primary")
|
397 |
auto_btn = gr.Button("π΄ Auto: OFF", variant="secondary")
|
398 |
clear_btn = gr.Button("π New Discussion", variant="stop")
|
399 |
topic_btn = gr.Button("π² Random Topic", variant="secondary")
|
400 |
+
ruling_btn = gr.Button("βοΈ Request Ruling", variant="primary")
|
401 |
|
402 |
with gr.Row():
|
403 |
with gr.Column(scale=1):
|
404 |
gr.Markdown("### βοΈ Depth Guardian")
|
405 |
+
intervention_display = gr.Textbox(label="", interactive=False)
|
406 |
+
with gr.Column(scale=1):
|
407 |
+
gr.Markdown("### π Cross-Disciplinary")
|
408 |
+
outsider_display = gr.Textbox(label="", interactive=False)
|
409 |
with gr.Column(scale=1):
|
410 |
+
gr.Markdown("### π Cultural Lens")
|
411 |
+
cultural_display = gr.Textbox(label="", interactive=False)
|
412 |
+
|
413 |
+
with gr.Row():
|
414 |
+
with gr.Column(scale=3):
|
415 |
+
gr.Markdown("### βοΈ Final Judgment")
|
416 |
+
judge_display = gr.Textbox(label="", interactive=False, lines=4)
|
417 |
|
418 |
with gr.Accordion("π¬ Guide the Discussion", open=False):
|
419 |
+
topic_input = gr.Textbox(label="Set Custom Topic", placeholder="e.g., Ethics of AGI in cultural contexts...")
|
420 |
+
with gr.Row():
|
421 |
+
qbox = gr.Textbox(label="Ask the Depth Guardian", placeholder="What perspectives are missing?")
|
422 |
+
ruling_qbox = gr.Textbox(label="Specific Question for Judge", placeholder="What should be our guiding principle?")
|
423 |
+
with gr.Row():
|
424 |
+
overseer_out = gr.Textbox(label="Depth Guardian Response", interactive=False)
|
425 |
+
judge_out = gr.Textbox(label="Judge's Response", interactive=False)
|
426 |
+
|
427 |
+
# Custom CSS for scrollable conversation
|
428 |
+
demo.css = """
|
429 |
+
.convo-scroll {
|
430 |
+
max-height: 400px;
|
431 |
+
overflow-y: auto;
|
432 |
+
padding: 10px;
|
433 |
+
border: 1px solid #e0e0e0;
|
434 |
+
border-radius: 5px;
|
435 |
+
}
|
436 |
+
"""
|
437 |
|
438 |
# Event handlers
|
439 |
def clear_convo():
|
|
|
441 |
conversation = []
|
442 |
turn_count = 0
|
443 |
current_topic = ""
|
444 |
+
return "**New discussion started**", "", "", "", "", "", ""
|
445 |
|
446 |
def new_topic():
|
447 |
clear_convo()
|
448 |
topic = generate_topic()
|
449 |
+
return "", "", "", "", "", topic, topic
|
450 |
+
|
451 |
+
def ask_judge(query):
|
452 |
+
try:
|
453 |
+
context = "\n".join([m['text'] for m in conversation[-3:]]) if conversation else "No context"
|
454 |
+
messages = [{"role": "user", "content": f"Discussion Topic: {current_topic}\n\nRecent context:\n{context}\n\nSpecific Question: {query}"}]
|
455 |
+
return chat_completion(JUDGE_PROMPT, messages)
|
456 |
+
except Exception as e:
|
457 |
+
return f"[Judge Error: {str(e)}]"
|
458 |
|
459 |
step_btn.click(
|
460 |
step,
|
461 |
inputs=[topic_input],
|
462 |
+
outputs=[convo_display, intervention_display, outsider_display, cultural_display, judge_display, topic_display]
|
463 |
)
|
464 |
qbox.submit(overseer_respond, inputs=qbox, outputs=overseer_out)
|
465 |
+
ruling_qbox.submit(ask_judge, inputs=ruling_qbox, outputs=judge_out)
|
466 |
auto_btn.click(toggle_auto, outputs=auto_btn)
|
467 |
clear_btn.click(
|
468 |
clear_convo,
|
469 |
+
outputs=[convo_display, intervention_display, outsider_display, cultural_display, judge_display, topic_display, overseer_out]
|
470 |
)
|
471 |
topic_btn.click(
|
472 |
new_topic,
|
473 |
+
outputs=[convo_display, intervention_display, outsider_display, cultural_display, judge_display, topic_display, overseer_out]
|
474 |
+
)
|
475 |
+
ruling_btn.click(
|
476 |
+
request_ruling,
|
477 |
+
outputs=[judge_display]
|
478 |
)
|
479 |
|
480 |
demo.launch()
|