Spaces:
Runtime error
Runtime error
Commit
·
d2c10a6
1
Parent(s):
433d7c5
Convert to blocks + add advanced options
Browse files
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=
|
| 105 |
-
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
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
['./images/
|
| 130 |
-
['./images/
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 [Paper](https://arxiv.org/abs/2302.06588)
|
| 120 |
+
[Blog post](https://gradientscience.org/photoguard/)
|
| 121 |
+
[](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)
|