forked from bmaltais/kohya_ss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_model_gui.py
275 lines (238 loc) · 9.35 KB
/
convert_model_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import gradio as gr
import subprocess
import os
import sys
from .common_gui import get_folder_path, get_file_path, scriptdir, list_files, list_dirs, 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 convert_model(
source_model_input,
source_model_type,
target_model_folder_input,
target_model_name_input,
target_model_type,
target_save_precision_type,
unet_use_linear_projection,
):
# Check for caption_text_input
if source_model_type == "":
log.info("Invalid source model type")
return
# Check if source model exist
if os.path.isfile(source_model_input):
log.info("The provided source model is a file")
elif os.path.isdir(source_model_input):
log.info("The provided model is a folder")
else:
log.info("The provided source model is neither a file nor a folder")
return
# Check if source model exist
if os.path.isdir(target_model_folder_input):
log.info("The provided model folder exist")
else:
log.info("The provided target folder does not exist")
return
run_cmd = [
rf"{PYTHON}",
rf"{scriptdir}/sd-scripts/tools/convert_diffusers20_original_sd.py",
]
v1_models = [
"runwayml/stable-diffusion-v1-5",
"CompVis/stable-diffusion-v1-4",
]
# Check if v1 models
if str(source_model_type) in v1_models:
log.info("SD v1 model specified. Setting --v1 parameter")
run_cmd.append("--v1")
else:
log.info("SD v2 model specified. Setting --v2 parameter")
run_cmd.append("--v2")
if not target_save_precision_type == "unspecified":
run_cmd.append(f"--{target_save_precision_type}")
if target_model_type == "diffuser" or target_model_type == "diffuser_safetensors":
run_cmd.append("--reference_model")
run_cmd.append(source_model_type)
if target_model_type == "diffuser_safetensors":
run_cmd.append("--use_safetensors")
# Fix for stabilityAI diffusers format
if unet_use_linear_projection:
run_cmd.append("--unet_use_linear_projection")
# Add the source model input path
run_cmd.append(rf"{source_model_input}")
# Determine the target model path
if target_model_type == "diffuser" or target_model_type == "diffuser_safetensors":
target_model_path = os.path.join(
target_model_folder_input, target_model_name_input
)
else:
target_model_path = os.path.join(
target_model_folder_input,
f"{target_model_name_input}.{target_model_type}",
)
# Add the target model path
run_cmd.append(rf"{target_model_path}")
# Log the command
log.info(" ".join(run_cmd))
env = setup_environment()
# Run the command
subprocess.run(run_cmd, env=env, shell=False)
###
# Gradio UI
###
def gradio_convert_model_tab(headless=False):
from .common_gui import create_refresh_button
default_source_model = os.path.join(scriptdir, "outputs")
default_target_folder = os.path.join(scriptdir, "outputs")
current_source_model = default_source_model
current_target_folder = default_target_folder
def list_source_model(path):
nonlocal current_source_model
current_source_model = path
return list(list_files(path, exts=[".ckpt", ".safetensors"], all=True))
def list_target_folder(path):
nonlocal current_target_folder
current_target_folder = path
return list(list_dirs(path))
with gr.Tab("Convert model"):
gr.Markdown(
"This utility can be used to convert from one stable diffusion model format to another."
)
model_ext = gr.Textbox(value="*.safetensors *.ckpt", visible=False)
model_ext_name = gr.Textbox(value="Model types", visible=False)
with gr.Group(), gr.Row():
with gr.Column(), gr.Row():
source_model_input = gr.Dropdown(
label="Source model (path to source model folder of file to convert...)",
interactive=True,
choices=[""] + list_source_model(default_source_model),
value="",
allow_custom_value=True,
)
create_refresh_button(
source_model_input,
lambda: None,
lambda: {"choices": list_source_model(current_source_model)},
"open_folder_small",
)
button_source_model_dir = gr.Button(
folder_symbol,
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not headless),
)
button_source_model_dir.click(
get_folder_path,
outputs=source_model_input,
show_progress=False,
)
button_source_model_file = gr.Button(
document_symbol,
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not headless),
)
button_source_model_file.click(
get_file_path,
inputs=[source_model_input, model_ext, model_ext_name],
outputs=source_model_input,
show_progress=False,
)
source_model_input.change(
fn=lambda path: gr.Dropdown(choices=[""] + list_source_model(path)),
inputs=source_model_input,
outputs=source_model_input,
show_progress=False,
)
with gr.Column(), gr.Row():
source_model_type = gr.Dropdown(
label="Source model type",
choices=[
"stabilityai/stable-diffusion-2-1-base",
"stabilityai/stable-diffusion-2-base",
"stabilityai/stable-diffusion-2-1",
"stabilityai/stable-diffusion-2",
"runwayml/stable-diffusion-v1-5",
"CompVis/stable-diffusion-v1-4",
],
allow_custom_value=True,
)
with gr.Group(), gr.Row():
with gr.Column(), gr.Row():
target_model_folder_input = gr.Dropdown(
label="Target model folder (path to target model folder of file name to create...)",
interactive=True,
choices=[""] + list_target_folder(default_target_folder),
value="",
allow_custom_value=True,
)
create_refresh_button(
target_model_folder_input,
lambda: None,
lambda: {"choices": list_target_folder(current_target_folder)},
"open_folder_small",
)
button_target_model_folder = gr.Button(
folder_symbol,
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not headless),
)
button_target_model_folder.click(
get_folder_path,
outputs=target_model_folder_input,
show_progress=False,
)
target_model_folder_input.change(
fn=lambda path: gr.Dropdown(
choices=[""] + list_target_folder(path)
),
inputs=target_model_folder_input,
outputs=target_model_folder_input,
show_progress=False,
)
with gr.Column(), gr.Row():
target_model_name_input = gr.Textbox(
label="Target model name",
placeholder="target model name...",
interactive=True,
)
with gr.Row():
target_model_type = gr.Dropdown(
label="Target model type",
choices=[
"diffuser",
"diffuser_safetensors",
"ckpt",
"safetensors",
],
)
target_save_precision_type = gr.Dropdown(
label="Target model precision",
choices=["unspecified", "fp16", "bf16", "float"],
value="unspecified",
)
unet_use_linear_projection = gr.Checkbox(
label="UNet linear projection",
value=False,
info="Enable for Hugging Face's stabilityai models",
)
convert_button = gr.Button("Convert model")
convert_button.click(
convert_model,
inputs=[
source_model_input,
source_model_type,
target_model_folder_input,
target_model_name_input,
target_model_type,
target_save_precision_type,
unet_use_linear_projection,
],
show_progress=False,
)