Path: blob/master/extensions-builtin/Lora/scripts/lora_script.py
2448 views
import re12import gradio as gr3from fastapi import FastAPI45import network6import networks7import lora # noqa:F4018import lora_patches9import extra_networks_lora10import ui_extra_networks_lora11from modules import script_callbacks, ui_extra_networks, extra_networks, shared121314def unload():15networks.originals.undo()161718def before_ui():19ui_extra_networks.register_page(ui_extra_networks_lora.ExtraNetworksPageLora())2021networks.extra_network_lora = extra_networks_lora.ExtraNetworkLora()22extra_networks.register_extra_network(networks.extra_network_lora)23extra_networks.register_extra_network_alias(networks.extra_network_lora, "lyco")242526networks.originals = lora_patches.LoraPatches()2728script_callbacks.on_model_loaded(networks.assign_network_names_to_compvis_modules)29script_callbacks.on_script_unloaded(unload)30script_callbacks.on_before_ui(before_ui)31script_callbacks.on_infotext_pasted(networks.infotext_pasted)323334shared.options_templates.update(shared.options_section(('extra_networks', "Extra Networks"), {35"sd_lora": shared.OptionInfo("None", "Add network to prompt", gr.Dropdown, lambda: {"choices": ["None", *networks.available_networks]}, refresh=networks.list_available_networks),36"lora_preferred_name": shared.OptionInfo("Alias from file", "When adding to prompt, refer to Lora by", gr.Radio, {"choices": ["Alias from file", "Filename"]}),37"lora_add_hashes_to_infotext": shared.OptionInfo(True, "Add Lora hashes to infotext"),38"lora_bundled_ti_to_infotext": shared.OptionInfo(True, "Add Lora name as TI hashes for bundled Textual Inversion").info('"Add Textual Inversion hashes to infotext" needs to be enabled'),39"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"),40"lora_hide_unknown_for_versions": shared.OptionInfo([], "Hide networks of unknown versions for model versions", gr.CheckboxGroup, {"choices": ["SD1", "SD2", "SDXL"]}),41"lora_in_memory_limit": shared.OptionInfo(0, "Number of Lora networks to keep cached in memory", gr.Number, {"precision": 0}),42"lora_not_found_warning_console": shared.OptionInfo(False, "Lora not found warning in console"),43"lora_not_found_gradio_warning": shared.OptionInfo(False, "Lora not found warning popup in webui"),44}))454647shared.options_templates.update(shared.options_section(('compatibility', "Compatibility"), {48"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"),49}))505152def create_lora_json(obj: network.NetworkOnDisk):53return {54"name": obj.name,55"alias": obj.alias,56"path": obj.filename,57"metadata": obj.metadata,58}596061def api_networks(_: gr.Blocks, app: FastAPI):62@app.get("/sdapi/v1/loras")63async def get_loras():64return [create_lora_json(obj) for obj in networks.available_networks.values()]6566@app.post("/sdapi/v1/refresh-loras")67async def refresh_loras():68return networks.list_available_networks()697071script_callbacks.on_app_started(api_networks)7273re_lora = re.compile("<lora:([^:]+):")747576def infotext_pasted(infotext, d):77hashes = d.get("Lora hashes")78if not hashes:79return8081hashes = [x.strip().split(':', 1) for x in hashes.split(",")]82hashes = {x[0].strip().replace(",", ""): x[1].strip() for x in hashes}8384def network_replacement(m):85alias = m.group(1)86shorthash = hashes.get(alias)87if shorthash is None:88return m.group(0)8990network_on_disk = networks.available_network_hash_lookup.get(shorthash)91if network_on_disk is None:92return m.group(0)9394return f'<lora:{network_on_disk.get_alias()}:'9596d["Prompt"] = re.sub(re_lora, network_replacement, d["Prompt"])979899script_callbacks.on_infotext_pasted(infotext_pasted)100101shared.opts.onchange("lora_in_memory_limit", networks.purge_networks_from_memory)102103104