Policy tab: make rule fields editable, add a loaded-policy status line
Browse files- ui/tab_policy.py +46 -0
ui/tab_policy.py
CHANGED
|
@@ -66,6 +66,30 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 66 |
gr.Markdown("Upload a markdown file:")
|
| 67 |
upload_file = gr.File(label="Upload Markdown File", file_types=[".md"])
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# --- Structured rule editor (model-agnostic; native where supported) -------------------------------
|
| 70 |
with gr.Accordion("🧩 Your policy as rules (switch any rule on or off)", open=True):
|
| 71 |
gr.Markdown(
|
|
@@ -139,36 +163,47 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 139 |
header += " (off)"
|
| 140 |
with gr.Accordion(header, open=(not rule_has_content(rule)) or len(rules) == 1):
|
| 141 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
enabled_cb = gr.Checkbox(
|
| 143 |
label="Use this rule",
|
| 144 |
value=enabled,
|
| 145 |
scale=0,
|
| 146 |
min_width=140,
|
| 147 |
info="Switch off to test without it. The rule is kept.",
|
|
|
|
| 148 |
)
|
| 149 |
remove_btn = gr.Button("🗑 Remove", scale=0, min_width=100, variant="secondary")
|
| 150 |
name_tb = gr.Textbox(
|
| 151 |
label="Rule name",
|
| 152 |
value=rule.get("name", ""),
|
| 153 |
placeholder="e.g. Threats and coercion",
|
|
|
|
| 154 |
)
|
| 155 |
flag_tb = gr.Textbox(
|
| 156 |
label="✅ Flag this (violation)",
|
| 157 |
value=rule.get("flag", ""),
|
| 158 |
lines=2,
|
| 159 |
placeholder="What counts as a violation of this rule…",
|
|
|
|
| 160 |
)
|
| 161 |
dont_tb = gr.Textbox(
|
| 162 |
label="🚫 Don't flag this",
|
| 163 |
value=rule.get("dont_flag", ""),
|
| 164 |
lines=2,
|
| 165 |
placeholder="Lookalikes that must NOT be flagged, e.g. counter-speech, quoting to report…",
|
|
|
|
| 166 |
)
|
| 167 |
exc_tb = gr.Textbox(
|
| 168 |
label="⚠️ Exception",
|
| 169 |
value=rule.get("exception", ""),
|
| 170 |
lines=2,
|
| 171 |
placeholder="Carve-outs to the violation rule…",
|
|
|
|
| 172 |
)
|
| 173 |
for field, comp in (
|
| 174 |
("flag", flag_tb),
|
|
@@ -219,6 +254,13 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 219 |
inputs=[structured_rules_state, current_policy_state],
|
| 220 |
outputs=current_policy_state,
|
| 221 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Manual Edition Accordion
|
| 224 |
with gr.Accordion("✏️ Manual Editing", open=False):
|
|
@@ -279,6 +321,10 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 279 |
# Drop blank starter rows, keep anything with content, then append.
|
| 280 |
kept = [r for r in (rules or []) if rule_has_content(r)]
|
| 281 |
new_label = name if not kept else f"{(label or '').strip()} + {name}".lstrip(" +")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
return kept + added, (version or 0) + 1, new_label
|
| 283 |
|
| 284 |
load_preset_btn.click(
|
|
|
|
| 66 |
gr.Markdown("Upload a markdown file:")
|
| 67 |
upload_file = gr.File(label="Upload Markdown File", file_types=[".md"])
|
| 68 |
|
| 69 |
+
# --- What is actually loaded --------------------------------------------------------
|
| 70 |
+
# Adding a preset used to change only the rule list further down the page, which
|
| 71 |
+
# on a laptop is below the fold: people pressed the button and saw nothing move.
|
| 72 |
+
# This line sits directly under the loader and names what is live right now.
|
| 73 |
+
def _policy_status(rules, label):
|
| 74 |
+
"""One line naming the policy currently under test."""
|
| 75 |
+
kept = [r for r in (rules or []) if rule_has_content(r)]
|
| 76 |
+
if not kept:
|
| 77 |
+
return "⚪ **No policy loaded** — add a preset above, or write your own rules below."
|
| 78 |
+
on = [r for r in kept if rule_is_enabled(r)]
|
| 79 |
+
sources = []
|
| 80 |
+
for r in kept:
|
| 81 |
+
# Just the ID ("MH-P1"), matching the tag in each rule's header.
|
| 82 |
+
s = (r.get("source") or "").split(" · ")[0].strip() or "yours"
|
| 83 |
+
if s not in sources:
|
| 84 |
+
sources.append(s)
|
| 85 |
+
off_note = "" if len(on) == len(kept) else f" ({len(kept) - len(on)} switched off)"
|
| 86 |
+
return (
|
| 87 |
+
f"🟢 **Policy loaded:** {', '.join(sources)} — "
|
| 88 |
+
f"**{len(on)}** of {len(kept)} rule{'' if len(kept) == 1 else 's'} active{off_note}."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
policy_status = gr.Markdown(_policy_status([new_rule()], ""))
|
| 92 |
+
|
| 93 |
# --- Structured rule editor (model-agnostic; native where supported) -------------------------------
|
| 94 |
with gr.Accordion("🧩 Your policy as rules (switch any rule on or off)", open=True):
|
| 95 |
gr.Markdown(
|
|
|
|
| 163 |
header += " (off)"
|
| 164 |
with gr.Accordion(header, open=(not rule_has_content(rule)) or len(rules) == 1):
|
| 165 |
with gr.Row():
|
| 166 |
+
# interactive=True is explicit on every field below. Gradio
|
| 167 |
+
# normally infers it from whether a component is wired as an
|
| 168 |
+
# event input, but that inference does not hold for components
|
| 169 |
+
# built inside gr.render: the listeners are attached after the
|
| 170 |
+
# component is constructed, so the fields ship as display-only
|
| 171 |
+
# and cannot be typed into.
|
| 172 |
enabled_cb = gr.Checkbox(
|
| 173 |
label="Use this rule",
|
| 174 |
value=enabled,
|
| 175 |
scale=0,
|
| 176 |
min_width=140,
|
| 177 |
info="Switch off to test without it. The rule is kept.",
|
| 178 |
+
interactive=True,
|
| 179 |
)
|
| 180 |
remove_btn = gr.Button("🗑 Remove", scale=0, min_width=100, variant="secondary")
|
| 181 |
name_tb = gr.Textbox(
|
| 182 |
label="Rule name",
|
| 183 |
value=rule.get("name", ""),
|
| 184 |
placeholder="e.g. Threats and coercion",
|
| 185 |
+
interactive=True,
|
| 186 |
)
|
| 187 |
flag_tb = gr.Textbox(
|
| 188 |
label="✅ Flag this (violation)",
|
| 189 |
value=rule.get("flag", ""),
|
| 190 |
lines=2,
|
| 191 |
placeholder="What counts as a violation of this rule…",
|
| 192 |
+
interactive=True,
|
| 193 |
)
|
| 194 |
dont_tb = gr.Textbox(
|
| 195 |
label="🚫 Don't flag this",
|
| 196 |
value=rule.get("dont_flag", ""),
|
| 197 |
lines=2,
|
| 198 |
placeholder="Lookalikes that must NOT be flagged, e.g. counter-speech, quoting to report…",
|
| 199 |
+
interactive=True,
|
| 200 |
)
|
| 201 |
exc_tb = gr.Textbox(
|
| 202 |
label="⚠️ Exception",
|
| 203 |
value=rule.get("exception", ""),
|
| 204 |
lines=2,
|
| 205 |
placeholder="Carve-outs to the violation rule…",
|
| 206 |
+
interactive=True,
|
| 207 |
)
|
| 208 |
for field, comp in (
|
| 209 |
("flag", flag_tb),
|
|
|
|
| 254 |
inputs=[structured_rules_state, current_policy_state],
|
| 255 |
outputs=current_policy_state,
|
| 256 |
)
|
| 257 |
+
# Driven off the rule list itself, so every route that changes the policy
|
| 258 |
+
# (preset, upload, "Add as rules", a toggle, Clear) updates the same line.
|
| 259 |
+
structured_rules_state.change(
|
| 260 |
+
_policy_status,
|
| 261 |
+
inputs=[structured_rules_state, policy_label_state],
|
| 262 |
+
outputs=policy_status,
|
| 263 |
+
)
|
| 264 |
|
| 265 |
# Manual Edition Accordion
|
| 266 |
with gr.Accordion("✏️ Manual Editing", open=False):
|
|
|
|
| 321 |
# Drop blank starter rows, keep anything with content, then append.
|
| 322 |
kept = [r for r in (rules or []) if rule_has_content(r)]
|
| 323 |
new_label = name if not kept else f"{(label or '').strip()} + {name}".lstrip(" +")
|
| 324 |
+
gr.Info(
|
| 325 |
+
f"Added {len(added)} rule{'' if len(added) == 1 else 's'} from {name} — "
|
| 326 |
+
f"your policy now has {len(kept) + len(added)}."
|
| 327 |
+
)
|
| 328 |
return kept + added, (version or 0) + 1, new_label
|
| 329 |
|
| 330 |
load_preset_btn.click(
|