Spaces:
Sleeping
Sleeping
[Update]Change code similar to previous code
Browse files
app.py
CHANGED
|
@@ -1,47 +1,111 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# gr.Markdown("Please upload your model id.")
|
| 35 |
diffusion_model_id = gr.Textbox(label='diffusion_model_id')
|
| 36 |
-
|
| 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()
|