Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
|
3 |
+
from diffusers.utils import load_image
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
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')
|
42 |
+
current_size = image.size
|
43 |
+
if current_size[0] > current_size[1]:
|
44 |
+
center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
|
45 |
+
else:
|
46 |
+
center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
|
47 |
+
resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
|
48 |
+
return resized_image
|
49 |
+
|
50 |
+
def get_normal_map(image):
|
51 |
+
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
|
52 |
+
with torch.no_grad(), torch.autocast("cuda"):
|
53 |
+
depth_map = depth_estimator(image).predicted_depth
|
54 |
+
image = transforms.functional.center_crop(image, min(image.shape[-2:]))
|
55 |
+
depth_map = torch.nn.functional.interpolate(
|
56 |
+
depth_map.unsqueeze(1),
|
57 |
+
size=(1024, 1024),
|
58 |
+
mode="bicubic",
|
59 |
+
align_corners=False,
|
60 |
+
)
|
61 |
+
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
|
62 |
+
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
|
63 |
+
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
|
64 |
+
image = torch.cat([depth_map] * 3, dim=1)
|
65 |
+
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
|
66 |
+
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
67 |
+
return image
|
68 |
+
|
69 |
+
|
70 |
+
@spaces.GPU
|
71 |
+
def generate_(prompt, negative_prompt, canny_image, num_steps, controlnet_conditioning_scale, seed):
|
72 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
73 |
+
images = pipe(
|
74 |
+
prompt, negative_prompt=negative_prompt, image=canny_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
75 |
+
generator=generator,
|
76 |
+
).images
|
77 |
+
return images
|
78 |
+
|
79 |
+
@spaces.GPU
|
80 |
+
def process(normal_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
81 |
+
# resize input_image to 1024x1024
|
82 |
+
normal_image = resize_image(normal_image)
|
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)
|