ui.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import html
  2. import gradio as gr
  3. import modules.textual_inversion.textual_inversion
  4. import modules.textual_inversion.preprocess
  5. from modules import sd_hijack, shared
  6. def create_embedding(name, initialization_text, nvpt, overwrite_old):
  7. filename = modules.textual_inversion.textual_inversion.create_embedding(name, nvpt, overwrite_old, init_text=initialization_text)
  8. sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings()
  9. return gr.Dropdown.update(choices=sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys())), f"Created: {filename}", ""
  10. def preprocess(*args):
  11. modules.textual_inversion.preprocess.preprocess(*args)
  12. return f"Preprocessing {'interrupted' if shared.state.interrupted else 'finished'}.", ""
  13. def train_embedding(*args):
  14. assert not shared.cmd_opts.lowvram, 'Training models with lowvram not possible'
  15. apply_optimizations = shared.opts.training_xattention_optimizations
  16. try:
  17. if not apply_optimizations:
  18. sd_hijack.undo_optimizations()
  19. embedding, filename = modules.textual_inversion.textual_inversion.train_embedding(*args)
  20. res = f"""
  21. Training {'interrupted' if shared.state.interrupted else 'finished'} at {embedding.step} steps.
  22. Embedding saved to {html.escape(filename)}
  23. """
  24. return res, ""
  25. except Exception:
  26. raise
  27. finally:
  28. if not apply_optimizations:
  29. sd_hijack.apply_optimizations()