RamAnanth1
commited on
Commit
•
fdf4694
1
Parent(s):
c0b3c87
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
2 |
+
from diffusers.utils import load_image
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
controlnet_conditioning_scale = 0.5 # recommended for good generalization
|
9 |
+
|
10 |
+
controlnet = ControlNetModel.from_pretrained(
|
11 |
+
"diffusers/controlnet-canny-sdxl-1.0",
|
12 |
+
torch_dtype=torch.float16
|
13 |
+
)
|
14 |
+
|
15 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
16 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
17 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
18 |
+
controlnet=controlnet,
|
19 |
+
vae=vae,
|
20 |
+
torch_dtype=torch.float16,
|
21 |
+
)
|
22 |
+
pipe.enable_model_cpu_offload()
|
23 |
+
|
24 |
+
low_threshold = 100
|
25 |
+
high_threshold = 200
|
26 |
+
|
27 |
+
def get_canny_filter(image):
|
28 |
+
|
29 |
+
if not isinstance(image, np.ndarray):
|
30 |
+
image = np.array(image)
|
31 |
+
|
32 |
+
image = cv2.Canny(image, low_threshold, high_threshold)
|
33 |
+
image = image[:, :, None]
|
34 |
+
image = np.concatenate([image, image, image], axis=2)
|
35 |
+
canny_image = Image.fromarray(image)
|
36 |
+
return canny_image
|
37 |
+
|
38 |
+
def process(input_image, prompt)
|
39 |
+
canny_image = get_canny_filter(input_image)
|
40 |
+
images = pipe(
|
41 |
+
prompt,image=image, controlnet_conditioning_scale=controlnet_conditioning_scale,
|
42 |
+
).images
|
43 |
+
|
44 |
+
return [canny_image,images[0]]
|
45 |
+
|
46 |
+
block = gr.Blocks().queue()
|
47 |
+
|
48 |
+
with block:
|
49 |
+
gr.Markdown("## ControlNet SDXL Canny")
|
50 |
+
gr.HTML('''
|
51 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
52 |
+
This is a demo for ControlNet SDXL, which is a neural network structure to control Stable Diffusion XL model by adding extra condition such as canny edge detection.
|
53 |
+
</p>
|
54 |
+
''')
|
55 |
+
gr.HTML("<p>You can duplicate this Space to run it privately without a queue and load additional checkpoints. : <a style='display:inline-block' href='https://huggingface.co/spaces/RamAnanth1/controlnet-sdxl-canny?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a> </p>")
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column():
|
58 |
+
input_image = gr.Image(source='upload', type="numpy")
|
59 |
+
prompt = gr.Textbox(label="Prompt")
|
60 |
+
run_button = gr.Button(label="Run")
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Column():
|
64 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
65 |
+
ips = [input_image, prompt]
|
66 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
67 |
+
# examples_list = [
|
68 |
+
# # [
|
69 |
+
# # "bird.png",
|
70 |
+
# # "bird",
|
71 |
+
# # "Canny Edge Map"
|
72 |
+
|
73 |
+
# # ],
|
74 |
+
|
75 |
+
# # [
|
76 |
+
# # "turtle.png",
|
77 |
+
# # "turtle",
|
78 |
+
# # "Scribble",
|
79 |
+
# # "best quality, extremely detailed",
|
80 |
+
# # 'longbody, lowres, bad anatomy, bad hands, missing fingers, pubic hair,extra digit, fewer digits, cropped, worst quality, low quality',
|
81 |
+
# # 1,
|
82 |
+
# # 512,
|
83 |
+
# # 20,
|
84 |
+
# # 9.0,
|
85 |
+
# # 123490213,
|
86 |
+
# # 0.0,
|
87 |
+
# # 100,
|
88 |
+
# # 200
|
89 |
+
|
90 |
+
# # ],
|
91 |
+
# [
|
92 |
+
# "pose1.png",
|
93 |
+
# "Chef in the Kitchen",
|
94 |
+
# "Pose",
|
95 |
+
# # "best quality, extremely detailed",
|
96 |
+
# # 'longbody, lowres, bad anatomy, bad hands, missing fingers, pubic hair,extra digit, fewer digits, cropped, worst quality, low quality',
|
97 |
+
# # 1,
|
98 |
+
# # 512,
|
99 |
+
# # 20,
|
100 |
+
# # 9.0,
|
101 |
+
# # 123490213,
|
102 |
+
# # 0.0,
|
103 |
+
# # 100,
|
104 |
+
# # 200
|
105 |
+
|
106 |
+
# ]
|
107 |
+
# ]
|
108 |
+
# examples = gr.Examples(examples=examples_list,inputs = [input_image, prompt], outputs = [result_gallery], cache_examples = True, fn = process)
|
109 |
+
gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=RamAnanth1.ControlNet)")
|
110 |
+
|
111 |
+
block.launch(debug = True)
|