ysharma HF staff commited on
Commit
d639c7d
1 Parent(s): 8b000ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -33
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(reference_prompt, target_prompt, image):
57
- #reference_prompt = "a poster in flat design style"
58
- #target_prompts = [target_prompts] #["a train in flat design style", "the sun in flat design style"]
 
 
 
 
 
59
  controlnet_conditioning_scale = 0.8
60
- num_images_per_prompt = 1 # adjust according to VRAM size
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, [reference_prompt, target_prompt],
66
- image=depth_map,
67
  num_inference_steps=50,
68
  controlnet_conditioning_scale=controlnet_conditioning_scale,
69
  num_images_per_prompt=num_images_per_prompt,
70
- latents=latents)
71
- print(f"images -{images}")
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
- with gr.Row(variant='panel'):
80
- with gr.Group():
81
- gr.Markdown("### <center>Reference Prompt and Image</center>")
82
- ref_prompt = gr.Textbox(label="Enter a Prompt describing the reference image", placeholder='a photo of <object> in <style name> style')
83
- depth_map = gr.Image(label="Upload the image to get Depth Map", type='filepath' )
84
- with gr.Group():
85
- gr.Markdown("### <center>Prompt for generation and generated Image</center>")
86
- prompt = gr.Textbox(label="Enter a Prompt", placeholder='a photo of <object> in <style name> style')
87
- output = gr.Image(label="Style-Aligned ControlNet",type='pil')
 
 
 
 
 
 
 
 
 
 
88
  btn = gr.Button("Generate", size='sm')
 
 
 
 
 
 
 
89
 
90
- btn.click(fn=style_aligned_controlnet, inputs=[ref_prompt, prompt, depth_map], outputs=output, api_name="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()