derek-thomas HF staff commited on
Commit
f88e679
1 Parent(s): 17915d3

init commit

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +74 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .idea
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import glob
3
+ import os
4
+ from PIL import Image
5
+ import numpy as np
6
+ from fawkes.protection import Fawkes # Make sure the import path is correct
7
+
8
+
9
+ def run_protection_interface(uploaded_image, gpu='0', mode='low', feature_extractor="arcface_extractor_0",
10
+ th=0.01, max_step=1000, sd=1e6, lr=2, batch_size=1, format='png',
11
+ separate_target=False, no_align=False, debug=False):
12
+ """
13
+ Gradio compatible function for running protection.
14
+ """
15
+ if uploaded_image is None:
16
+ return None, "No image uploaded."
17
+
18
+ # Save the uploaded image to a temporary directory
19
+ temp_dir = "temp_imgs"
20
+ os.makedirs(temp_dir, exist_ok=True)
21
+ img_path = os.path.join(temp_dir, "uploaded_image.png")
22
+ uploaded_image.save(img_path)
23
+
24
+ # Run the protection process
25
+ protector = Fawkes(feature_extractor, gpu, batch_size, mode=mode)
26
+ image_paths = [img_path]
27
+ protector.run_protection(image_paths, th=th, sd=sd, lr=lr,
28
+ max_step=max_step, batch_size=batch_size,
29
+ format=format, separate_target=separate_target,
30
+ debug=debug, no_align=no_align)
31
+
32
+ # Load and return the processed image
33
+ processed_img_path = img_path.replace(".png", "_cloaked.png")
34
+ if os.path.exists(processed_img_path):
35
+ processed_image = Image.open(processed_img_path)
36
+ return processed_image, "Protection process completed."
37
+ else:
38
+ return None, "Protection process failed or no cloaked image generated."
39
+
40
+
41
+ with gr.Blocks() as demo:
42
+ with gr.Row():
43
+ with gr.Column():
44
+ gr.Markdown("### Upload Image")
45
+ uploaded_image = gr.Image(type="pil", label="Upload Image")
46
+
47
+ with gr.Column():
48
+ gr.Markdown("### Configuration Options")
49
+ gpu = gr.Textbox(label="GPU", value='0')
50
+ mode = gr.Dropdown(label="Mode", choices=['low', 'mid', 'high'], value='low')
51
+ feature_extractor = gr.Textbox(label="Feature Extractor", value="arcface_extractor_0")
52
+ th = gr.Slider(label="Threshold", minimum=0.001, maximum=0.05, value=0.01)
53
+ max_step = gr.Slider(label="Max Steps", minimum=500, maximum=2000, value=1000)
54
+ sd = gr.Slider(label="Penalty Number (SD)", minimum=1e5, maximum=1e7, value=1e6)
55
+ lr = gr.Slider(label="Learning Rate", minimum=1, maximum=25, value=2)
56
+ batch_size = gr.Slider(label="Batch Size", minimum=1, maximum=10, value=1)
57
+ format = gr.Radio(label="Output Format", choices=['png', 'jpg', 'jpeg'])
58
+ separate_target = gr.Checkbox(label="Separate Target")
59
+ no_align = gr.Checkbox(label="No Align")
60
+ debug = gr.Checkbox(label="Debug")
61
+
62
+ run_button = gr.Button("Run Protection")
63
+ output_image = gr.Image(label="Processed Image")
64
+ output_text = gr.Textbox(label="Output Message")
65
+
66
+ run_button.click(
67
+ fn=run_protection_interface,
68
+ inputs=[uploaded_image, gpu, mode, feature_extractor, th, max_step, sd, lr, batch_size, format,
69
+ separate_target, no_align, debug],
70
+ outputs=[output_image, output_text]
71
+ )
72
+
73
+ if __name__ == "__main__":
74
+ demo.launch()