xinchen9 commited on
Commit
a63a8f0
1 Parent(s): a8b571f

[Update]Change code similar to previous code

Browse files
Files changed (1) hide show
  1. app.py +102 -38
app.py CHANGED
@@ -1,47 +1,111 @@
1
  import gradio as gr
2
 
3
- with gr.Blocks() as demo:
4
- with gr.Row():
5
- prompt = gr.Textbox(label='Input Prompt')
6
- with gr.Row():
7
- shown_columns_1 = gr.CheckboxGroup(
8
- choices=["Church","Parachute","Tench", "Garbage Truck"],
9
- label="Undersirable Objects",
10
- elem_id="column-object",
11
- interactive=True,
12
- )
13
- with gr.Row():
14
- shown_columns_2 = gr.CheckboxGroup(
15
- choices=["Van Gogh"],
16
- label="Undersirable Styles",
17
- elem_id="column-style",
18
- interactive=True,
19
- )
20
- with gr.Row():
21
- shown_columns_3 = gr.CheckboxGroup(
22
- choices=["Violence","Illegal Activity","Nudity"],
23
- label="Undersirable Concepts (Outputs that may be offensive in nature)",
24
- elem_id="column-select",
25
- interactive=True,
26
- )
27
- with gr.Row():
28
- with gr.Column(scale=1, min_width=300):
29
- img1 = gr.Image("images/cheetah.jpg",label="Unlearning")
30
- with gr.Column(scale=1, min_width=300):
31
- img2 = gr.Image("images/cheetah.jpg",label="Attacking")
32
-
33
- with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
34
  # gr.Markdown("Please upload your model id.")
35
  diffusion_model_id = gr.Textbox(label='diffusion_model_id')
36
- shown_columns_4 = gr.Slider(
37
- 1, 100, value=40,
38
- step=1, label="Attacking Steps", info="Choose between 1 and 100",
39
- interactive=True,)
40
-
41
- # concept = gr.Textbox(label='concept')
42
  attacker = gr.Textbox(label='attacker')
43
 
44
  start_button = gr.Button("Attack!")
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- demo.launch()
 
1
  import gradio as gr
2
 
3
+ import gradio as gr
4
+ import os
5
+ import requests
6
+ import json
7
+ from huggingface_hub import login
8
+
9
+
10
+ myip = os.environ["myip"]
11
+ myport = os.environ["myport"]
12
+
13
+
14
+ is_spaces = True if "SPACE_ID" in os.environ else False
15
+
16
+ is_shared_ui = False
17
+
18
+
19
+ def excute_udiff(diffusion_model_id, concept, attacker):
20
+ print(f"my IP is {myip}, my port is {myport}")
21
+ print(f"my input is diffusion_model_id: {diffusion_model_id}, concept: {concept}, attacker: {attacker}")
22
+ result = requests.post('http://{}:{}/udiff'.format(myip, myport), json={"diffusion_model_id": diffusion_model_id, "concept": concept, "attacker": attacker})
23
+ result = result.text[1:-1]
24
+
25
+ return result
26
+
27
+
28
+ css = '''
29
+ .instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
30
+ .arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
31
+ #component-4, #component-3, #component-10{min-height: 0}
32
+ .duplicate-button img{margin: 0}
33
+ #img_1, #img_2, #img_3, #img_4{height:15rem}
34
+ #mdStyle{font-size: 0.7rem}
35
+ #titleCenter {text-align:center}
36
+ '''
37
+
38
+
39
+ with gr.Blocks(css=css) as demo:
40
+ gr.Markdown("# Unlearn Diffusions Attack")
41
+ gr.Markdown("### It will generate a prompt to lead your model output unsafe image.")
42
+ 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.")
43
+
44
+ with gr.Row() as udiff:
45
+ with gr.Column():
46
  # gr.Markdown("Please upload your model id.")
47
  diffusion_model_id = gr.Textbox(label='diffusion_model_id')
48
+ concept = gr.Textbox(label='concept')
 
 
 
 
 
49
  attacker = gr.Textbox(label='attacker')
50
 
51
  start_button = gr.Button("Attack!")
52
 
53
+ with gr.Column():
54
+ result = gr.Textbox(label="unsafe prompt")
55
+
56
+ with gr.Column():
57
+ gr.Examples(examples=[
58
+ ["CompVis/stable-diffusion-v1-4", "nudity", "text_grad"]
59
+ ], inputs=[diffusion_model_id, concept, attacker])
60
+
61
+ start_button.click(fn=excute_udiff, inputs=[diffusion_model_id, concept, attacker], outputs=result, api_name="udiff")
62
+
63
+
64
+ # demo.queue(default_enabled=False, api_open=False, max_size=5).launch(debug=True, show_api=False)
65
+ demo.queue().launch(server_name='0.0.0.0')
66
+
67
+ # with gr.Blocks() as demo:
68
+ # with gr.Row():
69
+ # prompt = gr.Textbox(label='Input Prompt')
70
+ # with gr.Row():
71
+ # shown_columns_1 = gr.CheckboxGroup(
72
+ # choices=["Church","Parachute","Tench", "Garbage Truck"],
73
+ # label="Undersirable Objects",
74
+ # elem_id="column-object",
75
+ # interactive=True,
76
+ # )
77
+ # with gr.Row():
78
+ # shown_columns_2 = gr.CheckboxGroup(
79
+ # choices=["Van Gogh"],
80
+ # label="Undersirable Styles",
81
+ # elem_id="column-style",
82
+ # interactive=True,
83
+ # )
84
+ # with gr.Row():
85
+ # shown_columns_3 = gr.CheckboxGroup(
86
+ # choices=["Violence","Illegal Activity","Nudity"],
87
+ # label="Undersirable Concepts (Outputs that may be offensive in nature)",
88
+ # elem_id="column-select",
89
+ # interactive=True,
90
+ # )
91
+ # with gr.Row():
92
+ # with gr.Column(scale=1, min_width=300):
93
+ # img1 = gr.Image("images/cheetah.jpg",label="Unlearning")
94
+ # with gr.Column(scale=1, min_width=300):
95
+ # img2 = gr.Image("images/cheetah.jpg",label="Attacking")
96
+
97
+ # with gr.Row():
98
+ # # gr.Markdown("Please upload your model id.")
99
+ # diffusion_model_id = gr.Textbox(label='diffusion_model_id')
100
+ # shown_columns_4 = gr.Slider(
101
+ # 1, 100, value=40,
102
+ # step=1, label="Attacking Steps", info="Choose between 1 and 100",
103
+ # interactive=True,)
104
+
105
+ # # concept = gr.Textbox(label='concept')
106
+ # attacker = gr.Textbox(label='attacker')
107
+
108
+ # start_button = gr.Button("Attack!")
109
+
110
 
111
+ # demo.launch()