Skip to content

Commit 2b1687a

Browse files
authored
Merge pull request #85 from kohya-ss/dev
Dev
2 parents 4769a57 + ad605e3 commit 2b1687a

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ __Stable Diffusion web UI now seems to support LoRA trained by ``sd-scripts``.__
88

99
Note: Currently the models models for SD 2.x does not seem to be supported in Web UI. The models trained by the scripts 0.4.0 seem to be supported.
1010

11+
- 1 Feb. 2023, 2023/2/1
12+
- Add ``send to metadata editor`` button in ``Additional Network`` in ``txt2img`` and other tabs. Thanks to space-nuko!
13+
- ``txt2img``タブ等にメタデータエディタに送るボタンが付きました。space-nuko氏に感謝します。
1114
- 31 Jan. 2023, 2023/1/31
1215
- Metadata editor for LoRA models is now integrated in ``Additional Network`` tab. Documentation will be added later. Thanks to space-nuko!
1316
- LoRAモデル用のメタデータエディタ ``Additional Network`` タブに追加されました。ドキュメントはのちほど追加予定です。space-nuko氏に感謝します。

javascript/additional_networks.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,31 @@ function addnet_switch_to_img2img(){
1010
return args_to_array(arguments);
1111
}
1212

13-
function addnet_switch_to_addnet(){
14-
Array.from(gradioApp().querySelector('#tabs').querySelectorAll('button')).filter(e => e.textContent.trim() === "Additional Networks")[0].click()
13+
function addnet_switch_to_metadata_editor(){
14+
Array.from(gradioApp().querySelector('#tabs').querySelectorAll('button')).filter(e => e.textContent.trim() === "Additional Networks")[0].click();
15+
return args_to_array(arguments);
16+
}
17+
18+
function addnet_send_to_metadata_editor() {
19+
var module = arguments[0];
20+
var model_path = arguments[1];
21+
22+
if (model_path == "None") {
23+
return args_to_array(arguments);
24+
}
25+
26+
console.log(arguments);
27+
console.log(model_path);
28+
var select = gradioApp().querySelector("#additional_networks_metadata_editor_model > label > select");
29+
30+
var opt = [...select.options].filter(o => o.text == model_path)[0];
31+
if (opt == null) {
32+
return;
33+
}
34+
35+
addnet_switch_to_metadata_editor();
36+
select.selectedIndex = opt.index;
37+
select.dispatchEvent(new Event("change", { bubbles: true }));
38+
1539
return args_to_array(arguments);
1640
}

scripts/additional_networks.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import modules.scripts as scripts
66
from modules import shared, script_callbacks
7-
from modules.ui_components import ToolButton
87
import gradio as gr
98

109
import modules.ui
@@ -13,6 +12,7 @@
1312
from scripts.model_util import lora_models, MAX_MODEL_COUNT
1413

1514

15+
memo_symbol = '\U0001F4DD' # 📝
1616
addnet_paste_params = {"txt2img": [], "img2img": []}
1717

1818

@@ -62,6 +62,16 @@ def ui(self, is_img2img):
6262
model = gr.Dropdown(list(lora_models.keys()),
6363
label=f"Model {i+1}",
6464
value="None")
65+
with gr.Row(visible=False):
66+
model_path = gr.Textbox(value="None", interactive=False, visible=False)
67+
model.change(lambda module, model, i=i: model_util.lora_models.get(model, "None"), inputs=[module, model], outputs=[model_path])
68+
69+
# Sending from the script UI to the metadata editor has to bypass
70+
# gradio since this button will exit the gr.Blocks context by the
71+
# time the metadata editor tab is created, so event handlers can't
72+
# be registered on it by then.
73+
model_info = gr.Button(value=memo_symbol, elem_id=f"additional_networks_send_to_metadata_editor_{i}")
74+
model_info.click(fn=None, _js="addnet_send_to_metadata_editor", inputs=[module, model_path], outputs=[])
6575

6676
module.change(lambda module, model, i=i: xyz_grid_support.update_axis_params(i, module, model), inputs=[module, model], outputs=[])
6777
model.change(lambda module, model, i=i: xyz_grid_support.update_axis_params(i, module, model), inputs=[module, model], outputs=[])

scripts/metadata_editor.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ def save_metadata(module, model_path, cover_image, display_name, author, source,
290290
return f"Model saved: {model_name}", model_hash, legacy_hash
291291

292292

293+
model_name_filter = ""
294+
295+
296+
def get_filtered_model_paths(s):
297+
if not s:
298+
return ["None"] + list(model_util.lora_models.values())
299+
300+
return ["None"] + [v for v in model_util.lora_models.values() if v and s in v.lower()]
301+
302+
303+
def get_filtered_model_paths_global():
304+
global model_name_filter
305+
return get_filtered_model_paths(model_name_filter)
306+
307+
293308
def setup_ui(addnet_paste_params):
294309
"""
295310
:dict addnet_paste_params: Dictionary of txt2img/img2img controls for each model weight slider,
@@ -304,9 +319,23 @@ def setup_ui(addnet_paste_params):
304319

305320
# Module and model selector
306321
with gr.Row():
307-
module = gr.Dropdown(["LoRA"], label="Network module", value="LoRA", interactive=True)
308-
model = gr.Dropdown(["None"] + list(model_util.lora_models.values()), label="Model", value="None", interactive=True)
309-
modules.ui.create_refresh_button(model, model_util.update_models, lambda: {"choices": ["None"] + list(model_util.lora_models.values())}, "refresh_lora_models")
322+
model_filter = gr.Textbox("", label="Model path filter", placeholder="Filter models by path name")
323+
def update_model_filter(s):
324+
global model_name_filter
325+
model_name_filter = s.strip().lower()
326+
model_filter.change(update_model_filter, inputs=[model_filter], outputs=[])
327+
with gr.Row():
328+
module = gr.Dropdown(["LoRA"], label="Network module", value="LoRA", interactive=True, elem_id="additional_networks_metadata_editor_module")
329+
model = gr.Dropdown(get_filtered_model_paths_global(), label="Model", value="None", interactive=True,
330+
elem_id="additional_networks_metadata_editor_model")
331+
modules.ui.create_refresh_button(model, model_util.update_models, lambda: {"choices": get_filtered_model_paths_global()}, "refresh_lora_models")
332+
333+
def submit_model_filter(s):
334+
global model_name_filter
335+
model_name_filter = s
336+
paths = get_filtered_model_paths(s)
337+
return gr.Dropdown.update(choices=paths, value="None")
338+
model_filter.submit(submit_model_filter, inputs=[model_filter], outputs=[model])
310339

311340
# Model hashes and path
312341
with gr.Row():

style.css

+5
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@
1616
min-width: 2.5em;
1717
height: 2.4em;
1818
}
19+
20+
21+
[id^=additional_networks_send_to_metadata_editor_] {
22+
transform: translateY(25%);
23+
}

0 commit comments

Comments
 (0)