xinchen9 commited on
Commit
6882aba
1 Parent(s): 7ccf651

Upload 2 files

Browse files
Files changed (2) hide show
  1. about.py +89 -89
  2. app.py +142 -126
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 excute_udiff(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://{}:{}/udiff'.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
- prompt1 = ""
52
- prompt2 = ""
53
- img1 = None
54
- img2 = None
55
- if response.status_code == 200:
56
- response_json = response.json()
57
- print(response_json)
58
- prompt1 = response_json['input_prompt']
59
- prompt2 = response_json['output_prompt']
60
- img1 = process_image_from_binary(response_json['no_attack_img'])
61
- img2 = process_image_from_binary(response_json['attack_img'])
62
- else:
63
- print(f"Request failed with status code {response.status_code}")
64
-
65
- return prompt1, prompt2, img1, img2
66
-
67
-
68
- css = '''
69
- .instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
70
- .arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
71
- #component-4, #component-3, #component-10{min-height: 0}
72
- .duplicate-button img{margin: 0}
73
- #img_1, #img_2, #img_3, #img_4{height:15rem}
74
- #mdStyle{font-size: 0.7rem}
75
- #titleCenter {text-align:center}
76
- '''
77
-
78
-
79
- with gr.Blocks(css=custom_css) as demo:
80
- gr.HTML(TITLE)
81
- gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
82
-
83
- # gr.Markdown("# Demo of UnlearnDiffAtk.")
84
- # gr.Markdown("### UnlearnDiffAtk is an effective and efficient adversarial prompt generation approach for unlearned diffusion models(DMs).")
85
- # # gr.Markdown("####For more details, please visit the [project](https://www.optml-group.com/posts/mu_attack),
86
- # # check the [code](https://github.com/OPTML-Group/Diffusion-MU-Attack), and read the [paper](https://arxiv.org/abs/2310.11868).")
87
- # 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.")
88
-
89
-
90
- with gr.Row() as udiff:
91
- with gr.Row():
92
- drop = gr.Dropdown(["Object-Church", "Object-Parachute", "Object-Garbage_Truck","Style-VanGogh",
93
- "Nudity"],
94
- label="Unlearning undesirable concepts")
95
- with gr.Column():
96
- # gr.Markdown("Please upload your model id.")
97
- drop_model = gr.Dropdown(["ESD", "FMN", "SPM"],
98
- label="Unlearned DMs")
99
- # diffusion_model_T = gr.Textbox(label='diffusion_model_id')
100
- # concept = gr.Textbox(label='concept')
101
- # attacker = gr.Textbox(label='attacker')
102
-
103
- # start_button = gr.Button("Attack!")
104
- with gr.Column():
105
- atk_idx = gr.Textbox(label="attack index")
106
-
107
- with gr.Column():
108
- shown_columns_step = gr.Slider(
109
- 0, 100, value=40,
110
- step=1, label="Attack Steps", info="Choose between 0 and 100",
111
- interactive=True,)
112
- with gr.Row() as attack:
113
- with gr.Column(min_width=512):
114
- text_input = gr.Textbox(label="Input Prompt")
115
-
116
- orig_img = gr.Image(label="Image Generated by Input Prompt",width=512,show_share_button=False,show_download_button=False)
117
- with gr.Column():
118
- start_button = gr.Button("UnlearnDiffAtk!",size='lg')
119
- with gr.Column(min_width=512):
120
- text_ouput = gr.Textbox(label="Prompt Genetated by UnlearnDiffAtk")
121
- result_img = gr.Image(label="Image Gnerated by Prompt of UnlearnDiffAtk",width=512,show_share_button=False,show_download_button=False)
122
-
123
-
124
- start_button.click(fn=excute_udiff, inputs=[drop_model, drop, shown_columns_step, atk_idx], outputs=[text_input, text_ouput, orig_img, result_img], api_name="udiff")
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')