ui.py 1.5 KB

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