|
from typing import List, Any |
|
import torch |
|
from diffusers import StableCascadePriorPipeline, StableCascadeDecoderPipeline |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
if device.type != 'cuda': |
|
raise ValueError("Se requiere ejecutar en GPU") |
|
|
|
|
|
dtype = torch.bfloat16 if torch.cuda.get_device_capability(device.index)[0] >= 8 else torch.float16 |
|
|
|
class EndpointHandler(): |
|
def __init__(self): |
|
|
|
pass |
|
|
|
def __call__(self, data: Any) -> List[Any]: |
|
|
|
num_images_per_prompt = 1 |
|
|
|
|
|
prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", torch_dtype=dtype).to(device) |
|
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", torch_dtype=dtype).to(device) |
|
|
|
prompt = data.get("inputs", "Una imagen interesante") |
|
negative_prompt = data.get("negative_prompt", "") |
|
|
|
prior_output = prior( |
|
prompt=prompt, |
|
height=512, |
|
width=512, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=7.5, |
|
num_inference_steps=50, |
|
num_images_per_prompt=num_images_per_prompt, |
|
) |
|
|
|
decoder_output = decoder( |
|
image_embeddings=prior_output["image_embeddings"].half(), |
|
prompt=prompt, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=7.5, |
|
output_type="pil", |
|
num_inference_steps=20 |
|
) |
|
|
|
|
|
return [decoder_output.images[0]] |
|
|