RyanMullins commited on
Commit
f5e3203
1 Parent(s): 97aff03

Mock version of the gamified SynthID Text Space

Browse files
Files changed (2) hide show
  1. .gitignore +27 -0
  2. app.py +166 -0
.gitignore ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # Distribution / packaging
7
+ .Python
8
+ build/
9
+ develop-eggs/
10
+ dist/
11
+ downloads/
12
+ eggs/
13
+ .eggs/
14
+ lib/
15
+ lib64/
16
+ parts/
17
+ sdist/
18
+ var/
19
+ wheels/
20
+ share/python-wheels/
21
+ *.egg-info/
22
+ .installed.cfg
23
+ *.egg
24
+ MANIFEST
25
+
26
+ # Gradio certs, etc. that are added if sharing during development
27
+ .gradio/
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections.abc import Sequence
2
+ import random
3
+
4
+ import gradio as gr
5
+ import immutabledict
6
+ import spaces
7
+ import torch
8
+
9
+
10
+ #### Version 1: Baseline
11
+ # Step 1: Select and load your model
12
+ # Step 2: Load the test dataset (4-5 examples)
13
+ # Step 3: Run generation with and wihtout watermarking, display the outputs
14
+ # Step 4: User clicks the reveal button to see the watermarked vs not gens
15
+
16
+ #### Version 2: Gamification
17
+ # Stesp 1-3 the same
18
+ # Step 4: User marks specific generations as watermarked
19
+ # Step 5: User clicks the reveal button to see the watermarked vs not gens
20
+
21
+ # If the watewrmark is not detected, consider the use case. Could be because of
22
+ # the nature of the task (e.g., fatcual responses are lower entropy) or it could
23
+ # be another
24
+
25
+ GEMMA_2B = 'google/gemma-2b'
26
+
27
+ PROMPTS: tuple[str] = (
28
+ 'prompt 1',
29
+ 'prompt 2',
30
+ 'prompt 3',
31
+ 'prompt 4',
32
+ )
33
+
34
+ WATERMARKING_CONFIG = immutabledict.immutabledict({
35
+ "ngram_len": 5,
36
+ "keys": [
37
+ 654,
38
+ 400,
39
+ 836,
40
+ 123,
41
+ 340,
42
+ 443,
43
+ 597,
44
+ 160,
45
+ 57,
46
+ 29,
47
+ 590,
48
+ 639,
49
+ 13,
50
+ 715,
51
+ 468,
52
+ 990,
53
+ 966,
54
+ 226,
55
+ 324,
56
+ 585,
57
+ 118,
58
+ 504,
59
+ 421,
60
+ 521,
61
+ 129,
62
+ 669,
63
+ 732,
64
+ 225,
65
+ 90,
66
+ 960,
67
+ ],
68
+ "sampling_table_size": 2**16,
69
+ "sampling_table_seed": 0,
70
+ "context_history_size": 1024,
71
+ "device": (
72
+ torch.device("cuda:0")
73
+ if torch.cuda.is_available()
74
+ else torch.device("cpu")
75
+ ),
76
+ })
77
+
78
+ _CORRECT_ANSWERS: dict[str, bool] = {}
79
+
80
+ with gr.Blocks() as demo:
81
+ prompt_inputs = [
82
+ gr.Textbox(value=prompt, lines=4, label='Prompt')
83
+ for prompt in PROMPTS
84
+ ]
85
+ generate_btn = gr.Button('Generate')
86
+
87
+ with gr.Column(visible=False) as generations_col:
88
+ generations_grp = gr.CheckboxGroup(
89
+ label='All generations, in random order',
90
+ info='Select the generations you think are watermarked!',
91
+ )
92
+ reveal_btn = gr.Button('Reveal', visible=False)
93
+
94
+ with gr.Column(visible=False) as detections_col:
95
+ revealed_grp = gr.CheckboxGroup(
96
+ label='Ground truth for all generations',
97
+ info=(
98
+ 'Watermarked generations are checked, and your selection are '
99
+ 'marked as correct or incorrect in the text.'
100
+ ),
101
+ )
102
+ detect_btn = gr.Button('Detect', visible=False)
103
+
104
+ def generate(*prompts) -> Sequence[str]:
105
+ standard = [f'{prompt} response' for prompt in prompts]
106
+ watermarked = [f'{prompt} watermarked response' for prompt in prompts]
107
+ responses = standard + watermarked
108
+ random.shuffle(responses)
109
+
110
+ _CORRECT_ANSWERS.update({
111
+ response: response in watermarked
112
+ for response in responses
113
+ })
114
+
115
+ # Load model
116
+ return {
117
+ generate_btn: gr.Button(visible=False),
118
+ generations_col: gr.Column(visible=True),
119
+ generations_grp: gr.CheckboxGroup(
120
+ responses,
121
+ ),
122
+ reveal_btn: gr.Button(visible=True),
123
+ }
124
+
125
+ generate_btn.click(
126
+ generate,
127
+ inputs=prompt_inputs,
128
+ outputs=[generate_btn, generations_col, generations_grp, reveal_btn]
129
+ )
130
+
131
+ def reveal(user_selections: list[str]):
132
+ choices: list[str] = []
133
+ value: list[str] = []
134
+
135
+ for response, is_watermarked in _CORRECT_ANSWERS.items():
136
+ if is_watermarked and response in user_selections:
137
+ choice = f'Correct! {response}'
138
+ elif not is_watermarked and response not in user_selections:
139
+ choice = f'Correct! {response}'
140
+ else:
141
+ choice = f'Incorrect. {response}'
142
+
143
+ choices.append(choice)
144
+ if is_watermarked:
145
+ value.append(choice)
146
+
147
+ return {
148
+ reveal_btn: gr.Button(visible=False),
149
+ detections_col: gr.Column(visible=True),
150
+ revealed_grp: gr.CheckboxGroup(choices=choices, value=value),
151
+ detect_btn: gr.Button(visible=True),
152
+ }
153
+
154
+ reveal_btn.click(
155
+ reveal,
156
+ inputs=generations_grp,
157
+ outputs=[
158
+ reveal_btn,
159
+ detections_col,
160
+ revealed_grp,
161
+ detect_btn
162
+ ],
163
+ )
164
+
165
+ if __name__ == '__main__':
166
+ demo.launch()