cesar-tek commited on
Commit
0cb6022
·
verified ·
1 Parent(s): a770651

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +34 -6
  2. app.py +229 -0
  3. requirements.txt +7 -0
README.md CHANGED
@@ -1,12 +1,40 @@
1
  ---
2
- title: DeAI
3
- emoji: 📚
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.2.0
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: SD3.5 Watermark Remover
3
+ emoji: 🎨
4
+ colorFrom: purple
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: "4.44.1"
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
+ hardware: zero-a100
12
  ---
13
 
14
+ # 🎨 SD3.5 Watermark Remover
15
+
16
+ Remove watermarks from images using **Stable Diffusion 3.5 Large** regeneration, powered by ZeroGPU.
17
+
18
+ ## How It Works
19
+
20
+ This tool uses SD3.5's img2img pipeline to regenerate images while preserving their content. By applying controlled denoising, watermarks are effectively removed without manual editing.
21
+
22
+ ### Key Parameters
23
+
24
+ - **Strength (0.1-0.6)**: Controls how much the image changes
25
+ - Lower values (0.2-0.3): Best for subtle watermarks, preserves most detail
26
+ - Higher values (0.4-0.5): Better for prominent watermarks
27
+
28
+ - **Inference Steps**: More steps = higher quality, slower processing
29
+
30
+ ## Based On
31
+
32
+ This Space is based on the [WatermarkAttacker](https://github.com/XuandongZhao/WatermarkAttacker) research project.
33
+
34
+ ## Model
35
+
36
+ Uses [Stable Diffusion 3.5 Large](https://huggingface.co/stabilityai/stable-diffusion-3.5-large) from Stability AI.
37
+
38
+ ## License
39
+
40
+ MIT License - See the original repository for more details.
app.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SD3.5 Watermark Remover - Hugging Face Space
3
+
4
+ Uses Stable Diffusion 3.5 img2img regeneration to remove watermarks
5
+ while preserving image quality. Powered by ZeroGPU.
6
+
7
+ Based on: https://github.com/XuandongZhao/WatermarkAttacker
8
+ """
9
+
10
+ import gradio as gr
11
+ import numpy as np
12
+ import spaces
13
+ import torch
14
+ from PIL import Image
15
+ from diffusers import StableDiffusion3Img2ImgPipeline
16
+
17
+ # Model configuration
18
+ MODEL_ID = "stabilityai/stable-diffusion-3.5-large"
19
+ MAX_IMAGE_SIZE = 1536
20
+ MAX_SEED = np.iinfo(np.int32).max
21
+
22
+ # Determine device and dtype
23
+ device = "cuda" if torch.cuda.is_available() else "cpu"
24
+ torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
25
+
26
+ # Load model at startup (will use ZeroGPU when decorated function is called)
27
+ print(f"Loading SD3.5 model: {MODEL_ID}")
28
+ pipe = StableDiffusion3Img2ImgPipeline.from_pretrained(
29
+ MODEL_ID,
30
+ torch_dtype=torch_dtype,
31
+ )
32
+ pipe = pipe.to(device)
33
+ print("Model loaded successfully!")
34
+
35
+
36
+ def preprocess_image(image: Image.Image) -> tuple[Image.Image, tuple[int, int]]:
37
+ """Pad image to multiple of 64 for SD3 compatibility."""
38
+ original_size = image.size
39
+ w, h = original_size
40
+
41
+ # SD3 works best with dimensions that are multiples of 64
42
+ new_w = (w + 63) // 64 * 64
43
+ new_h = (h + 63) // 64 * 64
44
+
45
+ if (new_w, new_h) != (w, h):
46
+ padded_img = Image.new('RGB', (new_w, new_h), (0, 0, 0))
47
+ padded_img.paste(image, (0, 0))
48
+ return padded_img, original_size
49
+
50
+ return image, original_size
51
+
52
+
53
+ def postprocess_image(image: Image.Image, original_size: tuple[int, int]) -> Image.Image:
54
+ """Crop image back to original size."""
55
+ if image.size != original_size:
56
+ return image.crop((0, 0, original_size[0], original_size[1]))
57
+ return image
58
+
59
+
60
+ @spaces.GPU(duration=90)
61
+ def remove_watermark(
62
+ input_image: Image.Image,
63
+ strength: float = 0.3,
64
+ num_inference_steps: int = 28,
65
+ seed: int = 42,
66
+ randomize_seed: bool = True,
67
+ progress=gr.Progress(track_tqdm=True),
68
+ ) -> tuple[Image.Image, int]:
69
+ """
70
+ Remove watermark from image using SD3.5 img2img regeneration.
71
+
72
+ Args:
73
+ input_image: Input image with watermark
74
+ strength: Denoising strength (0.0-1.0). Lower = higher quality, less change.
75
+ num_inference_steps: Number of denoising steps
76
+ seed: Random seed for reproducibility
77
+ randomize_seed: Whether to randomize the seed
78
+
79
+ Returns:
80
+ Tuple of (output image, seed used)
81
+ """
82
+ if input_image is None:
83
+ raise gr.Error("Please upload an image first!")
84
+
85
+ # Convert to RGB if needed
86
+ if input_image.mode != 'RGB':
87
+ input_image = input_image.convert('RGB')
88
+
89
+ # Handle seed
90
+ if randomize_seed:
91
+ seed = np.random.randint(0, MAX_SEED)
92
+
93
+ generator = torch.Generator(device=device).manual_seed(seed)
94
+
95
+ # Preprocess image
96
+ processed_image, original_size = preprocess_image(input_image)
97
+
98
+ # Run regeneration
99
+ result = pipe(
100
+ prompt="", # Empty prompt for pure regeneration
101
+ image=processed_image,
102
+ strength=strength,
103
+ num_inference_steps=num_inference_steps,
104
+ guidance_scale=0.0, # No guidance for pure regeneration
105
+ generator=generator,
106
+ ).images[0]
107
+
108
+ # Postprocess to original size
109
+ result = postprocess_image(result, original_size)
110
+
111
+ return result, seed
112
+
113
+
114
+ # Example images (you can add your own)
115
+ examples = [
116
+ ["examples/watermarked_sample.png", 0.3, 28, 42, True],
117
+ ]
118
+
119
+ # Custom CSS
120
+ css = """
121
+ #col-container {
122
+ margin: 0 auto;
123
+ max-width: 900px;
124
+ }
125
+
126
+ .gr-button-primary {
127
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
128
+ }
129
+
130
+ footer {
131
+ visibility: hidden;
132
+ }
133
+ """
134
+
135
+ # Build Gradio interface
136
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
137
+ with gr.Column(elem_id="col-container"):
138
+ gr.Markdown("""
139
+ # 🎨 SD3.5 Watermark Remover
140
+
141
+ Remove watermarks from images using **Stable Diffusion 3.5** regeneration.
142
+
143
+ This tool uses img2img to regenerate the image while preserving its content,
144
+ effectively removing watermarks without manual editing.
145
+
146
+ **How it works:** Lower strength values produce higher quality outputs closer to the original.
147
+ Start with 0.3 and adjust as needed.
148
+ """)
149
+
150
+ with gr.Row():
151
+ with gr.Column(scale=1):
152
+ input_image = gr.Image(
153
+ label="Input Image (with watermark)",
154
+ type="pil",
155
+ height=400,
156
+ )
157
+
158
+ run_button = gr.Button("🚀 Remove Watermark", variant="primary", size="lg")
159
+
160
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
161
+ strength = gr.Slider(
162
+ label="Strength",
163
+ info="Lower = higher quality, less change. Higher = more aggressive removal.",
164
+ minimum=0.1,
165
+ maximum=0.6,
166
+ step=0.05,
167
+ value=0.3,
168
+ )
169
+
170
+ num_inference_steps = gr.Slider(
171
+ label="Inference Steps",
172
+ info="More steps = higher quality, slower processing.",
173
+ minimum=10,
174
+ maximum=50,
175
+ step=1,
176
+ value=28,
177
+ )
178
+
179
+ seed = gr.Slider(
180
+ label="Seed",
181
+ minimum=0,
182
+ maximum=MAX_SEED,
183
+ step=1,
184
+ value=42,
185
+ )
186
+
187
+ randomize_seed = gr.Checkbox(
188
+ label="Randomize seed",
189
+ value=True,
190
+ )
191
+
192
+ with gr.Column(scale=1):
193
+ output_image = gr.Image(
194
+ label="Output Image (watermark removed)",
195
+ type="pil",
196
+ height=400,
197
+ )
198
+
199
+ output_seed = gr.Number(label="Seed Used", interactive=False)
200
+
201
+ gr.Markdown("""
202
+ ### 💡 Tips
203
+ - **Strength 0.2-0.3**: Best for subtle watermarks, preserves most detail
204
+ - **Strength 0.4-0.5**: Better for prominent watermarks, may alter some details
205
+ - **Increase steps**: If quality is poor, try 35-40 steps
206
+
207
+ ---
208
+
209
+ Based on [WatermarkAttacker](https://github.com/XuandongZhao/WatermarkAttacker) research.
210
+ Uses Stable Diffusion 3.5 Large from [Stability AI](https://huggingface.co/stabilityai/stable-diffusion-3.5-large).
211
+ """)
212
+
213
+ # Connect events
214
+ gr.on(
215
+ triggers=[run_button.click],
216
+ fn=remove_watermark,
217
+ inputs=[
218
+ input_image,
219
+ strength,
220
+ num_inference_steps,
221
+ seed,
222
+ randomize_seed,
223
+ ],
224
+ outputs=[output_image, output_seed],
225
+ )
226
+
227
+
228
+ if __name__ == "__main__":
229
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ diffusers>=0.31.0
2
+ transformers
3
+ accelerate
4
+ torch
5
+ Pillow
6
+ gradio>=4.0.0
7
+ spaces