tombetthauser commited on
Commit
9f8acba
Β·
1 Parent(s): 97f9c0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -41
app.py CHANGED
@@ -85,23 +85,23 @@ def load_learned_embed_in_clip(learned_embeds_path, text_encoder, tokenizer, tok
85
 
86
  # ----- ControlNet Canny Edges Pipe / Setup -----------------------------------------------------------------
87
 
88
- import gradio as gr
89
- from PIL import Image
90
- import numpy as np
91
- import cv2
92
 
93
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
94
- from diffusers import UniPCMultistepScheduler
95
- import torch
96
 
97
- controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
98
- controlnet_pipe = StableDiffusionControlNetPipeline.from_pretrained(
99
- "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
100
- )
101
 
102
- controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.scheduler.config)
103
- controlnet_pipe.enable_model_cpu_offload()
104
- controlnet_pipe.enable_xformers_memory_efficient_attention()
105
 
106
 
107
 
@@ -443,27 +443,27 @@ canny_interface = gr.Interface(fn=canny_process_image, inputs=[canny_input_image
443
 
444
 
445
 
446
- # ----- ControlNet Canny Gradio Setup -----------------------------------------------------------------
447
 
448
- # import gradio as gr
449
- # from PIL import Image
450
- # import numpy as np
451
- # import cv2
452
 
453
- # from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
454
- # from diffusers import UniPCMultistepScheduler
455
- # import torch
 
 
 
 
456
 
457
- # controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
458
- # controlnet_pipe = StableDiffusionControlNetPipeline.from_pretrained(
459
- # "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
460
- # )
461
 
462
- # controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.scheduler.config)
463
- # controlnet_pipe.enable_model_cpu_offload()
464
- # controlnet_pipe.enable_xformers_memory_efficient_attention()
465
 
466
- def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input_high_threshold, input_invert):
 
467
  np_image = np.array(input_image)
468
 
469
  output_image = input_image
@@ -478,25 +478,131 @@ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input
478
  canny_1 = 255 - canny_1
479
 
480
  canny_2 = Image.fromarray(canny_1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
 
482
  prompt = canny_input_prompt
483
- generator = torch.Generator(device="cpu").manual_seed(2)
484
-
485
- # output_image = controlnet_pipe(
486
- # prompt,
487
- # canny_2,
488
- # negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
489
- # generator=generator,
490
- # num_inference_steps=20,
491
- # )
492
  output_image = controlnet_pipe(
493
  prompt,
494
  canny_2,
495
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
 
496
  num_inference_steps=20,
497
  )
498
 
499
- return output_image[0][0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500
 
501
 
502
  canny_input_prompt = gr.inputs.Textbox(label="Enter a single word or phrase")
@@ -514,6 +620,6 @@ controlnet_canny_interface = gr.Interface(fn=controlnet_edges, inputs=[canny_inp
514
 
515
  # ----- Launch Tabs -----------------------------------------------------------------
516
 
517
- tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta, canny_interface, controlnet_canny_interface], ["Artbots", "Advanced", "Beta", "Edges", "ControlNet"])
518
  # tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta], ["Artbots", "Advanced", "Beta"])
519
  tabbed_interface.launch()
 
85
 
86
  # ----- ControlNet Canny Edges Pipe / Setup -----------------------------------------------------------------
87
 
88
+ # import gradio as gr
89
+ # from PIL import Image
90
+ # import numpy as np
91
+ # import cv2
92
 
93
+ # from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
94
+ # from diffusers import UniPCMultistepScheduler
95
+ # import torch
96
 
97
+ # controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
98
+ # controlnet_pipe = StableDiffusionControlNetPipeline.from_pretrained(
99
+ # "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
100
+ # )
101
 
102
+ # controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.scheduler.config)
103
+ # controlnet_pipe.enable_model_cpu_offload()
104
+ # controlnet_pipe.enable_xformers_memory_efficient_attention()
105
 
106
 
107
 
 
443
 
444
 
445
 
446
+ # ----- New ControlNet Canny Gradio Setup with Block -----------------------------------------------------------------
447
 
 
 
 
 
448
 
449
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
450
+ from diffusers import UniPCMultistepScheduler
451
+ from PIL import Image
452
+ import gradio as gr
453
+ import numpy as np
454
+ import torch
455
+ import cv2
456
 
457
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
458
+ controlnet_pipe = StableDiffusionControlNetPipeline.from_pretrained(
459
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
460
+ )
461
 
462
+ controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.scheduler.config)
463
+ controlnet_pipe.enable_model_cpu_offload()
 
464
 
465
+
466
+ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input_high_threshold, input_invert, canny_input_seed):
467
  np_image = np.array(input_image)
468
 
469
  output_image = input_image
 
478
  canny_1 = 255 - canny_1
479
 
480
  canny_2 = Image.fromarray(canny_1)
481
+ canny_1 = Image.fromarray(canny_1)
482
+
483
+ input_width, input_height = canny_2.size
484
+
485
+ limit_size = 768
486
+ # limit_size = 32
487
+
488
+ if input_width > input_height:
489
+ new_width = min(input_width, limit_size)
490
+ new_height = int(new_width * input_height / input_width)
491
+ else:
492
+ new_height = min(input_height, limit_size)
493
+ new_width = int(new_height * input_width / input_height)
494
+
495
+ canny_2 = canny_2.resize((new_width, new_height))
496
+ canny_1 = canny_1.resize((new_width, new_height))
497
 
498
  prompt = canny_input_prompt
499
+ generator = torch.Generator(device="cpu").manual_seed(canny_input_seed)
500
+
 
 
 
 
 
 
 
501
  output_image = controlnet_pipe(
502
  prompt,
503
  canny_2,
504
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
505
+ generator=generator,
506
  num_inference_steps=20,
507
  )
508
 
509
+ return [canny_1, output_image[0][0]]
510
+ # return output_image[0][0]
511
+
512
+ import random
513
+ def random_seed():
514
+ return random.randint(0, 99999999999999)
515
+
516
+
517
+ with gr.Blocks() as canny_blocks_interface:
518
+ gr.Markdown('''
519
+ # <span style="display: inline-block; height: 30px; width: 30px; margin-bottom: -3px; border-radius: 7px; background-size: 50px; background-position: center; background-image: url(http://www.astronaut.horse/thumbnail.jpg)"></span> ControlNet + Canny Edge-Tracing
520
+ This tool allows you to apply a Stable Diffusion text prompt to an existing image composition using an edge-tracing tool called Canny Edge Detector. Note that you cannot currently apply trained artist concepts from the other tabs in this application to this process currently as they were trained using a more recent version of Stable Diffusion.
521
+ <br><br>
522
+ <a href="https://wikipedia.org/wiki/Canny_edge_detector">https://wikipedia.org/wiki/canny_edge_detector</a>
523
+ <br>
524
+ <a href="http://www.astronaut.horse">http://www.astronaut.horse</a>
525
+ <br>
526
+ <a href="https://discord.gg/ZctfW4SvGw">https://discord.com</a><br>
527
+ <br>
528
+ ''')
529
+ with gr.Row():
530
+ with gr.Column():
531
+ canny_input_prompt = gr.inputs.Textbox(label="Enter a single word or phrase")
532
+ canny_input_low_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Lower Threshold:", default=100)
533
+ canny_input_high_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Upper Threshold:", default=120)
534
+ canny_input_seed = gr.Slider(0, 99999999999999, label="seed", dtype=int, value=random_seed, interactive=True, step=1)
535
+ canny_input_invert = gr.inputs.Checkbox(label="Invert Image")
536
+ with gr.Column():
537
+ canny_input_image = gr.inputs.Image(label="input image")
538
+ go_button = gr.Button('generate image')
539
+ # with gr.Row():
540
+ with gr.Accordion(label='traced edge image', open=False):
541
+ canny_output_1 = gr.outputs.Image(type="pil", label="traced edges")
542
+ with gr.Row():
543
+ canny_output_2 = gr.outputs.Image(type="pil", label="final image")
544
+ go_button.click(fn=controlnet_edges, inputs=[canny_input_prompt, canny_input_image, canny_input_low_threshold, canny_input_high_threshold, canny_input_invert, canny_input_seed], outputs=[canny_output_1, canny_output_2])
545
+
546
+
547
+ # canny_blocks_interface.launch(debug=False)
548
+
549
+
550
+
551
+
552
+ # ----- Old ControlNet Canny Gradio Setup without Block (working) -----------------------------------------------------------------
553
+
554
+ # import gradio as gr
555
+ # from PIL import Image
556
+ # import numpy as np
557
+ # import cv2
558
+
559
+ # from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
560
+ # from diffusers import UniPCMultistepScheduler
561
+ # import torch
562
+
563
+ # controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
564
+ # controlnet_pipe = StableDiffusionControlNetPipeline.from_pretrained(
565
+ # "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
566
+ # )
567
+
568
+ # controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.scheduler.config)
569
+ # controlnet_pipe.enable_model_cpu_offload()
570
+ # controlnet_pipe.enable_xformers_memory_efficient_attention()
571
+
572
+ # def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input_high_threshold, input_invert):
573
+ # np_image = np.array(input_image)
574
+
575
+ # output_image = input_image
576
+ # numpy_image = np.array(output_image)
577
+
578
+ # low_threshold = 80
579
+ # high_threshold = 100
580
+ # canny_1 = cv2.Canny(numpy_image, input_low_threshold, input_high_threshold)
581
+ # canny_1 = canny_1[:, :, None]
582
+ # canny_1 = np.concatenate([canny_1, canny_1, canny_1], axis=2)
583
+ # if input_invert:
584
+ # canny_1 = 255 - canny_1
585
+
586
+ # canny_2 = Image.fromarray(canny_1)
587
+
588
+ # prompt = canny_input_prompt
589
+ # generator = torch.Generator(device="cpu").manual_seed(2)
590
+
591
+ # # output_image = controlnet_pipe(
592
+ # # prompt,
593
+ # # canny_2,
594
+ # # negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
595
+ # # generator=generator,
596
+ # # num_inference_steps=20,
597
+ # # )
598
+ # output_image = controlnet_pipe(
599
+ # prompt,
600
+ # canny_2,
601
+ # negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
602
+ # num_inference_steps=20,
603
+ # )
604
+
605
+ # return output_image[0][0]
606
 
607
 
608
  canny_input_prompt = gr.inputs.Textbox(label="Enter a single word or phrase")
 
620
 
621
  # ----- Launch Tabs -----------------------------------------------------------------
622
 
623
+ tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta, canny_blocks_interface], ["Artbots", "Advanced", "Beta", "ControlNet"])
624
  # tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta], ["Artbots", "Advanced", "Beta"])
625
  tabbed_interface.launch()