forked from bmaltais/kohya_ss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_lora_from_dylora_gui.py
170 lines (145 loc) · 4.95 KB
/
extract_lora_from_dylora_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import gradio as gr
import subprocess
import os
import sys
from .common_gui import (
get_file_path,
scriptdir,
list_files,
create_refresh_button, setup_environment
)
from .custom_logging import setup_logging
# Set up logging
log = setup_logging()
folder_symbol = "\U0001f4c2" # 📂
refresh_symbol = "\U0001f504" # 🔄
save_style_symbol = "\U0001f4be" # 💾
document_symbol = "\U0001F4C4" # 📄
PYTHON = sys.executable
def extract_dylora(
model,
save_to,
unit,
):
# Check for caption_text_input
if model == "":
log.info("Invalid DyLoRA model file")
return
# Check if source model exist
if not os.path.isfile(model):
log.info("The provided DyLoRA model is not a file")
return
if os.path.dirname(save_to) == "":
# only filename given. prepend dir
save_to = os.path.join(os.path.dirname(model), save_to)
if os.path.isdir(save_to):
# only dir name given. set default lora name
save_to = os.path.join(save_to, "lora.safetensors")
if os.path.normpath(model) == os.path.normpath(save_to):
# same path. silently ignore but rename output
path, ext = os.path.splitext(save_to)
save_to = f"{path}_tmp{ext}"
run_cmd = [
rf"{PYTHON}",
rf"{scriptdir}/sd-scripts/networks/extract_lora_from_dylora.py",
"--save_to",
rf"{save_to}",
"--model",
rf"{model}",
"--unit",
str(unit),
]
env = setup_environment()
# Reconstruct the safe command string for display
command_to_run = " ".join(run_cmd)
log.info(f"Executing command: {command_to_run}")
# Run the command in the sd-scripts folder context
subprocess.run(run_cmd, env=env, shell=False)
log.info("Done extracting DyLoRA...")
###
# Gradio UI
###
def gradio_extract_dylora_tab(headless=False):
current_model_dir = os.path.join(scriptdir, "outputs")
current_save_dir = os.path.join(scriptdir, "outputs")
with gr.Tab("Extract DyLoRA"):
gr.Markdown("This utility can extract a DyLoRA network from a finetuned model.")
lora_ext = gr.Textbox(value="*.safetensors *.pt", visible=False)
lora_ext_name = gr.Textbox(value="LoRA model types", visible=False)
def list_models(path):
nonlocal current_model_dir
current_model_dir = path
return list(list_files(path, exts=[".ckpt", ".safetensors"], all=True))
def list_save_to(path):
nonlocal current_save_dir
current_save_dir = path
return list(list_files(path, exts=[".pt", ".safetensors"], all=True))
with gr.Group(), gr.Row():
model = gr.Dropdown(
label="DyLoRA model (path to the DyLoRA model to extract from)",
interactive=True,
choices=[""] + list_models(current_model_dir),
value="",
allow_custom_value=True,
)
create_refresh_button(
model,
lambda: None,
lambda: {"choices": list_models(current_model_dir)},
"open_folder_small",
)
button_model_file = gr.Button(
folder_symbol,
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not headless),
)
button_model_file.click(
get_file_path,
inputs=[model, lora_ext, lora_ext_name],
outputs=model,
show_progress=False,
)
save_to = gr.Dropdown(
label="Save to (path where to save the extracted LoRA model...)",
interactive=True,
choices=[""] + list_save_to(current_save_dir),
value="",
allow_custom_value=True,
)
create_refresh_button(
save_to,
lambda: None,
lambda: {"choices": list_save_to(current_save_dir)},
"open_folder_small",
)
unit = gr.Slider(
minimum=1,
maximum=256,
label="Network Dimension (Rank)",
value=1,
step=1,
interactive=True,
)
model.change(
fn=lambda path: gr.Dropdown(choices=[""] + list_models(path)),
inputs=model,
outputs=model,
show_progress=False,
)
save_to.change(
fn=lambda path: gr.Dropdown(choices=[""] + list_save_to(path)),
inputs=save_to,
outputs=save_to,
show_progress=False,
)
extract_button = gr.Button("Extract LoRA model")
extract_button.click(
extract_dylora,
inputs=[
model,
save_to,
unit,
],
show_progress=False,
)