Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ torch.set_float32_matmul_precision("highest")
|
|
| 32 |
'''
|
| 33 |
|
| 34 |
DESCRIPTIONXX = """
|
| 35 |
-
## ⚡⚡⚡⚡ REALVISXL V5.0 BF16 (Tester
|
| 36 |
"""
|
| 37 |
|
| 38 |
examples = [
|
|
@@ -273,43 +273,167 @@ def generate_30(
|
|
| 273 |
height: int = 768,
|
| 274 |
guidance_scale: float = 4,
|
| 275 |
num_inference_steps: int = 125,
|
|
|
|
|
|
|
| 276 |
use_resolution_binning: bool = True,
|
| 277 |
progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
|
| 278 |
):
|
| 279 |
seed = random.randint(0, MAX_SEED)
|
| 280 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 281 |
-
options = {
|
| 282 |
-
"prompt": [prompt],
|
| 283 |
-
"negative_prompt": [negative_prompt],
|
| 284 |
-
"negative_prompt_2": [neg_prompt_2],
|
| 285 |
-
"width": width,
|
| 286 |
-
"height": height,
|
| 287 |
-
"guidance_scale": guidance_scale,
|
| 288 |
-
"num_inference_steps": num_inference_steps,
|
| 289 |
-
"generator": generator,
|
| 290 |
-
"output_type": "pil",
|
| 291 |
-
"callback_on_step_end": pyx.scheduler_swap_callback
|
| 292 |
-
}
|
| 293 |
-
if use_resolution_binning:
|
| 294 |
-
options["use_resolution_binning"] = True
|
| 295 |
images = []
|
| 296 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 297 |
filename = pyx.uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
|
| 298 |
#upload_to_ftp(filename)
|
| 299 |
pyx.upload_to_ftp(filename)
|
| 300 |
#uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 314 |
os.symlink(sd_image_path, unique_name)
|
| 315 |
return [unique_name]
|
|
@@ -490,6 +614,22 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 490 |
step=10,
|
| 491 |
value=170,
|
| 492 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
gr.Examples(
|
| 495 |
examples=examples,
|
|
@@ -519,6 +659,8 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 519 |
height,
|
| 520 |
guidance_scale,
|
| 521 |
num_inference_steps,
|
|
|
|
|
|
|
| 522 |
],
|
| 523 |
outputs=[result],
|
| 524 |
)
|
|
|
|
| 32 |
'''
|
| 33 |
|
| 34 |
DESCRIPTIONXX = """
|
| 35 |
+
## ⚡⚡⚡⚡ REALVISXL V5.0 BF16 (Tester L) ⚡⚡⚡⚡
|
| 36 |
"""
|
| 37 |
|
| 38 |
examples = [
|
|
|
|
| 273 |
height: int = 768,
|
| 274 |
guidance_scale: float = 4,
|
| 275 |
num_inference_steps: int = 125,
|
| 276 |
+
denoising_start=0.0,
|
| 277 |
+
denoising_end =1.0,
|
| 278 |
use_resolution_binning: bool = True,
|
| 279 |
progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
|
| 280 |
):
|
| 281 |
seed = random.randint(0, MAX_SEED)
|
| 282 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
images = []
|
| 284 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 285 |
filename = pyx.uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
|
| 286 |
#upload_to_ftp(filename)
|
| 287 |
pyx.upload_to_ftp(filename)
|
| 288 |
#uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
|
| 289 |
+
|
| 290 |
+
if denoising_start=0.0:
|
| 291 |
+
options = {
|
| 292 |
+
"prompt": [prompt],
|
| 293 |
+
"negative_prompt": [negative_prompt],
|
| 294 |
+
"negative_prompt_2": [neg_prompt_2],
|
| 295 |
+
"width": width,
|
| 296 |
+
"height": height,
|
| 297 |
+
"guidance_scale": guidance_scale,
|
| 298 |
+
"num_inference_steps": num_inference_steps,
|
| 299 |
+
"generator": generator,
|
| 300 |
+
"output_type": "latent",
|
| 301 |
+
"denoising_start": denoising_start,
|
| 302 |
+
"denoising_end": denoising_end,
|
| 303 |
+
"callback_on_step_end": pyx.scheduler_swap_callback
|
| 304 |
+
}
|
| 305 |
+
if use_resolution_binning:
|
| 306 |
+
options["use_resolution_binning"] = True
|
| 307 |
+
batch_options = options.copy()
|
| 308 |
+
rv_image = pipe(**batch_options).images
|
| 309 |
+
sd_latent_path = f"rv_L_{denoising_end}.l"
|
| 310 |
+
torch.save(rv_image, sd_latent_path)
|
| 311 |
+
|
| 312 |
+
if denoising_end!=1.0 and denoising_start!=0.0:
|
| 313 |
+
latent_file = f'rv_L_{denoising_start}.l'
|
| 314 |
+
loaded_latent = torch.load(latent_file)
|
| 315 |
+
print("Shape of loaded latent:", loaded_latent.shape)
|
| 316 |
+
# Get the expected shape for the latent tensor from the pipeline
|
| 317 |
+
batch_size = 1 # Usually 1 for single image generation
|
| 318 |
+
num_channels = pipe.vae.config.latent_channels #typically 4
|
| 319 |
+
latent_shape = (batch_size, num_channels, height, width)
|
| 320 |
+
latents = torch.randn(latent_shape, dtype=torch.bfloat16, device=device)
|
| 321 |
+
if loaded_latent.shape == latents.shape:
|
| 322 |
+
print("Tensor already in expected shape. Not resizing...")
|
| 323 |
+
lat_file = loaded_latent.to(latents.device).to(latents.dtype)
|
| 324 |
+
elif loaded_latent.shape[1:] == latents.shape[1:]: #check if spatial dimensions are the same, in case of different batch sizes
|
| 325 |
+
latents[0] = loaded_latent[0].to(latents.device).to(latents.dtype) #copy only the first element if different batch size
|
| 326 |
+
lat_file = latents.to(latents.device).to(latents.dtype)
|
| 327 |
+
else:
|
| 328 |
+
print("Warning: Loaded latent shape does not match expected shape. Resizing...")
|
| 329 |
+
latents = torch.nn.functional.interpolate(loaded_latent, size=(height, width), mode='bilinear', align_corners=False).to(latents.device).to(latents.dtype)
|
| 330 |
+
if latents.shape[0] > 1:
|
| 331 |
+
latents = latents[0].unsqueeze(0)
|
| 332 |
+
lat_file = latents.to(latents.device).to(latents.dtype)
|
| 333 |
+
print("-- using latent file --")
|
| 334 |
+
|
| 335 |
+
options = {
|
| 336 |
+
"prompt": [prompt],
|
| 337 |
+
"negative_prompt": [negative_prompt],
|
| 338 |
+
"negative_prompt_2": [neg_prompt_2],
|
| 339 |
+
"width": width,
|
| 340 |
+
"height": height,
|
| 341 |
+
"guidance_scale": guidance_scale,
|
| 342 |
+
"num_inference_steps": num_inference_steps,
|
| 343 |
+
"generator": generator,
|
| 344 |
+
"latents": lat_file,
|
| 345 |
+
"output_type": "latent",
|
| 346 |
+
"denoising_start": denoising_start,
|
| 347 |
+
"denoising_end": denoising_end,
|
| 348 |
+
"callback_on_step_end": pyx.scheduler_swap_callback
|
| 349 |
+
}
|
| 350 |
+
if use_resolution_binning:
|
| 351 |
+
options["use_resolution_binning"] = True
|
| 352 |
+
batch_options = options.copy()
|
| 353 |
+
rv_image = pipe(**batch_options).images
|
| 354 |
+
sd_latent_path = f"rv_L_{denoising_end}.l"
|
| 355 |
+
torch.save(rv_image, sd_latent_path)
|
| 356 |
+
|
| 357 |
+
if denoising_end=1.0 and denoising_start!=0.0:
|
| 358 |
+
latent_file = f'rv_L_{denoising_start}.l'
|
| 359 |
+
loaded_latent = torch.load(latent_file)
|
| 360 |
+
print("Shape of loaded latent:", loaded_latent.shape)
|
| 361 |
+
# Get the expected shape for the latent tensor from the pipeline
|
| 362 |
+
batch_size = 1 # Usually 1 for single image generation
|
| 363 |
+
num_channels = pipe.vae.config.latent_channels #typically 4
|
| 364 |
+
latent_shape = (batch_size, num_channels, height, width)
|
| 365 |
+
latents = torch.randn(latent_shape, dtype=torch.bfloat16, device=device)
|
| 366 |
+
if loaded_latent.shape == latents.shape:
|
| 367 |
+
print("Tensor already in expected shape. Not resizing...")
|
| 368 |
+
lat_file = loaded_latent.to(latents.device).to(latents.dtype)
|
| 369 |
+
elif loaded_latent.shape[1:] == latents.shape[1:]: #check if spatial dimensions are the same, in case of different batch sizes
|
| 370 |
+
latents[0] = loaded_latent[0].to(latents.device).to(latents.dtype) #copy only the first element if different batch size
|
| 371 |
+
lat_file = latents.to(latents.device).to(latents.dtype)
|
| 372 |
+
else:
|
| 373 |
+
print("Warning: Loaded latent shape does not match expected shape. Resizing...")
|
| 374 |
+
latents = torch.nn.functional.interpolate(loaded_latent, size=(height, width), mode='bilinear', align_corners=False).to(latents.device).to(latents.dtype)
|
| 375 |
+
if latents.shape[0] > 1:
|
| 376 |
+
latents = latents[0].unsqueeze(0)
|
| 377 |
+
lat_file = latents.to(latents.device).to(latents.dtype)
|
| 378 |
+
print("-- using latent file --")
|
| 379 |
+
options = {
|
| 380 |
+
"prompt": [prompt],
|
| 381 |
+
"negative_prompt": [negative_prompt],
|
| 382 |
+
"negative_prompt_2": [neg_prompt_2],
|
| 383 |
+
"width": width,
|
| 384 |
+
"height": height,
|
| 385 |
+
"guidance_scale": guidance_scale,
|
| 386 |
+
"num_inference_steps": num_inference_steps,
|
| 387 |
+
"generator": generator,
|
| 388 |
+
"latents": lat_file,
|
| 389 |
+
"output_type": "pil",
|
| 390 |
+
"denoising_start": denoising_start,
|
| 391 |
+
"denoising_end": denoising_end,
|
| 392 |
+
"callback_on_step_end": pyx.scheduler_swap_callback
|
| 393 |
+
}
|
| 394 |
+
if use_resolution_binning:
|
| 395 |
+
options["use_resolution_binning"] = True
|
| 396 |
+
batch_options = options.copy()
|
| 397 |
+
rv_image = pipe(**batch_options).images[0]
|
| 398 |
+
sd_image_path = f"rv_L_{timestamp}.png"
|
| 399 |
+
rv_image.save(sd_image_path,optimize=False,compress_level=0)
|
| 400 |
+
pyx.upload_to_ftp(sd_image_path)
|
| 401 |
+
torch.set_float32_matmul_precision("medium")
|
| 402 |
+
with torch.no_grad():
|
| 403 |
+
upscale = upscaler(rv_image, tiling=True, tile_width=256, tile_height=256)
|
| 404 |
+
downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
|
| 405 |
+
downscale_path = f"rv_L_upscale_{timestamp}.png"
|
| 406 |
+
downscale1.save(downscale_path,optimize=False,compress_level=0)
|
| 407 |
+
pyx.upload_to_ftp(downscale_path)
|
| 408 |
+
|
| 409 |
+
if denoising_end=1.0 and denoising_start=0.0:
|
| 410 |
+
options = {
|
| 411 |
+
"prompt": [prompt],
|
| 412 |
+
"negative_prompt": [negative_prompt],
|
| 413 |
+
"negative_prompt_2": [neg_prompt_2],
|
| 414 |
+
"width": width,
|
| 415 |
+
"height": height,
|
| 416 |
+
"guidance_scale": guidance_scale,
|
| 417 |
+
"num_inference_steps": num_inference_steps,
|
| 418 |
+
"generator": generator,
|
| 419 |
+
"output_type": "pil",
|
| 420 |
+
"callback_on_step_end": pyx.scheduler_swap_callback
|
| 421 |
+
}
|
| 422 |
+
if use_resolution_binning:
|
| 423 |
+
options["use_resolution_binning"] = True
|
| 424 |
+
batch_options = options.copy()
|
| 425 |
+
rv_image = pipe(**batch_options).images[0]
|
| 426 |
+
sd_image_path = f"rv_L_{timestamp}.png"
|
| 427 |
+
rv_image.save(sd_image_path,optimize=False,compress_level=0)
|
| 428 |
+
pyx.upload_to_ftp(sd_image_path)
|
| 429 |
+
torch.set_float32_matmul_precision("medium")
|
| 430 |
+
with torch.no_grad():
|
| 431 |
+
upscale = upscaler(rv_image, tiling=True, tile_width=256, tile_height=256)
|
| 432 |
+
downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
|
| 433 |
+
downscale_path = f"rv_L_upscale_{timestamp}.png"
|
| 434 |
+
downscale1.save(downscale_path,optimize=False,compress_level=0)
|
| 435 |
+
pyx.upload_to_ftp(downscale_path)
|
| 436 |
+
|
| 437 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 438 |
os.symlink(sd_image_path, unique_name)
|
| 439 |
return [unique_name]
|
|
|
|
| 614 |
step=10,
|
| 615 |
value=170,
|
| 616 |
)
|
| 617 |
+
denoising_start = gr.Slider(
|
| 618 |
+
label="Denoising Start",
|
| 619 |
+
minimum=0.0,
|
| 620 |
+
maximum=0.9,
|
| 621 |
+
step=0.1,
|
| 622 |
+
value=0.0,
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
denoising_end = gr.Slider(
|
| 626 |
+
label="Denoising End",
|
| 627 |
+
minimum=0.1,
|
| 628 |
+
maximum=1.0,
|
| 629 |
+
step=0.1,
|
| 630 |
+
value=1.0,
|
| 631 |
+
)
|
| 632 |
+
|
| 633 |
|
| 634 |
gr.Examples(
|
| 635 |
examples=examples,
|
|
|
|
| 659 |
height,
|
| 660 |
guidance_scale,
|
| 661 |
num_inference_steps,
|
| 662 |
+
denoising_start,
|
| 663 |
+
denoising_end,
|
| 664 |
],
|
| 665 |
outputs=[result],
|
| 666 |
)
|