Kaixuanliu commited on
Commit
b67c71f
1 Parent(s): 4e83cae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -40,6 +40,26 @@ def process_image_from_binary(img_stream):
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}")
@@ -48,21 +68,17 @@ def excute_udiff(diffusion_model_id, concept, steps, 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 = '''
@@ -111,17 +127,17 @@ with gr.Blocks(css=custom_css) as demo:
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')
 
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 excute_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}")
 
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 = '''
 
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=excute_prepare, inputs=[drop_model, drop, shown_columns_step, atk_idx], outputs=[text_input, orig_img], api_name="prepare")
140
+ attack_button.click(fn=excute_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')