lora_script.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import re
  2. import gradio as gr
  3. from fastapi import FastAPI
  4. import network
  5. import networks
  6. import lora # noqa:F401
  7. import lora_patches
  8. import extra_networks_lora
  9. import ui_extra_networks_lora
  10. from modules import script_callbacks, ui_extra_networks, extra_networks, shared
  11. def unload():
  12. networks.originals.undo()
  13. def before_ui():
  14. ui_extra_networks.register_page(ui_extra_networks_lora.ExtraNetworksPageLora())
  15. networks.extra_network_lora = extra_networks_lora.ExtraNetworkLora()
  16. extra_networks.register_extra_network(networks.extra_network_lora)
  17. extra_networks.register_extra_network_alias(networks.extra_network_lora, "lyco")
  18. networks.originals = lora_patches.LoraPatches()
  19. script_callbacks.on_model_loaded(networks.assign_network_names_to_compvis_modules)
  20. script_callbacks.on_script_unloaded(unload)
  21. script_callbacks.on_before_ui(before_ui)
  22. script_callbacks.on_infotext_pasted(networks.infotext_pasted)
  23. shared.options_templates.update(shared.options_section(('extra_networks', "Extra Networks"), {
  24. "sd_lora": shared.OptionInfo("None", "Add network to prompt", gr.Dropdown, lambda: {"choices": ["None", *networks.available_networks]}, refresh=networks.list_available_networks),
  25. "lora_preferred_name": shared.OptionInfo("Alias from file", "When adding to prompt, refer to Lora by", gr.Radio, {"choices": ["Alias from file", "Filename"]}),
  26. "lora_add_hashes_to_infotext": shared.OptionInfo(True, "Add Lora hashes to infotext"),
  27. "lora_show_all": shared.OptionInfo(False, "Always show all networks on the Lora page").info("otherwise, those detected as for incompatible version of Stable Diffusion will be hidden"),
  28. "lora_hide_unknown_for_versions": shared.OptionInfo([], "Hide networks of unknown versions for model versions", gr.CheckboxGroup, {"choices": ["SD1", "SD2", "SDXL"]}),
  29. "lora_in_memory_limit": shared.OptionInfo(0, "Number of Lora networks to keep cached in memory", gr.Number, {"precision": 0}),
  30. "lora_not_found_warning_console": shared.OptionInfo(False, "Lora not found warning in console"),
  31. "lora_not_found_gradio_warning": shared.OptionInfo(False, "Lora not found warning popup in webui"),
  32. }))
  33. shared.options_templates.update(shared.options_section(('compatibility', "Compatibility"), {
  34. "lora_functional": shared.OptionInfo(False, "Lora/Networks: use old method that takes longer when you have multiple Loras active and produces same results as kohya-ss/sd-webui-additional-networks extension"),
  35. }))
  36. def create_lora_json(obj: network.NetworkOnDisk):
  37. return {
  38. "name": obj.name,
  39. "alias": obj.alias,
  40. "path": obj.filename,
  41. "metadata": obj.metadata,
  42. }
  43. def api_networks(_: gr.Blocks, app: FastAPI):
  44. @app.get("/sdapi/v1/loras")
  45. async def get_loras():
  46. return [create_lora_json(obj) for obj in networks.available_networks.values()]
  47. @app.post("/sdapi/v1/refresh-loras")
  48. async def refresh_loras():
  49. return networks.list_available_networks()
  50. script_callbacks.on_app_started(api_networks)
  51. re_lora = re.compile("<lora:([^:]+):")
  52. def infotext_pasted(infotext, d):
  53. hashes = d.get("Lora hashes")
  54. if not hashes:
  55. return
  56. hashes = [x.strip().split(':', 1) for x in hashes.split(",")]
  57. hashes = {x[0].strip().replace(",", ""): x[1].strip() for x in hashes}
  58. def network_replacement(m):
  59. alias = m.group(1)
  60. shorthash = hashes.get(alias)
  61. if shorthash is None:
  62. return m.group(0)
  63. network_on_disk = networks.available_network_hash_lookup.get(shorthash)
  64. if network_on_disk is None:
  65. return m.group(0)
  66. return f'<lora:{network_on_disk.get_alias()}:'
  67. d["Prompt"] = re.sub(re_lora, network_replacement, d["Prompt"])
  68. script_callbacks.on_infotext_pasted(infotext_pasted)
  69. shared.opts.onchange("lora_in_memory_limit", networks.purge_networks_from_memory)