Fabrice-TIERCELIN commited on
Commit
1ae6f28
1 Parent(s): 9b94e37

Upload the app

Browse files
Files changed (3) hide show
  1. README.md +16 -6
  2. app.py +361 -0
  3. requirements.txt +8 -0
README.md CHANGED
@@ -1,12 +1,22 @@
1
  ---
2
- title: Inpaint Prod
3
- emoji: 🏆
4
- colorFrom: gray
5
- colorTo: gray
 
 
 
 
 
 
 
 
 
6
  sdk: gradio
7
- sdk_version: 4.20.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: SDXL inpaint (any size)
3
+ emoji: ↔️
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ tags:
7
+ - Image-to-Image
8
+ - Image-2-Image
9
+ - Img-to-Img
10
+ - Img-2-Img
11
+ - SDXL
12
+ - Stable Diffusion
13
+ - language models
14
+ - LLMs
15
  sdk: gradio
16
+ sdk_version: 3.41.2
17
  app_file: app.py
18
  pinned: false
19
+ license: mit
20
  ---
21
 
22
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionXLInpaintPipeline
2
+ from PIL import Image, ImageFilter
3
+
4
+ import gradio as gr
5
+ import numpy as np
6
+ import time
7
+ import math
8
+ import random
9
+ import imageio
10
+ import torch
11
+
12
+ max_64_bit_int = 2**63 - 1
13
+
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ floatType = torch.float16 if torch.cuda.is_available() else torch.float32
16
+ variant = "fp16" if torch.cuda.is_available() else None
17
+ pipe = StableDiffusionXLInpaintPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype = floatType, variant = variant)
18
+ pipe = pipe.to(device)
19
+
20
+ def check(
21
+ source_img,
22
+ prompt,
23
+ uploaded_mask,
24
+ negative_prompt,
25
+ denoising_steps,
26
+ num_inference_steps,
27
+ guidance_scale,
28
+ image_guidance_scale,
29
+ strength,
30
+ randomize_seed,
31
+ seed,
32
+ debug_mode,
33
+ progress = gr.Progress()
34
+ ):
35
+ if source_img is None:
36
+ raise gr.Error("Please provide an image.")
37
+
38
+ if prompt is None or prompt == "":
39
+ raise gr.Error("Please provide a prompt input.")
40
+
41
+ def inpaint(
42
+ source_img,
43
+ prompt,
44
+ uploaded_mask,
45
+ negative_prompt,
46
+ denoising_steps,
47
+ num_inference_steps,
48
+ guidance_scale,
49
+ image_guidance_scale,
50
+ strength,
51
+ randomize_seed,
52
+ seed,
53
+ debug_mode,
54
+ progress = gr.Progress()
55
+ ):
56
+ check(
57
+ source_img,
58
+ prompt,
59
+ uploaded_mask,
60
+ negative_prompt,
61
+ denoising_steps,
62
+ num_inference_steps,
63
+ guidance_scale,
64
+ image_guidance_scale,
65
+ strength,
66
+ randomize_seed,
67
+ seed,
68
+ debug_mode
69
+ )
70
+ start = time.time()
71
+ progress(0, desc = "Preparing data...")
72
+
73
+ if negative_prompt is None:
74
+ negative_prompt = ""
75
+
76
+ if denoising_steps is None:
77
+ denoising_steps = 1000
78
+
79
+ if num_inference_steps is None:
80
+ num_inference_steps = 25
81
+
82
+ if guidance_scale is None:
83
+ guidance_scale = 7
84
+
85
+ if image_guidance_scale is None:
86
+ image_guidance_scale = 1.1
87
+
88
+ if strength is None:
89
+ strength = 0.99
90
+
91
+ if randomize_seed:
92
+ seed = random.randint(0, max_64_bit_int)
93
+
94
+ random.seed(seed)
95
+ #pipe = pipe.manual_seed(seed)
96
+
97
+ input_image = source_img["image"].convert("RGB")
98
+
99
+ original_height, original_width, original_channel = np.array(input_image).shape
100
+ output_width = original_width
101
+ output_height = original_height
102
+
103
+ if uploaded_mask is None:
104
+ mask_image = source_img["mask"].convert("RGB")
105
+ else:
106
+ mask_image = uploaded_mask.convert("RGB")
107
+ mask_image = mask_image.resize((original_width, original_height))
108
+
109
+ # Limited to 1 million pixels
110
+ if 1024 * 1024 < output_width * output_height:
111
+ factor = ((1024 * 1024) / (output_width * output_height))**0.5
112
+ process_width = math.floor(output_width * factor)
113
+ process_height = math.floor(output_height * factor)
114
+
115
+ limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
116
+ else:
117
+ process_width = output_width
118
+ process_height = output_height
119
+
120
+ limitation = "";
121
+
122
+ # Width and height must be multiple of 8
123
+ if (process_width % 8) != 0 or (process_height % 8) != 0:
124
+ if ((process_width - (process_width % 8) + 8) * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
125
+ process_width = process_width - (process_width % 8) + 8
126
+ process_height = process_height - (process_height % 8) + 8
127
+ elif (process_height % 8) <= (process_width % 8) and ((process_width - (process_width % 8) + 8) * process_height) <= (1024 * 1024):
128
+ process_width = process_width - (process_width % 8) + 8
129
+ process_height = process_height - (process_height % 8)
130
+ elif (process_width % 8) <= (process_height % 8) and (process_width * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
131
+ process_width = process_width - (process_width % 8)
132
+ process_height = process_height - (process_height % 8) + 8
133
+ else:
134
+ process_width = process_width - (process_width % 8)
135
+ process_height = process_height - (process_height % 8)
136
+
137
+ progress(None, desc = "Processing...")
138
+ output_image = pipe(
139
+ seeds = [seed],
140
+ width = process_width,
141
+ height = process_height,
142
+ prompt = prompt,
143
+ negative_prompt = negative_prompt,
144
+ image = input_image,
145
+ mask_image = mask_image,
146
+ num_inference_steps = num_inference_steps,
147
+ guidance_scale = guidance_scale,
148
+ image_guidance_scale = image_guidance_scale,
149
+ strength = strength,
150
+ denoising_steps = denoising_steps,
151
+ show_progress_bar = True
152
+ ).images[0]
153
+
154
+ if limitation != "":
155
+ output_image = output_image.resize((output_width, output_height))
156
+
157
+ if debug_mode == False:
158
+ input_image = None
159
+ mask_image = None
160
+
161
+ end = time.time()
162
+ secondes = int(end - start)
163
+ minutes = secondes // 60
164
+ secondes = secondes - (minutes * 60)
165
+ hours = minutes // 60
166
+ minutes = minutes - (hours * 60)
167
+ return [
168
+ output_image,
169
+ "Start again to get a different result. The new image is " + str(output_width) + " pixels large and " + str(output_height) + " pixels high, so an image of " + f'{output_width * output_height:,}' + " pixels. The image have been generated in " + str(hours) + " h, " + str(minutes) + " min, " + str(secondes) + " sec." + limitation,
170
+ input_image,
171
+ mask_image
172
+ ]
173
+
174
+ def toggle_debug(is_debug_mode):
175
+ if is_debug_mode:
176
+ return [gr.update(visible = True)] * 2
177
+ else:
178
+ return [gr.update(visible = False)] * 2
179
+
180
+ with gr.Blocks() as interface:
181
+ gr.Markdown(
182
+ """
183
+ <p style="text-align: center;"><b><big><big><big>Inpaint</big></big></big></b></p>
184
+ <p style="text-align: center;">Modifies one detail of your image, at any resolution, freely, without account, without watermark, without installation, which can be downloaded</p>
185
+ <br/>
186
+ <br/>
187
+ 🚀 Powered by <i>SDXL 1.0</i> artificial intellingence. For illustration purpose, not information purpose. The new content is not based on real information but imagination.
188
+ <br/>
189
+ <ul>
190
+ <li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
191
+ <li>To <b>upscale</b> your image, I recommend to use <i>Ilaria Upscaler</i>,</li>
192
+ <li>To <b>slightly change</b> your image, I recommend to use <i>Image-to-Image SDXL</i>,</li>
193
+ <li>If you need to enlarge the <b>viewpoint</b> of your image, I recommend you to use <i>Uncrop</i>,</li>
194
+ <li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
195
+ <li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
196
+ <li>To modify <b>anything else</b> on your image, I recommend to use <i>Instruct Pix2Pix</i>.</li>
197
+ </ul>
198
+ <br/>
199
+ 🐌 Slow process... ~1 hour.<br>You can duplicate this space on a free account, it works on CPU and should also run on CUDA.<br/>
200
+ <a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Inpaint?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
201
+ <br/>
202
+ ⚖️ You can use, modify and share the generated images but not for commercial uses.
203
+
204
+ """
205
+ )
206
+ with gr.Column():
207
+ source_img = gr.Image(label = "Your image", source = "upload", tool = "sketch", type = "pil")
208
+ prompt = gr.Textbox(label = "Prompt", info = "Describe the subject, the background and the style of image; 77 token limit", placeholder = "Describe what you want to see in the entire image")
209
+ with gr.Accordion("Upload a mask", open = False):
210
+ uploaded_mask = gr.Image(label = "Already made mask (black pixels will be preserved, white pixels will be redrawn)", source = "upload", type = "pil")
211
+ with gr.Accordion("Advanced options", open = False):
212
+ negative_prompt = gr.Textbox(label = "Negative prompt", placeholder = "Describe what you do NOT want to see in the entire image", value = "Ugly, malformed, noise, blur, watermark")
213
+ denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
214
+ num_inference_steps = gr.Slider(minimum = 10, maximum = 100, value = 25, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
215
+ guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
216
+ image_guidance_scale = gr.Slider(minimum = 1, value = 1.1, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
217
+ strength = gr.Number(value = 0.99, minimum = 0.01, maximum = 1.0, step = 0.01, label = "Strength", info = "lower=follow the original area, higher=redraw from scratch")
218
+ randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different")
219
+ seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)")
220
+ debug_mode = gr.Checkbox(label = "Debug mode", value = False, info = "Show intermediate results")
221
+
222
+ submit = gr.Button("Inpaint", variant = "primary")
223
+
224
+ inpainted_image = gr.Image(label = "Inpainted image")
225
+ information = gr.Label(label = "Information")
226
+ original_image = gr.Image(label = "Original image", visible = False)
227
+ mask_image = gr.Image(label = "Mask image", visible = False)
228
+
229
+ submit.click(toggle_debug, debug_mode, [
230
+ original_image,
231
+ mask_image
232
+ ], queue = False, show_progress = False).then(check, inputs = [
233
+ source_img,
234
+ prompt,
235
+ uploaded_mask,
236
+ negative_prompt,
237
+ denoising_steps,
238
+ num_inference_steps,
239
+ guidance_scale,
240
+ image_guidance_scale,
241
+ strength,
242
+ randomize_seed,
243
+ seed,
244
+ debug_mode
245
+ ], outputs = [], queue = False, show_progress = False).success(inpaint, inputs = [
246
+ source_img,
247
+ prompt,
248
+ uploaded_mask,
249
+ negative_prompt,
250
+ denoising_steps,
251
+ num_inference_steps,
252
+ guidance_scale,
253
+ image_guidance_scale,
254
+ strength,
255
+ randomize_seed,
256
+ seed,
257
+ debug_mode
258
+ ], outputs = [
259
+ inpainted_image,
260
+ information,
261
+ original_image,
262
+ mask_image
263
+ ], scroll_to_output = True)
264
+
265
+ gr.Examples(
266
+ inputs = [
267
+ source_img,
268
+ prompt,
269
+ uploaded_mask,
270
+ negative_prompt,
271
+ denoising_steps,
272
+ num_inference_steps,
273
+ guidance_scale,
274
+ image_guidance_scale,
275
+ strength,
276
+ randomize_seed,
277
+ seed,
278
+ debug_mode
279
+ ],
280
+ outputs = [
281
+ inpainted_image,
282
+ information,
283
+ original_image,
284
+ mask_image
285
+ ],
286
+ examples = [
287
+ [
288
+ "./Examples/Example1.png",
289
+ "A deer, in a forest landscape, ultrarealistic, realistic, photorealistic, 8k",
290
+ "./Examples/Mask1.webp",
291
+ "Painting, drawing, cartoon, ugly, malformed, noise, blur, watermark",
292
+ 1000,
293
+ 25,
294
+ 7,
295
+ 1.1,
296
+ 0.99,
297
+ True,
298
+ 42,
299
+ False
300
+ ],
301
+ [
302
+ "./Examples/Example2.webp",
303
+ "A cat, ultrarealistic, realistic, photorealistic, 8k",
304
+ "./Examples/Mask2.png",
305
+ "Painting, drawing, cartoon, ugly, malformed, noise, blur, watermark",
306
+ 1000,
307
+ 25,
308
+ 7,
309
+ 1.1,
310
+ 0.99,
311
+ True,
312
+ 42,
313
+ False
314
+ ],
315
+ [
316
+ "./Examples/Example3.jpg",
317
+ "An angry old woman, ultrarealistic, realistic, photorealistic, 8k",
318
+ "./Examples/Mask3.gif",
319
+ "Painting, drawing, cartoon, ugly, malformed, noise, blur, watermark",
320
+ 1000,
321
+ 25,
322
+ 7,
323
+ 1.5,
324
+ 0.99,
325
+ True,
326
+ 42,
327
+ False
328
+ ],
329
+ [
330
+ "./Examples/Example4.gif",
331
+ "A laptop, ultrarealistic, realistic, photorealistic, 8k",
332
+ "./Examples/Mask4.bmp",
333
+ "Painting, drawing, cartoon, ugly, malformed, noise, blur, watermark",
334
+ 1000,
335
+ 25,
336
+ 7,
337
+ 1.1,
338
+ 0.99,
339
+ True,
340
+ 42,
341
+ False
342
+ ],
343
+ [
344
+ "./Examples/Example5.bmp",
345
+ "A sand castle, ultrarealistic, realistic, photorealistic, 8k",
346
+ "./Examples/Mask5.png",
347
+ "Painting, drawing, cartoon, ugly, malformed, noise, blur, watermark",
348
+ 1000,
349
+ 50,
350
+ 7,
351
+ 1.5,
352
+ 0.5,
353
+ True,
354
+ 42,
355
+ False
356
+ ],
357
+ ],
358
+ cache_examples = False,
359
+ )
360
+
361
+ interface.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torchvision
2
+ diffusers
3
+ transformers
4
+ accelerate
5
+ ftfy
6
+ scipy
7
+ imageio
8
+ invisible_watermark