Spaces:
Runtime error
Runtime error
columns and printing fixed
Browse files- app.py +8 -3
- speaking_probes/generate.py +10 -9
app.py
CHANGED
@@ -15,15 +15,20 @@ def load_model(model_name):
|
|
15 |
model_name = st.selectbox("Select a model: ", options=['gpt2', 'gpt2-medium', 'gpt2-large'])
|
16 |
model, model_params, tokenizer = load_model(model_name)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
neurons = model_params.K_heads[int(neuron_layer), int(neuron_dim)]
|
21 |
prompt = st.text_area("Prompt: ")
|
22 |
submitted = st.button("Send!")
|
23 |
|
24 |
|
25 |
if submitted:
|
26 |
-
speaking_probe(model, model_params, tokenizer, prompt, *neurons, num_generations=1,
|
27 |
repetition_penalty=2.,
|
28 |
num_beams=3, min_length=1, do_sample=True,
|
29 |
max_new_tokens=100)
|
|
|
|
|
|
|
|
15 |
model_name = st.selectbox("Select a model: ", options=['gpt2', 'gpt2-medium', 'gpt2-large'])
|
16 |
model, model_params, tokenizer = load_model(model_name)
|
17 |
|
18 |
+
col1, col2, *_ = st.columns(5)
|
19 |
+
neuron_layer = col1.text_input("Layer: ", value='0')
|
20 |
+
neuron_dim = col2.text_input("Dim: ", value='0')
|
21 |
+
|
22 |
neurons = model_params.K_heads[int(neuron_layer), int(neuron_dim)]
|
23 |
prompt = st.text_area("Prompt: ")
|
24 |
submitted = st.button("Send!")
|
25 |
|
26 |
|
27 |
if submitted:
|
28 |
+
decoded = speaking_probe(model, model_params, tokenizer, prompt, *neurons, num_generations=1,
|
29 |
repetition_penalty=2.,
|
30 |
num_beams=3, min_length=1, do_sample=True,
|
31 |
max_new_tokens=100)
|
32 |
+
|
33 |
+
for text in decoded:
|
34 |
+
st.code(text, language=None)
|
speaking_probes/generate.py
CHANGED
@@ -151,7 +151,6 @@ def speaking_probe(model, model_params, tokenizer, prompt, *neurons,
|
|
151 |
if len(bad_words_ids) == 0:
|
152 |
bad_words_ids = None
|
153 |
|
154 |
-
print(prompt)
|
155 |
input_ids = tokenizer_with_neurons.encode(prompt, return_tensors='pt').to(model.device)
|
156 |
input_ids = torch.cat([deepcopy(input_ids) for _ in range(num_generations)], dim=0)
|
157 |
outputs = model.generate(input_ids, pad_token_id=model.config.eos_token_id,
|
@@ -161,18 +160,17 @@ def speaking_probe(model, model_params, tokenizer, prompt, *neurons,
|
|
161 |
**kwargs)
|
162 |
|
163 |
decoded = tokenizer_with_neurons.batch_decode(outputs.sequences, skip_special_tokens=True)
|
164 |
-
|
165 |
-
for i in range(len(decoded)):
|
166 |
-
print("\n\ngenerate:", decoded[i])
|
167 |
-
|
168 |
if has_extra_neurons:
|
169 |
model.resize_token_embeddings(num_non_neuron_tokens)
|
170 |
model.transformer.wte.weight.data = model.transformer.wte.weight.data[:num_non_neuron_tokens]
|
171 |
|
172 |
if return_outputs:
|
173 |
-
return outputs
|
|
|
|
|
|
|
174 |
|
175 |
-
|
176 |
# main
|
177 |
if __name__ == "__main__":
|
178 |
parser = ArgumentParser()
|
@@ -209,10 +207,13 @@ if __name__ == "__main__":
|
|
209 |
i1, i2 = map(lambda x: int(x.strip()), args.neuron.split(','))
|
210 |
neuron = model_params.K_heads[i1, i2]
|
211 |
neurons = [neuron]
|
212 |
-
|
213 |
-
|
|
|
214 |
num_generations=args.num_generations,
|
215 |
repetition_penalty=args.repetition_penalty,
|
216 |
num_beams=args.num_beams, top_p=args.top_p, top_k=args.top_k,
|
217 |
min_length=args.min_length, do_sample=not args.no_sample,
|
218 |
max_length=args.max_length, max_new_tokens=args.max_new_tokens)
|
|
|
|
|
|
151 |
if len(bad_words_ids) == 0:
|
152 |
bad_words_ids = None
|
153 |
|
|
|
154 |
input_ids = tokenizer_with_neurons.encode(prompt, return_tensors='pt').to(model.device)
|
155 |
input_ids = torch.cat([deepcopy(input_ids) for _ in range(num_generations)], dim=0)
|
156 |
outputs = model.generate(input_ids, pad_token_id=model.config.eos_token_id,
|
|
|
160 |
**kwargs)
|
161 |
|
162 |
decoded = tokenizer_with_neurons.batch_decode(outputs.sequences, skip_special_tokens=True)
|
163 |
+
|
|
|
|
|
|
|
164 |
if has_extra_neurons:
|
165 |
model.resize_token_embeddings(num_non_neuron_tokens)
|
166 |
model.transformer.wte.weight.data = model.transformer.wte.weight.data[:num_non_neuron_tokens]
|
167 |
|
168 |
if return_outputs:
|
169 |
+
return decoded, outputs
|
170 |
+
else:
|
171 |
+
return decoded
|
172 |
+
|
173 |
|
|
|
174 |
# main
|
175 |
if __name__ == "__main__":
|
176 |
parser = ArgumentParser()
|
|
|
207 |
i1, i2 = map(lambda x: int(x.strip()), args.neuron.split(','))
|
208 |
neuron = model_params.K_heads[i1, i2]
|
209 |
neurons = [neuron]
|
210 |
+
|
211 |
+
print(prompt)
|
212 |
+
decoded = speaking_probe(model, model_params, tokenizer, prompt, *neurons,
|
213 |
num_generations=args.num_generations,
|
214 |
repetition_penalty=args.repetition_penalty,
|
215 |
num_beams=args.num_beams, top_p=args.top_p, top_k=args.top_k,
|
216 |
min_length=args.min_length, do_sample=not args.no_sample,
|
217 |
max_length=args.max_length, max_new_tokens=args.max_new_tokens)
|
218 |
+
for i in range(len(decoded)):
|
219 |
+
print("\n\ngenerate:", decoded[i])
|