nekofura commited on
Commit
13cf6b9
1 Parent(s): 6e54472

Update image_generator.py

Browse files
Files changed (1) hide show
  1. image_generator.py +12 -5
image_generator.py CHANGED
@@ -1,13 +1,20 @@
1
- from diffusers import DiffusionPipeline, LCMScheduler, DPMSolverMultistepScheduler
2
  import torch
3
 
4
  loaded_pipe = None
5
  loaded_pipe_id = None
6
 
7
- def load_model(pipe_id):
8
  global loaded_pipe, loaded_pipe_id
9
  if loaded_pipe_id != pipe_id:
10
- loaded_pipe = DiffusionPipeline.from_pretrained(pipe_id, torch_dtype=torch.float16).to("cuda")
 
 
 
 
 
 
 
11
  loaded_pipe_id = pipe_id
12
  return loaded_pipe
13
 
@@ -18,9 +25,9 @@ def set_scheduler(pipe, scheduler_type):
18
  pipe.scheduler = DPMSolverMultistepScheduler(use_karras_sigmas="yes")
19
  return pipe
20
 
21
- def generate_image(prompt, num_inference_steps, seed, guidance_scale, negative_prompt=None, pipe_id="Linaqruf/animagine-xl", scheduler_type="LCM"):
22
  global loaded_pipe
23
- pipe = load_model(pipe_id)
24
  pipe = set_scheduler(pipe, scheduler_type)
25
  generator = torch.manual_seed(seed)
26
  image = pipe(prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, generator=generator, guidance_scale=guidance_scale).images[0]
 
1
+ from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, LCMScheduler, DPMSolverMultistepScheduler
2
  import torch
3
 
4
  loaded_pipe = None
5
  loaded_pipe_id = None
6
 
7
+ def load_model(pipe_id, unet_model_id):
8
  global loaded_pipe, loaded_pipe_id
9
  if loaded_pipe_id != pipe_id:
10
+ unet = UNet2DConditionModel.from_pretrained(
11
+ unet_model_id,
12
+ torch_dtype=torch.float16,
13
+ variant="fp16",
14
+ )
15
+ loaded_pipe = StableDiffusionXLPipeline.from_pretrained(
16
+ pipe_id, unet=unet, torch_dtype=torch.float16, variant="fp16",
17
+ ).to("cuda")
18
  loaded_pipe_id = pipe_id
19
  return loaded_pipe
20
 
 
25
  pipe.scheduler = DPMSolverMultistepScheduler(use_karras_sigmas="yes")
26
  return pipe
27
 
28
+ def generate_image(prompt, num_inference_steps, seed, guidance_scale, negative_prompt=None, pipe_id="stabilityai/stable-diffusion-xl-base-1.0", unet_model_id="latent-consistency/lcm-sdxl", scheduler_type="LCM"):
29
  global loaded_pipe
30
+ pipe = load_model(pipe_id, unet_model_id)
31
  pipe = set_scheduler(pipe, scheduler_type)
32
  generator = torch.manual_seed(seed)
33
  image = pipe(prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, generator=generator, guidance_scale=guidance_scale).images[0]