Spaces:
Sleeping
Sleeping
File size: 21,375 Bytes
788dd2e | 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 | """
ImmunoOrg Gradio Dashboard
============================
Interactive demo showing live network/org graphs, belief map, and metrics.
"""
from __future__ import annotations
import json
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import gradio as gr
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from immunoorg.environment import ImmunoOrgEnvironment
from immunoorg.models import (
ImmunoAction, ActionType, TacticalAction, StrategicAction, DiagnosticAction,
PreferenceInjection,
)
from visualization.metrics import (
plot_improvement_trajectory, plot_curriculum_progress,
plot_belief_accuracy_convergence, plot_reward_breakdown,
)
# Global state
env: ImmunoOrgEnvironment | None = None
episode_log: list[dict] = []
belief_accuracy_history: list[float] = []
war_room_log: list[str] = [] # War room transcript lines
pipeline_event_log: list[dict] = [] # Mesh gate events
def build_network_graph_viz() -> go.Figure:
"""Build a Plotly network graph visualization."""
if not env or not env.network:
return go.Figure().update_layout(title="No environment", template="plotly_dark")
nodes = env.network.get_all_nodes()
edges = env.network.get_all_edges()
# Position nodes by tier
tier_x = {"dmz": 0, "web": 1, "app": 2, "data": 3, "management": 2.5}
tier_counts: dict[str, int] = {}
positions: dict[str, tuple[float, float]] = {}
for node in nodes:
tier = node.tier
tier_counts[tier] = tier_counts.get(tier, 0) + 1
x = tier_x.get(tier, 2)
y = tier_counts[tier] * 1.5
positions[node.id] = (x, y)
# Edges
edge_x, edge_y = [], []
for edge in edges:
if edge.source in positions and edge.target in positions:
x0, y0 = positions[edge.source]
x1, y1 = positions[edge.target]
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
# Nodes
node_x = [positions[n.id][0] for n in nodes if n.id in positions]
node_y = [positions[n.id][1] for n in nodes if n.id in positions]
node_colors = []
node_labels = []
for n in nodes:
if n.id not in positions:
continue
if n.compromised and not n.isolated:
node_colors.append("#ef4444") # Red
elif n.isolated:
node_colors.append("#6b7280") # Gray
elif n.health < 0.5:
node_colors.append("#f59e0b") # Yellow
else:
node_colors.append("#22c55e") # Green
status = "๐ดCOMPROMISED" if n.compromised else "๐ขOK"
node_labels.append(f"{n.id}<br>{n.type.value}<br>HP:{n.health:.0%} {status}")
fig = go.Figure()
fig.add_trace(go.Scatter(x=edge_x, y=edge_y, mode="lines",
line=dict(width=1, color="#4b5563"), hoverinfo="none"))
fig.add_trace(go.Scatter(x=node_x, y=node_y, mode="markers+text",
marker=dict(size=20, color=node_colors, line=dict(width=2, color="white")),
text=[n.id.split("-")[-1] for n in nodes if n.id in positions],
textposition="top center",
hovertext=node_labels, hoverinfo="text"))
fig.update_layout(
title="๐ฅ๏ธ Network Graph โ Technical Layer",
template="plotly_dark", showlegend=False,
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
height=400,
annotations=[
dict(x=tier_x[t], y=0, text=t.upper(), showarrow=False,
font=dict(size=12, color="#9ca3af"))
for t in tier_x
],
)
return fig
def build_org_graph_viz() -> go.Figure:
"""Build an org graph visualization."""
if not env or not env.org:
return go.Figure().update_layout(title="No environment", template="plotly_dark")
nodes = env.org.get_all_nodes()
edges = env.org.get_active_edges()
# Circular layout
import math
active_nodes = [n for n in nodes if n.active]
positions = {}
for i, n in enumerate(active_nodes):
angle = 2 * math.pi * i / max(1, len(active_nodes))
positions[n.id] = (math.cos(angle) * 3, math.sin(angle) * 3)
edge_x, edge_y = [], []
for e in edges:
if e.source in positions and e.target in positions:
x0, y0 = positions[e.source]
x1, y1 = positions[e.target]
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
node_x = [positions[n.id][0] for n in active_nodes if n.id in positions]
node_y = [positions[n.id][1] for n in active_nodes if n.id in positions]
labels = [f"๐ข {n.name}\nTrust: {n.trust_score:.2f}\nLatency: {n.response_latency:.1f}"
for n in active_nodes if n.id in positions]
fig = go.Figure()
fig.add_trace(go.Scatter(x=edge_x, y=edge_y, mode="lines",
line=dict(width=2, color="#6366f1"), hoverinfo="none"))
fig.add_trace(go.Scatter(
x=node_x, y=node_y, mode="markers+text",
marker=dict(size=30, color="#6366f1", line=dict(width=2, color="white")),
text=[n.name[:8] for n in active_nodes if n.id in positions],
textposition="top center", hovertext=labels, hoverinfo="text",
))
fig.update_layout(
title="๐๏ธ Organizational Graph โ Socio Layer",
template="plotly_dark", showlegend=False,
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
height=400,
)
return fig
def reset_env(difficulty: int) -> tuple:
global env, episode_log, belief_accuracy_history
env = ImmunoOrgEnvironment(difficulty=int(difficulty), seed=42)
obs = env.reset()
episode_log = []
belief_accuracy_history = []
status = f"โ
Episode started | Difficulty: {difficulty} | Phase: {obs.current_phase.value}"
net_fig = build_network_graph_viz()
org_fig = build_org_graph_viz()
obs_text = format_obs(obs)
return status, net_fig, org_fig, obs_text, "0.00", "0.000"
def take_action(action_type: str, action_name: str, target: str, reasoning: str) -> tuple:
global episode_log, belief_accuracy_history, war_room_log, pipeline_event_log
if not env:
return "โ Environment not initialized", None, None, "", "0.00", "0.000", "", ""
action = build_action(action_type, action_name, target, reasoning)
obs, reward, terminated = env.step(action)
acc = env.belief_map.calculate_belief_accuracy() if env.belief_map else 0.0
belief_accuracy_history.append(acc)
episode_log.append({"step": env.state.step_count, "action": action_name, "reward": reward})
# Capture War Room transcript
if env.war_room and env.war_room.debate_history:
transcript = env.war_room.get_latest_transcript()
war_room_log.append(transcript)
# Capture Pipeline events
if env.devsecops_mesh:
recent_events = env.devsecops_mesh.get_recent_events(3)
for evt in recent_events:
pipeline_event_log.append({
"gate": evt.gate.value, "severity": evt.severity.value,
"threat": evt.threat_type or "clean", "summary": evt.payload_summary[:60],
"score": f"{evt.security_score:.1f}",
})
status = f"{'๐ DONE' if terminated else 'โถ๏ธ Step'} {env.state.step_count} | Phase: {obs.current_phase.value} | Reward: {reward:+.3f}"
if terminated:
status += f" | Reason: {env.state.termination_reason}"
net_fig = build_network_graph_viz()
org_fig = build_org_graph_viz()
obs_text = format_obs(obs)
threat = f"{obs.threat_level:.2f}"
cum_reward = f"{env.state.cumulative_reward:.3f}"
war_room_text = "\n\n".join(war_room_log[-3:]) or "No debates yet."
pipeline_text = format_pipeline_log()
return status, net_fig, org_fig, obs_text, threat, cum_reward, war_room_text, pipeline_text
def format_pipeline_log() -> str:
if not pipeline_event_log:
return "No pipeline events yet."
lines = []
for evt in pipeline_event_log[-8:]:
icon = "๐ซ" if evt["severity"] == "blocked" else ("โ ๏ธ" if evt["severity"] == "warned" else ("๐ง" if evt["severity"] == "sanitized" else "โ
"))
lines.append(f"{icon} [{evt['gate']}] {evt['threat']} | score:{evt['score']} | {evt['summary']}")
return "\n".join(lines)
def inject_preference(directive: str, priority: str) -> str:
if not env:
return "โ Environment not initialized"
injection = PreferenceInjection(
directive=directive,
priority_override=priority,
source="board",
injected_at=env.state.sim_time if env.state else 0.0,
)
env.war_room.inject_preference(injection)
return f"โก Preference injected: [{priority}] {directive}"
def build_5track_chart() -> go.Figure:
"""Build a bar chart of the 5 reward tracks."""
if not env or not env.reward_calc:
return go.Figure().update_layout(title="No data", template="plotly_dark")
tracks = env.reward_calc.get_track_scores()
labels = list(tracks.keys())
values = list(tracks.values())
colors = ["#22c55e", "#ef4444", "#6366f1", "#f59e0b", "#06b6d4"]
fig = go.Figure(go.Bar(
x=labels, y=values,
marker_color=colors[:len(labels)],
text=[f"{v:+.3f}" for v in values],
textposition="outside",
))
fig.update_layout(
title="๐ 5-Track Composable Reward (Running Totals)",
template="plotly_dark", height=300,
xaxis_title="Track", yaxis_title="Cumulative Score",
showlegend=False,
)
return fig
def build_honeytoken_table() -> str:
"""Format honeytoken activations as markdown table."""
if not env or not env.migration_engine:
return "No migration active."
data = env.migration_engine.get_honeytoken_map_data()
if not data:
return "๐ฏ No honeytoken activations yet. Start migration to deploy tokens."
lines = ["| Token | Type | Geo | IP | Confidence |",
"| :--- | :--- | :--- | :--- | :---: |"]
for row in data[-8:]:
lines.append(
f"| {row['token_id']} | {row['type']} | {row['geo']} "
f"| {row['ip']} | {row['confidence']:.0%} |"
)
return "\n".join(lines)
def build_migration_progress() -> str:
"""Format migration progress for the dashboard."""
if not env or not env.migration_engine:
return "Migration engine not initialized."
prog = env.migration_engine.get_progress()
if not prog.get("active"):
return "โธ Migration not started. Use action `start_migration` to begin 50-step MTD workflow."
bar_filled = int(prog['progress_pct'] * 20)
bar = 'โ' * bar_filled + 'โ' * (20 - bar_filled)
return (
f"๐ **Polymorphic Migration Active**\n"
f"Phase: `{prog['current_phase']}` | Step: {prog['current_step']}/{prog['total_steps']}\n"
f"`{bar}` {prog['progress_pct']:.0%}\n"
f"๐ฏ Honeytoken activations: **{prog['honeytoken_activations']}** | "
f"Zero-downtime: {'โ
' if prog['zero_downtime'] else 'โ'}"
)
def build_action(atype: str, aname: str, target: str, reasoning: str) -> ImmunoAction:
action = ImmunoAction(action_type=ActionType(atype), target=target, reasoning=reasoning)
if atype == "tactical":
try: action.tactical_action = TacticalAction(aname)
except ValueError: pass
elif atype == "strategic":
try: action.strategic_action = StrategicAction(aname)
except ValueError: pass
elif atype == "diagnostic":
try: action.diagnostic_action = DiagnosticAction(aname)
except ValueError: pass
return action
def format_obs(obs) -> str:
from immunoorg.agents.defender import format_observation_for_llm
return format_observation_for_llm(obs.model_dump())
def get_metrics_plots() -> tuple:
if not env:
empty = go.Figure().update_layout(template="plotly_dark", title="No data")
return empty, empty
belief_fig = plot_belief_accuracy_convergence(belief_accuracy_history) or go.Figure()
reward_fig = plot_reward_breakdown(
env.reward_calc.get_partial_rewards_summary() if env.reward_calc else {}
) or go.Figure()
return belief_fig, reward_fig
def build_dashboard():
"""Build the Gradio dashboard."""
tactical_actions = [a.value for a in TacticalAction]
strategic_actions = [a.value for a in StrategicAction]
diagnostic_actions = [a.value for a in DiagnosticAction]
all_actions = tactical_actions + strategic_actions + diagnostic_actions
with gr.Blocks(
title="ImmunoOrg โ The Self-Healing Autonomous Enterprise",
theme=gr.themes.Base(primary_hue="indigo", secondary_hue="emerald"),
css="""
.gradio-container { max-width: 1400px !important; }
h1 { text-align: center; background: linear-gradient(135deg, #6366f1, #22c55e);
-webkit-background-clip: text; -webkit-text-fill-color: transparent; }
"""
) as demo:
gr.Markdown("# ๐ก๏ธ ImmunoOrg: The Self-Healing Autonomous Enterprise")
gr.Markdown("*Dual-layer RL environment: Network Security ร Organizational Dynamics*")
with gr.Row():
status_box = gr.Textbox(label="Status", interactive=False, scale=3)
threat_box = gr.Textbox(label="Threat Level", interactive=False, scale=1)
reward_box = gr.Textbox(label="Cumulative Reward", interactive=False, scale=1)
with gr.Row():
with gr.Column(scale=1):
network_plot = gr.Plot(label="Network Graph")
with gr.Column(scale=1):
org_plot = gr.Plot(label="Org Graph")
# โโ Control Panel โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### ๐ฎ Control Panel")
difficulty = gr.Slider(1, 4, value=1, step=1, label="Difficulty Level")
reset_btn = gr.Button("๐ Reset Episode", variant="primary")
action_type = gr.Radio(["tactical", "strategic", "diagnostic"], value="tactical", label="Action Type")
action_name = gr.Dropdown(all_actions, label="Action", value="scan_logs")
target = gr.Textbox(label="Target (node/dept ID)", value="")
reasoning = gr.Textbox(label="Reasoning", lines=2, value="Investigating the situation.")
step_btn = gr.Button("โถ๏ธ Execute Action", variant="primary")
with gr.Column(scale=2):
obs_display = gr.Markdown(label="Observation")
# โโ War Room Feed โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
gr.Markdown("---")
gr.Markdown("### โ๏ธ War Room โ Multi-Agent Debate Feed")
gr.Markdown("*CISO ๐ด vs DevOps ๐ต vs Lead Architect ๐ฃ โ 2-of-3 consensus required*")
with gr.Row():
with gr.Column(scale=2):
war_room_feed = gr.Textbox(
label="Live Debate Transcript",
lines=12, interactive=False,
value="No debates yet. Threat level must reach 0.45 to trigger War Room."
)
with gr.Column(scale=1):
gr.Markdown("**โก Preference Injection (Snorkel AI Bonus)**")
pref_directive = gr.Textbox(
label="Board Directive",
value="Prioritize HIPAA compliance over all else"
)
pref_priority = gr.Dropdown(
["HIPAA", "UPTIME", "LEGAL_HOLD", "GDPR", "PR_CRISIS"],
label="Override Type", value="HIPAA"
)
inject_btn = gr.Button("โก Inject Board Directive", variant="stop")
inject_status = gr.Textbox(label="Injection Status", interactive=False)
# โโ CI/CD Pipeline Gate View โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
gr.Markdown("---")
gr.Markdown("### ๐ AI DevSecOps Mesh โ Pipeline Gate Events")
gr.Markdown("*Gate 1: AST | Gate 2: Semantic | Gate 3: Terraform | Gate 4: MicroVM*")
with gr.Row():
pipeline_feed = gr.Textbox(
label="Pipeline Events (last 8)", lines=8, interactive=False,
value="No pipeline events yet."
)
# โโ Migration + Honeytoken Panel โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
gr.Markdown("---")
gr.Markdown("### ๐ Polymorphic Migration + ๐ฏ Honeytoken Map")
with gr.Row():
with gr.Column(scale=1):
migration_display = gr.Markdown("Migration not started.")
refresh_migration_btn = gr.Button("๐ Refresh Migration Status")
with gr.Column(scale=2):
honeytoken_display = gr.Markdown("No honeytoken activations yet.")
# โโ 5-Track Reward Dashboard โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
gr.Markdown("---")
gr.Markdown("### ๐ 5-Track Composable Reward Model")
with gr.Row():
with gr.Column(scale=1):
track_chart = gr.Plot(label="Track Scores")
refresh_tracks_btn = gr.Button("๐ Refresh Reward Tracks")
with gr.Column(scale=1):
belief_plot = gr.Plot(label="Belief Map Accuracy")
with gr.Column(scale=1):
reward_plot = gr.Plot(label="Reward Breakdown")
metrics_btn = gr.Button("๐ Refresh All Metrics")
# โโ Evidence Panel โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
gr.Markdown("---")
gr.Markdown("### ๐ Proof of Intelligence โ Hackathon Evidence")
with gr.Row():
if os.path.exists("evidence_policy_comparison.png"):
gr.Image("evidence_policy_comparison.png", label="Policy Comparison: Random vs Heuristic")
else:
gr.Markdown("*Run `python generate_evidence.py` to generate charts*")
if os.path.exists("evidence_self_improvement.png"):
gr.Image("evidence_self_improvement.png", label="Self-Improvement Trajectory")
else:
gr.Markdown("*Run `python generate_evidence.py` to generate charts*")
with gr.Row():
if os.path.exists("evidence_org_before_after.png"):
gr.Image("evidence_org_before_after.png", label="Before vs After Org Restructuring")
else:
gr.Markdown("*Run `python generate_evidence.py` to generate charts*")
with gr.Row():
if os.path.exists("demo_results.json"):
with open("demo_results.json") as f:
demo_data = json.load(f)
summary_parts = ["**Demo Results Summary:**\n"]
level_results = demo_data.get("level_results", {})
for lvl in sorted(level_results.keys(), key=int):
r = level_results[lvl]
rand_r = r.get("random", {}).get("avg_reward", 0)
heur_r = r.get("heuristic", {}).get("avg_reward", 0)
summary_parts.append(f"- **Level {lvl}:** Random={rand_r:+.2f} | Heuristic={heur_r:+.2f}")
si = demo_data.get("self_improvement", [])
if si:
summary_parts.append(f"\n**Self-Improvement:** Gen 0 reward={si[0]['total_reward']:+.2f} โ Gen {len(si)-1} reward={si[-1]['total_reward']:+.2f}")
gr.Markdown("\n".join(summary_parts))
# Events
reset_btn.click(
reset_env, inputs=[difficulty],
outputs=[status_box, network_plot, org_plot, obs_display, threat_box, reward_box]
)
step_btn.click(
take_action, inputs=[action_type, action_name, target, reasoning],
outputs=[status_box, network_plot, org_plot, obs_display,
threat_box, reward_box, war_room_feed, pipeline_feed]
)
inject_btn.click(
inject_preference, inputs=[pref_directive, pref_priority],
outputs=[inject_status]
)
metrics_btn.click(
get_metrics_plots, outputs=[belief_plot, reward_plot]
)
refresh_tracks_btn.click(
build_5track_chart, outputs=[track_chart]
)
refresh_migration_btn.click(
lambda: (build_migration_progress(), build_honeytoken_table()),
outputs=[migration_display, honeytoken_display]
)
return demo
if __name__ == "__main__":
demo = build_dashboard()
demo.launch(server_name="0.0.0.0", server_port=7861, share=False)
|