Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from controlnet_aux import OpenposeDetector
|
11 |
+
|
12 |
+
|
13 |
+
openpose = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
14 |
+
|
15 |
+
controlnet = ControlNetModel.from_pretrained(
|
16 |
+
"briaai/BRIA-2.3-ControlNet-Pose",
|
17 |
+
torch_dtype=torch.float16
|
18 |
+
).to('cuda')
|
19 |
+
|
20 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
21 |
+
"briaai/BRIA-2.3",
|
22 |
+
controlnet=controlnet,
|
23 |
+
torch_dtype=torch.float16,
|
24 |
+
device_map='auto',
|
25 |
+
low_cpu_mem_usage=True,
|
26 |
+
offload_state_dict=True,
|
27 |
+
).to('cuda').to(torch.float16)
|
28 |
+
|
29 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler(
|
30 |
+
beta_start=0.00085,
|
31 |
+
beta_end=0.012,
|
32 |
+
beta_schedule="scaled_linear",
|
33 |
+
num_train_timesteps=1000,
|
34 |
+
steps_offset=1
|
35 |
+
)
|
36 |
+
# pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
|
37 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
38 |
+
pipe.force_zeros_for_empty_prompt = False
|
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 |
+
|
51 |
+
@spaces.GPU
|
52 |
+
def generate_(prompt, negative_prompt, grayscale_image, num_steps, controlnet_conditioning_scale, seed):
|
53 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
54 |
+
images = pipe(
|
55 |
+
prompt, negative_prompt=negative_prompt, image=grayscale_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
56 |
+
generator=generator,
|
57 |
+
).images
|
58 |
+
return images
|
59 |
+
|
60 |
+
@spaces.GPU
|
61 |
+
def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
62 |
+
|
63 |
+
# resize input_image to 1024x1024
|
64 |
+
input_image = resize_image(input_image)
|
65 |
+
|
66 |
+
pose_image = openpose(image, include_body=True, include_hand=True, include_face=True)[0]
|
67 |
+
|
68 |
+
images = generate_(prompt, negative_prompt, pose_image, num_steps, controlnet_conditioning_scale, seed)
|
69 |
+
|
70 |
+
return [grayscale_image,images[0]]
|
71 |
+
|
72 |
+
block = gr.Blocks().queue()
|
73 |
+
|
74 |
+
with block:
|
75 |
+
gr.Markdown("## BRIA 2.3 ControlNet Pose")
|
76 |
+
gr.HTML('''
|
77 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
78 |
+
This is a demo for ControlNet Pose that using
|
79 |
+
<a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">BRIA 2.3 text-to-image model</a> as backbone.
|
80 |
+
Trained on licensed data, BRIA 2.3 provide full legal liability coverage for copyright and privacy infringement.
|
81 |
+
</p>
|
82 |
+
''')
|
83 |
+
with gr.Row():
|
84 |
+
with gr.Column():
|
85 |
+
input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
|
86 |
+
prompt = gr.Textbox(label="Prompt")
|
87 |
+
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")
|
88 |
+
num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
|
89 |
+
controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
|
90 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
|
91 |
+
run_button = gr.Button(value="Run")
|
92 |
+
|
93 |
+
|
94 |
+
with gr.Column():
|
95 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
|
96 |
+
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
97 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
98 |
+
|
99 |
+
block.launch(debug = True)
|