2022-10-07 23:22:22 +03:00
|
|
|
import html
|
|
|
|
import os
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
import modules.textual_inversion.textual_inversion
|
|
|
|
import modules.textual_inversion.preprocess
|
2022-10-11 19:03:08 +03:00
|
|
|
from modules import sd_hijack, shared, devices
|
2022-10-11 15:51:22 +03:00
|
|
|
from modules.hypernetworks import hypernetwork
|
2022-10-07 23:22:22 +03:00
|
|
|
|
|
|
|
|
2022-10-11 18:04:47 +03:00
|
|
|
def create_hypernetwork(name, enable_sizes):
|
2022-10-07 23:22:22 +03:00
|
|
|
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 18:04:47 +03:00
|
|
|
hypernet = modules.hypernetworks.hypernetwork.Hypernetwork(name=name, enable_sizes=[int(x) for x in enable_sizes])
|
2022-10-11 14:53:02 +03:00
|
|
|
hypernet.save(fn)
|
2022-10-07 23:22:22 +03: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 14:53:02 +03:00
|
|
|
initial_hypernetwork = shared.loaded_hypernetwork
|
2022-10-07 23:22:22 +03:00
|
|
|
|
2022-10-11 18:33:57 +03:00
|
|
|
assert not shared.cmd_opts.lowvram and not shared.cmd_opts.medvram, 'Training models with lowvram or medvram is not possible'
|
|
|
|
|
2022-10-07 23:22:22 +03:00
|
|
|
try:
|
|
|
|
sd_hijack.undo_optimizations()
|
|
|
|
|
2022-10-11 15:54:34 +03:00
|
|
|
hypernetwork, filename = modules.hypernetworks.hypernetwork.train_hypernetwork(*args)
|
2022-10-07 23:22:22 +03:00
|
|
|
|
|
|
|
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 14:53:02 +03:00
|
|
|
shared.loaded_hypernetwork = initial_hypernetwork
|
2022-10-11 19:03:08 +03:00
|
|
|
shared.sd_model.cond_stage_model.to(devices.device)
|
|
|
|
shared.sd_model.first_stage_model.to(devices.device)
|
2022-10-07 23:22:22 +03:00
|
|
|
sd_hijack.apply_optimizations()
|
|
|
|
|