JackHoltone commited on
Commit
4ecd736
·
verified ·
1 Parent(s): 50d9d8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ from PIL import Image
4
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetImg2ImgPipeline, DDIMScheduler
5
+ from hidiffusion import apply_hidiffusion, remove_hidiffusion
6
+ import cv2
7
+
8
+ controlnet = ControlNetModel.from_pretrained(
9
+ "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16, variant="fp16"
10
+ ).to("cuda")
11
+ scheduler = DDIMScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
12
+
13
+ pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
14
+ "stabilityai/stable-diffusion-xl-base-1.0",
15
+ controlnet=controlnet,
16
+ scheduler = scheduler,
17
+ torch_dtype=torch.float16,
18
+ ).to("cuda")
19
+
20
+ # Apply hidiffusion with a single line of code.
21
+ apply_hidiffusion(pipe)
22
+
23
+ pipe.enable_model_cpu_offload()
24
+ pipe.enable_xformers_memory_efficient_attention()
25
+
26
+ path = './assets/lara.jpeg'
27
+ ori_image = Image.open(path)
28
+ # get canny image
29
+ image = np.array(ori_image)
30
+ image = cv2.Canny(image, 50, 120)
31
+ image = image[:, :, None]
32
+ image = np.concatenate([image, image, image], axis=2)
33
+ canny_image = Image.fromarray(image)
34
+
35
+ controlnet_conditioning_scale = 0.5 # recommended for good generalization
36
+ prompt = "Lara Croft with brown hair, and is wearing a tank top, a brown backpack. The room is dark and has an old-fashioned decor with a patterned floor and a wall featuring a design with arches and a dark area on the right side, muted color, high detail, 8k high definition award winning"
37
+ negative_prompt = "underexposed, poorly drawn hands, duplicate hands, overexposed, bad art, beginner, amateur, abstract, disfigured, deformed, close up, weird colors, watermark"
38
+
39
+ image = pipe(prompt,
40
+ image=ori_image,
41
+ control_image=canny_image,
42
+ height=1536,
43
+ width=2048,
44
+ strength=0.99,
45
+ num_inference_steps=50,
46
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
47
+ guidance_scale=12.5,
48
+ negative_prompt = negative_prompt,
49
+ eta=1.0
50
+ ).images[0]
51
+
52
+ image.save("lara.jpg")