Wire workshop policies and samples into the UI
Browse filesThe workshop policies and samples were pre-loaded as files but not reachable
from the UI: the policy preset dropdown still listed only the 3 original
presets, and workshop_samples.csv was never read. Surface all preset policies
(via list_preset_policy_names) in the dropdown + load handlers, and load the
20 workshop samples into the Testing tab's "Load Example" dropdown (via
load_workshop_samples). Content itself matches the ROOST x Musubi workbook
verbatim; this just makes it selectable.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- ui/tab_policy.py +5 -5
- ui/tab_testing.py +10 -3
- utils/helpers.py +61 -22
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
|
| 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=
|
| 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,7 +172,7 @@ def build_policy_tab(base_dir: str) -> dict:
|
|
| 172 |
return "", "*No policy loaded*", ""
|
| 173 |
|
| 174 |
# Check presets first
|
| 175 |
-
preset_choices =
|
| 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 =
|
| 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 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 |
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 |
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 |
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)
|
ui/tab_testing.py
CHANGED
|
@@ -11,7 +11,10 @@ import json
|
|
| 11 |
|
| 12 |
from utils.constants import MODELS, TEST_EXAMPLES
|
| 13 |
from utils.model_interface import extract_model_id, get_model_info
|
| 14 |
-
from utils.helpers import check_token_availability
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def parse_json_response(response: str) -> dict:
|
|
@@ -163,13 +166,17 @@ def format_test_result(result: dict) -> tuple[str, dict, str, str, str]:
|
|
| 163 |
|
| 164 |
def build_testing_tab() -> dict:
|
| 165 |
"""Build the testing tab UI and set up simple handlers."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
with gr.Tab("π§ͺ Testing"):
|
| 167 |
with gr.Row():
|
| 168 |
with gr.Column(scale=1):
|
| 169 |
gr.Markdown("### Input")
|
| 170 |
with gr.Group():
|
| 171 |
test_input = gr.Textbox(label="Test Content", placeholder="Enter content to test...", lines=5)
|
| 172 |
-
example_dropdown = gr.Dropdown(label="Load Example", choices=list(
|
| 173 |
load_example_btn = gr.Button("Load Example", variant="secondary")
|
| 174 |
run_test_btn = gr.Button("Run Test", variant="primary")
|
| 175 |
save_mode = gr.Radio(
|
|
@@ -207,7 +214,7 @@ def build_testing_tab() -> dict:
|
|
| 207 |
|
| 208 |
# Simple handlers that don't need cross-tab coordination
|
| 209 |
load_example_btn.click(
|
| 210 |
-
lambda name:
|
| 211 |
inputs=example_dropdown,
|
| 212 |
outputs=test_input,
|
| 213 |
)
|
|
|
|
| 11 |
|
| 12 |
from utils.constants import MODELS, TEST_EXAMPLES
|
| 13 |
from utils.model_interface import extract_model_id, get_model_info
|
| 14 |
+
from utils.helpers import check_token_availability, load_workshop_samples
|
| 15 |
+
|
| 16 |
+
# Base dir (repo root) for locating example_samples/.
|
| 17 |
+
_BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 18 |
|
| 19 |
|
| 20 |
def parse_json_response(response: str) -> dict:
|
|
|
|
| 166 |
|
| 167 |
def build_testing_tab() -> dict:
|
| 168 |
"""Build the testing tab UI and set up simple handlers."""
|
| 169 |
+
# "Load Example" offerings: workshop samples first (ROOST x Musubi sheet),
|
| 170 |
+
# then the built-in demo examples. One combined {label: text} map drives both
|
| 171 |
+
# the dropdown choices and the load handler.
|
| 172 |
+
examples = {**load_workshop_samples(_BASE_DIR), **TEST_EXAMPLES}
|
| 173 |
with gr.Tab("π§ͺ Testing"):
|
| 174 |
with gr.Row():
|
| 175 |
with gr.Column(scale=1):
|
| 176 |
gr.Markdown("### Input")
|
| 177 |
with gr.Group():
|
| 178 |
test_input = gr.Textbox(label="Test Content", placeholder="Enter content to test...", lines=5)
|
| 179 |
+
example_dropdown = gr.Dropdown(label="Load Example", choices=list(examples.keys()), value=None)
|
| 180 |
load_example_btn = gr.Button("Load Example", variant="secondary")
|
| 181 |
run_test_btn = gr.Button("Run Test", variant="primary")
|
| 182 |
save_mode = gr.Radio(
|
|
|
|
| 214 |
|
| 215 |
# Simple handlers that don't need cross-tab coordination
|
| 216 |
load_example_btn.click(
|
| 217 |
+
lambda name: examples.get(name, ""),
|
| 218 |
inputs=example_dropdown,
|
| 219 |
outputs=test_input,
|
| 220 |
)
|
utils/helpers.py
CHANGED
|
@@ -206,39 +206,78 @@ def format_token_status(oauth_token: gr.OAuthToken | None) -> str:
|
|
| 206 |
return "\n".join(lines)
|
| 207 |
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
def load_preset_policy(preset_name: str, base_dir: str) -> tuple[str, str]:
|
| 210 |
"""Load preset policy from markdown file."""
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
"Toxicity Policy": "toxicity.md",
|
| 215 |
-
# Pre-loaded from the ROOST x Musubi workshop sheet (27 Oct 2026). Each room's
|
| 216 |
-
# P1 is its starting policy; the second entry is the deliberate edit that room
|
| 217 |
-
# compares against, so both sides of the comparison are selectable here.
|
| 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 |
-
if preset_name in preset_files:
|
| 230 |
-
policy_path = os.path.join(base_dir, "example_policies", preset_files[preset_name])
|
| 231 |
try:
|
| 232 |
with open(policy_path, "r") as f:
|
| 233 |
policy_text = f.read()
|
| 234 |
return policy_text, policy_text
|
| 235 |
except FileNotFoundError:
|
| 236 |
-
raise gr.Error(f"Policy file '{
|
| 237 |
except Exception as e:
|
| 238 |
-
raise gr.Error(f"Failed to load policy file '{
|
| 239 |
return "", ""
|
| 240 |
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
def load_policy_from_file(file_path: str) -> tuple[str, str]:
|
| 243 |
"""Load policy from uploaded file."""
|
| 244 |
try:
|
|
|
|
| 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:
|