Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -7,35 +7,31 @@ import numpy as np
|
|
7 |
import cv2
|
8 |
import gradio as gr
|
9 |
from torchvision import transforms
|
|
|
10 |
|
11 |
controlnet = ControlNetModel.from_pretrained(
|
12 |
"geyongtao/HumanWild",
|
13 |
torch_dtype=torch.float16
|
14 |
).to('cuda')
|
15 |
|
|
|
|
|
|
|
|
|
16 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
17 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
18 |
controlnet=controlnet,
|
|
|
19 |
torch_dtype=torch.float16,
|
20 |
-
|
21 |
low_cpu_mem_usage=True,
|
22 |
offload_state_dict=True,
|
23 |
).to('cuda')
|
|
|
24 |
|
25 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler(
|
26 |
-
beta_start=0.00085,
|
27 |
-
beta_end=0.012,
|
28 |
-
beta_schedule="scaled_linear",
|
29 |
-
num_train_timesteps=1000,
|
30 |
-
steps_offset=1
|
31 |
-
)
|
32 |
-
# pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
|
33 |
# pipe.enable_xformers_memory_efficient_attention()
|
34 |
pipe.force_zeros_for_empty_prompt = False
|
35 |
|
36 |
-
# from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
37 |
-
# depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
|
38 |
-
# feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
|
39 |
|
40 |
def resize_image(image):
|
41 |
image = image.convert('RGB')
|
@@ -83,35 +79,38 @@ def process(normal_image, prompt, negative_prompt, num_steps, controlnet_conditi
|
|
83 |
# depth_image = get_depth_map(input_image)
|
84 |
images = generate_(prompt, negative_prompt, normal_image, num_steps, controlnet_conditioning_scale, seed)
|
85 |
|
86 |
-
return [
|
87 |
-
|
88 |
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
with gr.
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
|
117 |
-
|
|
|
|
7 |
import cv2
|
8 |
import gradio as gr
|
9 |
from torchvision import transforms
|
10 |
+
import fire
|
11 |
|
12 |
controlnet = ControlNetModel.from_pretrained(
|
13 |
"geyongtao/HumanWild",
|
14 |
torch_dtype=torch.float16
|
15 |
).to('cuda')
|
16 |
|
17 |
+
vae = AutoencoderKL.from_pretrained(
|
18 |
+
madebyollin/sdxl-vae-fp16-fix,
|
19 |
+
torch_dtype=torch.float16).to("cuda")
|
20 |
+
|
21 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
22 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
23 |
controlnet=controlnet,
|
24 |
+
vae=vae,
|
25 |
torch_dtype=torch.float16,
|
26 |
+
use_safetensors=True,
|
27 |
low_cpu_mem_usage=True,
|
28 |
offload_state_dict=True,
|
29 |
).to('cuda')
|
30 |
+
pipe.controlnet.to(memory_format=torch.channels_last)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# pipe.enable_xformers_memory_efficient_attention()
|
33 |
pipe.force_zeros_for_empty_prompt = False
|
34 |
|
|
|
|
|
|
|
35 |
|
36 |
def resize_image(image):
|
37 |
image = image.convert('RGB')
|
|
|
79 |
# depth_image = get_depth_map(input_image)
|
80 |
images = generate_(prompt, negative_prompt, normal_image, num_steps, controlnet_conditioning_scale, seed)
|
81 |
|
82 |
+
return [normal_image, images[0]]
|
|
|
83 |
|
84 |
|
85 |
+
def run_demo():
|
86 |
+
block = gr.Blocks().queue()
|
87 |
+
|
88 |
+
with block:
|
89 |
+
gr.Markdown("## Surface Normal ControlNet ")
|
90 |
+
gr.HTML('''
|
91 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
92 |
+
This is a demo for Surface Normal ControlNet that using
|
93 |
+
<a href="https://huggingface.co/geyongtao/HumanWild" target="_blank"> HumanWild model</a> as backbone.
|
94 |
+
</p>
|
95 |
+
''')
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
|
99 |
+
prompt = gr.Textbox(label="Prompt")
|
100 |
+
negative_prompt = gr.Textbox(label="Negative prompt", value="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers")
|
101 |
+
num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
|
102 |
+
controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
|
103 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
|
104 |
+
run_button = gr.Button(value="Run")
|
105 |
+
|
106 |
+
|
107 |
+
with gr.Column():
|
108 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
|
109 |
+
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
110 |
+
|
111 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
112 |
|
113 |
+
block.launch(debug = True)
|
114 |
|
115 |
+
if __name__ == '__main__':
|
116 |
+
fire.Fire(run_demo)
|