yamildiego commited on
Commit
69481a1
·
1 Parent(s): d65437f

test basic stable cascade

Browse files
Files changed (1) hide show
  1. handler.py +33 -45
handler.py CHANGED
@@ -3,10 +3,9 @@ import base64
3
  from PIL import Image
4
  from io import BytesIO
5
  from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
6
- from diffusers import StableDiffusionPipeline
7
- from diffusers import StableCascadeDecoderPipeline, StableCascadePriorPipeline
8
 
9
  import torch
 
10
 
11
 
12
  # # set device
@@ -17,56 +16,45 @@ if device.type != 'cuda':
17
  dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
18
 
19
  class EndpointHandler():
20
- def __init__(self, path=""):
21
- self.stable_diffusion_id = "Lykon/dreamshaper-8"
22
 
23
- self.prior_pipeline = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", torch_dtype=dtype)#.to(device)
24
- self.decoder_pipeline = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", torch_dtype=dtype)#.to(device)
25
 
26
 
27
- self.generator = torch.Generator(device=device.type).manual_seed(3)
28
 
29
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
30
- # """
31
- # :param data: A dictionary contains `inputs` and optional `image` field.
32
- # :return: A dictionary with `image` field contains image in base64.
33
- # """
34
- prompt = data.pop("inputs", None)
35
- num_inference_steps = data.pop("num_inference_steps", 30)
36
- guidance_scale = data.pop("guidance_scale", 7.4)
37
- negative_prompt = data.pop("negative_prompt", None)
38
- height = data.pop("height", None)
39
- width = data.pop("width", None)
40
 
41
- self.prior_pipeline.to(device)
42
- self.decoder_pipeline.to(device)
43
-
44
- prior_output = self.prior_pipeline(
45
- prompt=prompt,
46
- height=height,
47
- width=width,
48
- num_inference_steps=num_inference_steps,
49
- # timesteps=DEFAULT_STAGE_C_TIMESTEPS,
50
- negative_prompt=negative_prompt,
51
- guidance_scale=guidance_scale,
52
- num_images_per_prompt=1,
53
- generator=self.generator,
54
- # callback=callback_prior,
55
- # callback_steps=callback_steps
56
  )
57
-
58
-
59
- decoder_output = self.decoder_pipeline(
60
- image_embeddings=prior_output.image_embeddings,
61
- prompt=prompt,
62
- num_inference_steps=num_inference_steps,
63
- # timesteps=decoder_timesteps,
64
- guidance_scale=guidance_scale,
65
- negative_prompt=negative_prompt,
66
- generator=self.generator,
67
- output_type="pil",
68
  ).images
69
-
70
- return decoder_output[0]
71
 
72
 
 
3
  from PIL import Image
4
  from io import BytesIO
5
  from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
 
 
6
 
7
  import torch
8
+ from diffusers import StableCascadeDecoderPipeline, StableCascadePriorPipeline
9
 
10
 
11
  # # set device
 
16
  dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
17
 
18
  class EndpointHandler():
19
+ # def __init__(self, path=""):
20
+ # self.stable_diffusion_id = "Lykon/dreamshaper-8"
21
 
22
+ # self.prior_pipeline = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", torch_dtype=dtype)#.to(device)
23
+ # self.decoder_pipeline = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", torch_dtype=dtype)#.to(device)
24
 
25
 
26
+ # self.generator = torch.Generator(device=device.type).manual_seed(3)
27
 
28
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
29
+ # import torch
30
+
31
+ device = "cuda"
32
+ num_images_per_prompt = 2
33
+
34
+ prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", torch_dtype=torch.bfloat16).to(device)
35
+ decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", torch_dtype=torch.float16).to(device)
36
+
37
+ prompt = "Anthropomorphic cat dressed as a pilot"
38
+ negative_prompt = ""
39
 
40
+ prior_output = prior(
41
+ prompt=prompt,
42
+ height=1024,
43
+ width=1024,
44
+ negative_prompt=negative_prompt,
45
+ guidance_scale=4.0,
46
+ num_images_per_prompt=num_images_per_prompt,
47
+ num_inference_steps=20
 
 
 
 
 
 
 
48
  )
49
+ decoder_output = decoder(
50
+ image_embeddings=prior_output.image_embeddings.half(),
51
+ prompt=prompt,
52
+ negative_prompt=negative_prompt,
53
+ guidance_scale=0.0,
54
+ output_type="pil",
55
+ num_inference_steps=10
 
 
 
 
56
  ).images
57
+ return decoder_output
58
+
59
 
60