| import torch | |
| from PIL.Image import Image | |
| from diffusers import StableDiffusionXLPipeline | |
| from pipelines.models import TextToImageRequest | |
| from torch import Generator | |
| def load_pipeline() -> StableDiffusionXLPipeline: | |
| pipe = StableDiffusionXLPipeline.from_pretrained( | |
| "./models/bfp16", | |
| torch_dtype=torch.bfloat16, | |
| local_files_only=True, | |
| ).to("cuda") | |
| prompt = "3 happy foxes play together" | |
| pipe.fuse_qkv_projections() | |
| pipe(prompt) | |
| return pipe | |
| def infer(request: TextToImageRequest, pipeline: StableDiffusionXLPipeline) -> Image: | |
| generator = Generator(pipeline.device).manual_seed(request.seed) if request.seed else None | |
| return pipeline( | |
| prompt=request.prompt, | |
| negative_prompt=request.negative_prompt, | |
| width=request.width, | |
| height=request.height, | |
| generator=generator, | |
| ).images[0] | |