Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -30,6 +30,7 @@ pipeline = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
30 |
torch_dtype=torch.float16,
|
31 |
).to("cuda")
|
32 |
pipeline.enable_model_cpu_offload()
|
|
|
33 |
|
34 |
sa_args = sa_handler.StyleAlignedArgs(share_group_norm=False,
|
35 |
share_layer_norm=False,
|
@@ -42,53 +43,64 @@ handler = sa_handler.Handler(pipeline)
|
|
42 |
handler.register(sa_args, )
|
43 |
|
44 |
|
45 |
-
# get depth maps
|
46 |
-
def get_depth_maps(image):
|
47 |
-
image = load_image(image) #("./example_image/train.png")
|
48 |
-
depth_image1 = pipeline_calls.get_depth_map(image, feature_processor, depth_estimator)
|
49 |
-
#depth_image2 = load_image("./example_image/sun.png").resize((1024, 1024))
|
50 |
-
#mediapy.show_images([depth_image1, depth_image2])
|
51 |
-
return depth_image1 #[depth_image1, depth_image2]
|
52 |
-
|
53 |
|
54 |
|
55 |
# run ControlNet depth with StyleAligned
|
56 |
-
def style_aligned_controlnet(
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
controlnet_conditioning_scale = 0.8
|
60 |
-
num_images_per_prompt =
|
61 |
-
depth_map = get_depth_maps(image)
|
62 |
latents = torch.randn(1 + num_images_per_prompt, 4, 128, 128).to(pipeline.unet.dtype)
|
63 |
-
#for deph_map, target_prompt in zip((depth_image1, depth_image2), target_prompts):
|
64 |
latents[1:] = torch.randn(num_images_per_prompt, 4, 128, 128).to(pipeline.unet.dtype)
|
65 |
-
images = pipeline_calls.controlnet_call(pipeline, [
|
66 |
-
image=
|
67 |
num_inference_steps=50,
|
68 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
69 |
num_images_per_prompt=num_images_per_prompt,
|
70 |
-
|
71 |
-
|
72 |
-
return images[0]
|
73 |
-
|
74 |
-
#mediapy.show_images([images[0], deph_map] + images[1:], titles=["reference", "depth"] + [f'result {i}' for i in range(1, len(images))])
|
75 |
-
|
76 |
|
77 |
|
78 |
with gr.Blocks() as demo:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
btn = gr.Button("Generate", size='sm')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
btn.click(fn=style_aligned_controlnet,
|
|
|
|
|
|
|
91 |
|
92 |
|
93 |
demo.launch()
|
94 |
-
|
|
|
30 |
torch_dtype=torch.float16,
|
31 |
).to("cuda")
|
32 |
pipeline.enable_model_cpu_offload()
|
33 |
+
pipeline.enable_vae_slicing()
|
34 |
|
35 |
sa_args = sa_handler.StyleAlignedArgs(share_group_norm=False,
|
36 |
share_layer_norm=False,
|
|
|
43 |
handler.register(sa_args, )
|
44 |
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
48 |
# run ControlNet depth with StyleAligned
|
49 |
+
def style_aligned_controlnet(ref_style_prompt, depth_map, ref_image, img_generation_prompt):
|
50 |
+
if depth_map == True:
|
51 |
+
image = load_image(ref_image)
|
52 |
+
depth_image = pipeline_calls.get_depth_map(image, feature_processor, depth_estimator)
|
53 |
+
else:
|
54 |
+
depth_image = load_image(ref_image).resize((1024, 1024))
|
55 |
+
#reference_prompt = ref_style_prompt #"a poster in minimalist origami style"
|
56 |
+
#target_prompts = img_generation_prompt #["mona lisa"] #, "gal gadot"]
|
57 |
controlnet_conditioning_scale = 0.8
|
58 |
+
num_images_per_prompt = 3 # adjust according to VRAM size
|
|
|
59 |
latents = torch.randn(1 + num_images_per_prompt, 4, 128, 128).to(pipeline.unet.dtype)
|
|
|
60 |
latents[1:] = torch.randn(num_images_per_prompt, 4, 128, 128).to(pipeline.unet.dtype)
|
61 |
+
images = pipeline_calls.controlnet_call(pipeline, [ref_style_prompt, img_generation_prompt],
|
62 |
+
image=depth_image,
|
63 |
num_inference_steps=50,
|
64 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
65 |
num_images_per_prompt=num_images_per_prompt,
|
66 |
+
latents=latents)
|
67 |
+
#mediapy.show_images([images[0], depth_image2] + images[1:], titles=["reference", "depth"] + [f'result {i}' for i in range(1, len(images))])
|
68 |
+
return [images[0], depth_image] + images[1:], gr.Image(value=images[0], visible=True)
|
|
|
|
|
|
|
69 |
|
70 |
|
71 |
with gr.Blocks() as demo:
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
|
75 |
+
with gr.Column(variant='panel'):
|
76 |
+
ref_style_prompt = gr.Textbox(
|
77 |
+
label='Reference style prompt',
|
78 |
+
info="Enter a Prompt to generate the reference image", placeholder='a poster in <style name> style'
|
79 |
+
)
|
80 |
+
depth_map = gr.Checkbox(label='Depth-map',)
|
81 |
+
ref_style_image = gr.Image(visible=False, label='Reference style image')
|
82 |
+
|
83 |
+
with gr.Column(variant='panel'):
|
84 |
+
ref_image = gr.Image(label="Upload the reference image",
|
85 |
+
type='filepath' )
|
86 |
+
img_generation_prompt = gr.Textbox(
|
87 |
+
label='ControlNet Prompt',
|
88 |
+
info="Enter a Prompt to generate images using ControlNet and Style-aligned",
|
89 |
+
)
|
90 |
+
|
91 |
btn = gr.Button("Generate", size='sm')
|
92 |
+
gallery = gr.Gallery(label="Style-Aligned ControlNet - Generated images",
|
93 |
+
elem_id="gallery",
|
94 |
+
columns=5,
|
95 |
+
rows=1,
|
96 |
+
object_fit="contain",
|
97 |
+
height="auto",
|
98 |
+
)
|
99 |
|
100 |
+
btn.click(fn=style_aligned_controlnet,
|
101 |
+
inputs=[ref_style_prompt, depth_map, ref_image, img_generation_prompt],
|
102 |
+
outputs=[gallery, ref_style_image],
|
103 |
+
api_name="style_aligned_controlnet")
|
104 |
|
105 |
|
106 |
demo.launch()
|
|