2022-10-02 15:03:39 +03:00
|
|
|
import html
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2022-10-02 22:41:21 +03:00
|
|
|
import modules.textual_inversion.textual_inversion
|
|
|
|
import modules.textual_inversion.preprocess
|
2022-10-02 15:03:39 +03:00
|
|
|
from modules import sd_hijack, shared
|
|
|
|
|
|
|
|
|
2022-10-20 02:24:24 +03:00
|
|
|
def create_embedding(name, initialization_text, nvpt, overwrite_old):
|
|
|
|
filename = modules.textual_inversion.textual_inversion.create_embedding(name, nvpt, overwrite_old, init_text=initialization_text)
|
2022-10-02 15:03:39 +03:00
|
|
|
|
|
|
|
sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings()
|
|
|
|
|
|
|
|
return gr.Dropdown.update(choices=sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys())), f"Created: {filename}", ""
|
|
|
|
|
|
|
|
|
2022-10-02 22:41:21 +03:00
|
|
|
def preprocess(*args):
|
|
|
|
modules.textual_inversion.preprocess.preprocess(*args)
|
|
|
|
|
|
|
|
return "Preprocessing finished.", ""
|
|
|
|
|
|
|
|
|
2022-10-02 15:03:39 +03:00
|
|
|
def train_embedding(*args):
|
2022-10-11 18:33:57 +03:00
|
|
|
|
2022-10-11 23:07:09 +03:00
|
|
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram not possible'
|
2022-10-11 18:33:57 +03:00
|
|
|
|
2022-10-31 14:26:08 +03:00
|
|
|
apply_optimizations = shared.opts.training_xattention_optimizations
|
2022-10-02 15:03:39 +03:00
|
|
|
try:
|
2022-10-31 14:26:08 +03:00
|
|
|
if not apply_optimizations:
|
|
|
|
sd_hijack.undo_optimizations()
|
2022-10-02 15:03:39 +03:00
|
|
|
|
2022-10-02 22:41:21 +03:00
|
|
|
embedding, filename = modules.textual_inversion.textual_inversion.train_embedding(*args)
|
2022-10-02 15:03:39 +03:00
|
|
|
|
|
|
|
res = f"""
|
2022-10-02 21:09:10 +03:00
|
|
|
Training {'interrupted' if shared.state.interrupted else 'finished'} at {embedding.step} steps.
|
2022-10-02 15:03:39 +03:00
|
|
|
Embedding saved to {html.escape(filename)}
|
|
|
|
"""
|
|
|
|
return res, ""
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
finally:
|
2022-10-31 14:26:08 +03:00
|
|
|
if not apply_optimizations:
|
|
|
|
sd_hijack.apply_optimizations()
|
2022-10-02 22:41:21 +03:00
|
|
|
|