geyongtao commited on
Commit
076d107
1 Parent(s): 0e67ad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -40
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
- device_map='auto',
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 [depth_image, images[0]]
87
-
88
 
89
 
90
- block = gr.Blocks().queue()
91
-
92
- with block:
93
- gr.Markdown("## BRIA 2.2 ControlNet Depth")
94
- gr.HTML('''
95
- <p style="margin-bottom: 10px; font-size: 94%">
96
- This is a demo for ControlNet Surface Normal that using
97
- <a href="https://huggingface.co/geyongtao/HumanWild" target="_blank"> HumanWild model</a> as backbone.
98
- </p>
99
- ''')
100
- with gr.Row():
101
- with gr.Column():
102
- input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
103
- prompt = gr.Textbox(label="Prompt")
104
- 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")
105
- num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
106
- controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
107
- seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
108
- run_button = gr.Button(value="Run")
109
-
110
-
111
- with gr.Column():
112
- result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
113
- ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
 
 
 
114
 
115
- run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
116
 
117
- block.launch(debug = True)
 
 
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)