Commit ·
69481a1
1
Parent(s): d65437f
test basic stable cascade
Browse files- 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 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
guidance_scale=guidance_scale,
|
| 65 |
-
negative_prompt=negative_prompt,
|
| 66 |
-
generator=self.generator,
|
| 67 |
-
output_type="pil",
|
| 68 |
).images
|
| 69 |
-
|
| 70 |
-
|
| 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 |
|