JSpon commited on
Commit
628b810
Β·
verified Β·
1 Parent(s): 716934d

Build the preset dropdown from the policy registry

Browse files

The ten workshop policies were registered in load_preset_policy's file map but the
dropdown's choices were a separate hardcoded list in three places, so the files
shipped without ever appearing in the UI.

PRESET_POLICY_FILES is now module-level and preset_policy_names() derives the
dropdown from it, so a policy cannot be registered yet be unselectable.

Files changed (2) hide show
  1. ui/tab_policy.py +5 -5
  2. utils/helpers.py +30 -58
ui/tab_policy.py CHANGED
@@ -7,7 +7,7 @@ import gradio as gr
7
 
8
  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
 
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
  policy_limits_markdown,
@@ -142,11 +142,11 @@ def build_policy_tab(base_dir: str) -> dict:
142
  with gr.Column():
143
  preset_dropdown = gr.Dropdown(
144
  label="Select Preset",
145
- choices=list_preset_policy_names(),
146
  value=None
147
  )
148
  load_preset_btn = gr.Button("Load Preset")
149
- credit_markdown = gr.Markdown("**Credit:** The original example policies were taken from the [Zentropi website](https://zentropi.ai/labelers). The **Workshop β€”** presets are from the ROOST Γ— Musubi policy-following workshop (27 Oct 2026).")
150
 
151
  with gr.Column():
152
  gr.Markdown("Upload a markdown file:")
@@ -172,7 +172,7 @@ def build_policy_tab(base_dir: str) -> dict:
172
  return "", "*No policy loaded*", ""
173
 
174
  # Check presets first
175
- preset_choices = list_preset_policy_names()
176
  if name in preset_choices:
177
  policy_text, _ = load_preset_policy(name, base_dir)
178
  return policy_text, policy_text, policy_text
@@ -213,7 +213,7 @@ def build_policy_tab(base_dir: str) -> dict:
213
  uploaded_policies[upload_key] = policy_text
214
 
215
  # Build updated choices: presets + uploaded policies
216
- preset_choices = list_preset_policy_names()
217
  all_choices = preset_choices + sorted(uploaded_policies.keys())
218
 
219
  # Show warning if duplicate (gr.Warning is a function, not an exception)
 
7
 
8
  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
 
10
+ from utils.helpers import preset_policy_names, load_policy_from_file, load_preset_policy
11
  from utils.policy_rules import (
12
  new_rule,
13
  policy_limits_markdown,
 
142
  with gr.Column():
143
  preset_dropdown = gr.Dropdown(
144
  label="Select Preset",
145
+ choices=preset_policy_names(),
146
  value=None
147
  )
148
  load_preset_btn = gr.Button("Load Preset")
149
+ credit_markdown = gr.Markdown("**Credit:** The example policies were taken from the [Zentropi website](https://zentropi.ai/labelers).")
150
 
151
  with gr.Column():
152
  gr.Markdown("Upload a markdown file:")
 
172
  return "", "*No policy loaded*", ""
173
 
174
  # Check presets first
175
+ preset_choices = preset_policy_names()
176
  if name in preset_choices:
177
  policy_text, _ = load_preset_policy(name, base_dir)
178
  return policy_text, policy_text, policy_text
 
213
  uploaded_policies[upload_key] = policy_text
214
 
215
  # Build updated choices: presets + uploaded policies
216
+ preset_choices = preset_policy_names()
217
  all_choices = preset_choices + sorted(uploaded_policies.keys())
218
 
219
  # Show warning if duplicate (gr.Warning is a function, not an exception)
utils/helpers.py CHANGED
@@ -206,78 +206,50 @@ def format_token_status(oauth_token: gr.OAuthToken | None) -> str:
206
  return "\n".join(lines)
207
 
208
 
209
- # Selectable preset policies (dropdown label -> filename in example_policies/).
210
- # The "Workshop β€” …" entries are pre-loaded verbatim from the ROOST x Musubi
211
- # policy-following workshop sheet (27 Oct 2026): each room's first entry is its
212
- # starting ("P1") policy and the second is the deliberate edit that room compares
213
- # against, so both sides of the comparison are selectable.
214
  PRESET_POLICY_FILES = {
215
- "Hate Speech Policy": "hate_speech.md",
216
- "Violence Policy": "violence.md",
217
- "Toxicity Policy": "toxicity.md",
218
- "Workshop β€” Hate Speech": "workshop_hate_speech.md",
219
- "Workshop β€” Hate Speech (Edit: AddressHate one-paragraph baseline)": "workshop_hate_speech__edit_addresshate_one_paragra.md",
220
- "Workshop β€” Mental Health": "workshop_mental_health.md",
221
- "Workshop β€” Mental Health (Edit: ages 13-15)": "workshop_mental_health__edit_ages_13_15.md",
222
- "Workshop β€” Sexual Content": "workshop_sexual_content.md",
223
- "Workshop β€” Sexual Content (Edit: carve-outs removed)": "workshop_sexual_content__edit_carve_outs_removed.md",
224
- "Workshop β€” Scams": "workshop_scams.md",
225
- "Workshop β€” Scams (Edit: giveaway carve-out removed)": "workshop_scams__edit_giveaway_carve_out_remo.md",
226
- "Workshop β€” Other Areas": "workshop_other_areas.md",
227
- "Workshop β€” Other Areas (Backup: violence and threats)": "workshop_other_areas__backup_violence_and_threats.md",
228
- }
229
-
230
-
231
- def list_preset_policy_names() -> list[str]:
232
- """Preset policy dropdown labels, in definition order."""
233
- return list(PRESET_POLICY_FILES.keys())
 
 
 
234
 
235
 
236
  def load_preset_policy(preset_name: str, base_dir: str) -> tuple[str, str]:
237
  """Load preset policy from markdown file."""
238
- if preset_name in PRESET_POLICY_FILES:
239
- filename = PRESET_POLICY_FILES[preset_name]
240
- policy_path = os.path.join(base_dir, "example_policies", filename)
241
  try:
242
  with open(policy_path, "r") as f:
243
  policy_text = f.read()
244
  return policy_text, policy_text
245
  except FileNotFoundError:
246
- raise gr.Error(f"Policy file '{filename}' not found at {policy_path}. Please check the file exists.")
247
  except Exception as e:
248
- raise gr.Error(f"Failed to load policy file '{filename}': {str(e)}")
249
  return "", ""
250
 
251
 
252
- def load_workshop_samples(base_dir: str) -> dict[str, str]:
253
- """
254
- Load the workshop sample content as an ordered {label: text} map for the
255
- "Load Example" dropdown. Reads example_samples/workshop_samples.csv (columns
256
- sample_id, room, text, language). Labels read like "HS-01 Β· 1. Hate speech".
257
- Returns {} if the file is missing so the app still runs without it.
258
- """
259
- import csv
260
-
261
- path = os.path.join(base_dir, "example_samples", "workshop_samples.csv")
262
- samples: dict[str, str] = {}
263
- if not os.path.exists(path):
264
- return samples
265
- try:
266
- with open(path, newline="", encoding="utf-8") as f:
267
- for row in csv.DictReader(f):
268
- sid = (row.get("sample_id") or "").strip()
269
- room = (row.get("room") or "").strip()
270
- text = row.get("text") or ""
271
- if not sid or not text.strip():
272
- continue
273
- label = f"{sid} Β· {room}" if room else sid
274
- samples[label] = text
275
- except Exception:
276
- # A malformed CSV shouldn't take down the app; just skip the samples.
277
- return {}
278
- return samples
279
-
280
-
281
  def load_policy_from_file(file_path: str) -> tuple[str, str]:
282
  """Load policy from uploaded file."""
283
  try:
 
206
  return "\n".join(lines)
207
 
208
 
209
+ # Preset policies offered in the Policy Definition tab. This dict is the single
210
+ # source of truth: the dropdown is built from its keys, so a policy file can never
211
+ # be registered here yet be missing from the UI.
 
 
212
  PRESET_POLICY_FILES = {
213
+ "Hate Speech Policy": "hate_speech.md",
214
+ "Violence Policy": "violence.md",
215
+ "Toxicity Policy": "toxicity.md",
216
+ # Pre-loaded from the ROOST x Musubi workshop sheet (27 Oct 2026). Each room's
217
+ # P1 is its starting policy; the second entry is the deliberate edit that room
218
+ # compares against, so both sides of the comparison are selectable here.
219
+ "Workshop β€” Hate Speech": "workshop_hate_speech.md",
220
+ "Workshop β€” Hate Speech (Edit: AddressHate one-paragraph baseline)": "workshop_hate_speech__edit_addresshate_one_paragra.md",
221
+ "Workshop β€” Mental Health": "workshop_mental_health.md",
222
+ "Workshop β€” Mental Health (Edit: ages 13-15)": "workshop_mental_health__edit_ages_13_15.md",
223
+ "Workshop β€” Sexual Content": "workshop_sexual_content.md",
224
+ "Workshop β€” Sexual Content (Edit: carve-outs removed)": "workshop_sexual_content__edit_carve_outs_removed.md",
225
+ "Workshop β€” Scams": "workshop_scams.md",
226
+ "Workshop β€” Scams (Edit: giveaway carve-out removed)": "workshop_scams__edit_giveaway_carve_out_remo.md",
227
+ "Workshop β€” Other Areas": "workshop_other_areas.md",
228
+ "Workshop β€” Other Areas (Backup: violence and threats)": "workshop_other_areas__backup_violence_and_threats.md",
229
+ }
230
+
231
+
232
+ def preset_policy_names() -> list[str]:
233
+ """Names for the preset dropdown, in registration order."""
234
+ return list(PRESET_POLICY_FILES)
235
 
236
 
237
  def load_preset_policy(preset_name: str, base_dir: str) -> tuple[str, str]:
238
  """Load preset policy from markdown file."""
239
+ preset_files = PRESET_POLICY_FILES
240
+ if preset_name in preset_files:
241
+ policy_path = os.path.join(base_dir, "example_policies", preset_files[preset_name])
242
  try:
243
  with open(policy_path, "r") as f:
244
  policy_text = f.read()
245
  return policy_text, policy_text
246
  except FileNotFoundError:
247
+ raise gr.Error(f"Policy file '{preset_files[preset_name]}' not found at {policy_path}. Please check the file exists.")
248
  except Exception as e:
249
+ raise gr.Error(f"Failed to load policy file '{preset_files[preset_name]}': {str(e)}")
250
  return "", ""
251
 
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  def load_policy_from_file(file_path: str) -> tuple[str, str]:
254
  """Load policy from uploaded file."""
255
  try: