File size: 1,691 Bytes
173ea2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3

import torch
from diffusers import StableDiffusionPipeline
from BeamDiffusionModel.models.diffusionModel.configs.config_loader import CONFIG
from functools import partial
from BeamDiffusionModel.models.diffusionModel.Latents_Singleton import Latents


class StableDiffusion:
    def __init__(self):
        self.device = "cuda" if CONFIG.get("stable_diffusion", {}).get("use_cuda", True) and torch.cuda.is_available() else "cpu"
        self.torch_dtype = torch.float16 if CONFIG.get("stable_diffusion", {}).get("precision") == "float16" else torch.float32

        print(f"Loading model: {CONFIG['stable_diffusion']['id']} on {self.device}")

        self.pipe = StableDiffusionPipeline.from_pretrained(CONFIG["stable_diffusion"]["id"], torch_dtype=self.torch_dtype)
        self.pipe.to(self.device)

        self.unet = self.pipe.unet
        self.vae = self.pipe.vae

        print("Model loaded successfully!")


    def capture_latents(self, latents_store: Latents, pipe, step, timestep, callback_kwargs):
        latents = callback_kwargs["latents"]
        latents_store.add_latents(latents)
        return callback_kwargs

    def generate_image(self, prompt: str, latent=None, generator=None):
        latents = Latents()
        callback = partial(self.capture_latents, latents)
        img = self.pipe(prompt, latents=latent, callback_on_step_end=callback,
                             generator=generator, callback_on_step_end_tensor_inputs=["latents"],
                             num_inference_steps=CONFIG["stable_diffusion"]["diffusion_settings"]["steps"]).images[0]

        return img, latents.dump_and_clear()