Spaces:
Running
Running
Upload 2 files
Browse files
about.py
CHANGED
@@ -1,90 +1,90 @@
|
|
1 |
-
from dataclasses import dataclass
|
2 |
-
from enum import Enum
|
3 |
-
|
4 |
-
@dataclass
|
5 |
-
class Task:
|
6 |
-
benchmark: str
|
7 |
-
metric: str
|
8 |
-
col_name: str
|
9 |
-
|
10 |
-
|
11 |
-
# Select your tasks here
|
12 |
-
# ---------------------------------------------------
|
13 |
-
class Tasks(Enum):
|
14 |
-
# task_key in the json file, metric_key in the json file, name to display in the leaderboard
|
15 |
-
task0 = Task("anli_r1", "acc", "ANLI")
|
16 |
-
task1 = Task("logiqa", "acc_norm", "LogiQA")
|
17 |
-
|
18 |
-
NUM_FEWSHOT = 0 # Change with your few shot
|
19 |
-
# ---------------------------------------------------
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Your leaderboard name
|
25 |
-
TITLE = """<h1 align="center" id="space-title"> Demo of UnlearnDiffAtk</h1>"""
|
26 |
-
|
27 |
-
# subtitle
|
28 |
-
SUB_TITLE = """<h2 align="center" id="space-title">Effective and efficient adversarial prompt generation approach for diffusion models</h1>"""
|
29 |
-
|
30 |
-
# What does your leaderboard evaluate?
|
31 |
-
INTRODUCTION_TEXT = """
|
32 |
-
UnlearnDiffAtk is an effective and efficient adversarial prompt generation approach for unlearned diffusion models(DMs). For more details,
|
33 |
-
please refer to the [benchmark of UnlearnDiffAtk](https://huggingface.co/spaces/xinchen9/UnlearnDiffAtk-Benchmark), visit the [project](https://www.optml-group.com/posts/mu_attack),
|
34 |
-
check the [code](https://github.com/OPTML-Group/Diffusion-MU-Attack), and read the [paper](https://arxiv.org/abs/2310.11868).\\
|
35 |
-
The prompts were validated by us for undesirable concepts: ([Church](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/church.csv),
|
36 |
-
[Garbage Truck](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/garbage_truck.csv),
|
37 |
-
[Parachute](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/parachute.csv),
|
38 |
-
style ([Van Gogh](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/vangogh.csv)),
|
39 |
-
and objects ([Nudity](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/nudity.csv)).
|
40 |
-
|
41 |
-
"""
|
42 |
-
|
43 |
-
# Which evaluations are you running? how can people reproduce what you have?
|
44 |
-
LLM_BENCHMARKS_TEXT = f"""
|
45 |
-
## How it works
|
46 |
-
|
47 |
-
## Reproducibility
|
48 |
-
To reproduce our results, here is the commands you can run:
|
49 |
-
|
50 |
-
"""
|
51 |
-
|
52 |
-
EVALUATION_QUEUE_TEXT = """
|
53 |
-
## Some good practices before submitting a model
|
54 |
-
|
55 |
-
### 1) Make sure you can load your model and tokenizer using AutoClasses:
|
56 |
-
```python
|
57 |
-
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
58 |
-
config = AutoConfig.from_pretrained("your model name", revision=revision)
|
59 |
-
model = AutoModel.from_pretrained("your model name", revision=revision)
|
60 |
-
tokenizer = AutoTokenizer.from_pretrained("your model name", revision=revision)
|
61 |
-
```
|
62 |
-
If this step fails, follow the error messages to debug your model before submitting it. It's likely your model has been improperly uploaded.
|
63 |
-
|
64 |
-
Note: make sure your model is public!
|
65 |
-
Note: if your model needs `use_remote_code=True`, we do not support this option yet but we are working on adding it, stay posted!
|
66 |
-
|
67 |
-
### 2) Convert your model weights to [safetensors](https://huggingface.co/docs/safetensors/index)
|
68 |
-
It's a new format for storing weights which is safer and faster to load and use. It will also allow us to add the number of parameters of your model to the `Extended Viewer`!
|
69 |
-
|
70 |
-
### 3) Make sure your model has an open license!
|
71 |
-
This is a leaderboard for Open LLMs, and we'd love for as many people as possible to know they can use your model 🤗
|
72 |
-
|
73 |
-
### 4) Fill up your model card
|
74 |
-
When we add extra information about models to the leaderboard, it will be automatically taken from the model card
|
75 |
-
|
76 |
-
## In case of model failure
|
77 |
-
If your model is displayed in the `FAILED` category, its execution stopped.
|
78 |
-
Make sure you have followed the above steps first.
|
79 |
-
If everything is done, check you can launch the EleutherAIHarness on your model locally, using the above command without modifications (you can add `--limit` to limit the number of examples per task).
|
80 |
-
"""
|
81 |
-
|
82 |
-
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
83 |
-
CITATION_BUTTON_TEXT = r"""
|
84 |
-
@article{zhang2023generate,
|
85 |
-
title={To Generate or Not? Safety-Driven Unlearned Diffusion Models Are Still Easy To Generate Unsafe Images... For Now},
|
86 |
-
author={Zhang, Yimeng and Jia, Jinghan and Chen, Xin and Chen, Aochuan and Zhang, Yihua and Liu, Jiancheng and Ding, Ke and Liu, Sijia},
|
87 |
-
journal={arXiv preprint arXiv:2310.11868},
|
88 |
-
year={2023}
|
89 |
-
}
|
90 |
"""
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
from enum import Enum
|
3 |
+
|
4 |
+
@dataclass
|
5 |
+
class Task:
|
6 |
+
benchmark: str
|
7 |
+
metric: str
|
8 |
+
col_name: str
|
9 |
+
|
10 |
+
|
11 |
+
# Select your tasks here
|
12 |
+
# ---------------------------------------------------
|
13 |
+
class Tasks(Enum):
|
14 |
+
# task_key in the json file, metric_key in the json file, name to display in the leaderboard
|
15 |
+
task0 = Task("anli_r1", "acc", "ANLI")
|
16 |
+
task1 = Task("logiqa", "acc_norm", "LogiQA")
|
17 |
+
|
18 |
+
NUM_FEWSHOT = 0 # Change with your few shot
|
19 |
+
# ---------------------------------------------------
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
# Your leaderboard name
|
25 |
+
TITLE = """<h1 align="center" id="space-title"> Demo of UnlearnDiffAtk</h1>"""
|
26 |
+
|
27 |
+
# subtitle
|
28 |
+
SUB_TITLE = """<h2 align="center" id="space-title">Effective and efficient adversarial prompt generation approach for diffusion models</h1>"""
|
29 |
+
|
30 |
+
# What does your leaderboard evaluate?
|
31 |
+
INTRODUCTION_TEXT = """
|
32 |
+
UnlearnDiffAtk is an effective and efficient adversarial prompt generation approach for unlearned diffusion models(DMs). For more details,
|
33 |
+
please refer to the [benchmark of UnlearnDiffAtk](https://huggingface.co/spaces/xinchen9/UnlearnDiffAtk-Benchmark), visit the [project](https://www.optml-group.com/posts/mu_attack),
|
34 |
+
check the [code](https://github.com/OPTML-Group/Diffusion-MU-Attack), and read the [paper](https://arxiv.org/abs/2310.11868).\\
|
35 |
+
The prompts were validated by us for undesirable concepts: ([Church](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/church.csv),
|
36 |
+
[Garbage Truck](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/garbage_truck.csv),
|
37 |
+
[Parachute](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/parachute.csv),
|
38 |
+
style ([Van Gogh](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/vangogh.csv)),
|
39 |
+
and objects ([Nudity](https://github.com/OPTML-Group/Diffusion-MU-Attack/blob/e848ddd19df1f86d08e08cc9146f8a2bb126da12/prompts/nudity.csv)).
|
40 |
+
|
41 |
+
"""
|
42 |
+
|
43 |
+
# Which evaluations are you running? how can people reproduce what you have?
|
44 |
+
LLM_BENCHMARKS_TEXT = f"""
|
45 |
+
## How it works
|
46 |
+
|
47 |
+
## Reproducibility
|
48 |
+
To reproduce our results, here is the commands you can run:
|
49 |
+
|
50 |
+
"""
|
51 |
+
|
52 |
+
EVALUATION_QUEUE_TEXT = """
|
53 |
+
## Some good practices before submitting a model
|
54 |
+
|
55 |
+
### 1) Make sure you can load your model and tokenizer using AutoClasses:
|
56 |
+
```python
|
57 |
+
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
58 |
+
config = AutoConfig.from_pretrained("your model name", revision=revision)
|
59 |
+
model = AutoModel.from_pretrained("your model name", revision=revision)
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained("your model name", revision=revision)
|
61 |
+
```
|
62 |
+
If this step fails, follow the error messages to debug your model before submitting it. It's likely your model has been improperly uploaded.
|
63 |
+
|
64 |
+
Note: make sure your model is public!
|
65 |
+
Note: if your model needs `use_remote_code=True`, we do not support this option yet but we are working on adding it, stay posted!
|
66 |
+
|
67 |
+
### 2) Convert your model weights to [safetensors](https://huggingface.co/docs/safetensors/index)
|
68 |
+
It's a new format for storing weights which is safer and faster to load and use. It will also allow us to add the number of parameters of your model to the `Extended Viewer`!
|
69 |
+
|
70 |
+
### 3) Make sure your model has an open license!
|
71 |
+
This is a leaderboard for Open LLMs, and we'd love for as many people as possible to know they can use your model 🤗
|
72 |
+
|
73 |
+
### 4) Fill up your model card
|
74 |
+
When we add extra information about models to the leaderboard, it will be automatically taken from the model card
|
75 |
+
|
76 |
+
## In case of model failure
|
77 |
+
If your model is displayed in the `FAILED` category, its execution stopped.
|
78 |
+
Make sure you have followed the above steps first.
|
79 |
+
If everything is done, check you can launch the EleutherAIHarness on your model locally, using the above command without modifications (you can add `--limit` to limit the number of examples per task).
|
80 |
+
"""
|
81 |
+
|
82 |
+
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
83 |
+
CITATION_BUTTON_TEXT = r"""
|
84 |
+
@article{zhang2023generate,
|
85 |
+
title={To Generate or Not? Safety-Driven Unlearned Diffusion Models Are Still Easy To Generate Unsafe Images... For Now},
|
86 |
+
author={Zhang, Yimeng and Jia, Jinghan and Chen, Xin and Chen, Aochuan and Zhang, Yihua and Liu, Jiancheng and Ding, Ke and Liu, Sijia},
|
87 |
+
journal={arXiv preprint arXiv:2310.11868},
|
88 |
+
year={2023}
|
89 |
+
}
|
90 |
"""
|
app.py
CHANGED
@@ -1,127 +1,143 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
import os
|
4 |
-
import requests
|
5 |
-
import json
|
6 |
-
import base64
|
7 |
-
from io import BytesIO
|
8 |
-
from huggingface_hub import login
|
9 |
-
from PIL import Image
|
10 |
-
|
11 |
-
|
12 |
-
# myip = os.environ["0.0.0.0"]
|
13 |
-
# myport = os.environ["80"]
|
14 |
-
myip = "34.219.98.113"
|
15 |
-
myport=8000
|
16 |
-
|
17 |
-
is_spaces = True if "SPACE_ID" in os.environ else False
|
18 |
-
|
19 |
-
is_shared_ui = False
|
20 |
-
|
21 |
-
from css_html_js import custom_css
|
22 |
-
|
23 |
-
from about import (
|
24 |
-
CITATION_BUTTON_LABEL,
|
25 |
-
CITATION_BUTTON_TEXT,
|
26 |
-
EVALUATION_QUEUE_TEXT,
|
27 |
-
INTRODUCTION_TEXT,
|
28 |
-
LLM_BENCHMARKS_TEXT,
|
29 |
-
TITLE,
|
30 |
-
)
|
31 |
-
|
32 |
-
|
33 |
-
def process_image_from_binary(img_stream):
|
34 |
-
if img_stream is None:
|
35 |
-
print("no image binary")
|
36 |
-
return
|
37 |
-
image_data = base64.b64decode(img_stream)
|
38 |
-
image_bytes = BytesIO(image_data)
|
39 |
-
img = Image.open(image_bytes)
|
40 |
-
|
41 |
-
return img
|
42 |
-
|
43 |
-
def
|
44 |
-
print(f"my IP is {myip}, my port is {myport}")
|
45 |
-
print(f"my input is diffusion_model_id: {diffusion_model_id}, concept: {concept}, steps: {steps}")
|
46 |
-
response = requests.post('http://{}:{}/
|
47 |
-
json={"diffusion_model_id": diffusion_model_id, "concept": concept, "steps": steps, "attack_id": attack_id},
|
48 |
-
timeout=(10, 1200))
|
49 |
-
print(f"result: {response}")
|
50 |
-
# result = result.text[1:-1]
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
''
|
77 |
-
|
78 |
-
|
79 |
-
with
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
#
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
with gr.
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
demo.queue().launch(server_name='0.0.0.0')
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
+
from huggingface_hub import login
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
|
12 |
+
# myip = os.environ["0.0.0.0"]
|
13 |
+
# myport = os.environ["80"]
|
14 |
+
myip = "34.219.98.113"
|
15 |
+
myport=8000
|
16 |
+
|
17 |
+
is_spaces = True if "SPACE_ID" in os.environ else False
|
18 |
+
|
19 |
+
is_shared_ui = False
|
20 |
+
|
21 |
+
from css_html_js import custom_css
|
22 |
+
|
23 |
+
from about import (
|
24 |
+
CITATION_BUTTON_LABEL,
|
25 |
+
CITATION_BUTTON_TEXT,
|
26 |
+
EVALUATION_QUEUE_TEXT,
|
27 |
+
INTRODUCTION_TEXT,
|
28 |
+
LLM_BENCHMARKS_TEXT,
|
29 |
+
TITLE,
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
def process_image_from_binary(img_stream):
|
34 |
+
if img_stream is None:
|
35 |
+
print("no image binary")
|
36 |
+
return
|
37 |
+
image_data = base64.b64decode(img_stream)
|
38 |
+
image_bytes = BytesIO(image_data)
|
39 |
+
img = Image.open(image_bytes)
|
40 |
+
|
41 |
+
return img
|
42 |
+
|
43 |
+
def execute_prepare(diffusion_model_id, concept, steps, attack_id):
|
44 |
+
print(f"my IP is {myip}, my port is {myport}")
|
45 |
+
print(f"my input is diffusion_model_id: {diffusion_model_id}, concept: {concept}, steps: {steps}")
|
46 |
+
response = requests.post('http://{}:{}/prepare'.format(myip, myport),
|
47 |
+
json={"diffusion_model_id": diffusion_model_id, "concept": concept, "steps": steps, "attack_id": attack_id},
|
48 |
+
timeout=(10, 1200))
|
49 |
+
print(f"result: {response}")
|
50 |
+
# result = result.text[1:-1]
|
51 |
+
prompt = ""
|
52 |
+
img = None
|
53 |
+
if response.status_code == 200:
|
54 |
+
response_json = response.json()
|
55 |
+
print(response_json)
|
56 |
+
prompt = response_json['input_prompt']
|
57 |
+
img = process_image_from_binary(response_json['no_attack_img'])
|
58 |
+
else:
|
59 |
+
print(f"Request failed with status code {response.status_code}")
|
60 |
+
|
61 |
+
return prompt, img
|
62 |
+
|
63 |
+
def execute_udiff(diffusion_model_id, concept, steps, attack_id):
|
64 |
+
print(f"my IP is {myip}, my port is {myport}")
|
65 |
+
print(f"my input is diffusion_model_id: {diffusion_model_id}, concept: {concept}, steps: {steps}")
|
66 |
+
response = requests.post('http://{}:{}/udiff'.format(myip, myport),
|
67 |
+
json={"diffusion_model_id": diffusion_model_id, "concept": concept, "steps": steps, "attack_id": attack_id},
|
68 |
+
timeout=(10, 1200))
|
69 |
+
print(f"result: {response}")
|
70 |
+
# result = result.text[1:-1]
|
71 |
+
prompt = ""
|
72 |
+
img = None
|
73 |
+
if response.status_code == 200:
|
74 |
+
response_json = response.json()
|
75 |
+
print(response_json)
|
76 |
+
prompt = response_json['output_prompt']
|
77 |
+
img = process_image_from_binary(response_json['attack_img'])
|
78 |
+
else:
|
79 |
+
print(f"Request failed with status code {response.status_code}")
|
80 |
+
|
81 |
+
return prompt, img
|
82 |
+
|
83 |
+
|
84 |
+
css = '''
|
85 |
+
.instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
|
86 |
+
.arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
|
87 |
+
#component-4, #component-3, #component-10{min-height: 0}
|
88 |
+
.duplicate-button img{margin: 0}
|
89 |
+
#img_1, #img_2, #img_3, #img_4{height:15rem}
|
90 |
+
#mdStyle{font-size: 0.7rem}
|
91 |
+
#titleCenter {text-align:center}
|
92 |
+
'''
|
93 |
+
|
94 |
+
|
95 |
+
with gr.Blocks(css=custom_css) as demo:
|
96 |
+
gr.HTML(TITLE)
|
97 |
+
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
98 |
+
|
99 |
+
# gr.Markdown("# Demo of UnlearnDiffAtk.")
|
100 |
+
# gr.Markdown("### UnlearnDiffAtk is an effective and efficient adversarial prompt generation approach for unlearned diffusion models(DMs).")
|
101 |
+
# # gr.Markdown("####For more details, please visit the [project](https://www.optml-group.com/posts/mu_attack),
|
102 |
+
# # check the [code](https://github.com/OPTML-Group/Diffusion-MU-Attack), and read the [paper](https://arxiv.org/abs/2310.11868).")
|
103 |
+
# gr.Markdown("### Please notice that the process may take a long time, but the results will be saved. You can try it later if it waits for too long.")
|
104 |
+
|
105 |
+
|
106 |
+
with gr.Row() as udiff:
|
107 |
+
with gr.Row():
|
108 |
+
drop = gr.Dropdown(["Object-Church", "Object-Parachute", "Object-Garbage_Truck","Style-VanGogh",
|
109 |
+
"Nudity"],
|
110 |
+
label="Unlearning undesirable concepts")
|
111 |
+
with gr.Column():
|
112 |
+
# gr.Markdown("Please upload your model id.")
|
113 |
+
drop_model = gr.Dropdown(["ESD", "FMN", "SPM"],
|
114 |
+
label="Unlearned DMs")
|
115 |
+
# diffusion_model_T = gr.Textbox(label='diffusion_model_id')
|
116 |
+
# concept = gr.Textbox(label='concept')
|
117 |
+
# attacker = gr.Textbox(label='attacker')
|
118 |
+
|
119 |
+
# start_button = gr.Button("Attack!")
|
120 |
+
with gr.Column():
|
121 |
+
atk_idx = gr.Textbox(label="attack index")
|
122 |
+
|
123 |
+
with gr.Column():
|
124 |
+
shown_columns_step = gr.Slider(
|
125 |
+
0, 100, value=40,
|
126 |
+
step=1, label="Attack Steps", info="Choose between 0 and 100",
|
127 |
+
interactive=True,)
|
128 |
+
with gr.Row() as attack:
|
129 |
+
with gr.Column(min_width=512):
|
130 |
+
start_button = gr.Button("Attack prepare!",size='lg')
|
131 |
+
text_input = gr.Textbox(label="Input Prompt")
|
132 |
+
|
133 |
+
orig_img = gr.Image(label="Image Generated by Input Prompt",width=512,show_share_button=False,show_download_button=False)
|
134 |
+
with gr.Column():
|
135 |
+
attack_button = gr.Button("UnlearnDiffAtk!",size='lg')
|
136 |
+
text_ouput = gr.Textbox(label="Prompt Genetated by UnlearnDiffAtk")
|
137 |
+
result_img = gr.Image(label="Image Gnerated by Prompt of UnlearnDiffAtk",width=512,show_share_button=False,show_download_button=False)
|
138 |
+
|
139 |
+
start_button.click(fn=execute_prepare, inputs=[drop_model, drop, shown_columns_step, atk_idx], outputs=[text_input, orig_img], api_name="prepare")
|
140 |
+
attack_button.click(fn=execute_udiff, inputs=[drop_model, drop, shown_columns_step, atk_idx], outputs=[text_ouput, result_img], api_name="udiff")
|
141 |
+
|
142 |
+
|
143 |
demo.queue().launch(server_name='0.0.0.0')
|