anonymousauthors commited on
Commit
ef88f1b
β€’
1 Parent(s): 1ff8db6

Update pages/2_😈_BlackBox_and_WhiteBox_Attacks.py

Browse files
pages/2_😈_BlackBox_and_WhiteBox_Attacks.py CHANGED
@@ -2,7 +2,8 @@ import streamlit as st
2
  from streamlit_extras.stateful_button import button
3
  import os
4
  import openai
5
- from transformers import GPT2Tokenizer, GPT2Model, AutoTokenizer, AutoModelForCausalLM
 
6
  import pickle
7
  import torch
8
  from copy import deepcopy
@@ -23,7 +24,7 @@ st.sidebar.markdown('`Input text`: a sentence or paragraph.')
23
  st.sidebar.markdown('`Number of replacements`: the number of secret language samples.')
24
  st.sidebar.markdown('`Steps for searching Secret Langauge`: the steps in the SecretFinding process.')
25
  st.sidebar.markdown('#### Two methods')
26
- st.sidebar.markdown('1. Searching secret languages based on models: this method calculates secret languages using [GPT-2](https://huggingface.co/gpt2), [EleutherAI/gpt-neo-1.3B](https://huggingface.co/EleutherAI/gpt-neo-1.3B)') #, [EleutherAI/gpt-neo-2.7B](https://huggingface.co/EleutherAI/gpt-neo-2.7B), [EleutherAI/gpt-neox-20b](https://huggingface.co/EleutherAI/gpt-neox-20b), or [EleutherAI/gpt-j-6B](https://huggingface.co/EleutherAI/gpt-j-6B).')
27
  st.sidebar.markdown('2. Use the secret language we found on ALBERT, DistillBERT, and Roberta: this method replaces words directly with the secret language dictionary derived from ALBERT, DistillBERT, and Roberta.')
28
 
29
  st.sidebar.markdown('#### Return')
@@ -38,6 +39,12 @@ st.sidebar.markdown(
38
  # title
39
  st.title('Blackbox Attack')
40
 
 
 
 
 
 
 
41
  # online search
42
  def run(model, tokenizer, embedidng_layer=None, _bar_text=None, bar=None, text='Which name is also used to describe the Amazon rainforest in English?',
43
  loss_funt=torch.nn.MSELoss(), lr=1, noise_mask=[1,2], restarts=10, step=100, device = torch.device('cpu'),
@@ -49,10 +56,10 @@ def run(model, tokenizer, embedidng_layer=None, _bar_text=None, bar=None, text='
49
  _input[k] = _input[k].to(device)
50
 
51
  ori_output = model(**_input)
52
- if 'last_hidden_state' in ori_output:
53
- ori_output = ori_output['last_hidden_state']
54
- else:
55
- ori_output = ori_output['logits']
56
 
57
  ori_embedding = embedidng_layer(_input['input_ids']).detach()
58
  ori_embedding.requires_grad = False
@@ -159,8 +166,8 @@ title = st.text_area('Input text.', 'Which name is also used to describe the Ama
159
  if option == 'Searching secret languages based on models':
160
  model_choice = st.selectbox(
161
  'Which model you would like to use?',
162
- # ('GPT-2', "EleutherAI/gpt-neo-1.3B", "EleutherAI/gpt-neo-2.7B", "EleutherAI/gpt-neox-20b", "EleutherAI/gpt-j-6B")
163
- ('GPT-2', "EleutherAI/gpt-neo-1.3B")
164
  )
165
  _cols = st.columns(2)
166
  restarts = _cols[0].number_input('Number of replacements.', value=10, min_value=1, step=1, format='%d')
@@ -170,12 +177,9 @@ else:
170
 
171
  if button('Tokenize', key='tokenizer'):
172
  if option == 'Searching secret languages based on models':
173
- if model_choice == 'GPT-2':
174
- tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
175
- else:
176
- tokenizer = AutoTokenizer.from_pretrained(model_choice)
177
  else:
178
- tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
179
  for key in st.session_state.keys():
180
  if key not in ['tokenizer', 'start'] and 'tokenizer_' not in key:
181
  del st.session_state[key]
@@ -211,19 +215,16 @@ if button('Tokenize', key='tokenizer'):
211
  chose_indices.append(_index)
212
  if len(chose_indices):
213
  if option == 'Searching secret languages based on models':
214
- if model_choice == 'GPT-2':
215
- model = GPT2Model.from_pretrained('gpt2')
216
- else:
217
- model = AutoModelForCausalLM.from_pretrained(model_choice)
218
  generator = pipeline('text-generation', model='gpt2')
219
  if not platform.system().lower() == 'darwin':
220
  generator1 = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B')
221
  with st.expander('**Original input text**: '+ title):
222
  st.markdown(f'The response of GPT-2 with the prompt :blue[{title}]')
223
- st.markdown('<blockquote>' + generator(title, max_length=30, num_return_sequences=1)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
224
  if not platform.system().lower() == 'darwin':
225
  st.markdown(f'The response of EleutherAI/gpt-neo-1.3B with the prompt :blue[{title}]')
226
- st.markdown('<blockquote>' + generator1(title, do_sample=True, min_length=50)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
227
 
228
  output_openai = get_codex_response(title)
229
  st.markdown(f'The response of [Codex](https://openai.com/blog/openai-codex/) with the prompt :blue[{title}]')
@@ -262,10 +263,10 @@ if button('Tokenize', key='tokenizer'):
262
  for i in range(restarts):
263
  with st.expander(outputs[i]):
264
  st.markdown(f'The response of GPT-2 with the prompt :blue[{outputs[i]}]')
265
- st.markdown('<blockquote>' + generator(outputs[i], max_length=30, num_return_sequences=1)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
266
  if not platform.system().lower() == 'darwin':
267
  st.markdown(f'The response of EleutherAI/gpt-neo-1.3B with the prompt :blue[{outputs[i]}]')
268
- st.markdown('<blockquote>' + generator1(outputs[i], do_sample=True, min_length=50)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
269
 
270
  output_openai = get_codex_response(outputs[i])
271
  st.markdown(f'The response of [Codex](https://openai.com/blog/openai-codex/) with the prompt :blue[{outputs[i]}]')
 
2
  from streamlit_extras.stateful_button import button
3
  import os
4
  import openai
5
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel, AutoTokenizer, AutoModelForCausalLM
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import pickle
8
  import torch
9
  from copy import deepcopy
 
24
  st.sidebar.markdown('`Number of replacements`: the number of secret language samples.')
25
  st.sidebar.markdown('`Steps for searching Secret Langauge`: the steps in the SecretFinding process.')
26
  st.sidebar.markdown('#### Two methods')
27
+ st.sidebar.markdown('1. Searching secret languages based on models: this method calculates secret languages using [GPT-2](https://huggingface.co/gpt2-medium), [EleutherAI/gpt-neo-1.3B](https://huggingface.co/EleutherAI/gpt-neo-1.3B)') #, [EleutherAI/gpt-neo-2.7B](https://huggingface.co/EleutherAI/gpt-neo-2.7B), [EleutherAI/gpt-neox-20b](https://huggingface.co/EleutherAI/gpt-neox-20b), or [EleutherAI/gpt-j-6B](https://huggingface.co/EleutherAI/gpt-j-6B).')
28
  st.sidebar.markdown('2. Use the secret language we found on ALBERT, DistillBERT, and Roberta: this method replaces words directly with the secret language dictionary derived from ALBERT, DistillBERT, and Roberta.')
29
 
30
  st.sidebar.markdown('#### Return')
 
39
  # title
40
  st.title('Blackbox Attack')
41
 
42
+ '''
43
+ They only use the last logit for text generation, so only using the last one would be fine.
44
+ https://github.com/huggingface/transformers/blob/ae54e3c3b18bac0832ad62ea9b896dfd52a09850/src/transformers/generation/utils.py#L2189
45
+ https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_utils.py#L2189
46
+ '''
47
+
48
  # online search
49
  def run(model, tokenizer, embedidng_layer=None, _bar_text=None, bar=None, text='Which name is also used to describe the Amazon rainforest in English?',
50
  loss_funt=torch.nn.MSELoss(), lr=1, noise_mask=[1,2], restarts=10, step=100, device = torch.device('cpu'),
 
56
  _input[k] = _input[k].to(device)
57
 
58
  ori_output = model(**_input)
59
+ # if 'last_hidden_state' in ori_output:
60
+ # ori_output = ori_output['last_hidden_state']
61
+ # else:
62
+ ori_output = ori_output['logits']
63
 
64
  ori_embedding = embedidng_layer(_input['input_ids']).detach()
65
  ori_embedding.requires_grad = False
 
166
  if option == 'Searching secret languages based on models':
167
  model_choice = st.selectbox(
168
  'Which model you would like to use?',
169
+ # ('gpt2-medium', "EleutherAI/gpt-neo-1.3B", "EleutherAI/gpt-neo-2.7B", "EleutherAI/gpt-neox-20b", "EleutherAI/gpt-j-6B")
170
+ ('gpt2-medium', "EleutherAI/gpt-neo-1.3B")
171
  )
172
  _cols = st.columns(2)
173
  restarts = _cols[0].number_input('Number of replacements.', value=10, min_value=1, step=1, format='%d')
 
177
 
178
  if button('Tokenize', key='tokenizer'):
179
  if option == 'Searching secret languages based on models':
180
+ tokenizer = AutoTokenizer.from_pretrained(model_choice)
 
 
 
181
  else:
182
+ tokenizer = AutoTokenizer.from_pretrained('gpt2-medium')
183
  for key in st.session_state.keys():
184
  if key not in ['tokenizer', 'start'] and 'tokenizer_' not in key:
185
  del st.session_state[key]
 
215
  chose_indices.append(_index)
216
  if len(chose_indices):
217
  if option == 'Searching secret languages based on models':
218
+ model = AutoModelForCausalLM.from_pretrained(model_choice)
 
 
 
219
  generator = pipeline('text-generation', model='gpt2')
220
  if not platform.system().lower() == 'darwin':
221
  generator1 = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B')
222
  with st.expander('**Original input text**: '+ title):
223
  st.markdown(f'The response of GPT-2 with the prompt :blue[{title}]')
224
+ st.markdown('<blockquote>' + generator(title, max_length=256, num_return_sequences=1)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
225
  if not platform.system().lower() == 'darwin':
226
  st.markdown(f'The response of EleutherAI/gpt-neo-1.3B with the prompt :blue[{title}]')
227
+ st.markdown('<blockquote>' + generator1(title, do_sample=True, max_length=256)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
228
 
229
  output_openai = get_codex_response(title)
230
  st.markdown(f'The response of [Codex](https://openai.com/blog/openai-codex/) with the prompt :blue[{title}]')
 
263
  for i in range(restarts):
264
  with st.expander(outputs[i]):
265
  st.markdown(f'The response of GPT-2 with the prompt :blue[{outputs[i]}]')
266
+ st.markdown('<blockquote>' + generator(outputs[i], max_length=256, num_return_sequences=1)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
267
  if not platform.system().lower() == 'darwin':
268
  st.markdown(f'The response of EleutherAI/gpt-neo-1.3B with the prompt :blue[{outputs[i]}]')
269
+ st.markdown('<blockquote>' + generator1(outputs[i], do_sample=True, max_length=256)[0]['generated_text'].replace(title, '', 1) + '</blockquote>', unsafe_allow_html=True)
270
 
271
  output_openai = get_codex_response(outputs[i])
272
  st.markdown(f'The response of [Codex](https://openai.com/blog/openai-codex/) with the prompt :blue[{outputs[i]}]')