Spaces:
Runtime error
Runtime error
Commit
•
92d4b39
1
Parent(s):
5541005
Update backend to the new Inpainting model
Browse filesA new inpainting specialist model was released: https://huggingface.co/runwayml/stable-diffusion-inpainting ,results are very good. Diffusers changed the official inpainting pipeline to use that model. This PR swaps the model to that.
app.py
CHANGED
@@ -1,427 +1,422 @@
|
|
1 |
-
import io
|
2 |
-
import base64
|
3 |
-
import os
|
4 |
-
|
5 |
-
import numpy as np
|
6 |
-
import torch
|
7 |
-
from torch import autocast
|
8 |
-
from diffusers import StableDiffusionPipeline,
|
9 |
-
from PIL import Image
|
10 |
-
from PIL import ImageOps
|
11 |
-
import gradio as gr
|
12 |
-
import base64
|
13 |
-
import skimage
|
14 |
-
import skimage.measure
|
15 |
-
from utils import *
|
16 |
-
|
17 |
-
try:
|
18 |
-
cuda_available = torch.cuda.is_available()
|
19 |
-
except:
|
20 |
-
cuda_available = False
|
21 |
-
finally:
|
22 |
-
if cuda_available:
|
23 |
-
device = "cuda"
|
24 |
-
else:
|
25 |
-
device = "cpu"
|
26 |
-
|
27 |
-
if device != "cuda":
|
28 |
-
import contextlib
|
29 |
-
autocast = contextlib.nullcontext
|
30 |
-
|
31 |
-
def load_html():
|
32 |
-
body, canvaspy = "", ""
|
33 |
-
with open("index.html", encoding="utf8") as f:
|
34 |
-
body = f.read()
|
35 |
-
with open("canvas.py", encoding="utf8") as f:
|
36 |
-
canvaspy = f.read()
|
37 |
-
body = body.replace("- paths:\n", "")
|
38 |
-
body = body.replace(" - ./canvas.py\n", "")
|
39 |
-
body = body.replace("from canvas import InfCanvas", canvaspy)
|
40 |
-
return body
|
41 |
-
|
42 |
-
|
43 |
-
def test(x):
|
44 |
-
x = load_html()
|
45 |
-
return f"""<iframe id="sdinfframe" style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
|
46 |
-
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
47 |
-
allow-scripts allow-same-origin allow-popups
|
48 |
-
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
49 |
-
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
|
50 |
-
|
51 |
-
|
52 |
-
DEBUG_MODE = False
|
53 |
-
|
54 |
-
try:
|
55 |
-
SAMPLING_MODE = Image.Resampling.LANCZOS
|
56 |
-
except Exception as e:
|
57 |
-
SAMPLING_MODE = Image.LANCZOS
|
58 |
-
|
59 |
-
try:
|
60 |
-
contain_func = ImageOps.contain
|
61 |
-
except Exception as e:
|
62 |
-
|
63 |
-
def contain_func(image, size, method=SAMPLING_MODE):
|
64 |
-
# from PIL: https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.contain
|
65 |
-
im_ratio = image.width / image.height
|
66 |
-
dest_ratio = size[0] / size[1]
|
67 |
-
if im_ratio != dest_ratio:
|
68 |
-
if im_ratio > dest_ratio:
|
69 |
-
new_height = int(image.height / image.width * size[0])
|
70 |
-
if new_height != size[1]:
|
71 |
-
size = (size[0], new_height)
|
72 |
-
else:
|
73 |
-
new_width = int(image.width / image.height * size[1])
|
74 |
-
if new_width != size[0]:
|
75 |
-
size = (new_width, size[1])
|
76 |
-
return image.resize(size, resample=method)
|
77 |
-
|
78 |
-
|
79 |
-
PAINT_SELECTION = "✥"
|
80 |
-
IMAGE_SELECTION = "🖼️"
|
81 |
-
BRUSH_SELECTION = "🖌️"
|
82 |
-
blocks = gr.Blocks()
|
83 |
-
model = {}
|
84 |
-
model["width"] = 1500
|
85 |
-
model["height"] = 600
|
86 |
-
model["sel_size"] = 256
|
87 |
-
|
88 |
-
def get_token():
|
89 |
-
token = ""
|
90 |
-
token = os.environ.get("hftoken", token)
|
91 |
-
return token
|
92 |
-
|
93 |
-
|
94 |
-
def save_token(token):
|
95 |
-
return
|
96 |
-
|
97 |
-
|
98 |
-
def get_model(token=""):
|
99 |
-
if "text2img" not in model:
|
100 |
-
if device=="cuda":
|
101 |
-
text2img = StableDiffusionPipeline.from_pretrained(
|
102 |
-
"CompVis/stable-diffusion-v1-4",
|
103 |
-
revision="fp16",
|
104 |
-
torch_dtype=torch.float16,
|
105 |
-
use_auth_token=token,
|
106 |
-
).to(device)
|
107 |
-
else:
|
108 |
-
text2img = StableDiffusionPipeline.from_pretrained(
|
109 |
-
"CompVis/stable-diffusion-v1-4",
|
110 |
-
use_auth_token=token,
|
111 |
-
).to(device)
|
112 |
-
model["safety_checker"] = text2img.safety_checker
|
113 |
-
inpaint =
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
)
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
)["sample"]
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
out[:, :,
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
)
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
#
|
258 |
-
|
259 |
-
#
|
260 |
-
#
|
261 |
-
#
|
262 |
-
# label="
|
263 |
-
#
|
264 |
-
#
|
265 |
-
#
|
266 |
-
#
|
267 |
-
#
|
268 |
-
#
|
269 |
-
#
|
270 |
-
#
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
"
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
pil =
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
#
|
378 |
-
#
|
379 |
-
#
|
380 |
-
#
|
381 |
-
#
|
382 |
-
#
|
383 |
-
#
|
384 |
-
#
|
385 |
-
#
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
],
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
fn=None, inputs=[canvas_control], outputs=[canvas_control], _js=mode_js,
|
424 |
-
)
|
425 |
-
|
426 |
-
demo.launch()
|
427 |
-
|
|
|
1 |
+
import io
|
2 |
+
import base64
|
3 |
+
import os
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import torch
|
7 |
+
from torch import autocast
|
8 |
+
from diffusers import StableDiffusionPipeline, DiffusionPipeline
|
9 |
+
from PIL import Image
|
10 |
+
from PIL import ImageOps
|
11 |
+
import gradio as gr
|
12 |
+
import base64
|
13 |
+
import skimage
|
14 |
+
import skimage.measure
|
15 |
+
from utils import *
|
16 |
+
|
17 |
+
try:
|
18 |
+
cuda_available = torch.cuda.is_available()
|
19 |
+
except:
|
20 |
+
cuda_available = False
|
21 |
+
finally:
|
22 |
+
if cuda_available:
|
23 |
+
device = "cuda"
|
24 |
+
else:
|
25 |
+
device = "cpu"
|
26 |
+
|
27 |
+
if device != "cuda":
|
28 |
+
import contextlib
|
29 |
+
autocast = contextlib.nullcontext
|
30 |
+
|
31 |
+
def load_html():
|
32 |
+
body, canvaspy = "", ""
|
33 |
+
with open("index.html", encoding="utf8") as f:
|
34 |
+
body = f.read()
|
35 |
+
with open("canvas.py", encoding="utf8") as f:
|
36 |
+
canvaspy = f.read()
|
37 |
+
body = body.replace("- paths:\n", "")
|
38 |
+
body = body.replace(" - ./canvas.py\n", "")
|
39 |
+
body = body.replace("from canvas import InfCanvas", canvaspy)
|
40 |
+
return body
|
41 |
+
|
42 |
+
|
43 |
+
def test(x):
|
44 |
+
x = load_html()
|
45 |
+
return f"""<iframe id="sdinfframe" style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
|
46 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
47 |
+
allow-scripts allow-same-origin allow-popups
|
48 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
49 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
|
50 |
+
|
51 |
+
|
52 |
+
DEBUG_MODE = False
|
53 |
+
|
54 |
+
try:
|
55 |
+
SAMPLING_MODE = Image.Resampling.LANCZOS
|
56 |
+
except Exception as e:
|
57 |
+
SAMPLING_MODE = Image.LANCZOS
|
58 |
+
|
59 |
+
try:
|
60 |
+
contain_func = ImageOps.contain
|
61 |
+
except Exception as e:
|
62 |
+
|
63 |
+
def contain_func(image, size, method=SAMPLING_MODE):
|
64 |
+
# from PIL: https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.contain
|
65 |
+
im_ratio = image.width / image.height
|
66 |
+
dest_ratio = size[0] / size[1]
|
67 |
+
if im_ratio != dest_ratio:
|
68 |
+
if im_ratio > dest_ratio:
|
69 |
+
new_height = int(image.height / image.width * size[0])
|
70 |
+
if new_height != size[1]:
|
71 |
+
size = (size[0], new_height)
|
72 |
+
else:
|
73 |
+
new_width = int(image.width / image.height * size[1])
|
74 |
+
if new_width != size[0]:
|
75 |
+
size = (new_width, size[1])
|
76 |
+
return image.resize(size, resample=method)
|
77 |
+
|
78 |
+
|
79 |
+
PAINT_SELECTION = "✥"
|
80 |
+
IMAGE_SELECTION = "🖼️"
|
81 |
+
BRUSH_SELECTION = "🖌️"
|
82 |
+
blocks = gr.Blocks()
|
83 |
+
model = {}
|
84 |
+
model["width"] = 1500
|
85 |
+
model["height"] = 600
|
86 |
+
model["sel_size"] = 256
|
87 |
+
|
88 |
+
def get_token():
|
89 |
+
token = ""
|
90 |
+
token = os.environ.get("hftoken", token)
|
91 |
+
return token
|
92 |
+
|
93 |
+
|
94 |
+
def save_token(token):
|
95 |
+
return
|
96 |
+
|
97 |
+
|
98 |
+
def get_model(token=""):
|
99 |
+
if "text2img" not in model:
|
100 |
+
if device=="cuda":
|
101 |
+
text2img = StableDiffusionPipeline.from_pretrained(
|
102 |
+
"CompVis/stable-diffusion-v1-4",
|
103 |
+
revision="fp16",
|
104 |
+
torch_dtype=torch.float16,
|
105 |
+
use_auth_token=token,
|
106 |
+
).to(device)
|
107 |
+
else:
|
108 |
+
text2img = StableDiffusionPipeline.from_pretrained(
|
109 |
+
"CompVis/stable-diffusion-v1-4",
|
110 |
+
use_auth_token=token,
|
111 |
+
).to(device)
|
112 |
+
model["safety_checker"] = text2img.safety_checker
|
113 |
+
inpaint = DiffusionPipeline.from_pretrained(
|
114 |
+
"runwayml/stable-diffusion-inpainting",
|
115 |
+
use_auth_token=token,
|
116 |
+
).to(device)
|
117 |
+
save_token(token)
|
118 |
+
try:
|
119 |
+
total_memory = torch.cuda.get_device_properties(0).total_memory // (
|
120 |
+
1024 ** 3
|
121 |
+
)
|
122 |
+
if total_memory <= 5:
|
123 |
+
inpaint.enable_attention_slicing()
|
124 |
+
except:
|
125 |
+
pass
|
126 |
+
model["text2img"] = text2img
|
127 |
+
model["inpaint"] = inpaint
|
128 |
+
return model["text2img"], model["inpaint"]
|
129 |
+
|
130 |
+
|
131 |
+
def run_outpaint(
|
132 |
+
sel_buffer_str,
|
133 |
+
prompt_text,
|
134 |
+
strength,
|
135 |
+
guidance,
|
136 |
+
step,
|
137 |
+
resize_check,
|
138 |
+
fill_mode,
|
139 |
+
enable_safety,
|
140 |
+
state,
|
141 |
+
):
|
142 |
+
base64_str = "base64"
|
143 |
+
if not cuda_available:
|
144 |
+
data = base64.b64decode(str(sel_buffer_str))
|
145 |
+
pil = Image.open(io.BytesIO(data))
|
146 |
+
sel_buffer = np.array(pil)
|
147 |
+
sel_buffer[:, :, 3]=255
|
148 |
+
sel_buffer[:, :, 0]=255
|
149 |
+
out_pil = Image.fromarray(sel_buffer)
|
150 |
+
out_buffer = io.BytesIO()
|
151 |
+
out_pil.save(out_buffer, format="PNG")
|
152 |
+
out_buffer.seek(0)
|
153 |
+
base64_bytes = base64.b64encode(out_buffer.read())
|
154 |
+
base64_str = base64_bytes.decode("ascii")
|
155 |
+
return (
|
156 |
+
gr.update(label=str(state + 1), value=base64_str,),
|
157 |
+
gr.update(label="Prompt"),
|
158 |
+
state + 1,
|
159 |
+
)
|
160 |
+
if True:
|
161 |
+
text2img, inpaint = get_model()
|
162 |
+
if enable_safety:
|
163 |
+
text2img.safety_checker = model["safety_checker"]
|
164 |
+
inpaint.safety_checker = model["safety_checker"]
|
165 |
+
else:
|
166 |
+
text2img.safety_checker = lambda images, **kwargs: (images, False)
|
167 |
+
inpaint.safety_checker = lambda images, **kwargs: (images, False)
|
168 |
+
data = base64.b64decode(str(sel_buffer_str))
|
169 |
+
pil = Image.open(io.BytesIO(data))
|
170 |
+
# base.output.clear_output()
|
171 |
+
# base.read_selection_from_buffer()
|
172 |
+
sel_buffer = np.array(pil)
|
173 |
+
img = sel_buffer[:, :, 0:3]
|
174 |
+
mask = sel_buffer[:, :, -1]
|
175 |
+
process_size = 512 if resize_check else model["sel_size"]
|
176 |
+
if mask.sum() > 0:
|
177 |
+
img, mask = functbl[fill_mode](img, mask)
|
178 |
+
init_image = Image.fromarray(img)
|
179 |
+
mask = 255 - mask
|
180 |
+
mask = skimage.measure.block_reduce(mask, (8, 8), np.max)
|
181 |
+
mask = mask.repeat(8, axis=0).repeat(8, axis=1)
|
182 |
+
mask_image = Image.fromarray(mask)
|
183 |
+
# mask_image=mask_image.filter(ImageFilter.GaussianBlur(radius = 8))
|
184 |
+
with autocast("cuda"):
|
185 |
+
images = inpaint(
|
186 |
+
prompt=prompt_text,
|
187 |
+
image=init_image.resize(
|
188 |
+
(process_size, process_size), resample=SAMPLING_MODE
|
189 |
+
),
|
190 |
+
mask_image=mask_image.resize((process_size, process_size)),
|
191 |
+
strength=strength,
|
192 |
+
num_inference_steps=step,
|
193 |
+
guidance_scale=guidance,
|
194 |
+
)["sample"]
|
195 |
+
else:
|
196 |
+
with autocast("cuda"):
|
197 |
+
images = text2img(
|
198 |
+
prompt=prompt_text, height=process_size, width=process_size,
|
199 |
+
)["sample"]
|
200 |
+
out = sel_buffer.copy()
|
201 |
+
out[:, :, 0:3] = np.array(
|
202 |
+
images[0].resize(
|
203 |
+
(model["sel_size"], model["sel_size"]), resample=SAMPLING_MODE,
|
204 |
+
)
|
205 |
+
)
|
206 |
+
out[:, :, -1] = 255
|
207 |
+
out_pil = Image.fromarray(out)
|
208 |
+
out_buffer = io.BytesIO()
|
209 |
+
out_pil.save(out_buffer, format="PNG")
|
210 |
+
out_buffer.seek(0)
|
211 |
+
base64_bytes = base64.b64encode(out_buffer.read())
|
212 |
+
base64_str = base64_bytes.decode("ascii")
|
213 |
+
return (
|
214 |
+
gr.update(label=str(state + 1), value=base64_str,),
|
215 |
+
gr.update(label="Prompt"),
|
216 |
+
state + 1,
|
217 |
+
)
|
218 |
+
|
219 |
+
|
220 |
+
def load_js(name):
|
221 |
+
if name in ["export", "commit", "undo"]:
|
222 |
+
return f"""
|
223 |
+
function (x)
|
224 |
+
{{
|
225 |
+
let frame=document.querySelector("gradio-app").querySelector("#sdinfframe").contentWindow;
|
226 |
+
frame.postMessage(["click","{name}"], "*");
|
227 |
+
return x;
|
228 |
+
}}
|
229 |
+
"""
|
230 |
+
ret = ""
|
231 |
+
with open(f"./js/{name}.js", "r") as f:
|
232 |
+
ret = f.read()
|
233 |
+
return ret
|
234 |
+
|
235 |
+
|
236 |
+
upload_button_js = load_js("upload")
|
237 |
+
outpaint_button_js = load_js("outpaint")
|
238 |
+
proceed_button_js = load_js("proceed")
|
239 |
+
mode_js = load_js("mode")
|
240 |
+
setup_button_js = load_js("setup")
|
241 |
+
if not cuda_available:
|
242 |
+
get_model = lambda x:x
|
243 |
+
get_model(get_token())
|
244 |
+
|
245 |
+
with blocks as demo:
|
246 |
+
# title
|
247 |
+
title = gr.Markdown(
|
248 |
+
"""
|
249 |
+
**stablediffusion-infinity**: Outpainting with Stable Diffusion on an infinite canvas: [https://github.com/lkwq007/stablediffusion-infinity](https://github.com/lkwq007/stablediffusion-infinity)
|
250 |
+
"""
|
251 |
+
)
|
252 |
+
# frame
|
253 |
+
frame = gr.HTML(test(2), visible=True)
|
254 |
+
# setup
|
255 |
+
# with gr.Row():
|
256 |
+
# token = gr.Textbox(
|
257 |
+
# label="Huggingface token",
|
258 |
+
# value="",
|
259 |
+
# placeholder="Input your token here",
|
260 |
+
# )
|
261 |
+
# canvas_width = gr.Number(
|
262 |
+
# label="Canvas width", value=1024, precision=0, elem_id="canvas_width"
|
263 |
+
# )
|
264 |
+
# canvas_height = gr.Number(
|
265 |
+
# label="Canvas height", value=600, precision=0, elem_id="canvas_height"
|
266 |
+
# )
|
267 |
+
# selection_size = gr.Number(
|
268 |
+
# label="Selection box size", value=256, precision=0, elem_id="selection_size"
|
269 |
+
# )
|
270 |
+
# setup_button = gr.Button("Start (may take a while)", variant="primary")
|
271 |
+
with gr.Row():
|
272 |
+
with gr.Column(scale=3, min_width=270):
|
273 |
+
# canvas control
|
274 |
+
canvas_control = gr.Radio(
|
275 |
+
label="Control",
|
276 |
+
choices=[PAINT_SELECTION, IMAGE_SELECTION, BRUSH_SELECTION],
|
277 |
+
value=PAINT_SELECTION,
|
278 |
+
elem_id="control",
|
279 |
+
)
|
280 |
+
with gr.Box():
|
281 |
+
with gr.Group():
|
282 |
+
run_button = gr.Button(value="Outpaint")
|
283 |
+
export_button = gr.Button(value="Export")
|
284 |
+
commit_button = gr.Button(value="✓")
|
285 |
+
retry_button = gr.Button(value="⟳")
|
286 |
+
undo_button = gr.Button(value="↶")
|
287 |
+
with gr.Column(scale=3, min_width=270):
|
288 |
+
sd_prompt = gr.Textbox(
|
289 |
+
label="Prompt", placeholder="input your prompt here", lines=4
|
290 |
+
)
|
291 |
+
with gr.Column(scale=2, min_width=150):
|
292 |
+
with gr.Box():
|
293 |
+
sd_resize = gr.Checkbox(label="Resize input to 515x512", value=True)
|
294 |
+
safety_check = gr.Checkbox(label="Enable Safety Checker", value=True)
|
295 |
+
sd_strength = gr.Slider(
|
296 |
+
label="Strength", minimum=0.0, maximum=1.0, value=0.75, step=0.01
|
297 |
+
)
|
298 |
+
with gr.Column(scale=1, min_width=150):
|
299 |
+
sd_step = gr.Number(label="Step", value=50, precision=0)
|
300 |
+
sd_guidance = gr.Number(label="Guidance", value=7.5)
|
301 |
+
with gr.Row():
|
302 |
+
with gr.Column(scale=4, min_width=600):
|
303 |
+
init_mode = gr.Radio(
|
304 |
+
label="Init mode",
|
305 |
+
choices=[
|
306 |
+
"patchmatch",
|
307 |
+
"edge_pad",
|
308 |
+
"cv2_ns",
|
309 |
+
"cv2_telea",
|
310 |
+
"gaussian",
|
311 |
+
"perlin",
|
312 |
+
],
|
313 |
+
value="patchmatch",
|
314 |
+
type="value",
|
315 |
+
)
|
316 |
+
|
317 |
+
proceed_button = gr.Button("Proceed", elem_id="proceed", visible=DEBUG_MODE)
|
318 |
+
# sd pipeline parameters
|
319 |
+
with gr.Accordion("Upload image", open=False):
|
320 |
+
image_box = gr.Image(image_mode="RGBA", source="upload", type="pil")
|
321 |
+
upload_button = gr.Button(
|
322 |
+
"Upload"
|
323 |
+
)
|
324 |
+
model_output = gr.Textbox(visible=DEBUG_MODE, elem_id="output", label="0")
|
325 |
+
model_input = gr.Textbox(visible=DEBUG_MODE, elem_id="input", label="Input")
|
326 |
+
upload_output = gr.Textbox(visible=DEBUG_MODE, elem_id="upload", label="0")
|
327 |
+
model_output_state = gr.State(value=0)
|
328 |
+
upload_output_state = gr.State(value=0)
|
329 |
+
# canvas_state = gr.State({"width":1024,"height":600,"selection_size":384})
|
330 |
+
|
331 |
+
def upload_func(image, state):
|
332 |
+
pil = image.convert("RGBA")
|
333 |
+
w, h = pil.size
|
334 |
+
if w > model["width"] - 100 or h > model["height"] - 100:
|
335 |
+
pil = contain_func(pil, (model["width"] - 100, model["height"] - 100))
|
336 |
+
out_buffer = io.BytesIO()
|
337 |
+
pil.save(out_buffer, format="PNG")
|
338 |
+
out_buffer.seek(0)
|
339 |
+
base64_bytes = base64.b64encode(out_buffer.read())
|
340 |
+
base64_str = base64_bytes.decode("ascii")
|
341 |
+
return (
|
342 |
+
gr.update(label=str(state + 1), value=base64_str),
|
343 |
+
state + 1,
|
344 |
+
)
|
345 |
+
|
346 |
+
upload_button.click(
|
347 |
+
fn=upload_func,
|
348 |
+
inputs=[image_box, upload_output_state],
|
349 |
+
outputs=[upload_output, upload_output_state],
|
350 |
+
_js=upload_button_js,
|
351 |
+
queue=False
|
352 |
+
)
|
353 |
+
|
354 |
+
def setup_func(token_val, width, height, size):
|
355 |
+
model["width"] = width
|
356 |
+
model["height"] = height
|
357 |
+
model["sel_size"] = size
|
358 |
+
try:
|
359 |
+
get_model(token_val)
|
360 |
+
except Exception as e:
|
361 |
+
return {token: gr.update(value="Invalid token!")}
|
362 |
+
return {
|
363 |
+
token: gr.update(visible=False),
|
364 |
+
canvas_width: gr.update(visible=False),
|
365 |
+
canvas_height: gr.update(visible=False),
|
366 |
+
selection_size: gr.update(visible=False),
|
367 |
+
setup_button: gr.update(visible=False),
|
368 |
+
frame: gr.update(visible=True),
|
369 |
+
upload_button: gr.update(value="Upload"),
|
370 |
+
}
|
371 |
+
|
372 |
+
# setup_button.click(
|
373 |
+
# fn=setup_func,
|
374 |
+
# inputs=[token, canvas_width, canvas_height, selection_size],
|
375 |
+
# outputs=[
|
376 |
+
# token,
|
377 |
+
# canvas_width,
|
378 |
+
# canvas_height,
|
379 |
+
# selection_size,
|
380 |
+
# setup_button,
|
381 |
+
# frame,
|
382 |
+
# upload_button,
|
383 |
+
# ],
|
384 |
+
# _js=setup_button_js,
|
385 |
+
# )
|
386 |
+
run_button.click(
|
387 |
+
fn=None, inputs=[run_button], outputs=[run_button], _js=outpaint_button_js,
|
388 |
+
)
|
389 |
+
retry_button.click(
|
390 |
+
fn=None, inputs=[run_button], outputs=[run_button], _js=outpaint_button_js,
|
391 |
+
)
|
392 |
+
proceed_button.click(
|
393 |
+
fn=run_outpaint,
|
394 |
+
inputs=[
|
395 |
+
model_input,
|
396 |
+
sd_prompt,
|
397 |
+
sd_strength,
|
398 |
+
sd_guidance,
|
399 |
+
sd_step,
|
400 |
+
sd_resize,
|
401 |
+
init_mode,
|
402 |
+
safety_check,
|
403 |
+
model_output_state,
|
404 |
+
],
|
405 |
+
outputs=[model_output, sd_prompt, model_output_state],
|
406 |
+
_js=proceed_button_js,
|
407 |
+
)
|
408 |
+
export_button.click(
|
409 |
+
fn=None, inputs=[export_button], outputs=[export_button], _js=load_js("export")
|
410 |
+
)
|
411 |
+
commit_button.click(
|
412 |
+
fn=None, inputs=[export_button], outputs=[export_button], _js=load_js("commit")
|
413 |
+
)
|
414 |
+
undo_button.click(
|
415 |
+
fn=None, inputs=[export_button], outputs=[export_button], _js=load_js("undo")
|
416 |
+
)
|
417 |
+
canvas_control.change(
|
418 |
+
fn=None, inputs=[canvas_control], outputs=[canvas_control], _js=mode_js,
|
419 |
+
)
|
420 |
+
|
421 |
+
demo.launch()
|
422 |
+
|
|
|
|
|
|
|
|
|
|