ui.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import html
  2. import os
  3. import re
  4. import gradio as gr
  5. import modules.hypernetworks.hypernetwork
  6. from modules import devices, sd_hijack, shared
  7. not_available = ["hardswish", "multiheadattention"]
  8. keys = list(x for x in modules.hypernetworks.hypernetwork.HypernetworkModule.activation_dict.keys() if x not in not_available)
  9. def create_hypernetwork(name, enable_sizes, overwrite_old, layer_structure=None, activation_func=None, weight_init=None, add_layer_norm=False, use_dropout=False, dropout_structure=None):
  10. filename = modules.hypernetworks.hypernetwork.create_hypernetwork(name, enable_sizes, overwrite_old, layer_structure, activation_func, weight_init, add_layer_norm, use_dropout, dropout_structure)
  11. return gr.Dropdown.update(choices=sorted([x for x in shared.hypernetworks.keys()])), f"Created: {filename}", ""
  12. def train_hypernetwork(*args):
  13. shared.loaded_hypernetworks = []
  14. assert not shared.cmd_opts.lowvram, 'Training models with lowvram is not possible'
  15. try:
  16. sd_hijack.undo_optimizations()
  17. hypernetwork, filename = modules.hypernetworks.hypernetwork.train_hypernetwork(*args)
  18. res = f"""
  19. Training {'interrupted' if shared.state.interrupted else 'finished'} at {hypernetwork.step} steps.
  20. Hypernetwork saved to {html.escape(filename)}
  21. """
  22. return res, ""
  23. except Exception:
  24. raise
  25. finally:
  26. shared.sd_model.cond_stage_model.to(devices.device)
  27. shared.sd_model.first_stage_model.to(devices.device)
  28. sd_hijack.apply_optimizations()