JSpon commited on
Commit
2d9473a
·
verified ·
1 Parent(s): 44de8b1

Stop the rule editor re-rendering on every keystroke

Browse files

Each rule textbox wrote back into structured_rules_state on blur, and the
@gr .render that builds those textboxes was bound to the same state. Editing a
field tore the inputs down and rebuilt them under the cursor, which reads as the
fields being frozen once you type in them.

Adding or removing a rule is the only thing that changes which components exist,
so a rules_version_state counter now drives the render and field edits no longer
trigger it. app.py passes the Blocks in so the initial render still fires on load.

Files changed (2) hide show
  1. app.py +1 -1
  2. ui/tab_policy.py +19 -9
app.py CHANGED
@@ -182,7 +182,7 @@ with gr.Blocks(title="Moderation Model Testing") as demo:
182
  reasoning_info = testing_components["reasoning_info"]
183
  reasoning_display = testing_components["reasoning_display"]
184
 
185
- policy_components = build_policy_tab(os.path.dirname(__file__))
186
  current_policy_state = policy_components["current_policy_state"]
187
  structured_rules_state = policy_components["structured_rules_state"]
188
 
 
182
  reasoning_info = testing_components["reasoning_info"]
183
  reasoning_display = testing_components["reasoning_display"]
184
 
185
+ policy_components = build_policy_tab(os.path.dirname(__file__), demo)
186
  current_policy_state = policy_components["current_policy_state"]
187
  structured_rules_state = policy_components["structured_rules_state"]
188
 
ui/tab_policy.py CHANGED
@@ -16,7 +16,7 @@ from utils.policy_rules import (
16
  )
17
 
18
 
19
- def build_policy_tab(base_dir: str) -> dict:
20
  """Build the policy definition tab UI."""
21
  with gr.Tab("📋 Policy Definition"):
22
  current_policy_state = gr.State(value="")
@@ -26,6 +26,12 @@ def build_policy_tab(base_dir: str) -> dict:
26
  # prompted models, and passed as a native category schema to any model that
27
  # accepts one (currently PolicyLM). Same policy either way.
28
  structured_rules_state = gr.State(value=[new_rule()])
 
 
 
 
 
 
29
 
30
  # --- Structured rule editor (model-agnostic; native where supported) -------------------------------
31
  with gr.Accordion("🧩 Structured Rules (recommended — one score per rule)", open=True):
@@ -57,14 +63,18 @@ def build_policy_tab(base_dir: str) -> dict:
57
 
58
  def _make_remover(idx: int):
59
  """Drop the rule at idx, keeping at least one (blank) rule."""
60
- def _rm(rules):
61
  rules = [dict(r) for r in (rules or [])]
62
  if 0 <= idx < len(rules):
63
  rules.pop(idx)
64
- return rules or [new_rule()]
65
  return _rm
66
 
67
- @gr.render(inputs=structured_rules_state)
 
 
 
 
68
  def render_rules(rules):
69
  rules = rules or [new_rule()]
70
  for i, rule in enumerate(rules):
@@ -108,14 +118,14 @@ def build_policy_tab(base_dir: str) -> dict:
108
  )
109
  remove_btn.click(
110
  _make_remover(i),
111
- inputs=structured_rules_state,
112
- outputs=structured_rules_state,
113
  )
114
 
115
  add_rule_btn.click(
116
- lambda rules: (rules or []) + [new_rule()],
117
- inputs=structured_rules_state,
118
- outputs=structured_rules_state,
119
  )
120
 
121
  def sync_rules_to_policy(rules, current_policy):
 
16
  )
17
 
18
 
19
+ def build_policy_tab(base_dir: str, demo: gr.Blocks | None = None) -> dict:
20
  """Build the policy definition tab UI."""
21
  with gr.Tab("📋 Policy Definition"):
22
  current_policy_state = gr.State(value="")
 
26
  # prompted models, and passed as a native category schema to any model that
27
  # accepts one (currently PolicyLM). Same policy either way.
28
  structured_rules_state = gr.State(value=[new_rule()])
29
+ # Re-render counter. The rule editor must NOT re-render on every field edit:
30
+ # each textbox writes back into structured_rules_state on blur, so a render
31
+ # bound to that state tears the boxes down and rebuilds them under the
32
+ # user's cursor, which reads as the fields being frozen. Only adding or
33
+ # removing a rule changes which components exist, so only those bump this.
34
+ rules_version_state = gr.State(value=0)
35
 
36
  # --- Structured rule editor (model-agnostic; native where supported) -------------------------------
37
  with gr.Accordion("🧩 Structured Rules (recommended — one score per rule)", open=True):
 
63
 
64
  def _make_remover(idx: int):
65
  """Drop the rule at idx, keeping at least one (blank) rule."""
66
+ def _rm(rules, version):
67
  rules = [dict(r) for r in (rules or [])]
68
  if 0 <= idx < len(rules):
69
  rules.pop(idx)
70
+ return (rules or [new_rule()]), version + 1
71
  return _rm
72
 
73
+ _render_triggers = [rules_version_state.change]
74
+ if demo is not None:
75
+ _render_triggers.append(demo.load)
76
+
77
+ @gr.render(inputs=structured_rules_state, triggers=_render_triggers)
78
  def render_rules(rules):
79
  rules = rules or [new_rule()]
80
  for i, rule in enumerate(rules):
 
118
  )
119
  remove_btn.click(
120
  _make_remover(i),
121
+ inputs=[structured_rules_state, rules_version_state],
122
+ outputs=[structured_rules_state, rules_version_state],
123
  )
124
 
125
  add_rule_btn.click(
126
+ lambda rules, version: ((rules or []) + [new_rule()], version + 1),
127
+ inputs=[structured_rules_state, rules_version_state],
128
+ outputs=[structured_rules_state, rules_version_state],
129
  )
130
 
131
  def sync_rules_to_policy(rules, current_policy):