Open presets in the rule editor; name the policy under test
Browse filesA preset used to land only in the free-form box, so its rules were invisible beside
any you had written and the two could not be compared. Loading one now parses it
into the rule editor, and every rule's header says where it came from — "HS-P1" for
a preset rule, "yours" for one you added — so an edited preset rule and your own sit
in the same list without being indistinguishable.
The Testing tab also says which policy a run will use. The policy lives on another
tab, so it was easy to run content against one loaded earlier and forgotten. The
line reports rule counts when rules are active and character counts when it is
free-form text, because structured rules override the text and the banner has to
describe what is actually sent, not what is on screen elsewhere.
- app.py +30 -1
- ui/tab_policy.py +41 -17
- ui/tab_testing.py +5 -0
|
@@ -28,7 +28,7 @@ from ui.sidebar import build_sidebar
|
|
| 28 |
from ui.tab_config import build_config_tab
|
| 29 |
from ui.tab_dataset import build_dataset_tab
|
| 30 |
from ui.tab_policy import build_policy_tab
|
| 31 |
-
from utils.policy_rules import new_rule, rules_have_content
|
| 32 |
from ui.tab_testing import (
|
| 33 |
build_testing_tab,
|
| 34 |
format_model_info,
|
|
@@ -187,6 +187,7 @@ with gr.Blocks(title="Moderation Model Testing") as demo:
|
|
| 187 |
current_policy_state = policy_components["current_policy_state"]
|
| 188 |
structured_rules_state = policy_components["structured_rules_state"]
|
| 189 |
rules_version_state = policy_components["rules_version_state"]
|
|
|
|
| 190 |
|
| 191 |
config_components = build_config_tab()
|
| 192 |
model_dropdown = config_components["model_dropdown"]
|
|
@@ -256,6 +257,34 @@ with gr.Blocks(title="Moderation Model Testing") as demo:
|
|
| 256 |
model_quick_select.change(lambda v: v, inputs=model_quick_select, outputs=model_dropdown)
|
| 257 |
model_dropdown.change(lambda v: v, inputs=model_dropdown, outputs=model_quick_select)
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
# --- Keep the policy across a page refresh -------------------------------------
|
| 260 |
# gr.State is per-session and is lost on reload, which in a workshop means retyping
|
| 261 |
# a policy after an accidental refresh. BrowserState keeps it in localStorage.
|
|
|
|
| 28 |
from ui.tab_config import build_config_tab
|
| 29 |
from ui.tab_dataset import build_dataset_tab
|
| 30 |
from ui.tab_policy import build_policy_tab
|
| 31 |
+
from utils.policy_rules import new_rule, rule_has_content, rule_is_enabled, rules_have_content
|
| 32 |
from ui.tab_testing import (
|
| 33 |
build_testing_tab,
|
| 34 |
format_model_info,
|
|
|
|
| 187 |
current_policy_state = policy_components["current_policy_state"]
|
| 188 |
structured_rules_state = policy_components["structured_rules_state"]
|
| 189 |
rules_version_state = policy_components["rules_version_state"]
|
| 190 |
+
policy_label_state = policy_components["policy_label_state"]
|
| 191 |
|
| 192 |
config_components = build_config_tab()
|
| 193 |
model_dropdown = config_components["model_dropdown"]
|
|
|
|
| 257 |
model_quick_select.change(lambda v: v, inputs=model_quick_select, outputs=model_dropdown)
|
| 258 |
model_dropdown.change(lambda v: v, inputs=model_dropdown, outputs=model_quick_select)
|
| 259 |
|
| 260 |
+
# --- Say which policy a run will use -------------------------------------------
|
| 261 |
+
active_policy_display = testing_components["active_policy_display"]
|
| 262 |
+
|
| 263 |
+
def _describe_active_policy(label, policy_text, rules):
|
| 264 |
+
"""One line naming the policy a run will actually use.
|
| 265 |
+
|
| 266 |
+
Structured rules override the free-form text, so the count has to come from
|
| 267 |
+
the rules whenever any are on — otherwise the banner would describe a document
|
| 268 |
+
that is not what gets sent.
|
| 269 |
+
"""
|
| 270 |
+
name = (label or "").strip() or "Custom (hand-written)"
|
| 271 |
+
rules = rules or []
|
| 272 |
+
on = [r for r in rules if rule_has_content(r) and rule_is_enabled(r)]
|
| 273 |
+
off = [r for r in rules if rule_has_content(r) and not rule_is_enabled(r)]
|
| 274 |
+
if on:
|
| 275 |
+
suffix = f" ({len(off)} switched off)" if off else ""
|
| 276 |
+
return f"**Policy:** {name} — {len(on)} rule{'s' if len(on) != 1 else ''}{suffix}"
|
| 277 |
+
if (policy_text or "").strip():
|
| 278 |
+
return f"**Policy:** {name} — free-form text, {len((policy_text or '').strip()):,} characters"
|
| 279 |
+
return "**Policy:** ⚠️ none loaded — set one on the 📋 Policy Definition tab"
|
| 280 |
+
|
| 281 |
+
for _trigger in (current_policy_state, structured_rules_state, policy_label_state):
|
| 282 |
+
_trigger.change(
|
| 283 |
+
_describe_active_policy,
|
| 284 |
+
inputs=[policy_label_state, current_policy_state, structured_rules_state],
|
| 285 |
+
outputs=active_policy_display,
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
# --- Keep the policy across a page refresh -------------------------------------
|
| 289 |
# gr.State is per-session and is lost on reload, which in a workshop means retyping
|
| 290 |
# a policy after an accidental refresh. BrowserState keeps it in localStorage.
|
|
@@ -10,6 +10,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
| 10 |
from utils.helpers import load_policy_from_file, load_preset_policy, list_preset_policy_names
|
| 11 |
from utils.policy_rules import (
|
| 12 |
new_rule,
|
|
|
|
| 13 |
rule_has_content,
|
| 14 |
rule_is_enabled,
|
| 15 |
policy_limits_markdown,
|
|
@@ -34,6 +35,10 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 34 |
# user's cursor, which reads as the fields being frozen. Only adding or
|
| 35 |
# removing a rule changes which components exist, so only those bump this.
|
| 36 |
rules_version_state = gr.State(value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# --- Structured rule editor (model-agnostic; native where supported) -------------------------------
|
| 39 |
with gr.Accordion("🧩 Structured Rules (recommended — one score per rule)", open=True):
|
|
@@ -101,7 +106,12 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 101 |
# why renaming re-renders.
|
| 102 |
enabled = rule_is_enabled(rule)
|
| 103 |
rule_name = (rule.get("name") or "").strip()
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if not enabled:
|
| 106 |
header += " — off"
|
| 107 |
with gr.Accordion(header, open=(not rule_has_content(rule)) or len(rules) == 1):
|
|
@@ -217,28 +227,41 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 217 |
clear_policy_btn = gr.Button("Clear Policy", variant="secondary")
|
| 218 |
|
| 219 |
# Handlers
|
| 220 |
-
def load_preset_handler(name, uploaded_policies):
|
| 221 |
-
"""Load policy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
if not name:
|
| 223 |
-
return
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
if name in preset_choices:
|
| 228 |
policy_text, _ = load_preset_policy(name, base_dir)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
# Check uploaded policies
|
| 232 |
-
if name in uploaded_policies:
|
| 233 |
policy_text = uploaded_policies[name]
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
load_preset_btn.click(
|
| 239 |
load_preset_handler,
|
| 240 |
-
inputs=[preset_dropdown, uploaded_policies_state],
|
| 241 |
-
outputs=[current_policy_state, manual_text, policy_preview
|
|
|
|
| 242 |
)
|
| 243 |
|
| 244 |
def load_upload_handler(f, uploaded_policies):
|
|
@@ -316,4 +339,5 @@ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
|
|
| 316 |
"structured_rules_state": structured_rules_state,
|
| 317 |
# Exposed so restoring a saved policy can force the rule editor to re-render.
|
| 318 |
"rules_version_state": rules_version_state,
|
|
|
|
| 319 |
}
|
|
|
|
| 10 |
from utils.helpers import load_policy_from_file, load_preset_policy, list_preset_policy_names
|
| 11 |
from utils.policy_rules import (
|
| 12 |
new_rule,
|
| 13 |
+
parse_policy_markdown,
|
| 14 |
rule_has_content,
|
| 15 |
rule_is_enabled,
|
| 16 |
policy_limits_markdown,
|
|
|
|
| 35 |
# user's cursor, which reads as the fields being frozen. Only adding or
|
| 36 |
# removing a rule changes which components exist, so only those bump this.
|
| 37 |
rules_version_state = gr.State(value=0)
|
| 38 |
+
# Name of whatever is currently loaded ("HS-P1 · …", an upload, or "" for
|
| 39 |
+
# hand-written). The Testing tab shows this so nobody runs content against a
|
| 40 |
+
# policy they have forgotten they loaded.
|
| 41 |
+
policy_label_state = gr.State(value="")
|
| 42 |
|
| 43 |
# --- Structured rule editor (model-agnostic; native where supported) -------------------------------
|
| 44 |
with gr.Accordion("🧩 Structured Rules (recommended — one score per rule)", open=True):
|
|
|
|
| 106 |
# why renaming re-renders.
|
| 107 |
enabled = rule_is_enabled(rule)
|
| 108 |
rule_name = (rule.get("name") or "").strip()
|
| 109 |
+
# Just the ID ("HS-P1"), not the whole preset name — repeated down
|
| 110 |
+
# a six-rule list the full name drowns out the rule's own title.
|
| 111 |
+
origin = (rule.get("source") or "").strip().split(" · ")[0].strip()
|
| 112 |
+
# Say where the rule came from, so an edited preset rule and one
|
| 113 |
+
# you wrote yourself are not indistinguishable in a long list.
|
| 114 |
+
header = f"{i + 1}. {rule_name or 'Untitled rule'} · {origin or 'yours'}"
|
| 115 |
if not enabled:
|
| 116 |
header += " — off"
|
| 117 |
with gr.Accordion(header, open=(not rule_has_content(rule)) or len(rules) == 1):
|
|
|
|
| 227 |
clear_policy_btn = gr.Button("Clear Policy", variant="secondary")
|
| 228 |
|
| 229 |
# Handlers
|
| 230 |
+
def load_preset_handler(name, uploaded_policies, version):
|
| 231 |
+
"""Load a policy and open it in the rule editor.
|
| 232 |
+
|
| 233 |
+
A preset used to land only in the free-form box, so its rules were
|
| 234 |
+
invisible next to any you had written. Parsing it into the editor puts
|
| 235 |
+
both in the same list, each tagged with where it came from, so a rule you
|
| 236 |
+
added sits beside the preset's and can be compared, switched off or edited.
|
| 237 |
+
"""
|
| 238 |
+
blank = ("", "*No policy loaded*", "", [new_rule()], (version or 0) + 1, "")
|
| 239 |
if not name:
|
| 240 |
+
return blank
|
| 241 |
+
|
| 242 |
+
policy_text = ""
|
| 243 |
+
if name in list_preset_policy_names():
|
|
|
|
| 244 |
policy_text, _ = load_preset_policy(name, base_dir)
|
| 245 |
+
elif name in (uploaded_policies or {}):
|
|
|
|
|
|
|
|
|
|
| 246 |
policy_text = uploaded_policies[name]
|
| 247 |
+
if not policy_text:
|
| 248 |
+
return blank
|
| 249 |
+
|
| 250 |
+
parsed = parse_policy_markdown(policy_text)
|
| 251 |
+
rules = [
|
| 252 |
+
{"name": c.get("name", ""), "flag": c.get("violation_rule", ""),
|
| 253 |
+
"dont_flag": c.get("not_violation_rule", ""),
|
| 254 |
+
"exception": c.get("exception_override", ""),
|
| 255 |
+
"enabled": True, "source": name}
|
| 256 |
+
for c in parsed
|
| 257 |
+
] or [new_rule()]
|
| 258 |
+
return policy_text, policy_text, policy_text, rules, (version or 0) + 1, name
|
| 259 |
|
| 260 |
load_preset_btn.click(
|
| 261 |
load_preset_handler,
|
| 262 |
+
inputs=[preset_dropdown, uploaded_policies_state, rules_version_state],
|
| 263 |
+
outputs=[current_policy_state, manual_text, policy_preview,
|
| 264 |
+
structured_rules_state, rules_version_state, policy_label_state],
|
| 265 |
)
|
| 266 |
|
| 267 |
def load_upload_handler(f, uploaded_policies):
|
|
|
|
| 339 |
"structured_rules_state": structured_rules_state,
|
| 340 |
# Exposed so restoring a saved policy can force the rule editor to re-render.
|
| 341 |
"rules_version_state": rules_version_state,
|
| 342 |
+
"policy_label_state": policy_label_state,
|
| 343 |
}
|
|
@@ -185,6 +185,10 @@ def build_testing_tab() -> dict:
|
|
| 185 |
value=f"{MODELS[0]['name']} ({MODELS[0]['id']})",
|
| 186 |
info="Reasoning level, system prompt and sampling live in ⚙️ Model Choice & Configuration.",
|
| 187 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
test_input = gr.Textbox(label="Test Content", placeholder="Enter content to test...", lines=5)
|
| 189 |
example_dropdown = gr.Dropdown(label="Load Example", choices=list(examples.keys()), value=None)
|
| 190 |
load_example_btn = gr.Button("Load Example", variant="secondary")
|
|
@@ -231,6 +235,7 @@ def build_testing_tab() -> dict:
|
|
| 231 |
|
| 232 |
return {
|
| 233 |
"model_quick_select": model_quick_select,
|
|
|
|
| 234 |
"test_input": test_input,
|
| 235 |
"example_dropdown": example_dropdown,
|
| 236 |
"load_example_btn": load_example_btn,
|
|
|
|
| 185 |
value=f"{MODELS[0]['name']} ({MODELS[0]['id']})",
|
| 186 |
info="Reasoning level, system prompt and sampling live in ⚙️ Model Choice & Configuration.",
|
| 187 |
)
|
| 188 |
+
# Which policy this run will use. The policy lives on another tab,
|
| 189 |
+
# so without this it is easy to run content against one you loaded
|
| 190 |
+
# earlier and forgot about.
|
| 191 |
+
active_policy_display = gr.Markdown("**Policy:** ⚠️ none loaded")
|
| 192 |
test_input = gr.Textbox(label="Test Content", placeholder="Enter content to test...", lines=5)
|
| 193 |
example_dropdown = gr.Dropdown(label="Load Example", choices=list(examples.keys()), value=None)
|
| 194 |
load_example_btn = gr.Button("Load Example", variant="secondary")
|
|
|
|
| 235 |
|
| 236 |
return {
|
| 237 |
"model_quick_select": model_quick_select,
|
| 238 |
+
"active_policy_display": active_policy_display,
|
| 239 |
"test_input": test_input,
|
| 240 |
"example_dropdown": example_dropdown,
|
| 241 |
"load_example_btn": load_example_btn,
|