Yacine Jernite commited on
Commit
9c2c50e
·
1 Parent(s): 93b1033

can now edit policies

Browse files
Files changed (3) hide show
  1. ui/tab_config.py +1 -1
  2. ui/tab_policy.py +60 -44
  3. utils/constants.py +2 -3
ui/tab_config.py CHANGED
@@ -91,7 +91,7 @@ def build_config_tab() -> dict:
91
 
92
  gr.Markdown("---")
93
  with gr.Accordion("Generation Parameters", open=False):
94
- max_tokens = gr.Number(label="Max Tokens", value=4096, precision=0)
95
  temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.1, step=0.1)
96
  top_p = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
97
 
 
91
 
92
  gr.Markdown("---")
93
  with gr.Accordion("Generation Parameters", open=False):
94
+ max_tokens = gr.Number(label="Max Tokens", value=9192, precision=0)
95
  temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.1, step=0.1)
96
  top_p = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
97
 
ui/tab_policy.py CHANGED
@@ -13,58 +13,74 @@ from utils.helpers import load_policy_from_file, load_preset_policy
13
  def build_policy_tab(base_dir: str) -> dict:
14
  """Build the policy definition tab UI."""
15
  with gr.Tab("📋 Policy Definition"):
16
- input_method = gr.Radio(label="Input Method", choices=["Upload Markdown", "Enter Manually", "Select Preset"], value="Select Preset")
17
-
18
- upload_file = gr.File(label="Upload Markdown File", file_types=[".md"], visible=False)
19
- upload_preview = gr.Textbox(label="File Preview", lines=10, interactive=False, visible=False)
20
- load_upload_btn = gr.Button("Load Policy", visible=False)
21
-
22
- manual_text = gr.Textbox(label="Policy Text", placeholder="Enter policy markdown...", lines=20, visible=False)
23
- save_manual_btn = gr.Button("Save Policy", visible=False)
24
-
25
- preset_dropdown = gr.Dropdown(
26
- label="Select Preset", choices=["Hate Speech Policy", "Violence Policy", "Toxicity Policy"], value="Hate Speech Policy", visible=True
27
- )
28
- preset_preview = gr.Markdown(value="*Select a preset to preview*", visible=True)
29
- load_preset_btn = gr.Button("Load Preset", visible=True)
30
-
31
- gr.Markdown("---")
32
- gr.Markdown("### Current Policy")
33
- current_policy = gr.Markdown(value="*No policy loaded*")
34
- clear_policy_btn = gr.Button("Clear Policy", variant="secondary")
35
-
36
  current_policy_state = gr.State(value="")
37
-
38
- def update_ui(method):
39
- return (
40
- gr.update(visible=(method == "Upload Markdown")),
41
- gr.update(visible=(method == "Upload Markdown")),
42
- gr.update(visible=(method == "Upload Markdown")),
43
- gr.update(visible=(method == "Enter Manually")),
44
- gr.update(visible=(method == "Enter Manually")),
45
- gr.update(visible=(method == "Select Preset")),
46
- gr.update(visible=(method == "Select Preset")),
47
- gr.update(visible=(method == "Select Preset")),
 
 
 
 
 
 
 
 
 
 
 
48
  )
49
-
50
- input_method.change(update_ui, inputs=input_method, outputs=[upload_file, upload_preview, load_upload_btn, manual_text, save_manual_btn, preset_dropdown, preset_preview, load_preset_btn])
51
-
52
- # Policy loading handlers
 
 
 
 
 
 
 
 
53
  load_preset_btn.click(
54
- lambda name: load_preset_policy(name, base_dir),
55
  inputs=preset_dropdown,
56
- outputs=[current_policy_state, current_policy],
57
  )
58
- load_upload_btn.click(
59
- lambda f: load_policy_from_file(f.name) if f else ("", ""),
 
 
 
 
 
 
 
60
  inputs=upload_file,
61
- outputs=[current_policy_state, current_policy],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  )
63
- upload_file.change(lambda f: open(f.name).read() if f else "", inputs=upload_file, outputs=upload_preview)
64
- save_manual_btn.click(lambda t: (t, t), inputs=manual_text, outputs=[current_policy_state, current_policy])
65
- clear_policy_btn.click(lambda: ("", "*No policy loaded*"), outputs=[current_policy_state, current_policy])
66
 
67
  return {
68
  "current_policy_state": current_policy_state,
69
- "current_policy": current_policy,
70
  }
 
13
  def build_policy_tab(base_dir: str) -> dict:
14
  """Build the policy definition tab UI."""
15
  with gr.Tab("📋 Policy Definition"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  current_policy_state = gr.State(value="")
17
+
18
+ # Existing Policy Accordion
19
+ with gr.Accordion("📥 Load Existing Policy", open=False):
20
+ with gr.Row():
21
+ with gr.Column():
22
+ preset_dropdown = gr.Dropdown(
23
+ label="Select Preset",
24
+ choices=["Hate Speech Policy", "Violence Policy", "Toxicity Policy"],
25
+ value=None
26
+ )
27
+ load_preset_btn = gr.Button("Load Preset")
28
+
29
+ with gr.Column():
30
+ gr.Markdown("Upload a markdown file:")
31
+ upload_file = gr.File(label="Upload Markdown File", file_types=[".md"])
32
+
33
+ # Manual Edition Accordion
34
+ with gr.Accordion("✏️ Manual Edition", open=True):
35
+ manual_text = gr.Textbox(
36
+ label="Policy Text",
37
+ placeholder="Enter or edit policy markdown...",
38
+ lines=20
39
  )
40
+ policy_preview = gr.Markdown(value="*No policy loaded*")
41
+
42
+ # Clear button
43
+ clear_policy_btn = gr.Button("Clear Policy", variant="secondary")
44
+
45
+ # Handlers
46
+ def load_preset_handler(name):
47
+ if not name:
48
+ return "", "*No policy loaded*", ""
49
+ policy_text, _ = load_preset_policy(name, base_dir)
50
+ return policy_text, policy_text, policy_text
51
+
52
  load_preset_btn.click(
53
+ load_preset_handler,
54
  inputs=preset_dropdown,
55
+ outputs=[current_policy_state, manual_text, policy_preview],
56
  )
57
+
58
+ def load_upload_handler(f):
59
+ if f:
60
+ policy_text, _ = load_policy_from_file(f.name)
61
+ return policy_text, policy_text, policy_text
62
+ return "", "", "*No policy loaded*"
63
+
64
+ upload_file.change(
65
+ load_upload_handler,
66
  inputs=upload_file,
67
+ outputs=[current_policy_state, manual_text, policy_preview],
68
+ )
69
+
70
+ def update_preview(text):
71
+ return text if text else "*No policy loaded*"
72
+
73
+ manual_text.change(
74
+ lambda t: (t, update_preview(t)),
75
+ inputs=manual_text,
76
+ outputs=[current_policy_state, policy_preview],
77
+ )
78
+
79
+ clear_policy_btn.click(
80
+ lambda: ("", "", "*No policy loaded*"),
81
+ outputs=[current_policy_state, manual_text, policy_preview],
82
  )
 
 
 
83
 
84
  return {
85
  "current_policy_state": current_policy_state,
 
86
  }
utils/constants.py CHANGED
@@ -77,15 +77,14 @@ LM_PROMPT_INSTRUCT = """
77
  """
78
 
79
  # Response format for GPT-OSS
80
- RESPONSE_FORMAT = """
81
- ## Response Format
82
 
83
  Given a content item, please provide a JSON-formatted response with the following fields:
84
  - `label`: INT - 1 for a policy violation, 0 for no violation
85
  - `categories`: LIST[DCT] - a list of categories from the disallowed and allowed categories that the content item falls into along with the reasoning for each category. Each dict has the following keys:
86
  - `category`: STR - the category from the policy
87
  - `reasoning`: STR - the reasoning for why the content item falls into this category
88
- - `policy_source`: STR - the specific text from the policy that best supports the reasoning. If you need more than one, use [...] to concatenate them.
89
  """
90
 
91
  # Test examples from notebook
 
77
  """
78
 
79
  # Response format for GPT-OSS
80
+ RESPONSE_FORMAT = """## Response Format
 
81
 
82
  Given a content item, please provide a JSON-formatted response with the following fields:
83
  - `label`: INT - 1 for a policy violation, 0 for no violation
84
  - `categories`: LIST[DCT] - a list of categories from the disallowed and allowed categories that the content item falls into along with the reasoning for each category. Each dict has the following keys:
85
  - `category`: STR - the category from the policy
86
  - `reasoning`: STR - the reasoning for why the content item falls into this category
87
+ - `policy_source`: STR - specific text from the policy that best supports the reasoning. Use [...] to concatenate multi-part citations. Make sure to quote the policy text exactly and include all relevant passages.
88
  """
89
 
90
  # Test examples from notebook