Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

diffusers_load.py 1.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. import os
  2. import ldm_patched.modules.sd
  3. def first_file(path, filenames):
  4. for f in filenames:
  5. p = os.path.join(path, f)
  6. if os.path.exists(p):
  7. return p
  8. return None
  9. def load_diffusers(model_path, output_vae=True, output_clip=True, embedding_directory=None):
  10. diffusion_model_names = ["diffusion_pytorch_model.fp16.safetensors", "diffusion_pytorch_model.safetensors", "diffusion_pytorch_model.fp16.bin", "diffusion_pytorch_model.bin"]
  11. unet_path = first_file(os.path.join(model_path, "unet"), diffusion_model_names)
  12. vae_path = first_file(os.path.join(model_path, "vae"), diffusion_model_names)
  13. text_encoder_model_names = ["model.fp16.safetensors", "model.safetensors", "pytorch_model.fp16.bin", "pytorch_model.bin"]
  14. text_encoder1_path = first_file(os.path.join(model_path, "text_encoder"), text_encoder_model_names)
  15. text_encoder2_path = first_file(os.path.join(model_path, "text_encoder_2"), text_encoder_model_names)
  16. text_encoder_paths = [text_encoder1_path]
  17. if text_encoder2_path is not None:
  18. text_encoder_paths.append(text_encoder2_path)
  19. unet = ldm_patched.modules.sd.load_unet(unet_path)
  20. clip = None
  21. if output_clip:
  22. clip = ldm_patched.modules.sd.load_clip(text_encoder_paths, embedding_directory=embedding_directory)
  23. vae = None
  24. if output_vae:
  25. sd = ldm_patched.modules.utils.load_torch_file(vae_path)
  26. vae = ldm_patched.modules.sd.VAE(sd=sd)
  27. return (unet, clip, vae)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...