| | import torch |
| | from PIL.Image import Image |
| | from diffusers import StableDiffusionXLPipeline |
| | from pipelines.models import TextToImageRequest |
| | from torch import Generator |
| | from DeepCache import DeepCacheSDHelper |
| |
|
| |
|
| | def load_pipeline() -> StableDiffusionXLPipeline: |
| | pipeline = StableDiffusionXLPipeline.from_pretrained( |
| | "./models/newdream-sdxl-20", |
| | torch_dtype=torch.float16, |
| | |
| | use_safetensors=True, |
| | variant='fp16', |
| | ).to("cuda") |
| |
|
| | helper = DeepCacheSDHelper(pipe=pipe) |
| | helper.set_params(cache_interval=3, cache_branch_id=0) |
| | helper.enable() |
| |
|
| | for _ in range(5): |
| | pipeline(prompt="") |
| |
|
| | return pipeline |
| |
|
| |
|
| | def infer(request: TextToImageRequest, pipeline: StableDiffusionXLPipeline) -> Image: |
| | if request.seed is None: |
| | generator = None |
| | else: |
| | generator = Generator(pipeline.device).manual_seed(request.seed) |
| |
|
| | return pipeline( |
| | prompt=request.prompt, |
| | negative_prompt=request.negative_prompt, |
| | width=request.width, |
| | height=request.height, |
| | generator=generator, |
| | num_inference_steps=20, |
| | ).images[0] |
| |
|