jon-pascal
commited on
Commit
•
932d159
1
Parent(s):
9e438f2
Update app.py
Browse filessimplified UI for Maree
app.py
CHANGED
@@ -32,11 +32,11 @@ parser = argparse.ArgumentParser()
|
|
32 |
parser.add_argument("--opt", type=str, default='options/SUPIR_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)
|
@@ -59,7 +59,22 @@ if torch.cuda.device_count() > 0:
|
|
59 |
def check_upload(input_image):
|
60 |
if input_image is None:
|
61 |
raise gr.Error("Please provide an image to restore.")
|
62 |
-
return gr.update(visible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
def update_seed(is_randomize_seed, seed):
|
65 |
if is_randomize_seed:
|
@@ -68,8 +83,6 @@ def update_seed(is_randomize_seed, seed):
|
|
68 |
|
69 |
def reset():
|
70 |
return [
|
71 |
-
None,
|
72 |
-
0,
|
73 |
None,
|
74 |
None,
|
75 |
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
@@ -95,7 +108,6 @@ def reset():
|
|
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 |
|
@@ -103,59 +115,15 @@ def check(input_image):
|
|
103 |
if input_image is None:
|
104 |
raise gr.Error("Please provide an image to restore.")
|
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('Set this space to GPU config to make it work.')
|
116 |
-
return None, None
|
117 |
-
torch.cuda.set_device(SUPIR_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(SUPIR_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 |
-
# gamma correction
|
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 |
-
|
142 |
-
print("gradio.exceptions.Error 'No GPU is currently available for you after 60s'")
|
143 |
-
print('str(type(e)): ' + str(type(e))) # <class 'gradio.exceptions.Error'>
|
144 |
-
print('str(e): ' + str(e)) # You have exceeded your GPU quota...
|
145 |
-
try:
|
146 |
-
print('e.message: ' + e.message) # No GPU is currently available for you after 60s
|
147 |
-
except Exception as e2:
|
148 |
-
print('Failure')
|
149 |
-
if str(e).startswith("No GPU is currently available for you after 60s"):
|
150 |
-
print('Exception identified!!!')
|
151 |
-
#if str(type(e)) == "<class 'gradio.exceptions.Error'>":
|
152 |
-
#print('Exception of name ' + type(e).__name__)
|
153 |
raise e
|
154 |
|
155 |
def restore_in_Xmin(
|
156 |
noisy_image,
|
157 |
-
rotation,
|
158 |
-
denoise_image,
|
159 |
prompt,
|
160 |
a_prompt,
|
161 |
n_prompt,
|
@@ -180,51 +148,16 @@ def restore_in_Xmin(
|
|
180 |
spt_linear_CFG,
|
181 |
spt_linear_s_stage2,
|
182 |
model_select,
|
183 |
-
output_format,
|
184 |
allocation
|
185 |
):
|
186 |
-
print("
|
187 |
-
print("denoise_image:\n" + str(denoise_image))
|
188 |
-
print("rotation: " + str(rotation))
|
189 |
-
print("prompt: " + str(prompt))
|
190 |
-
print("a_prompt: " + str(a_prompt))
|
191 |
-
print("n_prompt: " + str(n_prompt))
|
192 |
-
print("num_samples: " + str(num_samples))
|
193 |
-
print("min_size: " + str(min_size))
|
194 |
-
print("downscale: " + str(downscale))
|
195 |
-
print("upscale: " + str(upscale))
|
196 |
-
print("edm_steps: " + str(edm_steps))
|
197 |
-
print("s_stage1: " + str(s_stage1))
|
198 |
-
print("s_stage2: " + str(s_stage2))
|
199 |
-
print("s_cfg: " + str(s_cfg))
|
200 |
-
print("randomize_seed: " + str(randomize_seed))
|
201 |
-
print("seed: " + str(seed))
|
202 |
-
print("s_churn: " + str(s_churn))
|
203 |
-
print("s_noise: " + str(s_noise))
|
204 |
-
print("color_fix_type: " + str(color_fix_type))
|
205 |
-
print("diff_dtype: " + str(diff_dtype))
|
206 |
-
print("ae_dtype: " + str(ae_dtype))
|
207 |
-
print("gamma_correction: " + str(gamma_correction))
|
208 |
-
print("linear_CFG: " + str(linear_CFG))
|
209 |
-
print("linear_s_stage2: " + str(linear_s_stage2))
|
210 |
-
print("spt_linear_CFG: " + str(spt_linear_CFG))
|
211 |
-
print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
|
212 |
-
print("model_select: " + str(model_select))
|
213 |
-
print("GPU time allocation: " + str(allocation) + " min")
|
214 |
-
print("output_format: " + str(output_format))
|
215 |
-
|
216 |
input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
|
217 |
|
218 |
-
if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
|
219 |
-
gr.Warning('Invalid image format. Please
|
220 |
return None, None, None, None
|
221 |
|
222 |
-
|
223 |
-
if noisy_image is None:
|
224 |
-
output_format = "png"
|
225 |
-
else:
|
226 |
-
output_format = input_format
|
227 |
-
print("final output_format: " + str(output_format))
|
228 |
|
229 |
if prompt is None:
|
230 |
prompt = ""
|
@@ -241,15 +174,7 @@ def restore_in_Xmin(
|
|
241 |
a_prompt = prompt + a_prompt
|
242 |
print("Final prompt: " + str(a_prompt))
|
243 |
|
244 |
-
denoise_image = np.array(Image.open(noisy_image
|
245 |
-
|
246 |
-
if rotation == 90:
|
247 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
248 |
-
elif rotation == 180:
|
249 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
250 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
251 |
-
elif rotation == -90:
|
252 |
-
denoise_image = np.array(list(zip(*denoise_image))[::-1])
|
253 |
|
254 |
if 1 < downscale:
|
255 |
input_height, input_width, input_channel = denoise_image.shape
|
@@ -259,10 +184,10 @@ def restore_in_Xmin(
|
|
259 |
|
260 |
if torch.cuda.device_count() == 0:
|
261 |
gr.Warning('Set this space to GPU config to make it work.')
|
262 |
-
return [noisy_image, denoise_image], gr.update(label="Downloadable results
|
263 |
|
264 |
if model_select != model.current_model:
|
265 |
-
print('
|
266 |
if model_select == 'v0-Q':
|
267 |
model.load_state_dict(ckpt_Q, strict=False)
|
268 |
elif model_select == 'v0-F':
|
@@ -273,46 +198,26 @@ def restore_in_Xmin(
|
|
273 |
model.model.dtype = convert_dtype(diff_dtype)
|
274 |
|
275 |
# Allocation
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
if allocation == 7:
|
297 |
-
return restore_in_7min(
|
298 |
-
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
|
299 |
-
)
|
300 |
-
if allocation == 8:
|
301 |
-
return restore_in_8min(
|
302 |
-
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
|
303 |
-
)
|
304 |
-
if allocation == 9:
|
305 |
-
return restore_in_9min(
|
306 |
-
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
|
307 |
-
)
|
308 |
-
if allocation == 10:
|
309 |
-
return restore_in_10min(
|
310 |
-
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
|
311 |
-
)
|
312 |
-
else:
|
313 |
-
return restore_in_6min(
|
314 |
-
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
|
315 |
-
)
|
316 |
|
317 |
@spaces.GPU(duration=59)
|
318 |
def restore_in_1min(*args, **kwargs):
|
@@ -385,7 +290,7 @@ def restore_on_gpu(
|
|
385 |
allocation
|
386 |
):
|
387 |
start = time.time()
|
388 |
-
print('
|
389 |
|
390 |
torch.cuda.set_device(SUPIR_device)
|
391 |
|
@@ -399,22 +304,23 @@ def restore_on_gpu(
|
|
399 |
LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
|
400 |
captions = ['']
|
401 |
|
402 |
-
samples = model.batchify_sample(
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
|
|
|
|
407 |
|
408 |
x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
|
409 |
0, 255).astype(np.uint8)
|
410 |
results = [x_samples[i] for i in range(num_samples)]
|
411 |
torch.cuda.empty_cache()
|
412 |
|
413 |
-
# All the results have the same size
|
414 |
input_height, input_width, input_channel = np.array(input_image).shape
|
415 |
result_height, result_width, result_channel = np.array(results[0]).shape
|
416 |
|
417 |
-
print('
|
418 |
end = time.time()
|
419 |
secondes = int(end - start)
|
420 |
minutes = math.floor(secondes / 60)
|
@@ -422,34 +328,16 @@ def restore_on_gpu(
|
|
422 |
hours = math.floor(minutes / 60)
|
423 |
minutes = minutes - (hours * 60)
|
424 |
information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
|
425 |
-
|
426 |
-
"Wait " + str(allocation) + " min before a new run to avoid quota penalty or use another computer. " + \
|
427 |
-
"The image" + (" has" if len(results) == 1 else "s have") + " been generated in " + \
|
428 |
-
((str(hours) + " h, ") if hours != 0 else "") + \
|
429 |
-
((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + \
|
430 |
-
str(secondes) + " sec. " + \
|
431 |
-
"The new image resolution is " + str(result_width) + \
|
432 |
-
" pixels large and " + str(result_height) + \
|
433 |
-
" pixels high, so a resolution of " + f'{result_width * result_height:,}' + " pixels."
|
434 |
-
print(information)
|
435 |
-
try:
|
436 |
-
print("Initial resolution: " + f'{input_width * input_height:,}')
|
437 |
-
print("Final resolution: " + f'{result_width * result_height:,}')
|
438 |
-
print("edm_steps: " + str(edm_steps))
|
439 |
-
print("num_samples: " + str(num_samples))
|
440 |
-
print("downscale: " + str(downscale))
|
441 |
-
print("Estimated minutes: " + 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:,}')
|
442 |
-
except Exception as e:
|
443 |
-
print('Exception of Estimation')
|
444 |
|
445 |
# Only one image can be shown in the slider
|
446 |
-
return [noisy_image] + [results[0]], gr.update(label="Downloadable results
|
447 |
|
448 |
def load_and_reset(param_setting):
|
449 |
-
print('
|
450 |
if torch.cuda.device_count() == 0:
|
451 |
gr.Warning('Set this space to GPU config to make it work.')
|
452 |
-
return None, None, None, None, None, None, None, None, None, None, None, None, None
|
453 |
edm_steps = default_setting.edm_steps
|
454 |
s_stage2 = 1.0
|
455 |
s_stage1 = -1.0
|
@@ -476,277 +364,279 @@ def load_and_reset(param_setting):
|
|
476 |
else:
|
477 |
raise NotImplementedError
|
478 |
gr.Info('The parameters are reset.')
|
479 |
-
print('
|
480 |
return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
|
481 |
linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
|
482 |
|
483 |
def log_information(result_gallery):
|
484 |
-
print('
|
485 |
if result_gallery is not None:
|
486 |
for i, result in enumerate(result_gallery):
|
487 |
print(result[0])
|
488 |
|
489 |
def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
|
490 |
-
print('
|
491 |
if result_gallery is not None:
|
492 |
for i, result in enumerate(result_gallery):
|
493 |
print(result[0])
|
494 |
return [result_slider[0], result_gallery[evt.index][0]]
|
495 |
|
496 |
title_html = """
|
497 |
-
<h1><center>
|
498 |
-
<big><center>Upscale your images up to x10 freely, without account, without watermark and download it</center></big>
|
499 |
-
<center><big><big>🤸<big><big><big><big><big><big>🤸</big></big></big></big></big></big></big></big></center>
|
500 |
-
|
501 |
-
<p>This is an online demo of SUPIR, a practicing model scaling for photo-realistic image restoration.
|
502 |
-
The content added by SUPIR is <b><u>imagination, not real-world information</u></b>.
|
503 |
-
SUPIR is for beauty and illustration only.
|
504 |
-
Most of the processes last few minutes.
|
505 |
-
If you want to upscale AI-generated images, be noticed that <i>PixArt Sigma</i> space can directly generate 5984x5984 images.
|
506 |
-
Due to Gradio issues, the generated image is slightly less satured than the original.
|
507 |
-
Please leave a <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">message in discussion</a> if you encounter issues.
|
508 |
-
You can also use <a href="https://huggingface.co/spaces/gokaygokay/AuraSR">AuraSR</a> to upscale x4.
|
509 |
-
|
510 |
-
<p><center><a href="https://arxiv.org/abs/2401.13627">Paper</a>   <a href="http://supir.xpixel.group/">Project Page</a>   <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a></center></p>
|
511 |
-
<p><center><a style="display:inline-block" href='https://github.com/Fanghua-Yu/SUPIR'><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Fanghua-Yu/SUPIR?style=social"></a></center></p>
|
512 |
"""
|
513 |
|
514 |
-
|
515 |
-
claim_md = """
|
516 |
-
## **Piracy**
|
517 |
-
The images are not stored but the logs are saved during a month.
|
518 |
-
## **How to get SUPIR**
|
519 |
-
You can get SUPIR on HuggingFace by [duplicating this space](https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true) and set GPU.
|
520 |
-
You can also install SUPIR on your computer following [this tutorial](https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai).
|
521 |
-
You can install _Pinokio_ on your computer and then install _SUPIR_ into it. It should be quite easy if you have an Nvidia GPU.
|
522 |
-
## **Terms of use**
|
523 |
-
By using this service, users are required to agree to the following terms: The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research. Please submit a feedback to us if you get any inappropriate answer! We will collect those to keep improving our models. For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
|
524 |
-
## **License**
|
525 |
-
The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/Fanghua-Yu/SUPIR) of SUPIR.
|
526 |
-
"""
|
527 |
-
|
528 |
# Gradio interface
|
529 |
with gr.Blocks() as interface:
|
530 |
if torch.cuda.device_count() == 0:
|
531 |
with gr.Row():
|
532 |
gr.HTML("""
|
533 |
-
<p style="background-color: red;"><big><big><big><b>⚠️To use
|
534 |
-
|
535 |
-
You can't use SUPIR directly here because this space runs on a CPU, which is not enough for SUPIR. Please provide <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">feedback</a> if you have issues.
|
536 |
-
</big></big></big></p>
|
537 |
""")
|
538 |
gr.HTML(title_html)
|
539 |
|
540 |
-
input_image = gr.Image(label="
|
541 |
-
rotation = gr.Radio([["No rotation", 0], ["⤵ Rotate +90°", 90], ["↩ Return 180°", 180], ["⤴ Rotate -90°", -90]], label="Orientation correction", info="Will apply the following rotation before restoring the image; the AI needs a good orientation to understand the content", value=0, interactive=True, visible=False)
|
542 |
with gr.Group():
|
543 |
-
prompt = gr.Textbox(
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
554 |
|
555 |
with gr.Accordion("Advanced options", open=False):
|
556 |
-
a_prompt = gr.Textbox(
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
575 |
with gr.Row():
|
576 |
with gr.Column():
|
577 |
-
model_select = gr.Radio(
|
578 |
-
|
|
|
|
|
|
|
|
|
|
|
579 |
with gr.Column():
|
580 |
-
color_fix_type = gr.Radio(
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
588 |
with gr.Row():
|
589 |
with gr.Column():
|
590 |
linear_CFG = gr.Checkbox(label="Linear CFG", value=True)
|
591 |
-
spt_linear_CFG = gr.Slider(
|
592 |
-
|
|
|
|
|
|
|
|
|
|
|
593 |
with gr.Column():
|
594 |
linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
|
595 |
-
spt_linear_s_stage2 = gr.Slider(
|
596 |
-
|
|
|
|
|
|
|
|
|
|
|
597 |
with gr.Column():
|
598 |
-
diff_dtype = gr.Radio(
|
599 |
-
|
|
|
|
|
|
|
|
|
600 |
with gr.Column():
|
601 |
-
ae_dtype = gr.Radio(
|
602 |
-
|
603 |
-
|
604 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
605 |
with gr.Group():
|
606 |
-
param_setting = gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
607 |
restart_button = gr.Button(value="Apply presetting")
|
608 |
|
609 |
with gr.Column():
|
610 |
-
diffusion_button = gr.Button(
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
"fp16",
|
671 |
-
"bf16",
|
672 |
-
1.0,
|
673 |
-
True,
|
674 |
-
4,
|
675 |
-
False,
|
676 |
-
0.,
|
677 |
-
"v0-Q",
|
678 |
-
"input",
|
679 |
-
4
|
680 |
-
],
|
681 |
-
[
|
682 |
-
"./Examples/Example3.webp",
|
683 |
-
0,
|
684 |
-
None,
|
685 |
-
"A red apple",
|
686 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
687 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
688 |
-
1,
|
689 |
-
1024,
|
690 |
-
1,
|
691 |
-
1,
|
692 |
-
200,
|
693 |
-
-1,
|
694 |
-
1,
|
695 |
-
7.5,
|
696 |
-
False,
|
697 |
-
42,
|
698 |
-
5,
|
699 |
-
1.003,
|
700 |
-
"Wavelet",
|
701 |
-
"fp16",
|
702 |
-
"bf16",
|
703 |
-
1.0,
|
704 |
-
True,
|
705 |
-
4,
|
706 |
-
False,
|
707 |
-
0.,
|
708 |
-
"v0-Q",
|
709 |
-
"input",
|
710 |
-
4
|
711 |
-
],
|
712 |
-
[
|
713 |
-
"./Examples/Example3.webp",
|
714 |
-
0,
|
715 |
-
None,
|
716 |
-
"A red marble",
|
717 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
718 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
719 |
-
1,
|
720 |
-
1024,
|
721 |
-
1,
|
722 |
-
1,
|
723 |
-
200,
|
724 |
-
-1,
|
725 |
-
1,
|
726 |
-
7.5,
|
727 |
-
False,
|
728 |
-
42,
|
729 |
-
5,
|
730 |
-
1.003,
|
731 |
-
"Wavelet",
|
732 |
-
"fp16",
|
733 |
-
"bf16",
|
734 |
-
1.0,
|
735 |
-
True,
|
736 |
-
4,
|
737 |
-
False,
|
738 |
-
0.,
|
739 |
-
"v0-Q",
|
740 |
-
"input",
|
741 |
-
4
|
742 |
-
],
|
743 |
-
],
|
744 |
-
run_on_click = True,
|
745 |
-
fn = stage2_process,
|
746 |
-
inputs = [
|
747 |
input_image,
|
748 |
-
rotation,
|
749 |
-
denoise_image,
|
750 |
prompt,
|
751 |
a_prompt,
|
752 |
n_prompt,
|
@@ -765,117 +655,57 @@ with gr.Blocks() as interface:
|
|
765 |
color_fix_type,
|
766 |
diff_dtype,
|
767 |
ae_dtype,
|
768 |
-
gamma_correction
|
769 |
linear_CFG,
|
770 |
linear_s_stage2,
|
771 |
spt_linear_CFG,
|
772 |
spt_linear_s_stage2,
|
773 |
model_select,
|
774 |
-
output_format,
|
775 |
allocation
|
776 |
],
|
777 |
-
|
778 |
result_slider,
|
779 |
result_gallery,
|
780 |
restore_information,
|
781 |
reset_btn
|
782 |
-
]
|
783 |
-
|
|
|
|
|
|
|
|
|
|
|
784 |
)
|
785 |
|
786 |
-
with gr.Row():
|
787 |
-
gr.Markdown(claim_md)
|
788 |
-
|
789 |
-
input_image.upload(fn = check_upload, inputs = [
|
790 |
-
input_image
|
791 |
-
], outputs = [
|
792 |
-
rotation
|
793 |
-
], queue = False, show_progress = False)
|
794 |
-
|
795 |
-
denoise_button.click(fn = check, inputs = [
|
796 |
-
input_image
|
797 |
-
], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
|
798 |
-
input_image,
|
799 |
-
gamma_correction,
|
800 |
-
diff_dtype,
|
801 |
-
ae_dtype
|
802 |
-
], outputs=[
|
803 |
-
denoise_image,
|
804 |
-
denoise_information
|
805 |
-
])
|
806 |
-
|
807 |
-
diffusion_button.click(fn = update_seed, inputs = [
|
808 |
-
randomize_seed,
|
809 |
-
seed
|
810 |
-
], outputs = [
|
811 |
-
seed
|
812 |
-
], queue = False, show_progress = False).then(fn = check, inputs = [
|
813 |
-
input_image
|
814 |
-
], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
|
815 |
-
input_image,
|
816 |
-
rotation,
|
817 |
-
denoise_image,
|
818 |
-
prompt,
|
819 |
-
a_prompt,
|
820 |
-
n_prompt,
|
821 |
-
num_samples,
|
822 |
-
min_size,
|
823 |
-
downscale,
|
824 |
-
upscale,
|
825 |
-
edm_steps,
|
826 |
-
s_stage1,
|
827 |
-
s_stage2,
|
828 |
-
s_cfg,
|
829 |
-
randomize_seed,
|
830 |
-
seed,
|
831 |
-
s_churn,
|
832 |
-
s_noise,
|
833 |
-
color_fix_type,
|
834 |
-
diff_dtype,
|
835 |
-
ae_dtype,
|
836 |
-
gamma_correction,
|
837 |
-
linear_CFG,
|
838 |
-
linear_s_stage2,
|
839 |
-
spt_linear_CFG,
|
840 |
-
spt_linear_s_stage2,
|
841 |
-
model_select,
|
842 |
-
output_format,
|
843 |
-
allocation
|
844 |
-
], outputs = [
|
845 |
-
result_slider,
|
846 |
-
result_gallery,
|
847 |
-
restore_information,
|
848 |
-
reset_btn
|
849 |
-
]).success(fn = log_information, inputs = [
|
850 |
-
result_gallery
|
851 |
-
], outputs = [], queue = False, show_progress = False)
|
852 |
-
|
853 |
result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
|
854 |
result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
|
855 |
|
856 |
-
restart_button.click(
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
|
|
|
|
|
|
|
|
|
|
876 |
input_image,
|
877 |
-
rotation,
|
878 |
-
denoise_image,
|
879 |
prompt,
|
880 |
a_prompt,
|
881 |
n_prompt,
|
@@ -894,14 +724,16 @@ with gr.Blocks() as interface:
|
|
894 |
color_fix_type,
|
895 |
diff_dtype,
|
896 |
ae_dtype,
|
897 |
-
gamma_correction
|
898 |
linear_CFG,
|
899 |
linear_s_stage2,
|
900 |
spt_linear_CFG,
|
901 |
spt_linear_s_stage2,
|
902 |
model_select,
|
903 |
-
output_format,
|
904 |
allocation
|
905 |
-
],
|
906 |
-
|
|
|
|
|
|
|
907 |
interface.queue(10).launch()
|
|
|
32 |
parser.add_argument("--opt", type=str, default='options/SUPIR_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)
|
|
|
59 |
def check_upload(input_image):
|
60 |
if input_image is None:
|
61 |
raise gr.Error("Please provide an image to restore.")
|
62 |
+
return gr.update(visible=True)
|
63 |
+
|
64 |
+
def process_uploaded_image(image_path):
|
65 |
+
image = Image.open(image_path)
|
66 |
+
width, height = image.size
|
67 |
+
max_dim = max(width, height)
|
68 |
+
if max_dim > 1024:
|
69 |
+
if width > height:
|
70 |
+
new_width = 1024
|
71 |
+
new_height = int((1024 / width) * height)
|
72 |
+
else:
|
73 |
+
new_height = 1024
|
74 |
+
new_width = int((1024 / height) * width)
|
75 |
+
image = image.resize((new_width, new_height), Image.ANTIALIAS)
|
76 |
+
image.save(image_path)
|
77 |
+
return image_path
|
78 |
|
79 |
def update_seed(is_randomize_seed, seed):
|
80 |
if is_randomize_seed:
|
|
|
83 |
|
84 |
def reset():
|
85 |
return [
|
|
|
|
|
86 |
None,
|
87 |
None,
|
88 |
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
|
|
108 |
default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
|
109 |
0.,
|
110 |
"v0-Q",
|
|
|
111 |
6
|
112 |
]
|
113 |
|
|
|
115 |
if input_image is None:
|
116 |
raise gr.Error("Please provide an image to restore.")
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
def stage2_process(*args, **kwargs):
|
119 |
try:
|
120 |
return restore_in_Xmin(*args, **kwargs)
|
121 |
except Exception as e:
|
122 |
+
print(f"Exception occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
raise e
|
124 |
|
125 |
def restore_in_Xmin(
|
126 |
noisy_image,
|
|
|
|
|
127 |
prompt,
|
128 |
a_prompt,
|
129 |
n_prompt,
|
|
|
148 |
spt_linear_CFG,
|
149 |
spt_linear_s_stage2,
|
150 |
model_select,
|
|
|
151 |
allocation
|
152 |
):
|
153 |
+
print("Starting image restoration process...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
|
155 |
|
156 |
+
if input_format.lower() not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
|
157 |
+
gr.Warning('Invalid image format. Please use a supported image format.')
|
158 |
return None, None, None, None
|
159 |
|
160 |
+
output_format = "png"
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
if prompt is None:
|
163 |
prompt = ""
|
|
|
174 |
a_prompt = prompt + a_prompt
|
175 |
print("Final prompt: " + str(a_prompt))
|
176 |
|
177 |
+
denoise_image = np.array(Image.open(noisy_image))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
if 1 < downscale:
|
180 |
input_height, input_width, input_channel = denoise_image.shape
|
|
|
184 |
|
185 |
if torch.cuda.device_count() == 0:
|
186 |
gr.Warning('Set this space to GPU config to make it work.')
|
187 |
+
return [noisy_image, denoise_image], gr.update(label="Downloadable results", format=output_format, value=[denoise_image]), None, gr.update(visible=True)
|
188 |
|
189 |
if model_select != model.current_model:
|
190 |
+
print('Loading model: ' + model_select)
|
191 |
if model_select == 'v0-Q':
|
192 |
model.load_state_dict(ckpt_Q, strict=False)
|
193 |
elif model_select == 'v0-F':
|
|
|
198 |
model.model.dtype = convert_dtype(diff_dtype)
|
199 |
|
200 |
# Allocation
|
201 |
+
allocation_functions = {
|
202 |
+
1: restore_in_1min,
|
203 |
+
2: restore_in_2min,
|
204 |
+
3: restore_in_3min,
|
205 |
+
4: restore_in_4min,
|
206 |
+
5: restore_in_5min,
|
207 |
+
6: restore_in_6min,
|
208 |
+
7: restore_in_7min,
|
209 |
+
8: restore_in_8min,
|
210 |
+
9: restore_in_9min,
|
211 |
+
10: restore_in_10min,
|
212 |
+
}
|
213 |
+
|
214 |
+
restore_function = allocation_functions.get(allocation, restore_in_6min)
|
215 |
+
return restore_function(
|
216 |
+
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale,
|
217 |
+
edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type,
|
218 |
+
diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG,
|
219 |
+
spt_linear_s_stage2, model_select, output_format, allocation
|
220 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
@spaces.GPU(duration=59)
|
223 |
def restore_in_1min(*args, **kwargs):
|
|
|
290 |
allocation
|
291 |
):
|
292 |
start = time.time()
|
293 |
+
print('Starting GPU restoration...')
|
294 |
|
295 |
torch.cuda.set_device(SUPIR_device)
|
296 |
|
|
|
304 |
LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
|
305 |
captions = ['']
|
306 |
|
307 |
+
samples = model.batchify_sample(
|
308 |
+
LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
|
309 |
+
s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
|
310 |
+
num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
|
311 |
+
use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
|
312 |
+
cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2
|
313 |
+
)
|
314 |
|
315 |
x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
|
316 |
0, 255).astype(np.uint8)
|
317 |
results = [x_samples[i] for i in range(num_samples)]
|
318 |
torch.cuda.empty_cache()
|
319 |
|
|
|
320 |
input_height, input_width, input_channel = np.array(input_image).shape
|
321 |
result_height, result_width, result_channel = np.array(results[0]).shape
|
322 |
|
323 |
+
print('Restoration completed.')
|
324 |
end = time.time()
|
325 |
secondes = int(end - start)
|
326 |
minutes = math.floor(secondes / 60)
|
|
|
328 |
hours = math.floor(minutes / 60)
|
329 |
minutes = minutes - (hours * 60)
|
330 |
information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
|
331 |
+
"The image has been enhanced successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
|
333 |
# Only one image can be shown in the slider
|
334 |
+
return [noisy_image] + [results[0]], gr.update(label="Downloadable results", format=output_format, value=results), gr.update(value=information, visible=True), gr.update(visible=True)
|
335 |
|
336 |
def load_and_reset(param_setting):
|
337 |
+
print('Resetting parameters...')
|
338 |
if torch.cuda.device_count() == 0:
|
339 |
gr.Warning('Set this space to GPU config to make it work.')
|
340 |
+
return None, None, None, None, None, None, None, None, None, None, None, None, None
|
341 |
edm_steps = default_setting.edm_steps
|
342 |
s_stage2 = 1.0
|
343 |
s_stage1 = -1.0
|
|
|
364 |
else:
|
365 |
raise NotImplementedError
|
366 |
gr.Info('The parameters are reset.')
|
367 |
+
print('Parameters reset completed.')
|
368 |
return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
|
369 |
linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
|
370 |
|
371 |
def log_information(result_gallery):
|
372 |
+
print('Logging information...')
|
373 |
if result_gallery is not None:
|
374 |
for i, result in enumerate(result_gallery):
|
375 |
print(result[0])
|
376 |
|
377 |
def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
|
378 |
+
print('Result selected.')
|
379 |
if result_gallery is not None:
|
380 |
for i, result in enumerate(result_gallery):
|
381 |
print(result[0])
|
382 |
return [result_slider[0], result_gallery[evt.index][0]]
|
383 |
|
384 |
title_html = """
|
385 |
+
<h1><center>Maree's Magical Photo Tool</center></h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
386 |
"""
|
387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
# Gradio interface
|
389 |
with gr.Blocks() as interface:
|
390 |
if torch.cuda.device_count() == 0:
|
391 |
with gr.Row():
|
392 |
gr.HTML("""
|
393 |
+
<p style="background-color: red;"><big><big><big><b>⚠️To use this tool, set a GPU with sufficient VRAM.</b></big></big></big></p>
|
|
|
|
|
|
|
394 |
""")
|
395 |
gr.HTML(title_html)
|
396 |
|
397 |
+
input_image = gr.Image(label="Upload your photo", show_label=True, type="filepath", height=400, elem_id="image-input")
|
|
|
398 |
with gr.Group():
|
399 |
+
prompt = gr.Textbox(
|
400 |
+
label="Describe your photo",
|
401 |
+
info="Tell me about your photo so I can make it better.",
|
402 |
+
value="",
|
403 |
+
placeholder="Type a description...",
|
404 |
+
lines=3
|
405 |
+
)
|
406 |
+
upscale = gr.Radio(
|
407 |
+
[["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4]],
|
408 |
+
label="Upscale factor",
|
409 |
+
info="Choose how much to enlarge the photo",
|
410 |
+
value=2,
|
411 |
+
interactive=True
|
412 |
+
)
|
413 |
+
allocation = gr.Radio(
|
414 |
+
[["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5]],
|
415 |
+
label="GPU allocation time (for Jon)",
|
416 |
+
info="You can ignore this setting",
|
417 |
+
value=4,
|
418 |
+
interactive=True
|
419 |
+
)
|
420 |
|
421 |
with gr.Accordion("Advanced options", open=False):
|
422 |
+
a_prompt = gr.Textbox(
|
423 |
+
label="Additional image description",
|
424 |
+
info="Completes the main image description",
|
425 |
+
value='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
|
426 |
+
'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
|
427 |
+
'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
|
428 |
+
'hyper sharpness, perfect without deformations.',
|
429 |
+
lines=3
|
430 |
+
)
|
431 |
+
n_prompt = gr.Textbox(
|
432 |
+
label="Negative image description",
|
433 |
+
info="Disambiguate by listing what the image does NOT represent",
|
434 |
+
value='painting, oil painting, illustration, drawing, art, sketch, anime, '
|
435 |
+
'cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
|
436 |
+
'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
|
437 |
+
'deformed, lowres, over-smooth',
|
438 |
+
lines=3
|
439 |
+
)
|
440 |
+
edm_steps = gr.Slider(
|
441 |
+
label="Steps",
|
442 |
+
info="Lower=faster, higher=more details",
|
443 |
+
minimum=1,
|
444 |
+
maximum=200,
|
445 |
+
value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1,
|
446 |
+
step=1
|
447 |
+
)
|
448 |
+
num_samples = gr.Slider(
|
449 |
+
label="Num Samples",
|
450 |
+
info="Number of generated results",
|
451 |
+
minimum=1,
|
452 |
+
maximum=4 if not args.use_image_slider else 1,
|
453 |
+
value=1,
|
454 |
+
step=1
|
455 |
+
)
|
456 |
+
min_size = gr.Slider(
|
457 |
+
label="Minimum size",
|
458 |
+
info="Minimum height, minimum width of the result",
|
459 |
+
minimum=32,
|
460 |
+
maximum=4096,
|
461 |
+
value=1024,
|
462 |
+
step=32
|
463 |
+
)
|
464 |
+
downscale = gr.Radio(
|
465 |
+
[["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4]],
|
466 |
+
label="Pre-downscale factor",
|
467 |
+
info="Reducing blurred image reduces the process time",
|
468 |
+
value=1,
|
469 |
+
interactive=True
|
470 |
+
)
|
471 |
with gr.Row():
|
472 |
with gr.Column():
|
473 |
+
model_select = gr.Radio(
|
474 |
+
[["💃 Quality (v0-Q)", "v0-Q"], ["🎯 Fidelity (v0-F)", "v0-F"]],
|
475 |
+
label="Model Selection",
|
476 |
+
info="Pretrained model",
|
477 |
+
value="v0-Q",
|
478 |
+
interactive=True
|
479 |
+
)
|
480 |
with gr.Column():
|
481 |
+
color_fix_type = gr.Radio(
|
482 |
+
[["None", "None"], ["AdaIn (improve as a photo)", "AdaIn"], ["Wavelet (for JPEG artifacts)", "Wavelet"]],
|
483 |
+
label="Color-Fix Type",
|
484 |
+
info="AdaIn=Improve following a style, Wavelet=For JPEG artifacts",
|
485 |
+
value="AdaIn",
|
486 |
+
interactive=True
|
487 |
+
)
|
488 |
+
s_cfg = gr.Slider(
|
489 |
+
label="Text Guidance Scale",
|
490 |
+
info="Lower=follow the image, higher=follow the prompt",
|
491 |
+
minimum=1.0,
|
492 |
+
maximum=15.0,
|
493 |
+
value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
|
494 |
+
step=0.1
|
495 |
+
)
|
496 |
+
s_stage2 = gr.Slider(
|
497 |
+
label="Restoring Guidance Strength",
|
498 |
+
minimum=0.,
|
499 |
+
maximum=1.,
|
500 |
+
value=1.,
|
501 |
+
step=0.05
|
502 |
+
)
|
503 |
+
s_stage1 = gr.Slider(
|
504 |
+
label="Pre-denoising Guidance Strength",
|
505 |
+
minimum=-1.0,
|
506 |
+
maximum=6.0,
|
507 |
+
value=-1.0,
|
508 |
+
step=1.0
|
509 |
+
)
|
510 |
+
s_churn = gr.Slider(
|
511 |
+
label="S-Churn",
|
512 |
+
minimum=0,
|
513 |
+
maximum=40,
|
514 |
+
value=5,
|
515 |
+
step=1
|
516 |
+
)
|
517 |
+
s_noise = gr.Slider(
|
518 |
+
label="S-Noise",
|
519 |
+
minimum=1.0,
|
520 |
+
maximum=1.1,
|
521 |
+
value=1.003,
|
522 |
+
step=0.001
|
523 |
+
)
|
524 |
with gr.Row():
|
525 |
with gr.Column():
|
526 |
linear_CFG = gr.Checkbox(label="Linear CFG", value=True)
|
527 |
+
spt_linear_CFG = gr.Slider(
|
528 |
+
label="CFG Start",
|
529 |
+
minimum=1.0,
|
530 |
+
maximum=9.0,
|
531 |
+
value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
|
532 |
+
step=0.5
|
533 |
+
)
|
534 |
with gr.Column():
|
535 |
linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
|
536 |
+
spt_linear_s_stage2 = gr.Slider(
|
537 |
+
label="Guidance Start",
|
538 |
+
minimum=0.,
|
539 |
+
maximum=1.,
|
540 |
+
value=0.,
|
541 |
+
step=0.05
|
542 |
+
)
|
543 |
with gr.Column():
|
544 |
+
diff_dtype = gr.Radio(
|
545 |
+
[["fp32 (precision)", "fp32"], ["fp16 (medium)", "fp16"], ["bf16 (speed)", "bf16"]],
|
546 |
+
label="Diffusion Data Type",
|
547 |
+
value="fp32",
|
548 |
+
interactive=True
|
549 |
+
)
|
550 |
with gr.Column():
|
551 |
+
ae_dtype = gr.Radio(
|
552 |
+
[["fp32 (precision)", "fp32"], ["bf16 (speed)", "bf16"]],
|
553 |
+
label="Auto-Encoder Data Type",
|
554 |
+
value="fp32",
|
555 |
+
interactive=True
|
556 |
+
)
|
557 |
+
randomize_seed = gr.Checkbox(
|
558 |
+
label="\U0001F3B2 Randomize seed",
|
559 |
+
value=True,
|
560 |
+
info="If checked, result is always different"
|
561 |
+
)
|
562 |
+
seed = gr.Slider(
|
563 |
+
label="Seed",
|
564 |
+
minimum=0,
|
565 |
+
maximum=max_64_bit_int,
|
566 |
+
step=1,
|
567 |
+
randomize=True
|
568 |
+
)
|
569 |
with gr.Group():
|
570 |
+
param_setting = gr.Radio(
|
571 |
+
["Quality", "Fidelity"],
|
572 |
+
interactive=True,
|
573 |
+
label="Presetting",
|
574 |
+
value="Quality"
|
575 |
+
)
|
576 |
restart_button = gr.Button(value="Apply presetting")
|
577 |
|
578 |
with gr.Column():
|
579 |
+
diffusion_button = gr.Button(
|
580 |
+
value="🚀 Enhance Photo",
|
581 |
+
variant="primary",
|
582 |
+
elem_id="process_button"
|
583 |
+
)
|
584 |
+
reset_btn = gr.Button(
|
585 |
+
value="🧹 Reset",
|
586 |
+
variant="stop",
|
587 |
+
elem_id="reset_button",
|
588 |
+
visible=False
|
589 |
+
)
|
590 |
+
|
591 |
+
restore_information = gr.HTML(
|
592 |
+
value="Start the process again if you want a different result.",
|
593 |
+
visible=False
|
594 |
+
)
|
595 |
+
result_slider = ImageSlider(
|
596 |
+
label='Comparator',
|
597 |
+
show_label=False,
|
598 |
+
interactive=False,
|
599 |
+
elem_id="slider1",
|
600 |
+
show_download_button=False
|
601 |
+
)
|
602 |
+
result_gallery = gr.Gallery(
|
603 |
+
label='Downloadable results',
|
604 |
+
show_label=True,
|
605 |
+
interactive=False,
|
606 |
+
elem_id="gallery1"
|
607 |
+
)
|
608 |
+
|
609 |
+
input_image.upload(
|
610 |
+
fn=process_uploaded_image,
|
611 |
+
inputs=input_image,
|
612 |
+
outputs=input_image,
|
613 |
+
queue=False
|
614 |
+
)
|
615 |
+
|
616 |
+
input_image.upload(
|
617 |
+
fn=check_upload,
|
618 |
+
inputs=input_image,
|
619 |
+
outputs=[],
|
620 |
+
queue=False,
|
621 |
+
show_progress=False
|
622 |
+
)
|
623 |
+
|
624 |
+
diffusion_button.click(
|
625 |
+
fn=update_seed,
|
626 |
+
inputs=[randomize_seed, seed],
|
627 |
+
outputs=[seed],
|
628 |
+
queue=False,
|
629 |
+
show_progress=False
|
630 |
+
).then(
|
631 |
+
fn=check,
|
632 |
+
inputs=[input_image],
|
633 |
+
outputs=[],
|
634 |
+
queue=False,
|
635 |
+
show_progress=False
|
636 |
+
).success(
|
637 |
+
fn=stage2_process,
|
638 |
+
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
639 |
input_image,
|
|
|
|
|
640 |
prompt,
|
641 |
a_prompt,
|
642 |
n_prompt,
|
|
|
655 |
color_fix_type,
|
656 |
diff_dtype,
|
657 |
ae_dtype,
|
658 |
+
1.0, # gamma_correction
|
659 |
linear_CFG,
|
660 |
linear_s_stage2,
|
661 |
spt_linear_CFG,
|
662 |
spt_linear_s_stage2,
|
663 |
model_select,
|
|
|
664 |
allocation
|
665 |
],
|
666 |
+
outputs=[
|
667 |
result_slider,
|
668 |
result_gallery,
|
669 |
restore_information,
|
670 |
reset_btn
|
671 |
+
]
|
672 |
+
).success(
|
673 |
+
fn=log_information,
|
674 |
+
inputs=[result_gallery],
|
675 |
+
outputs=[],
|
676 |
+
queue=False,
|
677 |
+
show_progress=False
|
678 |
)
|
679 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
680 |
result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
|
681 |
result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
|
682 |
|
683 |
+
restart_button.click(
|
684 |
+
fn=load_and_reset,
|
685 |
+
inputs=[param_setting],
|
686 |
+
outputs=[
|
687 |
+
edm_steps,
|
688 |
+
s_cfg,
|
689 |
+
s_stage2,
|
690 |
+
s_stage1,
|
691 |
+
s_churn,
|
692 |
+
s_noise,
|
693 |
+
a_prompt,
|
694 |
+
n_prompt,
|
695 |
+
color_fix_type,
|
696 |
+
linear_CFG,
|
697 |
+
linear_s_stage2,
|
698 |
+
spt_linear_CFG,
|
699 |
+
spt_linear_s_stage2,
|
700 |
+
model_select
|
701 |
+
]
|
702 |
+
)
|
703 |
+
|
704 |
+
reset_btn.click(
|
705 |
+
fn=reset,
|
706 |
+
inputs=[],
|
707 |
+
outputs=[
|
708 |
input_image,
|
|
|
|
|
709 |
prompt,
|
710 |
a_prompt,
|
711 |
n_prompt,
|
|
|
724 |
color_fix_type,
|
725 |
diff_dtype,
|
726 |
ae_dtype,
|
727 |
+
1.0, # gamma_correction
|
728 |
linear_CFG,
|
729 |
linear_s_stage2,
|
730 |
spt_linear_CFG,
|
731 |
spt_linear_s_stage2,
|
732 |
model_select,
|
|
|
733 |
allocation
|
734 |
+
],
|
735 |
+
queue=False,
|
736 |
+
show_progress=False
|
737 |
+
)
|
738 |
+
|
739 |
interface.queue(10).launch()
|