Spaces:
Running
on
Zero
Running
on
Zero
frankaging
commited on
Commit
·
884bfb5
1
Parent(s):
4a08f06
o1 impl
Browse files- app.py +6 -21
- requirements.txt +1 -1
app.py
CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
|
|
4 |
import spaces
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
from huggingface_hub import login, hf_hub_download
|
|
|
7 |
import pyvene as pv
|
8 |
from threading import Thread
|
9 |
from typing import Iterator
|
@@ -66,7 +67,7 @@ if torch.cuda.is_available():
|
|
66 |
steer = Steer(embed_dim=params.shape[0], latent_dim=params.shape[1])
|
67 |
steer.proj.weight.data = params.float()
|
68 |
|
69 |
-
pv_model =
|
70 |
{
|
71 |
"component": f"model.layers[20].output",
|
72 |
"intervention": steer,
|
@@ -80,8 +81,8 @@ terminators = [tokenizer.eos_token_id] if tokenizer else []
|
|
80 |
def generate(
|
81 |
message: str,
|
82 |
chat_history: list[tuple[str, str]],
|
83 |
-
max_new_tokens: int,
|
84 |
subspaces_list: list[dict],
|
|
|
85 |
) -> Iterator[str]:
|
86 |
# limit to last 3 turns
|
87 |
start_idx = max(0, len(chat_history) - 3)
|
@@ -211,13 +212,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
211 |
value=3
|
212 |
)
|
213 |
add_button = gr.Button("Add Concept")
|
214 |
-
|
215 |
-
headers=["Concept", "Mag (scaled)"],
|
216 |
-
datatype=["str", "number"],
|
217 |
-
value=_build_table_data(default_subspaces),
|
218 |
-
interactive=False,
|
219 |
-
label="Active Concept Subspaces",
|
220 |
-
)
|
221 |
# Row with the remove dropdown + button
|
222 |
with gr.Row():
|
223 |
remove_dropdown = gr.Dropdown(
|
@@ -227,27 +222,17 @@ with gr.Blocks(css="style.css") as demo:
|
|
227 |
)
|
228 |
remove_button = gr.Button("Remove", variant="secondary")
|
229 |
|
230 |
-
# Place the max tokens slider at bottom, smaller
|
231 |
-
with gr.Row():
|
232 |
-
gr.Markdown("**Max New Tokens**", elem_classes=["small-label"])
|
233 |
-
max_token_slider = gr.Slider(
|
234 |
-
minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1,
|
235 |
-
value=DEFAULT_MAX_NEW_TOKENS,
|
236 |
-
label="", # hide the big label
|
237 |
-
container=False,
|
238 |
-
)
|
239 |
-
|
240 |
# Wire up events
|
241 |
search_box.change(update_dropdown_choices, [search_box], [concept_dropdown])
|
242 |
add_button.click(
|
243 |
add_concept_to_list,
|
244 |
[concept_dropdown, concept_magnitude, selected_subspaces],
|
245 |
-
[selected_subspaces,
|
246 |
)
|
247 |
remove_button.click(
|
248 |
remove_concept_from_list,
|
249 |
[remove_dropdown, selected_subspaces],
|
250 |
-
[selected_subspaces,
|
251 |
)
|
252 |
|
253 |
demo.launch()
|
|
|
4 |
import spaces
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
from huggingface_hub import login, hf_hub_download
|
7 |
+
import pyreft
|
8 |
import pyvene as pv
|
9 |
from threading import Thread
|
10 |
from typing import Iterator
|
|
|
67 |
steer = Steer(embed_dim=params.shape[0], latent_dim=params.shape[1])
|
68 |
steer.proj.weight.data = params.float()
|
69 |
|
70 |
+
pv_model = pyreft.IntervenableModel(
|
71 |
{
|
72 |
"component": f"model.layers[20].output",
|
73 |
"intervention": steer,
|
|
|
81 |
def generate(
|
82 |
message: str,
|
83 |
chat_history: list[tuple[str, str]],
|
|
|
84 |
subspaces_list: list[dict],
|
85 |
+
max_new_tokens: int=DEFAULT_MAX_NEW_TOKENS,
|
86 |
) -> Iterator[str]:
|
87 |
# limit to last 3 turns
|
88 |
start_idx = max(0, len(chat_history) - 3)
|
|
|
212 |
value=3
|
213 |
)
|
214 |
add_button = gr.Button("Add Concept")
|
215 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
# Row with the remove dropdown + button
|
217 |
with gr.Row():
|
218 |
remove_dropdown = gr.Dropdown(
|
|
|
222 |
)
|
223 |
remove_button = gr.Button("Remove", variant="secondary")
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
# Wire up events
|
226 |
search_box.change(update_dropdown_choices, [search_box], [concept_dropdown])
|
227 |
add_button.click(
|
228 |
add_concept_to_list,
|
229 |
[concept_dropdown, concept_magnitude, selected_subspaces],
|
230 |
+
[selected_subspaces, remove_dropdown]
|
231 |
)
|
232 |
remove_button.click(
|
233 |
remove_concept_from_list,
|
234 |
[remove_dropdown, selected_subspaces],
|
235 |
+
[selected_subspaces, remove_dropdown]
|
236 |
)
|
237 |
|
238 |
demo.launch()
|
requirements.txt
CHANGED
@@ -3,4 +3,4 @@ gradio==4.26.0
|
|
3 |
spaces==0.26.0
|
4 |
# TODO: pyreft main
|
5 |
huggingface-hub>=0.24.0
|
6 |
-
|
|
|
3 |
spaces==0.26.0
|
4 |
# TODO: pyreft main
|
5 |
huggingface-hub>=0.24.0
|
6 |
+
pyreft @ git+https://github.com/stanfordnlp/pyreft.git
|