2022-10-02 12:03:39 +00:00
|
|
|
import html
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2022-10-02 19:41:21 +00:00
|
|
|
import modules.textual_inversion.textual_inversion
|
|
|
|
import modules.textual_inversion.preprocess
|
2022-10-02 12:03:39 +00:00
|
|
|
from modules import sd_hijack, shared
|
|
|
|
|
|
|
|
|
2022-10-19 23:24:24 +00: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 12:03:39 +00: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 19:41:21 +00:00
|
|
|
def preprocess(*args):
|
|
|
|
modules.textual_inversion.preprocess.preprocess(*args)
|
|
|
|
|
|
|
|
return "Preprocessing finished.", ""
|
|
|
|
|
|
|
|
|
2022-10-02 12:03:39 +00:00
|
|
|
def train_embedding(*args):
|
2022-10-11 15:33:57 +00:00
|
|
|
|
2022-10-11 20:07:09 +00:00
|
|
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram not possible'
|
2022-10-11 15:33:57 +00:00
|
|
|
|
2022-10-02 12:03:39 +00:00
|
|
|
try:
|
|
|
|
sd_hijack.undo_optimizations()
|
|
|
|
|
2022-10-02 19:41:21 +00:00
|
|
|
embedding, filename = modules.textual_inversion.textual_inversion.train_embedding(*args)
|
2022-10-02 12:03:39 +00:00
|
|
|
|
|
|
|
res = f"""
|
2022-10-02 18:09:10 +00:00
|
|
|
Training {'interrupted' if shared.state.interrupted else 'finished'} at {embedding.step} steps.
|
2022-10-02 12:03:39 +00:00
|
|
|
Embedding saved to {html.escape(filename)}
|
|
|
|
"""
|
|
|
|
return res, ""
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
sd_hijack.apply_optimizations()
|
2022-10-02 19:41:21 +00:00
|
|
|
|