Antoine Chaffin commited on
Commit
a09d0a4
1 Parent(s): 57c301e

Loading the model only once and adding LLama2 prompt

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -21,12 +21,16 @@ EMBED_METHODS = [ 'aaronson', 'kirchenbauer', 'sampling', 'greedy' ]
21
  DETECT_METHODS = [ 'aaronson', 'aaronson_simplified', 'aaronson_neyman_pearson', 'kirchenbauer']
22
  PAYLOAD_BITS = 2
23
 
 
 
 
 
 
 
24
  def embed(user, max_length, window_size, method, prompt):
25
  uid = USERS.index(user)
26
 
27
- watermarker = Watermarker(modelname=args.model,
28
- window_size=window_size, payload_bits=PAYLOAD_BITS)
29
-
30
  watermarked_texts = watermarker.embed(key=args.key, messages=[ uid ],
31
  max_length=max_length, method=method, prompt=prompt)
32
  print("watermarked_texts: ", watermarked_texts)
@@ -46,7 +50,11 @@ def detect(attacked_text, window_size, method, prompt):
46
 
47
  return label
48
 
49
-
 
 
 
 
50
 
51
  with gr.Blocks() as demo:
52
  gr.Markdown("""# LLM generation watermarking
@@ -71,8 +79,8 @@ with gr.Blocks() as demo:
71
  with gr.Row():
72
  detection_label = gr.Label(label="Detection result")
73
 
74
- btn1.click(fn=embed, inputs=[user, text_length, window_size, embed_method, prompt], outputs=[watermarked_text], api_name="watermark")
75
- btn2.click(fn=detect, inputs=[watermarked_text, window_size, detect_method, prompt], outputs=[detection_label], api_name="detect")
76
 
77
  demo.launch()
78
 
 
21
  DETECT_METHODS = [ 'aaronson', 'aaronson_simplified', 'aaronson_neyman_pearson', 'kirchenbauer']
22
  PAYLOAD_BITS = 2
23
 
24
+ watermarker = Watermarker(modelname=args.model, window_size=window_size, payload_bits=PAYLOAD_BITS)
25
+ DEFAULT_SYSTEM_PROMPT = """\
26
+ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
27
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\
28
+ """
29
+
30
  def embed(user, max_length, window_size, method, prompt):
31
  uid = USERS.index(user)
32
 
33
+
 
 
34
  watermarked_texts = watermarker.embed(key=args.key, messages=[ uid ],
35
  max_length=max_length, method=method, prompt=prompt)
36
  print("watermarked_texts: ", watermarked_texts)
 
50
 
51
  return label
52
 
53
+ def get_prompt(message: str) -> str:
54
+ texts = [f'<s>[INST] <<SYS>>\n{DEFAULT_SYSTEM_PROMPT}\n<</SYS>>\n\n']
55
+ # The first user input is _not_ stripped
56
+ texts.append(f'{message} [/INST]')
57
+ return ''.join(texts)
58
 
59
  with gr.Blocks() as demo:
60
  gr.Markdown("""# LLM generation watermarking
 
79
  with gr.Row():
80
  detection_label = gr.Label(label="Detection result")
81
 
82
+ btn1.click(fn=embed, inputs=[user, text_length, window_size, embed_method, get_prompt(prompt)], outputs=[watermarked_text], api_name="watermark")
83
+ btn2.click(fn=detect, inputs=[watermarked_text, window_size, detect_method, get_prompt(prompt)], outputs=[detection_label], api_name="detect")
84
 
85
  demo.launch()
86