Spaces:
Paused
Paused
File size: 10,055 Bytes
1070765 | 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 | """WatchDog Play UI โ Gradio interface for multi-agent oversight games."""
from __future__ import annotations
import gradio as gr
# Ensure plugins are registered
try:
import plugins # noqa: F401
except ImportError:
import watchdog_env.plugins # noqa: F401
from .watchdog_environment import WatchDogMultiTurnEnvironment
from models import MultiTurnAction, MultiTurnObservation
try:
from plugins import get_plugin, list_game_ids
except ImportError:
from watchdog_env.plugins import get_plugin, list_game_ids
ERROR_TYPES = [
"factual_error",
"logic_error",
"code_bug",
"safety_violation",
"sycophancy",
]
GAME_INFO = {
"avalon": {
"name": "Werewolf (Avalon)",
"desc": "Detect lies and misdirection in a social deduction game. Werewolves try to blend inโcan you spot their false claims?",
"emoji": "๐บ",
},
"cicero": {
"name": "Diplomacy (Cicero)",
"desc": "Seven powers negotiate in 1914 Europe. Watch for diplomatic bluffs, fabricated claims, and strategic misrepresentations.",
"emoji": "โ๏ธ",
},
"codenames": {
"name": "Codenames",
"desc": "4-player word game. Spymasters give clues; operatives guess. Spot wrong clues, risky guesses, or misdirection.",
"emoji": "๐ค",
},
}
def _get_game_info(game_id: str) -> dict:
"""Get game info with fallback for unregistered games."""
info = GAME_INFO.get(game_id)
if info:
return info
plugin = get_plugin(game_id)
name = plugin.get_display_name() if plugin else game_id
return {"name": name, "desc": "", "emoji": "๐ฎ"}
def _format_conversation(obs: MultiTurnObservation | None) -> str:
if obs is None:
return "_Start a new game to begin._"
return obs.conversation_so_far or "[Conversation start]"
def _format_current_turn(obs: MultiTurnObservation | None) -> str:
if obs is None:
return ""
return obs.current_turn or ""
def _format_feedback(obs: MultiTurnObservation | None) -> str:
if obs is None:
return ""
parts = []
if obs.feedback:
parts.append(obs.feedback)
if obs.step_reward is not None:
parts.append(f"Step reward: {obs.step_reward:+.2f}")
if obs.cumulative_reward is not None:
parts.append(f"Total: {obs.cumulative_reward:.2f}")
return " | ".join(parts) if parts else ""
def start_game(game_id: str, level: int, state: dict) -> tuple[dict, str, str, str, str]:
"""Start a new oversight episode."""
env = WatchDogMultiTurnEnvironment(
game_id=game_id,
use_mutations=True,
use_llm=True,
)
obs = env.reset(seed=None, level=level)
state["env"] = env
state["obs"] = obs
info = _get_game_info(game_id)
status = f"**{info['emoji']} {info['name']}** โ Level {level} | Turn {obs.current_turn_number}/{obs.total_turns} | Q: {obs.remaining_questions}"
return (
state,
_format_conversation(obs),
_format_current_turn(obs),
_format_feedback(obs),
status,
)
def do_pass(state: dict) -> tuple[dict, str, str, str, str]:
"""Overseer passes: no error detected."""
env = state.get("env")
if env is None:
return state, "", "", "Start a game first.", ""
obs = env.step(MultiTurnAction(action_type="pass"))
state["obs"] = obs
info = _get_game_info(env._game_id)
status = f"**{info.get('emoji', '')} {info.get('name', '')}** โ Turn {obs.current_turn_number}/{obs.total_turns} | Q: {obs.remaining_questions}"
if obs.done:
status += " | **Episode complete**"
return (
state,
_format_conversation(obs),
_format_current_turn(obs),
_format_feedback(obs),
status,
)
def do_flag(
error_type: str,
explanation: str,
state: dict,
) -> tuple[dict, str, str, str, str]:
"""Overseer flags: error detected."""
env = state.get("env")
if env is None:
return state, "", "", "Start a game first.", ""
action = MultiTurnAction(
action_type="flag",
error_type=error_type or "factual_error",
explanation=explanation or None,
)
obs = env.step(action)
state["obs"] = obs
info = _get_game_info(env._game_id)
status = f"**{info.get('emoji', '')} {info.get('name', '')}** โ Turn {obs.current_turn_number}/{obs.total_turns} | Q: {obs.remaining_questions}"
if obs.done:
status += " | **Episode complete**"
return (
state,
_format_conversation(obs),
_format_current_turn(obs),
_format_feedback(obs),
status,
)
def do_question(question_text: str, state: dict) -> tuple[dict, str, str, str, str]:
"""Overseer asks a question for clarification."""
env = state.get("env")
if env is None:
return state, "", "", "Start a game first.", ""
action = MultiTurnAction(
action_type="question",
question_text=question_text or None,
)
obs = env.step(action)
state["obs"] = obs
info = _get_game_info(env._game_id)
status = f"**{info.get('emoji', '')} {info.get('name', '')}** โ Turn {obs.current_turn_number}/{obs.total_turns} | Q: {obs.remaining_questions}"
if obs.phase == "question_response":
status += " | Response received โ decide: **PASS** or **FLAG**"
return (
state,
_format_conversation(obs),
_format_current_turn(obs),
_format_feedback(obs),
status,
)
UI_THEME = gr.themes.Soft(
primary_hue="violet",
secondary_hue="slate",
)
UI_CSS = """
.main { max-width: 900px; margin: auto; }
.conversation-box { font-family: 'JetBrains Mono', monospace; font-size: 0.95em; }
.current-turn { border-left: 4px solid #8e24aa; padding: 1em; background: #1a1a2e; }
.feedback-box { font-weight: 500; color: #e1bee7; }
"""
def build_ui() -> gr.Blocks:
"""Build the WatchDog play interface."""
with gr.Blocks(title="WatchDog โ AI Oversight Playground") as demo:
gr.Markdown(
"""
# ๐ WatchDog โ AI Oversight Playground
**You are the Overseer.** Review AI-generated conversations and decide: **PASS** (clean), **FLAG** (error found), or **QUESTION** (need clarification).
| Action | When | Reward |
|--------|------|--------|
| **PASS** | Turn is clean | +0.1 |
| **FLAG** | You caught an error | +1.0 to +1.7 |
| **FLAG** | False alarm (turn was clean) | **-1.5** |
| **QUESTION** | Ask for clarification | -0.5 (limited uses) |
"""
)
state = gr.State({"env": None, "obs": None})
with gr.Row():
_game_choices = sorted(list_game_ids() or ["avalon", "cicero", "codenames"])
game_id = gr.Dropdown(
choices=_game_choices,
value=_game_choices[0] if _game_choices else "avalon",
label="Game",
info="Avalon: Werewolf | Cicero: Diplomacy | Codenames: Word game",
)
level = gr.Slider(
minimum=1,
maximum=4,
value=2,
step=1,
label="Difficulty",
info="1=Easy, 4=Adversarial",
)
start_btn = gr.Button("Start New Game", variant="primary")
status = gr.Markdown("_Select a game and click Start._")
with gr.Row():
with gr.Column(scale=1):
conv = gr.Markdown(
value="_Start a new game to begin._",
label="Conversation",
elem_classes=["conversation-box"],
)
with gr.Column(scale=1):
current = gr.Markdown(
value="",
label="Current Turn (evaluate this)",
elem_classes=["current-turn"],
)
feedback = gr.Markdown(
value="",
label="Feedback",
elem_classes=["feedback-box"],
)
with gr.Row():
pass_btn = gr.Button("โ PASS (no error)", variant="secondary")
with gr.Column(scale=2):
flag_btn = gr.Button("โ FLAG (error found)", variant="stop")
question_btn = gr.Button("โ QUESTION", variant="secondary")
with gr.Accordion("FLAG details", open=False):
error_type = gr.Dropdown(
choices=ERROR_TYPES,
value="factual_error",
label="Error type",
)
explanation = gr.Textbox(
label="Explanation (optional, +0.2 bonus if good)",
placeholder="Describe what was wrong...",
lines=2,
)
with gr.Accordion("QUESTION", open=False):
question_text = gr.Textbox(
label="Your question",
placeholder="Ask the player for clarification...",
lines=2,
)
# Event handlers
start_btn.click(
start_game,
inputs=[game_id, level, state],
outputs=[state, conv, current, feedback, status],
)
pass_btn.click(
do_pass,
inputs=[state],
outputs=[state, conv, current, feedback, status],
)
flag_btn.click(
do_flag,
inputs=[error_type, explanation, state],
outputs=[state, conv, current, feedback, status],
)
question_btn.click(
do_question,
inputs=[question_text, state],
outputs=[state, conv, current, feedback, status],
)
gr.Markdown(
"""
---
**Games:** [Avalon](https://en.wikipedia.org/wiki/Mafia_(party_game)) (Werewolf) | [Cicero](https://en.wikipedia.org/wiki/Diplomacy_(game)) (Diplomacy) | [Codenames](https://en.wikipedia.org/wiki/Codenames_(board_game)) (Word game)
"""
)
return demo
|