Who fucking knows?
Browse files
app.py
CHANGED
|
@@ -1,124 +1,203 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from concept_steerer import ConceptSteerer
|
| 4 |
-
import threading
|
| 5 |
|
| 6 |
-
# Global
|
| 7 |
steerer = None
|
| 8 |
-
steerer_lock = threading.Lock()
|
| 9 |
|
| 10 |
-
def
|
| 11 |
global steerer
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
def create_concept(name, pos_examples, neg_examples):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if len(pos_list) == 0 or len(neg_list) == 0:
|
| 25 |
-
return "Please provide at least one example for both positive and negative prompts."
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
steerer
|
| 29 |
-
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"Error
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
return
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
with gr.Blocks(title="LLM Concept Steering
|
| 45 |
gr.Markdown("# 🧠 LLM Concept Steering Demo")
|
| 46 |
-
gr.Markdown("
|
| 47 |
-
|
| 48 |
with gr.Tab("Create Concepts"):
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
create_btn = gr.Button("Register Concept")
|
| 55 |
with gr.Column():
|
| 56 |
-
|
| 57 |
-
|
| 58 |
create_btn.click(
|
| 59 |
create_concept,
|
| 60 |
-
inputs=[
|
| 61 |
-
outputs=
|
| 62 |
)
|
| 63 |
-
|
| 64 |
with gr.Tab("Generate with Steering"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
with gr.Column():
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
return
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
inputs=[prompt_input],
|
| 107 |
-
outputs=output_text
|
| 108 |
-
).then(
|
| 109 |
-
None,
|
| 110 |
-
None,
|
| 111 |
-
None,
|
| 112 |
-
_js=f"""
|
| 113 |
-
() => {{
|
| 114 |
-
// This JS snippet dynamically collects all slider values from the UI
|
| 115 |
-
const sliders = document.querySelectorAll('input[type="range"]');
|
| 116 |
-
const args = [document.querySelector('#prompt_input input').value];
|
| 117 |
-
sliders.forEach(slider => args.push(parseFloat(slider.value)));
|
| 118 |
-
return args;
|
| 119 |
-
}}
|
| 120 |
-
"""
|
| 121 |
-
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
demo.launch(server_name="0.0.0.0", share=
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from concept_steerer import ConceptSteerer
|
|
|
|
| 4 |
|
| 5 |
+
# Global model instance (loaded once)
|
| 6 |
steerer = None
|
|
|
|
| 7 |
|
| 8 |
+
def get_steerer():
|
| 9 |
global steerer
|
| 10 |
+
if steerer is None:
|
| 11 |
+
steerer = ConceptSteerer(model_name="unsloth/Llama-3.2-1B-Instruct")
|
| 12 |
+
return steerer
|
| 13 |
|
| 14 |
def create_concept(name, pos_examples, neg_examples):
|
| 15 |
+
if not name.strip():
|
| 16 |
+
return "❌ Concept name cannot be empty."
|
| 17 |
+
pos_list = [p.strip() for p in pos_examples.strip().split('\n') if p.strip()]
|
| 18 |
+
neg_list = [n.strip() for n in neg_examples.strip().split('\n') if n.strip()]
|
| 19 |
+
if not pos_list or not neg_list:
|
| 20 |
+
return "❌ Please provide at least one example for both positive and negative prompts."
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
steerer = get_steerer()
|
| 24 |
+
steerer.register_concept(name.strip(), pos_list, neg_list, layer=-2)
|
| 25 |
+
return f"✅ Concept '{name.strip()}' registered successfully!"
|
| 26 |
except Exception as e:
|
| 27 |
+
return f"❌ Error: {str(e)}"
|
| 28 |
|
| 29 |
+
def update_ui():
|
| 30 |
+
"""Returns updated UI components based on current concepts."""
|
| 31 |
+
steerer = get_steerer()
|
| 32 |
+
concepts = steerer.get_concept_names()
|
| 33 |
+
|
| 34 |
+
if not concepts:
|
| 35 |
+
return (
|
| 36 |
+
gr.Markdown("💡 No concepts yet. Create one in the 'Create Concepts' tab!"),
|
| 37 |
+
gr.Row(visible=False),
|
| 38 |
+
gr.Button(visible=True)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Build sliders dynamically
|
| 42 |
+
sliders = []
|
| 43 |
+
with gr.Row():
|
| 44 |
+
for concept in concepts:
|
| 45 |
+
slider = gr.Slider(-5.0, 5.0, value=0.0, label=concept, step=0.1)
|
| 46 |
+
sliders.append(slider)
|
| 47 |
+
|
| 48 |
+
return (
|
| 49 |
+
gr.Markdown(visible=False), # hide placeholder
|
| 50 |
+
gr.Row(*sliders, visible=True),
|
| 51 |
+
gr.Button(visible=True)
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def generate_with_steering(prompt, *slider_values):
|
| 55 |
+
steerer = get_steerer()
|
| 56 |
+
concepts = steerer.get_concept_names()
|
| 57 |
+
if not concepts:
|
| 58 |
+
# No steering: just generate normally
|
| 59 |
+
return steerer.generate(prompt, max_new_tokens=150)
|
| 60 |
+
|
| 61 |
+
steering_config = {name: float(val) for name, val in zip(concepts, slider_values)}
|
| 62 |
try:
|
| 63 |
+
output = steerer.generate(prompt, steering_config=steering_config, max_new_tokens=150)
|
| 64 |
+
return output
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"❌ Generation error: {str(e)}"
|
| 67 |
|
| 68 |
+
# Build the interface
|
| 69 |
+
with gr.Blocks(title="🧠 LLM Concept Steering") as demo:
|
| 70 |
gr.Markdown("# 🧠 LLM Concept Steering Demo")
|
| 71 |
+
gr.Markdown("Steer Llama 3.2 1B's behavior using activation vectors — no training needed!")
|
| 72 |
+
|
| 73 |
with gr.Tab("Create Concepts"):
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column():
|
| 76 |
+
name_input = gr.Textbox(label="Concept Name", placeholder="e.g., Formality")
|
| 77 |
+
pos_input = gr.Textbox(label="Positive Prompts (one per line)", lines=4)
|
| 78 |
+
neg_input = gr.Textbox(label="Negative Prompts (one per line)", lines=4)
|
| 79 |
create_btn = gr.Button("Register Concept")
|
| 80 |
with gr.Column():
|
| 81 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 82 |
create_btn.click(
|
| 83 |
create_concept,
|
| 84 |
+
inputs=[name_input, pos_input, neg_input],
|
| 85 |
+
outputs=status
|
| 86 |
)
|
| 87 |
+
|
| 88 |
with gr.Tab("Generate with Steering"):
|
| 89 |
+
prompt_box = gr.Textbox(label="Enter your prompt", lines=3, placeholder="e.g., Write a short story about a robot.")
|
| 90 |
+
gen_btn = gr.Button("Generate")
|
| 91 |
+
|
| 92 |
+
# Placeholder for dynamic sliders
|
| 93 |
+
placeholder_msg = gr.Markdown("Loading concepts...")
|
| 94 |
+
slider_row = gr.Row(visible=False)
|
| 95 |
+
output_box = gr.Textbox(label="Output", lines=8, interactive=False)
|
| 96 |
+
|
| 97 |
+
# Update sliders when tab is loaded or after concept creation
|
| 98 |
+
demo.load(update_ui, None, [placeholder_msg, slider_row, gen_btn])
|
| 99 |
+
|
| 100 |
+
# Connect generate button — we pass all children of slider_row as inputs
|
| 101 |
+
# But since we can't reference them before creation, we use a trick:
|
| 102 |
+
# We'll re-render the generate button's dependency after UI update.
|
| 103 |
+
# Simpler: just call generate_with_steering with all possible inputs.
|
| 104 |
+
# Gradio handles missing inputs gracefully if we set default None.
|
| 105 |
+
|
| 106 |
+
# Instead, we use a wrapper that fetches current slider values via State (not needed here)
|
| 107 |
+
# Better: restructure to use a single State for config — but for simplicity, we do this:
|
| 108 |
+
|
| 109 |
+
# We'll make the generate button depend on all possible sliders by rebuilding the UI
|
| 110 |
+
# But an easier fix: use a single "Generate" that reads from a hidden state.
|
| 111 |
+
# However, the cleanest modern way is to use **gr.on()** or just accept that we need to
|
| 112 |
+
# capture the slider row's children.
|
| 113 |
+
|
| 114 |
+
# WORKING SOLUTION: Use a lambda that captures the current UI
|
| 115 |
+
def make_generate_fn():
|
| 116 |
+
# This will be called after sliders exist
|
| 117 |
+
children = slider_row.children
|
| 118 |
+
if not children:
|
| 119 |
+
inputs = [prompt_box]
|
| 120 |
+
else:
|
| 121 |
+
inputs = [prompt_box] + children
|
| 122 |
+
gen_btn.click(
|
| 123 |
+
generate_with_steering,
|
| 124 |
+
inputs=inputs,
|
| 125 |
+
outputs=output_box
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Unfortunately, we can't easily re-bind click after load in pure Python.
|
| 129 |
+
# So here's a robust alternative: use a single button and read concepts at generate time.
|
| 130 |
+
|
| 131 |
+
# REVISED PLAN: Don't use dynamic inputs to .click(). Instead, store slider values in State.
|
| 132 |
+
# But to keep it simple and working NOW, let's use a different approach:
|
| 133 |
+
|
| 134 |
+
# ✅ FINAL WORKING FIX: Use a single "steering_dict" stored in a Gradio State.
|
| 135 |
+
# We'll add invisible number inputs that update a dict via change handlers.
|
| 136 |
+
|
| 137 |
+
pass # We'll refactor below
|
| 138 |
+
|
| 139 |
+
# ---- REFACTORED WORKING VERSION BELOW ----
|
| 140 |
+
|
| 141 |
+
steering_state = gr.State({})
|
| 142 |
+
|
| 143 |
+
def update_steering_state(name, value, state):
|
| 144 |
+
state = state or {}
|
| 145 |
+
state[name] = float(value)
|
| 146 |
+
return state
|
| 147 |
+
|
| 148 |
+
def generate_from_state(prompt, state):
|
| 149 |
+
steerer = get_steerer()
|
| 150 |
+
try:
|
| 151 |
+
output = steerer.generate(prompt, steering_config=state or {}, max_new_tokens=150)
|
| 152 |
+
return output
|
| 153 |
+
except Exception as e:
|
| 154 |
+
return f"❌ Error: {str(e)}"
|
| 155 |
+
|
| 156 |
+
with gr.Blocks() as demo2:
|
| 157 |
+
gr.Markdown("# 🧠 LLM Concept Steering (Fixed!)")
|
| 158 |
+
gr.Markdown("Now fully compatible with modern Gradio.")
|
| 159 |
+
|
| 160 |
+
with gr.Tab("Create Concepts"):
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column():
|
| 163 |
+
name_in = gr.Textbox(label="Concept Name")
|
| 164 |
+
pos_in = gr.Textbox(label="Positive Prompts", lines=3)
|
| 165 |
+
neg_in = gr.Textbox(label="Negative Prompts", lines=3)
|
| 166 |
+
create_btn2 = gr.Button("Register")
|
|
|
|
| 167 |
with gr.Column():
|
| 168 |
+
status2 = gr.Textbox(label="Result")
|
| 169 |
+
create_btn2.click(create_concept, [name_in, pos_in, neg_in], status2)
|
| 170 |
+
|
| 171 |
+
with gr.Tab("Generate"):
|
| 172 |
+
prompt_in = gr.Textbox(label="Prompt", lines=2)
|
| 173 |
+
state_holder = gr.State({})
|
| 174 |
+
output_out = gr.Textbox(label="Output", lines=8)
|
| 175 |
+
|
| 176 |
+
# Container for sliders
|
| 177 |
+
slider_box = gr.Box()
|
| 178 |
+
|
| 179 |
+
def build_sliders():
|
| 180 |
+
steerer = get_steerer()
|
| 181 |
+
concepts = steerer.get_concept_names()
|
| 182 |
+
if not concepts:
|
| 183 |
+
return [gr.Markdown("No concepts. Create one first!")]
|
| 184 |
+
components = []
|
| 185 |
+
for c in concepts:
|
| 186 |
+
num = gr.Number(value=0.0, label=c, minimum=-5, maximum=5, step=0.1)
|
| 187 |
+
num.change(
|
| 188 |
+
lambda v, name=c, s=state_holder: update_steering_state(name, v, s),
|
| 189 |
+
num,
|
| 190 |
+
state_holder
|
| 191 |
+
)
|
| 192 |
+
components.append(num)
|
| 193 |
+
return components
|
| 194 |
+
|
| 195 |
+
demo2.load(build_sliders, None, slider_box)
|
| 196 |
+
|
| 197 |
+
gen_btn2 = gr.Button("Generate")
|
| 198 |
+
gen_btn2.click(generate_from_state, [prompt_in, state_holder], output_out)
|
| 199 |
+
|
| 200 |
+
demo = demo2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
+
demo.launch(server_name="0.0.0.0", share=False)
|