yanranxiaoxi commited on
Commit
b1d6a6c
·
verified ·
1 Parent(s): 00fc29f

chore: 移除 GPU 限制

Browse files
Files changed (1) hide show
  1. app.py +853 -853
app.py CHANGED
@@ -1,854 +1,854 @@
1
- import os
2
- import gradio as gr
3
- import argparse
4
- import numpy as np
5
- import torch
6
- import einops
7
- import copy
8
- import math
9
- import time
10
- import random
11
- import spaces
12
- import re
13
- import uuid
14
-
15
- from gradio_imageslider import ImageSlider
16
- from PIL import Image
17
- from BOOXEL.util import HWC3, upscale_image, fix_resize, convert_dtype, create_BOOXEL_model, load_QF_ckpt
18
- from huggingface_hub import hf_hub_download
19
- from pillow_heif import register_heif_opener
20
-
21
- register_heif_opener()
22
-
23
- max_64_bit_int = np.iinfo(np.int32).max
24
-
25
- hf_hub_download(repo_id="laion/CLIP-ViT-bigG-14-laion2B-39B-b160k", filename="open_clip_pytorch_model.bin", local_dir="laion_CLIP-ViT-bigG-14-laion2B-39B-b160k")
26
- hf_hub_download(repo_id="ckpt/sd_xl_base_1.0", filename="sd_xl_base_1.0_0.9vae.safetensors", local_dir="ckpt_sd_xl_base_1.0")
27
- hf_hub_download(repo_id="yanranxiaoxi/booxel", filename="BOOXEL-v0.F.ckpt", local_dir="yanranxiaoxi_booxel", token=os.environ.get('MODEL_ACCESS_TOKEN'))
28
- hf_hub_download(repo_id="yanranxiaoxi/booxel", filename="BOOXEL-v0.Q.ckpt", local_dir="yanranxiaoxi_booxel", token=os.environ.get('MODEL_ACCESS_TOKEN'))
29
- hf_hub_download(repo_id="RunDiffusion/Juggernaut-XL-Lightning", filename="Juggernaut_RunDiffusionPhoto2_Lightning_4Steps.safetensors", local_dir="RunDiffusion_Juggernaut-XL-Lightning")
30
-
31
- parser = argparse.ArgumentParser()
32
- parser.add_argument("--opt", type=str, default='options/BOOXEL_v0.yaml')
33
- parser.add_argument("--ip", type=str, default='127.0.0.1')
34
- parser.add_argument("--port", type=int, default='6688')
35
- parser.add_argument("--no_llava", action='store_true', default=True)#False
36
- parser.add_argument("--use_image_slider", action='store_true', default=False)#False
37
- parser.add_argument("--log_history", action='store_true', default=False)
38
- parser.add_argument("--loading_half_params", action='store_true', default=False)#False
39
- parser.add_argument("--use_tile_vae", action='store_true', default=True)#False
40
- parser.add_argument("--encoder_tile_size", type=int, default=512)
41
- parser.add_argument("--decoder_tile_size", type=int, default=64)
42
- parser.add_argument("--load_8bit_llava", action='store_true', default=False)
43
- args = parser.parse_args()
44
-
45
- if torch.cuda.device_count() > 0:
46
- BOOXEL_device = 'cuda:0'
47
-
48
- # 加载 BOOXEL
49
- model, default_setting = create_BOOXEL_model(args.opt, BOOXEL_sign='Q', load_default_setting=True)
50
- if args.loading_half_params:
51
- model = model.half()
52
- if args.use_tile_vae:
53
- model.init_tile_vae(encoder_tile_size=args.encoder_tile_size, decoder_tile_size=args.decoder_tile_size)
54
- model = model.to(BOOXEL_device)
55
- model.first_stage_model.denoise_encoder_s1 = copy.deepcopy(model.first_stage_model.denoise_encoder)
56
- model.current_model = 'v0-Q'
57
- ckpt_Q, ckpt_F = load_QF_ckpt(args.opt)
58
-
59
- def check_upload(input_image):
60
- if input_image is None:
61
- raise gr.Error("请提供要处理的图像。")
62
- return gr.update(visible = True)
63
-
64
- def update_seed(is_randomize_seed, seed):
65
- if is_randomize_seed:
66
- return random.randint(0, max_64_bit_int)
67
- return seed
68
-
69
- def reset():
70
- return [
71
- None,
72
- 0,
73
- None,
74
- None,
75
- "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
76
- "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
77
- 1,
78
- 1024,
79
- 1,
80
- 2,
81
- 50,
82
- -1.0,
83
- 1.,
84
- default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
85
- True,
86
- random.randint(0, max_64_bit_int),
87
- 5,
88
- 1.003,
89
- "Wavelet",
90
- "fp32",
91
- "fp32",
92
- 1.0,
93
- True,
94
- False,
95
- default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
96
- 0.,
97
- "v0-Q",
98
- "input",
99
- 6
100
- ]
101
-
102
- def check(input_image):
103
- if input_image is None:
104
- raise gr.Error("请提供要处理的图像。")
105
-
106
- @spaces.GPU(duration=420)
107
- def stage1_process(
108
- input_image,
109
- gamma_correction,
110
- diff_dtype,
111
- ae_dtype
112
- ):
113
- print('stage1_process ==>>')
114
- if torch.cuda.device_count() == 0:
115
- gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
116
- return None, None
117
- torch.cuda.set_device(BOOXEL_device)
118
- LQ = HWC3(np.array(Image.open(input_image)))
119
- LQ = fix_resize(LQ, 512)
120
- # stage1
121
- LQ = np.array(LQ) / 255 * 2 - 1
122
- LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(BOOXEL_device)[:, :3, :, :]
123
-
124
- model.ae_dtype = convert_dtype(ae_dtype)
125
- model.model.dtype = convert_dtype(diff_dtype)
126
-
127
- LQ = model.batchify_denoise(LQ, is_stage1=True)
128
- LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
129
- # 伽玛校正
130
- LQ = LQ / 255.0
131
- LQ = np.power(LQ, gamma_correction)
132
- LQ *= 255.0
133
- LQ = LQ.round().clip(0, 255).astype(np.uint8)
134
- print('<<== stage1_process')
135
- return LQ, gr.update(visible = True)
136
-
137
- def stage2_process(*args, **kwargs):
138
- try:
139
- return restore_in_Xmin(*args, **kwargs)
140
- except Exception as e:
141
- print('异常的类型 ' + str(type(e)))
142
- if type(e).__name__ == "<class 'gradio.exceptions.Error'>":
143
- print('异常的名称 ' + type(e).__name__)
144
- raise e
145
-
146
- def restore_in_Xmin(
147
- noisy_image,
148
- rotation,
149
- denoise_image,
150
- prompt,
151
- a_prompt,
152
- n_prompt,
153
- num_samples,
154
- min_size,
155
- downscale,
156
- upscale,
157
- edm_steps,
158
- s_stage1,
159
- s_stage2,
160
- s_cfg,
161
- randomize_seed,
162
- seed,
163
- s_churn,
164
- s_noise,
165
- color_fix_type,
166
- diff_dtype,
167
- ae_dtype,
168
- gamma_correction,
169
- linear_CFG,
170
- linear_s_stage2,
171
- spt_linear_CFG,
172
- spt_linear_s_stage2,
173
- model_select,
174
- output_format,
175
- allocation
176
- ):
177
- print("noisy_image:\n" + str(noisy_image))
178
- print("denoise_image:\n" + str(denoise_image))
179
- print("rotation: " + str(rotation))
180
- print("prompt: " + str(prompt))
181
- print("a_prompt: " + str(a_prompt))
182
- print("n_prompt: " + str(n_prompt))
183
- print("num_samples: " + str(num_samples))
184
- print("min_size: " + str(min_size))
185
- print("downscale: " + str(downscale))
186
- print("upscale: " + str(upscale))
187
- print("edm_steps: " + str(edm_steps))
188
- print("s_stage1: " + str(s_stage1))
189
- print("s_stage2: " + str(s_stage2))
190
- print("s_cfg: " + str(s_cfg))
191
- print("randomize_seed: " + str(randomize_seed))
192
- print("seed: " + str(seed))
193
- print("s_churn: " + str(s_churn))
194
- print("s_noise: " + str(s_noise))
195
- print("color_fix_type: " + str(color_fix_type))
196
- print("diff_dtype: " + str(diff_dtype))
197
- print("ae_dtype: " + str(ae_dtype))
198
- print("gamma_correction: " + str(gamma_correction))
199
- print("linear_CFG: " + str(linear_CFG))
200
- print("linear_s_stage2: " + str(linear_s_stage2))
201
- print("spt_linear_CFG: " + str(spt_linear_CFG))
202
- print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
203
- print("model_select: " + str(model_select))
204
- print("GPU time allocation: " + str(allocation) + " min")
205
- print("output_format: " + str(output_format))
206
-
207
- input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
208
-
209
- if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
210
- gr.Warning('错误的图像格式。当前仅支持 *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp 或 *.heic。')
211
- return None, None, None, None
212
-
213
- if output_format == "input":
214
- if noisy_image is None:
215
- output_format = "png"
216
- else:
217
- output_format = input_format
218
- print("最终的 output_format:" + str(output_format))
219
-
220
- if prompt is None:
221
- prompt = ""
222
-
223
- if a_prompt is None:
224
- a_prompt = ""
225
-
226
- if n_prompt is None:
227
- n_prompt = ""
228
-
229
- if prompt != "" and a_prompt != "":
230
- a_prompt = prompt + ", " + a_prompt
231
- else:
232
- a_prompt = prompt + a_prompt
233
- print("最终提示词:" + str(a_prompt))
234
-
235
- denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
236
-
237
- if rotation == 90:
238
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
239
- elif rotation == 180:
240
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
241
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
242
- elif rotation == -90:
243
- denoise_image = np.array(list(zip(*denoise_image))[::-1])
244
-
245
- if 1 < downscale:
246
- input_height, input_width, input_channel = denoise_image.shape
247
- denoise_image = np.array(Image.fromarray(denoise_image).resize((input_width // downscale, input_height // downscale), Image.LANCZOS))
248
-
249
- denoise_image = HWC3(denoise_image)
250
-
251
- if torch.cuda.device_count() == 0:
252
- gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
253
- return [noisy_image, denoise_image], gr.update(label="可下载的结果为 *." + output_format + " 格式", format = output_format, value = [denoise_image]), None, gr.update(visible=True)
254
-
255
- if model_select != model.current_model:
256
- print('载入 ' + model_select)
257
- if model_select == 'v0-Q':
258
- model.load_state_dict(ckpt_Q, strict=False)
259
- elif model_select == 'v0-F':
260
- model.load_state_dict(ckpt_F, strict=False)
261
- model.current_model = model_select
262
-
263
- model.ae_dtype = convert_dtype(ae_dtype)
264
- model.model.dtype = convert_dtype(diff_dtype)
265
-
266
- # 分配
267
- if allocation == 1:
268
- return restore_in_1min(
269
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
270
- )
271
- if allocation == 2:
272
- return restore_in_2min(
273
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
274
- )
275
- if allocation == 3:
276
- return restore_in_3min(
277
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
278
- )
279
- if allocation == 4:
280
- return restore_in_4min(
281
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
282
- )
283
- if allocation == 5:
284
- return restore_in_5min(
285
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
286
- )
287
- if allocation == 7:
288
- return restore_in_7min(
289
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
290
- )
291
- if allocation == 8:
292
- return restore_in_8min(
293
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
294
- )
295
- if allocation == 9:
296
- return restore_in_9min(
297
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
298
- )
299
- if allocation == 10:
300
- return restore_in_10min(
301
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
302
- )
303
- else:
304
- return restore_in_6min(
305
- noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
306
- )
307
-
308
- @spaces.GPU(duration=59)
309
- def restore_in_1min(*args, **kwargs):
310
- return restore_on_gpu(*args, **kwargs)
311
-
312
- @spaces.GPU(duration=119)
313
- def restore_in_2min(*args, **kwargs):
314
- return restore_on_gpu(*args, **kwargs)
315
-
316
- @spaces.GPU(duration=179)
317
- def restore_in_3min(*args, **kwargs):
318
- return restore_on_gpu(*args, **kwargs)
319
-
320
- @spaces.GPU(duration=239)
321
- def restore_in_4min(*args, **kwargs):
322
- return restore_on_gpu(*args, **kwargs)
323
-
324
- @spaces.GPU(duration=299)
325
- def restore_in_5min(*args, **kwargs):
326
- return restore_on_gpu(*args, **kwargs)
327
-
328
- @spaces.GPU(duration=359)
329
- def restore_in_6min(*args, **kwargs):
330
- return restore_on_gpu(*args, **kwargs)
331
-
332
- @spaces.GPU(duration=419)
333
- def restore_in_7min(*args, **kwargs):
334
- return restore_on_gpu(*args, **kwargs)
335
-
336
- @spaces.GPU(duration=479)
337
- def restore_in_8min(*args, **kwargs):
338
- return restore_on_gpu(*args, **kwargs)
339
-
340
- @spaces.GPU(duration=539)
341
- def restore_in_9min(*args, **kwargs):
342
- return restore_on_gpu(*args, **kwargs)
343
-
344
- @spaces.GPU(duration=599)
345
- def restore_in_10min(*args, **kwargs):
346
- return restore_on_gpu(*args, **kwargs)
347
-
348
- def restore_on_gpu(
349
- noisy_image,
350
- input_image,
351
- prompt,
352
- a_prompt,
353
- n_prompt,
354
- num_samples,
355
- min_size,
356
- downscale,
357
- upscale,
358
- edm_steps,
359
- s_stage1,
360
- s_stage2,
361
- s_cfg,
362
- randomize_seed,
363
- seed,
364
- s_churn,
365
- s_noise,
366
- color_fix_type,
367
- diff_dtype,
368
- ae_dtype,
369
- gamma_correction,
370
- linear_CFG,
371
- linear_s_stage2,
372
- spt_linear_CFG,
373
- spt_linear_s_stage2,
374
- model_select,
375
- output_format,
376
- allocation
377
- ):
378
- start = time.time()
379
- print('restore ==>>')
380
-
381
- torch.cuda.set_device(BOOXEL_device)
382
-
383
- with torch.no_grad():
384
- input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
385
- LQ = np.array(input_image) / 255.0
386
- LQ = np.power(LQ, gamma_correction)
387
- LQ *= 255.0
388
- LQ = LQ.round().clip(0, 255).astype(np.uint8)
389
- LQ = LQ / 255 * 2 - 1
390
- LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(BOOXEL_device)[:, :3, :, :]
391
- captions = ['']
392
-
393
- samples = model.batchify_sample(LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
394
- s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
395
- num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
396
- use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
397
- cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2)
398
-
399
- x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
400
- 0, 255).astype(np.uint8)
401
- results = [x_samples[i] for i in range(num_samples)]
402
- torch.cuda.empty_cache()
403
-
404
- # 所有结果的大小相同
405
- input_height, input_width, input_channel = np.array(input_image).shape
406
- result_height, result_width, result_channel = np.array(results[0]).shape
407
-
408
- print('<<== restore')
409
- end = time.time()
410
- secondes = int(end - start)
411
- minutes = math.floor(secondes / 60)
412
- secondes = secondes - (minutes * 60)
413
- hours = math.floor(minutes / 60)
414
- minutes = minutes - (hours * 60)
415
- information = ("如果想获得不同的结果,请重新开始。" if randomize_seed else "") + \
416
- "如果您没有得到想要的图片,请在 « 图片描述 » 中添加更多细节。" + \
417
- "等待 " + str(allocation) + " 分钟以避免 GPU 配额处罚,或也可以使用另一台计算机。" + \
418
- "该图片已在 " + \
419
- ((str(hours) + " 小时 ") if hours != 0 else "") + \
420
- ((str(minutes) + " 分钟 ") if hours != 0 or minutes != 0 else "") + \
421
- str(secondes) + " 秒 内生成。" + \
422
- "新图像的分辨率为 " + str(result_width) + \
423
- " 像素宽, " + str(result_height) + \
424
- " 像素高,最终总分辨率为 " + f'{result_width * result_height:,}' + " 像素。"
425
- print(information)
426
- try:
427
- print("初始分辨率:" + f'{input_width * input_height:,}')
428
- print("最终分辨率:" + f'{result_width * result_height:,}')
429
- print("edm_steps: " + str(edm_steps))
430
- print("num_samples: " + str(num_samples))
431
- print("缩小规模:" + str(downscale))
432
- print("预计分钟数:" + f'{(((result_width * result_height**(1/1.75)) * input_width * input_height * (edm_steps**(1/2)) * (num_samples**(1/2.5)))**(1/2.5)) / 25000:,}')
433
- except Exception as e:
434
- print('估算错误')
435
-
436
- # 滑动块中只能显示一张图像
437
- return [noisy_image] + [results[0]], gr.update(label="可下载的结果为 *." + output_format + " 格式", format = output_format, value = results), gr.update(value = information, visible = True), gr.update(visible=True)
438
-
439
- def load_and_reset(param_setting):
440
- print('load_and_reset ==>>')
441
- if torch.cuda.device_count() == 0:
442
- gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
443
- return None, None, None, None, None, None, None, None, None, None, None, None, None, None
444
- edm_steps = default_setting.edm_steps
445
- s_stage2 = 1.0
446
- s_stage1 = -1.0
447
- s_churn = 5
448
- s_noise = 1.003
449
- # 积极提示词
450
- a_prompt = '电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。'
451
- # 消极提示词
452
- n_prompt = '绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑'
453
- color_fix_type = 'Wavelet'
454
- spt_linear_s_stage2 = 0.0
455
- linear_s_stage2 = False
456
- linear_CFG = True
457
- if param_setting == "Quality":
458
- s_cfg = default_setting.s_cfg_Quality
459
- spt_linear_CFG = default_setting.spt_linear_CFG_Quality
460
- model_select = "v0-Q"
461
- elif param_setting == "Fidelity":
462
- s_cfg = default_setting.s_cfg_Fidelity
463
- spt_linear_CFG = default_setting.spt_linear_CFG_Fidelity
464
- model_select = "v0-F"
465
- else:
466
- raise NotImplementedError
467
- gr.Info('参数已重置。')
468
- print('<<== load_and_reset')
469
- return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
470
- linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
471
-
472
- def log_information(result_gallery):
473
- print('log_information')
474
- if result_gallery is not None:
475
- for i, result in enumerate(result_gallery):
476
- print(result[0])
477
-
478
- def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
479
- print('on_select_result')
480
- if result_gallery is not None:
481
- for i, result in enumerate(result_gallery):
482
- print(result[0])
483
- return [result_slider[0], result_gallery[evt.index][0]]
484
-
485
- # Gradio 接口
486
- with gr.Blocks() as interface:
487
- gr.Markdown("""
488
- # BOOXEL —— Boost Pixel!
489
-
490
- 提供你的提示词,借助先进的生成实验和模型放大的力量,获取非凡的逼真画面。
491
-
492
- 我们收集了一个包含 600 万张高分辨率、高质量图像的真实世界采集的数据集用于模型训练,每张图像都关联了清晰且详尽的描述性文本注释。
493
-
494
- 我们提供了使用文本提示操纵恢复图像的能力,此外,还引入了消极质量提示和恢复指导的采样方法,以进一步提高生成图像的质量和保真度。
495
- """)
496
-
497
- input_image = gr.Image(label="输入图像(*.png, *.webp, *.jpeg, *.jpg, *.gif, *.bmp, *.heic)", show_label=True, type="filepath", height=600, elem_id="image-input")
498
- rotation = gr.Radio([["不旋转", 0], ["⤵ 旋转 +90°", 90], ["↩ 旋转 180°", 180], ["⤴ 旋转 -90°", -90]], label="方向校正", info="在还原图像之前,将应用以下旋转功能;人工智能需要良好的定位才能理解内容", value=0, interactive=True, visible=False)
499
- with gr.Group():
500
- prompt = gr.Textbox(label="图像描述", info="帮助人工智能理解图像所代表的内容;尽可能多地描述,尤其是我们在原始图像上看不到的细节;可以用任何语言书写", value="", placeholder="长春,上午,秋天,英短蓝白猫,走在,花丛小径上,真实图像", lines=3)
501
- upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8], ["x9", 9], ["x10", 10]], label="像素放大倍率", info="1 到 10 倍放大倍率", value=2, interactive=True)
502
- allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5], ["6 min", 6], ["7 min", 7], ["8 min(不建议)", 8], ["9 min(不建议)", 9], ["10 min(不建议)", 10]], label="GPU 分配时间", info="设置为较低值可中止运行;设置为较高值后,下次运行会受到配额处罚", value=5, interactive=True)
503
-
504
- with gr.Accordion("预降噪(可选)", open=False):
505
- gamma_correction = gr.Slider(label="伽玛校正", info="较低的值图像将会更亮,反之亦然", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
506
- denoise_button = gr.Button(value="预降噪")
507
- denoise_image = gr.Image(label="降噪图像", show_label=True, type="filepath", sources=[], interactive = False, height=600, elem_id="image-s1")
508
- denoise_information = gr.HTML(value="如果存在,去噪图像将被用于修复,而不是输入图像。", visible=False)
509
-
510
- with gr.Accordion("高级选项", open=False):
511
- output_format = gr.Radio([["与输入一致", "input"], ["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="生成的图像格式", info="文件扩展名", value="input", interactive=True)
512
- a_prompt = gr.Textbox(label="补充图片说明",
513
- info="完整的主图像描述",
514
- value='电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。',
515
- lines=3)
516
- n_prompt = gr.Textbox(label="负面图像描述",
517
- info="通过列出图像不代表的内容来消除歧义",
518
- value='绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引��,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑',
519
- lines=3)
520
- edm_steps = gr.Slider(label="步骤数", info="较低的值生成将会更快;较高的值将会获得更多的细节", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
521
- num_samples = gr.Slider(label="生成数", info="生成的结果图像的数量", minimum=1, maximum=4 if not args.use_image_slider else 1
522
- , value=1, step=1)
523
- min_size = gr.Slider(label="最小尺寸", info="结果的最小高度和最小宽度", minimum=32, maximum=4096, value=1024, step=32)
524
- downscale = gr.Radio([["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4], ["/5", 5], ["/6", 6], ["/7", 7], ["/8", 8], ["/9", 9], ["/10", 10]], label="缩减前因数", info="减少图像模糊,缩短处理时间", value=1, interactive=True)
525
- with gr.Row():
526
- with gr.Column():
527
- model_select = gr.Radio([["质量 (v0-Q)", "v0-Q"], ["保真度 (v0-F)", "v0-F"]], label="模型选择", info="预训练模型", value="v0-Q",
528
- interactive=True)
529
- with gr.Column():
530
- color_fix_type = gr.Radio([["None", "None"], ["AdaIn (改进风格)", "AdaIn"], ["Wavelet (针对 JPEG 伪图象)", "Wavelet"]], label="色彩修复类型", info="AdaIn 改进画面风格;Wavelet 用于 JPEG 伪图像", value="Wavelet",
531
- interactive=True)
532
- s_cfg = gr.Slider(label="文本指导等级", info="较低的值将更加跟随源图像;较高的值将更加跟随提示", minimum=1.0, maximum=15.0,
533
- value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
534
- s_stage2 = gr.Slider(label="修复指导强度", minimum=0., maximum=1., value=1., step=0.05)
535
- s_stage1 = gr.Slider(label="预降噪指导强度", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
536
- s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
537
- s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
538
- with gr.Row():
539
- with gr.Column():
540
- linear_CFG = gr.Checkbox(label="线性 CFG", value=True)
541
- spt_linear_CFG = gr.Slider(label="CFG 起始", minimum=1.0,
542
- maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
543
- with gr.Column():
544
- linear_s_stage2 = gr.Checkbox(label="线性修复指导", value=False)
545
- spt_linear_s_stage2 = gr.Slider(label="指导起始", minimum=0.,
546
- maximum=1., value=0., step=0.05)
547
- with gr.Column():
548
- diff_dtype = gr.Radio([["fp32 (精确)", "fp32"], ["fp16 (中等)", "fp16"], ["bf16 (快速)", "bf16"]], label="扩散数据类型", value="fp32",
549
- interactive=True)
550
- with gr.Column():
551
- ae_dtype = gr.Radio([["fp32 (精确)", "fp32"], ["bf16 (快速)", "bf16"]], label="自动编码器数据类型", value="fp32",
552
- interactive=True)
553
- randomize_seed = gr.Checkbox(label = "\U0001F3B2 随机种子", value=True, info="如果选中,结果将总是不同")
554
- seed = gr.Slider(label="种子", minimum=0, maximum=max_64_bit_int, step=1, randomize=True)
555
- with gr.Group():
556
- param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="预配", value="Quality")
557
- restart_button = gr.Button(value="应用预配")
558
-
559
- with gr.Column():
560
- diffusion_button = gr.Button(value="开始处理", variant="primary", elem_id="process_button")
561
- reset_btn = gr.Button(value="重新初始化页面", variant="stop", elem_id="reset_button", visible=False)
562
-
563
- restore_information = gr.HTML(value="重启进程,获得另一个结果。", visible=False)
564
- result_slider = ImageSlider(label='对比结果', show_label=True, interactive=False, elem_id="slider1", show_download_button=False)
565
- result_gallery = gr.Gallery(label='可下载的结果', show_label=True, interactive=False, elem_id="gallery1")
566
-
567
- gr.Examples(
568
- examples = [
569
- [
570
- "./Examples/Example1.png",
571
- 0,
572
- None,
573
- "一群人,快乐地在街上行走,逼真,8K,极其精细",
574
- "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
575
- "绘画,油画,插图,绘��,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
576
- 2,
577
- 1024,
578
- 1,
579
- 8,
580
- 200,
581
- -1,
582
- 1,
583
- 7.5,
584
- False,
585
- 42,
586
- 5,
587
- 1.003,
588
- "AdaIn",
589
- "fp16",
590
- "bf16",
591
- 1.0,
592
- True,
593
- 4,
594
- False,
595
- 0.,
596
- "v0-Q",
597
- "input",
598
- 5
599
- ],
600
- [
601
- "./Examples/Example2.jpeg",
602
- 0,
603
- None,
604
- "一只虎斑猫的头部,在一间房子里,逼真,8K,极其细腻。",
605
- "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
606
- "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
607
- 1,
608
- 1024,
609
- 1,
610
- 1,
611
- 200,
612
- -1,
613
- 1,
614
- 7.5,
615
- False,
616
- 42,
617
- 5,
618
- 1.003,
619
- "Wavelet",
620
- "fp16",
621
- "bf16",
622
- 1.0,
623
- True,
624
- 4,
625
- False,
626
- 0.,
627
- "v0-Q",
628
- "input",
629
- 4
630
- ],
631
- [
632
- "./Examples/Example3.webp",
633
- 0,
634
- None,
635
- "一个红色的苹果",
636
- "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
637
- "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
638
- 1,
639
- 1024,
640
- 1,
641
- 1,
642
- 200,
643
- -1,
644
- 1,
645
- 7.5,
646
- False,
647
- 42,
648
- 5,
649
- 1.003,
650
- "Wavelet",
651
- "fp16",
652
- "bf16",
653
- 1.0,
654
- True,
655
- 4,
656
- False,
657
- 0.,
658
- "v0-Q",
659
- "input",
660
- 4
661
- ],
662
- [
663
- "./Examples/Example3.webp",
664
- 0,
665
- None,
666
- "一块红色大理石",
667
- "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
668
- "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
669
- 1,
670
- 1024,
671
- 1,
672
- 1,
673
- 200,
674
- -1,
675
- 1,
676
- 7.5,
677
- False,
678
- 42,
679
- 5,
680
- 1.003,
681
- "Wavelet",
682
- "fp16",
683
- "bf16",
684
- 1.0,
685
- True,
686
- 4,
687
- False,
688
- 0.,
689
- "v0-Q",
690
- "input",
691
- 4
692
- ],
693
- ],
694
- run_on_click = True,
695
- fn = stage2_process,
696
- inputs = [
697
- input_image,
698
- rotation,
699
- denoise_image,
700
- prompt,
701
- a_prompt,
702
- n_prompt,
703
- num_samples,
704
- min_size,
705
- downscale,
706
- upscale,
707
- edm_steps,
708
- s_stage1,
709
- s_stage2,
710
- s_cfg,
711
- randomize_seed,
712
- seed,
713
- s_churn,
714
- s_noise,
715
- color_fix_type,
716
- diff_dtype,
717
- ae_dtype,
718
- gamma_correction,
719
- linear_CFG,
720
- linear_s_stage2,
721
- spt_linear_CFG,
722
- spt_linear_s_stage2,
723
- model_select,
724
- output_format,
725
- allocation
726
- ],
727
- outputs = [
728
- result_slider,
729
- result_gallery,
730
- restore_information,
731
- reset_btn
732
- ],
733
- cache_examples = False,
734
- )
735
-
736
- input_image.upload(fn = check_upload, inputs = [
737
- input_image
738
- ], outputs = [
739
- rotation
740
- ], queue = False, show_progress = False)
741
-
742
- denoise_button.click(fn = check, inputs = [
743
- input_image
744
- ], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
745
- input_image,
746
- gamma_correction,
747
- diff_dtype,
748
- ae_dtype
749
- ], outputs=[
750
- denoise_image,
751
- denoise_information
752
- ])
753
-
754
- diffusion_button.click(fn = update_seed, inputs = [
755
- randomize_seed,
756
- seed
757
- ], outputs = [
758
- seed
759
- ], queue = False, show_progress = False).then(fn = check, inputs = [
760
- input_image
761
- ], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
762
- input_image,
763
- rotation,
764
- denoise_image,
765
- prompt,
766
- a_prompt,
767
- n_prompt,
768
- num_samples,
769
- min_size,
770
- downscale,
771
- upscale,
772
- edm_steps,
773
- s_stage1,
774
- s_stage2,
775
- s_cfg,
776
- randomize_seed,
777
- seed,
778
- s_churn,
779
- s_noise,
780
- color_fix_type,
781
- diff_dtype,
782
- ae_dtype,
783
- gamma_correction,
784
- linear_CFG,
785
- linear_s_stage2,
786
- spt_linear_CFG,
787
- spt_linear_s_stage2,
788
- model_select,
789
- output_format,
790
- allocation
791
- ], outputs = [
792
- result_slider,
793
- result_gallery,
794
- restore_information,
795
- reset_btn
796
- ]).success(fn = log_information, inputs = [
797
- result_gallery
798
- ], outputs = [], queue = False, show_progress = False)
799
-
800
- result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
801
- result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
802
-
803
- restart_button.click(fn = load_and_reset, inputs = [
804
- param_setting
805
- ], outputs = [
806
- edm_steps,
807
- s_cfg,
808
- s_stage2,
809
- s_stage1,
810
- s_churn,
811
- s_noise,
812
- a_prompt,
813
- n_prompt,
814
- color_fix_type,
815
- linear_CFG,
816
- linear_s_stage2,
817
- spt_linear_CFG,
818
- spt_linear_s_stage2,
819
- model_select
820
- ])
821
-
822
- reset_btn.click(fn = reset, inputs = [], outputs = [
823
- input_image,
824
- rotation,
825
- denoise_image,
826
- prompt,
827
- a_prompt,
828
- n_prompt,
829
- num_samples,
830
- min_size,
831
- downscale,
832
- upscale,
833
- edm_steps,
834
- s_stage1,
835
- s_stage2,
836
- s_cfg,
837
- randomize_seed,
838
- seed,
839
- s_churn,
840
- s_noise,
841
- color_fix_type,
842
- diff_dtype,
843
- ae_dtype,
844
- gamma_correction,
845
- linear_CFG,
846
- linear_s_stage2,
847
- spt_linear_CFG,
848
- spt_linear_s_stage2,
849
- model_select,
850
- output_format,
851
- allocation
852
- ], queue = False, show_progress = False)
853
-
854
  interface.queue(10).launch()
 
1
+ import os
2
+ import gradio as gr
3
+ import argparse
4
+ import numpy as np
5
+ import torch
6
+ import einops
7
+ import copy
8
+ import math
9
+ import time
10
+ import random
11
+ import spaces
12
+ import re
13
+ import uuid
14
+
15
+ from gradio_imageslider import ImageSlider
16
+ from PIL import Image
17
+ from BOOXEL.util import HWC3, upscale_image, fix_resize, convert_dtype, create_BOOXEL_model, load_QF_ckpt
18
+ from huggingface_hub import hf_hub_download
19
+ from pillow_heif import register_heif_opener
20
+
21
+ register_heif_opener()
22
+
23
+ max_64_bit_int = np.iinfo(np.int32).max
24
+
25
+ hf_hub_download(repo_id="laion/CLIP-ViT-bigG-14-laion2B-39B-b160k", filename="open_clip_pytorch_model.bin", local_dir="laion_CLIP-ViT-bigG-14-laion2B-39B-b160k")
26
+ hf_hub_download(repo_id="ckpt/sd_xl_base_1.0", filename="sd_xl_base_1.0_0.9vae.safetensors", local_dir="ckpt_sd_xl_base_1.0")
27
+ hf_hub_download(repo_id="yanranxiaoxi/booxel", filename="BOOXEL-v0.F.ckpt", local_dir="yanranxiaoxi_booxel", token=os.environ.get('MODEL_ACCESS_TOKEN'))
28
+ hf_hub_download(repo_id="yanranxiaoxi/booxel", filename="BOOXEL-v0.Q.ckpt", local_dir="yanranxiaoxi_booxel", token=os.environ.get('MODEL_ACCESS_TOKEN'))
29
+ hf_hub_download(repo_id="RunDiffusion/Juggernaut-XL-Lightning", filename="Juggernaut_RunDiffusionPhoto2_Lightning_4Steps.safetensors", local_dir="RunDiffusion_Juggernaut-XL-Lightning")
30
+
31
+ parser = argparse.ArgumentParser()
32
+ parser.add_argument("--opt", type=str, default='options/BOOXEL_v0.yaml')
33
+ parser.add_argument("--ip", type=str, default='127.0.0.1')
34
+ parser.add_argument("--port", type=int, default='6688')
35
+ parser.add_argument("--no_llava", action='store_true', default=True)#False
36
+ parser.add_argument("--use_image_slider", action='store_true', default=False)#False
37
+ parser.add_argument("--log_history", action='store_true', default=False)
38
+ parser.add_argument("--loading_half_params", action='store_true', default=False)#False
39
+ parser.add_argument("--use_tile_vae", action='store_true', default=True)#False
40
+ parser.add_argument("--encoder_tile_size", type=int, default=512)
41
+ parser.add_argument("--decoder_tile_size", type=int, default=64)
42
+ parser.add_argument("--load_8bit_llava", action='store_true', default=False)
43
+ args = parser.parse_args()
44
+
45
+ if torch.cuda.device_count() > 0:
46
+ BOOXEL_device = 'cuda:0'
47
+
48
+ # 加载 BOOXEL
49
+ model, default_setting = create_BOOXEL_model(args.opt, BOOXEL_sign='Q', load_default_setting=True)
50
+ if args.loading_half_params:
51
+ model = model.half()
52
+ if args.use_tile_vae:
53
+ model.init_tile_vae(encoder_tile_size=args.encoder_tile_size, decoder_tile_size=args.decoder_tile_size)
54
+ model = model.to(BOOXEL_device)
55
+ model.first_stage_model.denoise_encoder_s1 = copy.deepcopy(model.first_stage_model.denoise_encoder)
56
+ model.current_model = 'v0-Q'
57
+ ckpt_Q, ckpt_F = load_QF_ckpt(args.opt)
58
+
59
+ def check_upload(input_image):
60
+ if input_image is None:
61
+ raise gr.Error("请提供要处理的图像。")
62
+ return gr.update(visible = True)
63
+
64
+ def update_seed(is_randomize_seed, seed):
65
+ if is_randomize_seed:
66
+ return random.randint(0, max_64_bit_int)
67
+ return seed
68
+
69
+ def reset():
70
+ return [
71
+ None,
72
+ 0,
73
+ None,
74
+ None,
75
+ "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
76
+ "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
77
+ 1,
78
+ 1024,
79
+ 1,
80
+ 2,
81
+ 50,
82
+ -1.0,
83
+ 1.,
84
+ default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
85
+ True,
86
+ random.randint(0, max_64_bit_int),
87
+ 5,
88
+ 1.003,
89
+ "Wavelet",
90
+ "fp32",
91
+ "fp32",
92
+ 1.0,
93
+ True,
94
+ False,
95
+ default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
96
+ 0.,
97
+ "v0-Q",
98
+ "input",
99
+ 6
100
+ ]
101
+
102
+ def check(input_image):
103
+ if input_image is None:
104
+ raise gr.Error("请提供要处理的图像。")
105
+
106
+ @spaces.GPU(duration=420)
107
+ def stage1_process(
108
+ input_image,
109
+ gamma_correction,
110
+ diff_dtype,
111
+ ae_dtype
112
+ ):
113
+ print('stage1_process ==>>')
114
+ # if torch.cuda.device_count() == 0:
115
+ # gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
116
+ # return None, None
117
+ torch.cuda.set_device(BOOXEL_device)
118
+ LQ = HWC3(np.array(Image.open(input_image)))
119
+ LQ = fix_resize(LQ, 512)
120
+ # stage1
121
+ LQ = np.array(LQ) / 255 * 2 - 1
122
+ LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(BOOXEL_device)[:, :3, :, :]
123
+
124
+ model.ae_dtype = convert_dtype(ae_dtype)
125
+ model.model.dtype = convert_dtype(diff_dtype)
126
+
127
+ LQ = model.batchify_denoise(LQ, is_stage1=True)
128
+ LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
129
+ # 伽玛校正
130
+ LQ = LQ / 255.0
131
+ LQ = np.power(LQ, gamma_correction)
132
+ LQ *= 255.0
133
+ LQ = LQ.round().clip(0, 255).astype(np.uint8)
134
+ print('<<== stage1_process')
135
+ return LQ, gr.update(visible = True)
136
+
137
+ def stage2_process(*args, **kwargs):
138
+ try:
139
+ return restore_in_Xmin(*args, **kwargs)
140
+ except Exception as e:
141
+ print('异常的类型 ' + str(type(e)))
142
+ if type(e).__name__ == "<class 'gradio.exceptions.Error'>":
143
+ print('异常的名称 ' + type(e).__name__)
144
+ raise e
145
+
146
+ def restore_in_Xmin(
147
+ noisy_image,
148
+ rotation,
149
+ denoise_image,
150
+ prompt,
151
+ a_prompt,
152
+ n_prompt,
153
+ num_samples,
154
+ min_size,
155
+ downscale,
156
+ upscale,
157
+ edm_steps,
158
+ s_stage1,
159
+ s_stage2,
160
+ s_cfg,
161
+ randomize_seed,
162
+ seed,
163
+ s_churn,
164
+ s_noise,
165
+ color_fix_type,
166
+ diff_dtype,
167
+ ae_dtype,
168
+ gamma_correction,
169
+ linear_CFG,
170
+ linear_s_stage2,
171
+ spt_linear_CFG,
172
+ spt_linear_s_stage2,
173
+ model_select,
174
+ output_format,
175
+ allocation
176
+ ):
177
+ print("noisy_image:\n" + str(noisy_image))
178
+ print("denoise_image:\n" + str(denoise_image))
179
+ print("rotation: " + str(rotation))
180
+ print("prompt: " + str(prompt))
181
+ print("a_prompt: " + str(a_prompt))
182
+ print("n_prompt: " + str(n_prompt))
183
+ print("num_samples: " + str(num_samples))
184
+ print("min_size: " + str(min_size))
185
+ print("downscale: " + str(downscale))
186
+ print("upscale: " + str(upscale))
187
+ print("edm_steps: " + str(edm_steps))
188
+ print("s_stage1: " + str(s_stage1))
189
+ print("s_stage2: " + str(s_stage2))
190
+ print("s_cfg: " + str(s_cfg))
191
+ print("randomize_seed: " + str(randomize_seed))
192
+ print("seed: " + str(seed))
193
+ print("s_churn: " + str(s_churn))
194
+ print("s_noise: " + str(s_noise))
195
+ print("color_fix_type: " + str(color_fix_type))
196
+ print("diff_dtype: " + str(diff_dtype))
197
+ print("ae_dtype: " + str(ae_dtype))
198
+ print("gamma_correction: " + str(gamma_correction))
199
+ print("linear_CFG: " + str(linear_CFG))
200
+ print("linear_s_stage2: " + str(linear_s_stage2))
201
+ print("spt_linear_CFG: " + str(spt_linear_CFG))
202
+ print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
203
+ print("model_select: " + str(model_select))
204
+ print("GPU time allocation: " + str(allocation) + " min")
205
+ print("output_format: " + str(output_format))
206
+
207
+ input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
208
+
209
+ if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
210
+ gr.Warning('错误的图像格式。当前仅支持 *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp 或 *.heic。')
211
+ return None, None, None, None
212
+
213
+ if output_format == "input":
214
+ if noisy_image is None:
215
+ output_format = "png"
216
+ else:
217
+ output_format = input_format
218
+ print("最终的 output_format:" + str(output_format))
219
+
220
+ if prompt is None:
221
+ prompt = ""
222
+
223
+ if a_prompt is None:
224
+ a_prompt = ""
225
+
226
+ if n_prompt is None:
227
+ n_prompt = ""
228
+
229
+ if prompt != "" and a_prompt != "":
230
+ a_prompt = prompt + ", " + a_prompt
231
+ else:
232
+ a_prompt = prompt + a_prompt
233
+ print("最终提示词:" + str(a_prompt))
234
+
235
+ denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
236
+
237
+ if rotation == 90:
238
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
239
+ elif rotation == 180:
240
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
241
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
242
+ elif rotation == -90:
243
+ denoise_image = np.array(list(zip(*denoise_image))[::-1])
244
+
245
+ if 1 < downscale:
246
+ input_height, input_width, input_channel = denoise_image.shape
247
+ denoise_image = np.array(Image.fromarray(denoise_image).resize((input_width // downscale, input_height // downscale), Image.LANCZOS))
248
+
249
+ denoise_image = HWC3(denoise_image)
250
+
251
+ # if torch.cuda.device_count() == 0:
252
+ # gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
253
+ # return [noisy_image, denoise_image], gr.update(label="可下载的结果为 *." + output_format + " 格式", format = output_format, value = [denoise_image]), None, gr.update(visible=True)
254
+
255
+ if model_select != model.current_model:
256
+ print('载入 ' + model_select)
257
+ if model_select == 'v0-Q':
258
+ model.load_state_dict(ckpt_Q, strict=False)
259
+ elif model_select == 'v0-F':
260
+ model.load_state_dict(ckpt_F, strict=False)
261
+ model.current_model = model_select
262
+
263
+ model.ae_dtype = convert_dtype(ae_dtype)
264
+ model.model.dtype = convert_dtype(diff_dtype)
265
+
266
+ # 分配
267
+ if allocation == 1:
268
+ return restore_in_1min(
269
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
270
+ )
271
+ if allocation == 2:
272
+ return restore_in_2min(
273
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
274
+ )
275
+ if allocation == 3:
276
+ return restore_in_3min(
277
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
278
+ )
279
+ if allocation == 4:
280
+ return restore_in_4min(
281
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
282
+ )
283
+ if allocation == 5:
284
+ return restore_in_5min(
285
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
286
+ )
287
+ if allocation == 7:
288
+ return restore_in_7min(
289
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
290
+ )
291
+ if allocation == 8:
292
+ return restore_in_8min(
293
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
294
+ )
295
+ if allocation == 9:
296
+ return restore_in_9min(
297
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
298
+ )
299
+ if allocation == 10:
300
+ return restore_in_10min(
301
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
302
+ )
303
+ else:
304
+ return restore_in_6min(
305
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
306
+ )
307
+
308
+ @spaces.GPU(duration=59)
309
+ def restore_in_1min(*args, **kwargs):
310
+ return restore_on_gpu(*args, **kwargs)
311
+
312
+ @spaces.GPU(duration=119)
313
+ def restore_in_2min(*args, **kwargs):
314
+ return restore_on_gpu(*args, **kwargs)
315
+
316
+ @spaces.GPU(duration=179)
317
+ def restore_in_3min(*args, **kwargs):
318
+ return restore_on_gpu(*args, **kwargs)
319
+
320
+ @spaces.GPU(duration=239)
321
+ def restore_in_4min(*args, **kwargs):
322
+ return restore_on_gpu(*args, **kwargs)
323
+
324
+ @spaces.GPU(duration=299)
325
+ def restore_in_5min(*args, **kwargs):
326
+ return restore_on_gpu(*args, **kwargs)
327
+
328
+ @spaces.GPU(duration=359)
329
+ def restore_in_6min(*args, **kwargs):
330
+ return restore_on_gpu(*args, **kwargs)
331
+
332
+ @spaces.GPU(duration=419)
333
+ def restore_in_7min(*args, **kwargs):
334
+ return restore_on_gpu(*args, **kwargs)
335
+
336
+ @spaces.GPU(duration=479)
337
+ def restore_in_8min(*args, **kwargs):
338
+ return restore_on_gpu(*args, **kwargs)
339
+
340
+ @spaces.GPU(duration=539)
341
+ def restore_in_9min(*args, **kwargs):
342
+ return restore_on_gpu(*args, **kwargs)
343
+
344
+ @spaces.GPU(duration=599)
345
+ def restore_in_10min(*args, **kwargs):
346
+ return restore_on_gpu(*args, **kwargs)
347
+
348
+ def restore_on_gpu(
349
+ noisy_image,
350
+ input_image,
351
+ prompt,
352
+ a_prompt,
353
+ n_prompt,
354
+ num_samples,
355
+ min_size,
356
+ downscale,
357
+ upscale,
358
+ edm_steps,
359
+ s_stage1,
360
+ s_stage2,
361
+ s_cfg,
362
+ randomize_seed,
363
+ seed,
364
+ s_churn,
365
+ s_noise,
366
+ color_fix_type,
367
+ diff_dtype,
368
+ ae_dtype,
369
+ gamma_correction,
370
+ linear_CFG,
371
+ linear_s_stage2,
372
+ spt_linear_CFG,
373
+ spt_linear_s_stage2,
374
+ model_select,
375
+ output_format,
376
+ allocation
377
+ ):
378
+ start = time.time()
379
+ print('restore ==>>')
380
+
381
+ torch.cuda.set_device(BOOXEL_device)
382
+
383
+ with torch.no_grad():
384
+ input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
385
+ LQ = np.array(input_image) / 255.0
386
+ LQ = np.power(LQ, gamma_correction)
387
+ LQ *= 255.0
388
+ LQ = LQ.round().clip(0, 255).astype(np.uint8)
389
+ LQ = LQ / 255 * 2 - 1
390
+ LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(BOOXEL_device)[:, :3, :, :]
391
+ captions = ['']
392
+
393
+ samples = model.batchify_sample(LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
394
+ s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
395
+ num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
396
+ use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
397
+ cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2)
398
+
399
+ x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
400
+ 0, 255).astype(np.uint8)
401
+ results = [x_samples[i] for i in range(num_samples)]
402
+ torch.cuda.empty_cache()
403
+
404
+ # 所有结果的大小相同
405
+ input_height, input_width, input_channel = np.array(input_image).shape
406
+ result_height, result_width, result_channel = np.array(results[0]).shape
407
+
408
+ print('<<== restore')
409
+ end = time.time()
410
+ secondes = int(end - start)
411
+ minutes = math.floor(secondes / 60)
412
+ secondes = secondes - (minutes * 60)
413
+ hours = math.floor(minutes / 60)
414
+ minutes = minutes - (hours * 60)
415
+ information = ("如果想获得不同的结果,请重新开始。" if randomize_seed else "") + \
416
+ "如果您没有得到想要的图片,请在 « 图片描述 » 中添加更多细节。" + \
417
+ "等待 " + str(allocation) + " 分钟以避免 GPU 配额处罚,或也可以使用另一台计算机。" + \
418
+ "该图片已在 " + \
419
+ ((str(hours) + " 小时 ") if hours != 0 else "") + \
420
+ ((str(minutes) + " 分钟 ") if hours != 0 or minutes != 0 else "") + \
421
+ str(secondes) + " 秒 内生成。" + \
422
+ "新图像的分辨率为 " + str(result_width) + \
423
+ " 像素宽, " + str(result_height) + \
424
+ " 像素高,最终总分辨率为 " + f'{result_width * result_height:,}' + " 像素。"
425
+ print(information)
426
+ try:
427
+ print("初始分辨率:" + f'{input_width * input_height:,}')
428
+ print("最终分辨率:" + f'{result_width * result_height:,}')
429
+ print("edm_steps: " + str(edm_steps))
430
+ print("num_samples: " + str(num_samples))
431
+ print("缩小规模:" + str(downscale))
432
+ print("预计分钟数:" + f'{(((result_width * result_height**(1/1.75)) * input_width * input_height * (edm_steps**(1/2)) * (num_samples**(1/2.5)))**(1/2.5)) / 25000:,}')
433
+ except Exception as e:
434
+ print('估算错误')
435
+
436
+ # 滑动块中只能显示一张图像
437
+ return [noisy_image] + [results[0]], gr.update(label="可下载的结果为 *." + output_format + " 格式", format = output_format, value = results), gr.update(value = information, visible = True), gr.update(visible=True)
438
+
439
+ def load_and_reset(param_setting):
440
+ print('load_and_reset ==>>')
441
+ # if torch.cuda.device_count() == 0:
442
+ # gr.Warning('将此 Spaces 设置为 GPU 配置以使其正常工作。')
443
+ # return None, None, None, None, None, None, None, None, None, None, None, None, None, None
444
+ edm_steps = default_setting.edm_steps
445
+ s_stage2 = 1.0
446
+ s_stage1 = -1.0
447
+ s_churn = 5
448
+ s_noise = 1.003
449
+ # 积极提示词
450
+ a_prompt = '电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。'
451
+ # 消极提示词
452
+ n_prompt = '绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑'
453
+ color_fix_type = 'Wavelet'
454
+ spt_linear_s_stage2 = 0.0
455
+ linear_s_stage2 = False
456
+ linear_CFG = True
457
+ if param_setting == "Quality":
458
+ s_cfg = default_setting.s_cfg_Quality
459
+ spt_linear_CFG = default_setting.spt_linear_CFG_Quality
460
+ model_select = "v0-Q"
461
+ elif param_setting == "Fidelity":
462
+ s_cfg = default_setting.s_cfg_Fidelity
463
+ spt_linear_CFG = default_setting.spt_linear_CFG_Fidelity
464
+ model_select = "v0-F"
465
+ else:
466
+ raise NotImplementedError
467
+ gr.Info('参数已重置。')
468
+ print('<<== load_and_reset')
469
+ return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
470
+ linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
471
+
472
+ def log_information(result_gallery):
473
+ print('log_information')
474
+ if result_gallery is not None:
475
+ for i, result in enumerate(result_gallery):
476
+ print(result[0])
477
+
478
+ def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
479
+ print('on_select_result')
480
+ if result_gallery is not None:
481
+ for i, result in enumerate(result_gallery):
482
+ print(result[0])
483
+ return [result_slider[0], result_gallery[evt.index][0]]
484
+
485
+ # Gradio 接口
486
+ with gr.Blocks() as interface:
487
+ gr.Markdown("""
488
+ # BOOXEL —— Boost Pixel!
489
+
490
+ 提供你的提示词,借助先进的生成实验和模型放大的力量,获取非凡的逼真画面。
491
+
492
+ 我们收集了一个包含 600 万张高分辨率、高质量图像的真实世界采集的数据集用于模型训练,每张图像都关联了清晰且详尽的描述性文本注释。
493
+
494
+ 我们提供了使用文本提示操纵恢复图像的能力,此外,还引入了消极质量提示和恢复指导的采样方法,以进一步提高生成图像的质量和保真度。
495
+ """)
496
+
497
+ input_image = gr.Image(label="输入图像(*.png, *.webp, *.jpeg, *.jpg, *.gif, *.bmp, *.heic)", show_label=True, type="filepath", height=600, elem_id="image-input")
498
+ rotation = gr.Radio([["不旋转", 0], ["⤵ 旋转 +90°", 90], ["↩ 旋转 180°", 180], ["⤴ 旋转 -90°", -90]], label="方向校正", info="在还原图像之前,将应用以下旋转功能;人工智能需要良好的定位才能理解内容", value=0, interactive=True, visible=False)
499
+ with gr.Group():
500
+ prompt = gr.Textbox(label="图像描述", info="帮助人工智能理解图像所代表的内容;尽可能多地描述,尤其是我们在原始图像上看不到的细节;可以用任何语言书写", value="", placeholder="长春,上午,秋天,英短蓝白猫,走在,花丛小径上,真实图像", lines=3)
501
+ upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8], ["x9", 9], ["x10", 10]], label="像素放大倍率", info="1 到 10 倍放大倍率", value=2, interactive=True)
502
+ allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5], ["6 min", 6], ["7 min", 7], ["8 min(不建议)", 8], ["9 min(不建议)", 9], ["10 min(不建议)", 10]], label="GPU 分配时间", info="设置为较低值可中止运行;设置为较高值后,下次运行会受到配额处罚", value=5, interactive=True)
503
+
504
+ with gr.Accordion("预降噪(可选)", open=False):
505
+ gamma_correction = gr.Slider(label="伽玛校正", info="较低的值图像将会更亮,反之亦然", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
506
+ denoise_button = gr.Button(value="预降噪")
507
+ denoise_image = gr.Image(label="降噪图像", show_label=True, type="filepath", sources=[], interactive = False, height=600, elem_id="image-s1")
508
+ denoise_information = gr.HTML(value="如果存在,去噪图像将被用于修复,而不是输入图像。", visible=False)
509
+
510
+ with gr.Accordion("高级选项", open=False):
511
+ output_format = gr.Radio([["与输入一致", "input"], ["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="生成的图像格式", info="文件扩展名", value="input", interactive=True)
512
+ a_prompt = gr.Textbox(label="补充图片说明",
513
+ info="完整的主图像描述",
514
+ value='电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。',
515
+ lines=3)
516
+ n_prompt = gr.Textbox(label="负面图像描述",
517
+ info="通过列出图像不代表的内容来消除歧义",
518
+ value='绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑',
519
+ lines=3)
520
+ edm_steps = gr.Slider(label="步骤数", info="较低的值生成将会更快;较高的值将会获得更多的细节", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
521
+ num_samples = gr.Slider(label="生成数", info="生成的结果图像的数量", minimum=1, maximum=4 if not args.use_image_slider else 1
522
+ , value=1, step=1)
523
+ min_size = gr.Slider(label="最小尺寸", info="结果的最小高度和最小宽度", minimum=32, maximum=4096, value=1024, step=32)
524
+ downscale = gr.Radio([["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4], ["/5", 5], ["/6", 6], ["/7", 7], ["/8", 8], ["/9", 9], ["/10", 10]], label="缩减前因数", info="减少图像模糊,缩短处理时间", value=1, interactive=True)
525
+ with gr.Row():
526
+ with gr.Column():
527
+ model_select = gr.Radio([["质量 (v0-Q)", "v0-Q"], ["保真度 (v0-F)", "v0-F"]], label="模型选择", info="预训练模型", value="v0-Q",
528
+ interactive=True)
529
+ with gr.Column():
530
+ color_fix_type = gr.Radio([["None", "None"], ["AdaIn (改进风格)", "AdaIn"], ["Wavelet (针对 JPEG 伪图象)", "Wavelet"]], label="色彩修复类型", info="AdaIn 改进画面风格;Wavelet 用于 JPEG 伪图像", value="Wavelet",
531
+ interactive=True)
532
+ s_cfg = gr.Slider(label="文本指导等级", info="较低的值将更加跟随源图像;较高的值将更加跟随提示", minimum=1.0, maximum=15.0,
533
+ value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
534
+ s_stage2 = gr.Slider(label="修复指导强度", minimum=0., maximum=1., value=1., step=0.05)
535
+ s_stage1 = gr.Slider(label="预降噪指导强度", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
536
+ s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
537
+ s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
538
+ with gr.Row():
539
+ with gr.Column():
540
+ linear_CFG = gr.Checkbox(label="线性 CFG", value=True)
541
+ spt_linear_CFG = gr.Slider(label="CFG 起始", minimum=1.0,
542
+ maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
543
+ with gr.Column():
544
+ linear_s_stage2 = gr.Checkbox(label="线性修复指导", value=False)
545
+ spt_linear_s_stage2 = gr.Slider(label="指导起始", minimum=0.,
546
+ maximum=1., value=0., step=0.05)
547
+ with gr.Column():
548
+ diff_dtype = gr.Radio([["fp32 (精确)", "fp32"], ["fp16 (中等)", "fp16"], ["bf16 (快速)", "bf16"]], label="扩散数据类型", value="fp32",
549
+ interactive=True)
550
+ with gr.Column():
551
+ ae_dtype = gr.Radio([["fp32 (精确)", "fp32"], ["bf16 (快速)", "bf16"]], label="自动编码器数据类型", value="fp32",
552
+ interactive=True)
553
+ randomize_seed = gr.Checkbox(label = "\U0001F3B2 随机种子", value=True, info="如果选中,结果将总是不同")
554
+ seed = gr.Slider(label="种子", minimum=0, maximum=max_64_bit_int, step=1, randomize=True)
555
+ with gr.Group():
556
+ param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="预配", value="Quality")
557
+ restart_button = gr.Button(value="应用预配")
558
+
559
+ with gr.Column():
560
+ diffusion_button = gr.Button(value="开始处理", variant="primary", elem_id="process_button")
561
+ reset_btn = gr.Button(value="重新初始化页面", variant="stop", elem_id="reset_button", visible=False)
562
+
563
+ restore_information = gr.HTML(value="重启进程,获得另一个结果。", visible=False)
564
+ result_slider = ImageSlider(label='对比结果', show_label=True, interactive=False, elem_id="slider1", show_download_button=False)
565
+ result_gallery = gr.Gallery(label='可下载的结果', show_label=True, interactive=False, elem_id="gallery1")
566
+
567
+ gr.Examples(
568
+ examples = [
569
+ [
570
+ "./Examples/Example1.png",
571
+ 0,
572
+ None,
573
+ "一群人,快乐地在街上行走,逼真,8K,极其精细",
574
+ "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
575
+ "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
576
+ 2,
577
+ 1024,
578
+ 1,
579
+ 8,
580
+ 200,
581
+ -1,
582
+ 1,
583
+ 7.5,
584
+ False,
585
+ 42,
586
+ 5,
587
+ 1.003,
588
+ "AdaIn",
589
+ "fp16",
590
+ "bf16",
591
+ 1.0,
592
+ True,
593
+ 4,
594
+ False,
595
+ 0.,
596
+ "v0-Q",
597
+ "input",
598
+ 5
599
+ ],
600
+ [
601
+ "./Examples/Example2.jpeg",
602
+ 0,
603
+ None,
604
+ "一只虎斑猫的头部,在一间房子里,逼真,8K,极其细腻。",
605
+ "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
606
+ "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG ��格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
607
+ 1,
608
+ 1024,
609
+ 1,
610
+ 1,
611
+ 200,
612
+ -1,
613
+ 1,
614
+ 7.5,
615
+ False,
616
+ 42,
617
+ 5,
618
+ 1.003,
619
+ "Wavelet",
620
+ "fp16",
621
+ "bf16",
622
+ 1.0,
623
+ True,
624
+ 4,
625
+ False,
626
+ 0.,
627
+ "v0-Q",
628
+ "input",
629
+ 4
630
+ ],
631
+ [
632
+ "./Examples/Example3.webp",
633
+ 0,
634
+ None,
635
+ "一个红色的苹果",
636
+ "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
637
+ "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
638
+ 1,
639
+ 1024,
640
+ 1,
641
+ 1,
642
+ 200,
643
+ -1,
644
+ 1,
645
+ 7.5,
646
+ False,
647
+ 42,
648
+ 5,
649
+ 1.003,
650
+ "Wavelet",
651
+ "fp16",
652
+ "bf16",
653
+ 1.0,
654
+ True,
655
+ 4,
656
+ False,
657
+ 0.,
658
+ "v0-Q",
659
+ "input",
660
+ 4
661
+ ],
662
+ [
663
+ "./Examples/Example3.webp",
664
+ 0,
665
+ None,
666
+ "一块红色大理石",
667
+ "电影级,高对比度,高度精细,使用哈苏相机拍摄,超精细照片,逼真的最大细节,32K,调色,超高清,极致的细节,皮肤毛孔细节,超清晰度,完美无变形。",
668
+ "绘画,油画,插图,绘图,艺术,素描,动漫,卡通,CG 风格,3D 渲染,虚幻引擎,模糊,混色,不清晰,怪异纹理,丑陋,肮脏,凌乱,质量最差,质量低,框架,水印,签名,JPEG 伪影,变形,低分辨率,过度平滑",
669
+ 1,
670
+ 1024,
671
+ 1,
672
+ 1,
673
+ 200,
674
+ -1,
675
+ 1,
676
+ 7.5,
677
+ False,
678
+ 42,
679
+ 5,
680
+ 1.003,
681
+ "Wavelet",
682
+ "fp16",
683
+ "bf16",
684
+ 1.0,
685
+ True,
686
+ 4,
687
+ False,
688
+ 0.,
689
+ "v0-Q",
690
+ "input",
691
+ 4
692
+ ],
693
+ ],
694
+ run_on_click = True,
695
+ fn = stage2_process,
696
+ inputs = [
697
+ input_image,
698
+ rotation,
699
+ denoise_image,
700
+ prompt,
701
+ a_prompt,
702
+ n_prompt,
703
+ num_samples,
704
+ min_size,
705
+ downscale,
706
+ upscale,
707
+ edm_steps,
708
+ s_stage1,
709
+ s_stage2,
710
+ s_cfg,
711
+ randomize_seed,
712
+ seed,
713
+ s_churn,
714
+ s_noise,
715
+ color_fix_type,
716
+ diff_dtype,
717
+ ae_dtype,
718
+ gamma_correction,
719
+ linear_CFG,
720
+ linear_s_stage2,
721
+ spt_linear_CFG,
722
+ spt_linear_s_stage2,
723
+ model_select,
724
+ output_format,
725
+ allocation
726
+ ],
727
+ outputs = [
728
+ result_slider,
729
+ result_gallery,
730
+ restore_information,
731
+ reset_btn
732
+ ],
733
+ cache_examples = False,
734
+ )
735
+
736
+ input_image.upload(fn = check_upload, inputs = [
737
+ input_image
738
+ ], outputs = [
739
+ rotation
740
+ ], queue = False, show_progress = False)
741
+
742
+ denoise_button.click(fn = check, inputs = [
743
+ input_image
744
+ ], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
745
+ input_image,
746
+ gamma_correction,
747
+ diff_dtype,
748
+ ae_dtype
749
+ ], outputs=[
750
+ denoise_image,
751
+ denoise_information
752
+ ])
753
+
754
+ diffusion_button.click(fn = update_seed, inputs = [
755
+ randomize_seed,
756
+ seed
757
+ ], outputs = [
758
+ seed
759
+ ], queue = False, show_progress = False).then(fn = check, inputs = [
760
+ input_image
761
+ ], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
762
+ input_image,
763
+ rotation,
764
+ denoise_image,
765
+ prompt,
766
+ a_prompt,
767
+ n_prompt,
768
+ num_samples,
769
+ min_size,
770
+ downscale,
771
+ upscale,
772
+ edm_steps,
773
+ s_stage1,
774
+ s_stage2,
775
+ s_cfg,
776
+ randomize_seed,
777
+ seed,
778
+ s_churn,
779
+ s_noise,
780
+ color_fix_type,
781
+ diff_dtype,
782
+ ae_dtype,
783
+ gamma_correction,
784
+ linear_CFG,
785
+ linear_s_stage2,
786
+ spt_linear_CFG,
787
+ spt_linear_s_stage2,
788
+ model_select,
789
+ output_format,
790
+ allocation
791
+ ], outputs = [
792
+ result_slider,
793
+ result_gallery,
794
+ restore_information,
795
+ reset_btn
796
+ ]).success(fn = log_information, inputs = [
797
+ result_gallery
798
+ ], outputs = [], queue = False, show_progress = False)
799
+
800
+ result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
801
+ result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
802
+
803
+ restart_button.click(fn = load_and_reset, inputs = [
804
+ param_setting
805
+ ], outputs = [
806
+ edm_steps,
807
+ s_cfg,
808
+ s_stage2,
809
+ s_stage1,
810
+ s_churn,
811
+ s_noise,
812
+ a_prompt,
813
+ n_prompt,
814
+ color_fix_type,
815
+ linear_CFG,
816
+ linear_s_stage2,
817
+ spt_linear_CFG,
818
+ spt_linear_s_stage2,
819
+ model_select
820
+ ])
821
+
822
+ reset_btn.click(fn = reset, inputs = [], outputs = [
823
+ input_image,
824
+ rotation,
825
+ denoise_image,
826
+ prompt,
827
+ a_prompt,
828
+ n_prompt,
829
+ num_samples,
830
+ min_size,
831
+ downscale,
832
+ upscale,
833
+ edm_steps,
834
+ s_stage1,
835
+ s_stage2,
836
+ s_cfg,
837
+ randomize_seed,
838
+ seed,
839
+ s_churn,
840
+ s_noise,
841
+ color_fix_type,
842
+ diff_dtype,
843
+ ae_dtype,
844
+ gamma_correction,
845
+ linear_CFG,
846
+ linear_s_stage2,
847
+ spt_linear_CFG,
848
+ spt_linear_s_stage2,
849
+ model_select,
850
+ output_format,
851
+ allocation
852
+ ], queue = False, show_progress = False)
853
+
854
  interface.queue(10).launch()