2022-10-08 04:22:22 +08:00
|
|
|
import html
|
|
|
|
import os
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
import modules.textual_inversion.textual_inversion
|
|
|
|
import modules.textual_inversion.preprocess
|
|
|
|
from modules import sd_hijack, shared
|
2022-10-11 19:53:02 +08:00
|
|
|
from modules.hypernetwork import hypernetwork
|
2022-10-08 04:22:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def create_hypernetwork(name):
|
|
|
|
fn = os.path.join(shared.cmd_opts.hypernetwork_dir, f"{name}.pt")
|
|
|
|
assert not os.path.exists(fn), f"file {fn} already exists"
|
|
|
|
|
2022-10-11 19:53:02 +08:00
|
|
|
hypernet = modules.hypernetwork.hypernetwork.Hypernetwork(name=name)
|
|
|
|
hypernet.save(fn)
|
2022-10-08 04:22:22 +08:00
|
|
|
|
|
|
|
shared.reload_hypernetworks()
|
|
|
|
|
|
|
|
return gr.Dropdown.update(choices=sorted([x for x in shared.hypernetworks.keys()])), f"Created: {fn}", ""
|
|
|
|
|
|
|
|
|
|
|
|
def train_hypernetwork(*args):
|
|
|
|
|
2022-10-11 19:53:02 +08:00
|
|
|
initial_hypernetwork = shared.loaded_hypernetwork
|
2022-10-08 04:22:22 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
sd_hijack.undo_optimizations()
|
|
|
|
|
|
|
|
hypernetwork, filename = modules.hypernetwork.hypernetwork.train_hypernetwork(*args)
|
|
|
|
|
|
|
|
res = f"""
|
|
|
|
Training {'interrupted' if shared.state.interrupted else 'finished'} at {hypernetwork.step} steps.
|
|
|
|
Hypernetwork saved to {html.escape(filename)}
|
|
|
|
"""
|
|
|
|
return res, ""
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
finally:
|
2022-10-11 19:53:02 +08:00
|
|
|
shared.loaded_hypernetwork = initial_hypernetwork
|
2022-10-08 04:22:22 +08:00
|
|
|
sd_hijack.apply_optimizations()
|
|
|
|
|