| import torch | |
| from PIL.Image import Image | |
| from diffusers import StableDiffusionXLPipeline | |
| from pipelines.models import TextToImageRequest | |
| from torch import Generator | |
| from opttf import opttf_pipeline | |
| def load_pipeline() -> StableDiffusionXLPipeline: | |
| pipeline = StableDiffusionXLPipeline.from_pretrained( | |
| "./models/newdream-sdxl-20", | |
| torch_dtype=torch.float16, | |
| local_files_only=True, | |
| ).to("cuda") | |
| pipeline = opttf_pipeline(pipeline) | |
| pipeline(prompt="") | |
| return pipeline | |
| def infer(request: TextToImageRequest, pipeline: StableDiffusionXLPipeline) -> Image: | |
| pipeline = opttf_pipeline(pipeline) | |
| 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] | |
| if __name__ == '__main__': | |
| load_pipeline() | |