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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
from typing import Dict, List, Tuple, Optional
|
| 4 |
|
| 5 |
+
# ==================== CORE SYSTEM STATE ====================
|
| 6 |
+
class LawTrainingSystem:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.session_active = False
|
| 9 |
+
self.current_case = None
|
| 10 |
+
self.student_side = None
|
| 11 |
+
self.conversation_history = []
|
| 12 |
+
self.processing_lock = False
|
| 13 |
+
|
| 14 |
+
def initialize_session(self, case_title: str, case_description: str, student_side: str):
|
| 15 |
+
"""Initialize a new training session"""
|
| 16 |
+
self.session_active = True
|
| 17 |
+
self.current_case = {"title": case_title, "description": case_description}
|
| 18 |
+
self.student_side = student_side
|
| 19 |
+
self.conversation_history = []
|
| 20 |
+
return f"Session initialized: {case_title} - You are the {student_side}"
|
| 21 |
+
|
| 22 |
+
def is_processing(self) -> bool:
|
| 23 |
+
"""Check if system is currently processing a response"""
|
| 24 |
+
return self.processing_lock
|
| 25 |
+
|
| 26 |
+
def set_processing(self, status: bool):
|
| 27 |
+
"""Set processing lock status"""
|
| 28 |
+
self.processing_lock = status
|
| 29 |
|
| 30 |
+
# Global system instance
|
| 31 |
+
system = LawTrainingSystem()
|
| 32 |
+
|
| 33 |
+
# ==================== AGENT RESPONSE SIMULATORS ====================
|
| 34 |
+
def generate_judge_response(student_message: str) -> str:
|
| 35 |
+
"""Simulate Judge AI response"""
|
| 36 |
+
# TODO: Replace with actual LangGraph Judge agent
|
| 37 |
+
time.sleep(1) # Simulate processing time
|
| 38 |
+
return f"Judge: Your argument shows {['good', 'adequate', 'weak'][len(student_message) % 3]} legal reasoning. Consider strengthening your constitutional basis."
|
| 39 |
+
|
| 40 |
+
def generate_opponent_response(student_message: str) -> str:
|
| 41 |
+
"""Simulate Opponent AI response"""
|
| 42 |
+
# TODO: Replace with actual LangGraph Opponent agent
|
| 43 |
+
time.sleep(1.5) # Simulate processing time
|
| 44 |
+
return f"Opponent: I object to your reasoning. The precedent clearly shows that {student_message[:20]}... is flawed because..."
|
| 45 |
+
|
| 46 |
+
def generate_narrator_response(student_message: str) -> str:
|
| 47 |
+
"""Simulate Narrator AI response"""
|
| 48 |
+
# TODO: Replace with actual LangGraph Narrator agent
|
| 49 |
+
time.sleep(0.8) # Simulate processing time
|
| 50 |
+
return f"Narrator: The courtroom falls silent as the student presents their argument. The judge leans forward, considering the points raised..."
|
| 51 |
+
|
| 52 |
+
# ==================== UI RESPONSE HANDLERS ====================
|
| 53 |
+
def handle_student_response(message: str, judge_history: List, opponent_history: List, narrator_history: List) -> Tuple:
|
| 54 |
+
"""Process student message and generate AI responses"""
|
| 55 |
+
|
| 56 |
+
# Prevent multiple simultaneous processing
|
| 57 |
+
if system.is_processing():
|
| 58 |
+
return message, judge_history, opponent_history, narrator_history, gr.update(interactive=False)
|
| 59 |
+
|
| 60 |
+
if not system.session_active:
|
| 61 |
+
return message, judge_history, opponent_history, narrator_history, gr.update(interactive=True)
|
| 62 |
+
|
| 63 |
+
# Set processing lock
|
| 64 |
+
system.set_processing(True)
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# Add student message to history
|
| 68 |
+
student_entry = [message, ""]
|
| 69 |
+
|
| 70 |
+
# Generate AI responses (in sequence to avoid interference)
|
| 71 |
+
judge_response = generate_judge_response(message)
|
| 72 |
+
opponent_response = generate_opponent_response(message)
|
| 73 |
+
narrator_response = generate_narrator_response(message)
|
| 74 |
+
|
| 75 |
+
# Update chat histories
|
| 76 |
+
judge_history.append(["", judge_response])
|
| 77 |
+
opponent_history.append(["", opponent_response])
|
| 78 |
+
narrator_history.append(["", narrator_response])
|
| 79 |
+
|
| 80 |
+
# Clear student input and re-enable
|
| 81 |
+
return "", judge_history, opponent_history, narrator_history, gr.update(interactive=True)
|
| 82 |
+
|
| 83 |
+
finally:
|
| 84 |
+
# Always release processing lock
|
| 85 |
+
system.set_processing(False)
|
| 86 |
+
|
| 87 |
+
def start_case_session(case_title: str, case_description: str, student_side: str) -> Tuple:
|
| 88 |
+
"""Initialize a new case session"""
|
| 89 |
+
if not case_title or not case_description:
|
| 90 |
+
return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(interactive=False)
|
| 91 |
+
|
| 92 |
+
# Initialize system
|
| 93 |
+
init_message = system.initialize_session(case_title, case_description, student_side)
|
| 94 |
+
|
| 95 |
+
# Generate initial context from Narrator
|
| 96 |
+
initial_context = generate_narrator_response(f"Case: {case_title}")
|
| 97 |
+
|
| 98 |
+
# Create initial chat histories
|
| 99 |
+
judge_history = [["", "Judge: I am ready to evaluate your arguments based on legal precedent and constitutional law."]]
|
| 100 |
+
opponent_history = [["", f"Opponent: I will argue for the {('prosecution' if student_side == 'defense' else 'defense')} side."]]
|
| 101 |
+
narrator_history = [["", initial_context]]
|
| 102 |
+
|
| 103 |
+
return (
|
| 104 |
+
judge_history,
|
| 105 |
+
opponent_history,
|
| 106 |
+
narrator_history,
|
| 107 |
+
gr.update(visible=True),
|
| 108 |
+
gr.update(interactive=True)
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
def upload_case_documents(files) -> str:
|
| 112 |
+
"""Handle case document uploads"""
|
| 113 |
+
if not files:
|
| 114 |
+
return "No documents uploaded"
|
| 115 |
+
|
| 116 |
+
# TODO: Process uploaded documents with document parsing
|
| 117 |
+
file_names = [f.name for f in files]
|
| 118 |
+
return f"Uploaded {len(files)} documents: {', '.join(file_names)}"
|
| 119 |
+
|
| 120 |
+
# ==================== GRADIO INTERFACE ====================
|
| 121 |
+
def create_law_training_interface():
|
| 122 |
+
"""Create the main Gradio interface"""
|
| 123 |
+
|
| 124 |
+
with gr.Blocks(
|
| 125 |
+
title="Law Training Agentic System",
|
| 126 |
+
theme=gr.themes.Soft(),
|
| 127 |
+
css="""
|
| 128 |
+
.chat-container { height: 400px; }
|
| 129 |
+
.upload-section { border: 2px dashed #ccc; padding: 20px; margin: 10px 0; }
|
| 130 |
+
.character-panel { border: 1px solid #ddd; border-radius: 8px; margin: 5px; }
|
| 131 |
+
.student-panel { border: 2px solid #4CAF50; }
|
| 132 |
+
"""
|
| 133 |
+
) as interface:
|
| 134 |
+
|
| 135 |
+
# Header
|
| 136 |
+
gr.Markdown("# ⚖️ Law Training Agentic System")
|
| 137 |
+
gr.Markdown("Practice your courtroom argumentation skills with AI-powered opponents, judges, and narrators")
|
| 138 |
+
|
| 139 |
+
# ==================== CASE SETUP SECTION ====================
|
| 140 |
+
with gr.Row():
|
| 141 |
+
with gr.Column(scale=2):
|
| 142 |
+
gr.Markdown("## 📋 Case Setup")
|
| 143 |
+
case_title = gr.Textbox(
|
| 144 |
+
label="Case Title",
|
| 145 |
+
placeholder="e.g., Freedom of Speech vs. Public Safety",
|
| 146 |
+
lines=1
|
| 147 |
+
)
|
| 148 |
+
case_description = gr.Textbox(
|
| 149 |
+
label="Case Description",
|
| 150 |
+
placeholder="Describe the legal scenario you want to practice...",
|
| 151 |
+
lines=3
|
| 152 |
+
)
|
| 153 |
+
student_side = gr.Radio(
|
| 154 |
+
choices=["prosecution", "defense"],
|
| 155 |
+
label="Your Role",
|
| 156 |
+
value="prosecution"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Column(scale=1):
|
| 160 |
+
gr.Markdown("## 📁 Documents")
|
| 161 |
+
file_upload = gr.File(
|
| 162 |
+
label="Upload Case Documents",
|
| 163 |
+
file_count="multiple",
|
| 164 |
+
file_types=[".pdf", ".txt", ".docx"],
|
| 165 |
+
elem_classes="upload-section"
|
| 166 |
+
)
|
| 167 |
+
upload_status = gr.Textbox(
|
| 168 |
+
label="Upload Status",
|
| 169 |
+
interactive=False,
|
| 170 |
+
lines=2
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Start session button
|
| 174 |
+
start_btn = gr.Button("🚀 Start Training Session", variant="primary", size="lg")
|
| 175 |
+
|
| 176 |
+
# ==================== COURTROOM INTERFACE ====================
|
| 177 |
+
courtroom_section = gr.Group(visible=False)
|
| 178 |
+
|
| 179 |
+
with courtroom_section:
|
| 180 |
+
gr.Markdown("## 🏛️ Courtroom")
|
| 181 |
+
|
| 182 |
+
# Four-panel layout for characters
|
| 183 |
+
with gr.Row(equal_height=True):
|
| 184 |
+
# Student Panel (Interactive)
|
| 185 |
+
with gr.Column(scale=1, elem_classes="character-panel student-panel"):
|
| 186 |
+
gr.Markdown("### 🎓 **Student (You)**")
|
| 187 |
+
student_input = gr.Textbox(
|
| 188 |
+
label="Your Argument",
|
| 189 |
+
placeholder="Present your legal argument here...",
|
| 190 |
+
lines=3,
|
| 191 |
+
interactive=False
|
| 192 |
+
)
|
| 193 |
+
submit_btn = gr.Button("📝 Submit Argument", variant="primary")
|
| 194 |
+
|
| 195 |
+
# Judge Panel (AI Response Only)
|
| 196 |
+
with gr.Column(scale=1, elem_classes="character-panel"):
|
| 197 |
+
gr.Markdown("### ⚖️ **Judge**")
|
| 198 |
+
judge_chat = gr.Chatbot(
|
| 199 |
+
label="Judge Feedback",
|
| 200 |
+
elem_classes="chat-container",
|
| 201 |
+
show_label=False
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
with gr.Row(equal_height=True):
|
| 205 |
+
# Opponent Panel (AI Response Only)
|
| 206 |
+
with gr.Column(scale=1, elem_classes="character-panel"):
|
| 207 |
+
gr.Markdown("### 🥊 **Opponent**")
|
| 208 |
+
opponent_chat = gr.Chatbot(
|
| 209 |
+
label="Opponent Arguments",
|
| 210 |
+
elem_classes="chat-container",
|
| 211 |
+
show_label=False
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# Narrator Panel (AI Response Only)
|
| 215 |
+
with gr.Column(scale=1, elem_classes="character-panel"):
|
| 216 |
+
gr.Markdown("### 📖 **Narrator**")
|
| 217 |
+
narrator_chat = gr.Chatbot(
|
| 218 |
+
label="Courtroom Narration",
|
| 219 |
+
elem_classes="chat-container",
|
| 220 |
+
show_label=False
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
# ==================== EVENT HANDLERS ====================
|
| 224 |
+
|
| 225 |
+
# File upload handler
|
| 226 |
+
file_upload.change(
|
| 227 |
+
fn=upload_case_documents,
|
| 228 |
+
inputs=[file_upload],
|
| 229 |
+
outputs=[upload_status]
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
# Start session handler
|
| 233 |
+
start_btn.click(
|
| 234 |
+
fn=start_case_session,
|
| 235 |
+
inputs=[case_title, case_description, student_side],
|
| 236 |
+
outputs=[judge_chat, opponent_chat, narrator_chat, courtroom_section, student_input]
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
# Student response handler
|
| 240 |
+
submit_btn.click(
|
| 241 |
+
fn=handle_student_response,
|
| 242 |
+
inputs=[student_input, judge_chat, opponent_chat, narrator_chat],
|
| 243 |
+
outputs=[student_input, judge_chat, opponent_chat, narrator_chat, submit_btn]
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# Enter key submission
|
| 247 |
+
student_input.submit(
|
| 248 |
+
fn=handle_student_response,
|
| 249 |
+
inputs=[student_input, judge_chat, opponent_chat, narrator_chat],
|
| 250 |
+
outputs=[student_input, judge_chat, opponent_chat, narrator_chat, submit_btn]
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
return interface
|
| 254 |
+
|
| 255 |
+
# ==================== MAIN EXECUTION ====================
|
| 256 |
+
if __name__ == "__main__":
|
| 257 |
+
# Create and launch the interface
|
| 258 |
+
app = create_law_training_interface()
|
| 259 |
+
|
| 260 |
+
# Launch configuration
|
| 261 |
+
app.launch(
|
| 262 |
+
server_name="0.0.0.0",
|
| 263 |
+
server_port=7860,
|
| 264 |
+
share=False,
|
| 265 |
+
debug=True
|
| 266 |
+
)
|