hadisalman commited on
Commit
d2c10a6
·
1 Parent(s): 433d7c5

Convert to blocks + add advanced options

Browse files
Files changed (1) hide show
  1. app.py +58 -50
app.py CHANGED
@@ -80,7 +80,7 @@ def immunize_fn(init_image, mask_image):
80
  adv_image = recover_image(adv_image, init_image, mask_image, background=True)
81
  return adv_image
82
 
83
- def run(image, prompt, seed, immunize=False):
84
  if seed == '':
85
  seed = DEFAULT_SEED
86
  else:
@@ -101,8 +101,8 @@ def run(image, prompt, seed, immunize=False):
101
  height = init_image.size[0],
102
  width = init_image.size[1],
103
  eta=1,
104
- guidance_scale=GUIDANCE_SCALE,
105
- num_inference_steps=NUM_INFERENCE_STEPS,
106
  ).images[0]
107
 
108
  image_edited = recover_image(image_edited, init_image, mask_image)
@@ -113,50 +113,58 @@ def run(image, prompt, seed, immunize=False):
113
  return [(image_edited, 'Edited Image')]
114
 
115
 
116
- demo = gr.Interface(fn=run,
117
- inputs=[
118
- gr.ImageMask(label='Drawing tool to mask regions you want to keep, e.g. faces'),
119
- gr.Textbox(label='Prompt', placeholder='A photo of a man in a wedding'),
120
- gr.Textbox(label='Seed (Change to get different edits!)', placeholder=str(DEFAULT_SEED), visible=True),
121
- gr.Checkbox(label='Immunize', value=False),
122
- ],
123
- cache_examples=False,
124
- outputs=[gr.Gallery(
125
- label="Generated images",
126
- show_label=False,
127
- elem_id="gallery").style(grid=[1,2], height="auto")],
128
- examples=[
129
- ['./images/hadi_and_trevor.jpg', 'man attending a wedding', '329357'],
130
- ['./images/trevor_2.jpg', 'two men in prison', '329357'],
131
- ['./images/elon_2.jpg', 'man in a metro station', '214213'],
132
- ],
133
- examples_per_page=20,
134
- allow_flagging='never',
135
- title="Interactive Demo: Immunize your Photos Against AI-powered Malicious Manipulation",
136
- description='''<u>Official</u> demo of our paper: <br>
137
- **Raising the Cost of Malicious AI-Powered Image Editing** <br>
138
- *[Hadi Salman](https://twitter.com/hadisalmanX)\*, [Alaa Khaddaj](https://twitter.com/Alaa_Khaddaj)\*, [Guillaume Leclerc](https://twitter.com/gpoleclerc)\*, [Andrew Ilyas](https://twitter.com/andrew_ilyas), [Aleksander Madry](https://twitter.com/aleks_madry)* <br>
139
- MIT &nbsp;&nbsp;[Paper](https://arxiv.org/abs/2302.06588)
140
- &nbsp;&nbsp;[Blog post](https://gradientscience.org/photoguard/)
141
- &nbsp;&nbsp;[![](https://badgen.net/badge/icon/GitHub?icon=github&label)](https://github.com/MadryLab/photoguard)
142
- <br />
143
- Below you can test our (encoder attack) immunization method for making images resistant to manipulation by Stable Diffusion. This immunization process forces the model to perform unrealistic edits.
144
- <br />
145
- **This is a research project and is not production-ready. See Section 5 in our paper for discussion on its limitations.**
146
- <details closed>
147
- <summary>Click for demo steps:</summary>
148
-
149
- + Upload an image (or select from the below examples!)
150
- + Mask (using the drawing tool) the parts of the image you want to maintain unedited (e.g., faces of people)
151
- + Add a prompt to edit the image accordingly (see examples below)
152
- + Play with the seed and click submit until you get a realistic edit that you are happy with (or use default seeds below)
153
-
154
- Now let's immunize your image and try again!
155
- + Click on the "immunize" button, then submit.
156
- + You will get the immunized image (which looks identical to the original one) and the edited image, which is now hopefully unrealistic!
157
- </details>
158
- ''',
159
- )
160
-
161
- # demo.launch()
162
- demo.launch(server_name='0.0.0.0', share=False, server_port=7860, inline=False)
 
 
 
 
 
 
 
 
 
80
  adv_image = recover_image(adv_image, init_image, mask_image, background=True)
81
  return adv_image
82
 
83
+ def run(image, prompt, seed, guidance_scale, num_inference_steps, immunize=False):
84
  if seed == '':
85
  seed = DEFAULT_SEED
86
  else:
 
101
  height = init_image.size[0],
102
  width = init_image.size[1],
103
  eta=1,
104
+ guidance_scale=guidance_scale,
105
+ num_inference_steps=num_inference_steps,
106
  ).images[0]
107
 
108
  image_edited = recover_image(image_edited, init_image, mask_image)
 
113
  return [(image_edited, 'Edited Image')]
114
 
115
 
116
+ description='''<u>Official</u> demo of our paper: <br>
117
+ **Raising the Cost of Malicious AI-Powered Image Editing** <br>
118
+ *[Hadi Salman](https://twitter.com/hadisalmanX), [Alaa Khaddaj](https://twitter.com/Alaa_Khaddaj), [Guillaume Leclerc](https://twitter.com/gpoleclerc), [Andrew Ilyas](https://twitter.com/andrew_ilyas), [Aleksander Madry](https://twitter.com/aleks_madry)* <br>
119
+ MIT &nbsp;&nbsp;[Paper](https://arxiv.org/abs/2302.06588)
120
+ &nbsp;&nbsp;[Blog post](https://gradientscience.org/photoguard/)
121
+ &nbsp;&nbsp;[![](https://badgen.net/badge/icon/GitHub?icon=github&label)](https://github.com/MadryLab/photoguard)
122
+ <br />
123
+ Below you can test our (encoder attack) immunization method for making images resistant to manipulation by Stable Diffusion. This immunization process forces the model to perform unrealistic edits. See Section 5 in our paper for a discussion of the intended use cases for this primitive.
124
+ <br />
125
+ '''
126
+
127
+ examples_list = [
128
+ ['./images/hadi_and_trevor.jpg', 'man attending a wedding', '329357', 7.5, 100],
129
+ ['./images/trevor_2.jpg', 'two men in prison', '329357', 7.5, 100],
130
+ ['./images/elon_2.jpg', 'man in a metro station', '214213', 7.5, 100],
131
+ ]
132
+
133
+
134
+ with gr.Blocks() as demo:
135
+ gr.HTML(value="""<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
136
+ Interactive Demo: Immunize your Photos Against AI-Powered Malicious Manipulation </h1><br>
137
+ """)
138
+ gr.Markdown(description)
139
+ with gr.Accordion(label='Click for demo steps:', open=False):
140
+ gr.Markdown('''
141
+ + Upload an image (or select from the examples below)
142
+ + Use the brush to mask the parts of the image you want to keep unedited (e.g., faces of people)
143
+ + Add a prompt to guide the edit (see examples below)
144
+ + Play with the seed and click submit until you get a realistic edit that you are happy with (we provided good example seeds for you below)
145
+
146
+ *Now let's immunize your image and try again:*
147
+ + Click on the "Immunize" button, then submit.
148
+ + You will get an immunized version of the image (which should look essentially identical to the original one) as well as its edited version (which should now look rather unrealistic)
149
+ ''')
150
+
151
+ with gr.Row():
152
+ with gr.Column():
153
+ imgmask = gr.ImageMask(label='Drawing tool to mask regions you want to keep, e.g. faces')
154
+ prompt = gr.Textbox(label='Prompt', placeholder='A photo of a man in a wedding')
155
+ seed = gr.Textbox(label='Seed (Change to get different edits)', placeholder=str(DEFAULT_SEED), visible=True)
156
+ with gr.Accordion("Advanced Options", open=False):
157
+ scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=25.0, value=GUIDANCE_SCALE, step=0.1)
158
+ num_steps = gr.Slider(label="Number of Inference Steps", minimum=10, maximum=250, value=NUM_INFERENCE_STEPS, step=5)
159
+ immunize = gr.Checkbox(label='Immunize', value=False)
160
+ b1 = gr.Button('Submit')
161
+ with gr.Column():
162
+ genimages = gr.Gallery(label="Generated images",
163
+ show_label=False,
164
+ elem_id="gallery").style(grid=[1,2], height="auto")
165
+ b1.click(run, [imgmask, prompt, seed, scale, num_steps, immunize], [genimages])
166
+ examples = gr.Examples(examples=examples_list,inputs = [imgmask, prompt, seed, scale, num_steps, immunize], outputs=[genimages], cache_examples=False, fn=run)
167
+
168
+
169
+ demo.launch()
170
+ # demo.launch(server_name='0.0.0.0', share=False, server_port=7860, inline=False)