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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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Standard library imports
import os
import subprocess
import sys
import json

# Third-party imports
import gradio as gr

# Local module imports
from .common_gui import (
    get_saveasfilename_path,
    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 check_model(model):
    if not model:
        return True
    if not os.path.isfile(model):
        log.info(f"The provided {model} is not a file")
        return False
    return True


def verify_conditions(sd_model, lora_models):
    lora_models_count = sum(1 for model in lora_models if model)
    if sd_model and lora_models_count >= 1:
        return True
    elif not sd_model and lora_models_count >= 2:
        return True
    return False


class GradioMergeLoRaTab:
    def __init__(self, headless=False):
        self.headless = headless
        self.build_tab()

    def save_inputs_to_json(self, file_path, inputs):
        with open(file_path, "w", encoding="utf-8") as file:
            json.dump(inputs, file)
        log.info(f"Saved inputs to {file_path}")

    def load_inputs_from_json(self, file_path):
        with open(file_path, "r", encoding="utf-8") as file:
            inputs = json.load(file)
        log.info(f"Loaded inputs from {file_path}")
        return inputs

    def build_tab(self):
        current_sd_model_dir = os.path.join(scriptdir, "outputs")
        current_save_dir = os.path.join(scriptdir, "outputs")
        current_a_model_dir = current_sd_model_dir
        current_b_model_dir = current_sd_model_dir
        current_c_model_dir = current_sd_model_dir
        current_d_model_dir = current_sd_model_dir

        def list_sd_models(path):
            nonlocal current_sd_model_dir
            current_sd_model_dir = path
            return list(list_files(path, exts=[".ckpt", ".safetensors"], all=True))

        def list_a_models(path):
            nonlocal current_a_model_dir
            current_a_model_dir = path
            return list(list_files(path, exts=[".pt", ".safetensors"], all=True))

        def list_b_models(path):
            nonlocal current_b_model_dir
            current_b_model_dir = path
            return list(list_files(path, exts=[".pt", ".safetensors"], all=True))

        def list_c_models(path):
            nonlocal current_c_model_dir
            current_c_model_dir = path
            return list(list_files(path, exts=[".pt", ".safetensors"], all=True))

        def list_d_models(path):
            nonlocal current_d_model_dir
            current_d_model_dir = path
            return list(list_files(path, exts=[".pt", ".safetensors"], all=True))

        def list_save_to(path):
            nonlocal current_save_dir
            current_save_dir = path
            return list(list_files(path, exts=[".ckpt", ".safetensors"], all=True))

        with gr.Tab("Merge LoRA"):
            gr.Markdown(
                "This utility can merge up to 4 LoRA together or alternatively merge up to 4 LoRA into a SD checkpoint."
            )

            lora_ext = gr.Textbox(value="*.safetensors *.pt", visible=False)
            lora_ext_name = gr.Textbox(value="LoRA model types", visible=False)
            ckpt_ext = gr.Textbox(value="*.safetensors *.ckpt", visible=False)
            ckpt_ext_name = gr.Textbox(value="SD model types", visible=False)

            with gr.Group(), gr.Row():
                sd_model = gr.Dropdown(
                    label="SD Model (Optional. Stable Diffusion model path, if you want to merge it with LoRA files)",
                    interactive=True,
                    choices=[""] + list_sd_models(current_sd_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    sd_model,
                    lambda: None,
                    lambda: {"choices": list_sd_models(current_sd_model_dir)},
                    "open_folder_small",
                )
                sd_model_file = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                sd_model_file.click(
                    get_file_path,
                    inputs=[sd_model, ckpt_ext, ckpt_ext_name],
                    outputs=sd_model,
                    show_progress=False,
                )
                sdxl_model = gr.Checkbox(label="SDXL model", value=False)

                sd_model.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_sd_models(path)),
                    inputs=sd_model,
                    outputs=sd_model,
                    show_progress=False,
                )

            with gr.Group(), gr.Row():
                lora_a_model = gr.Dropdown(
                    label='LoRA model "A" (path to the LoRA A model)',
                    interactive=True,
                    choices=[""] + list_a_models(current_a_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    lora_a_model,
                    lambda: None,
                    lambda: {"choices": list_a_models(current_a_model_dir)},
                    "open_folder_small",
                )
                button_lora_a_model_file = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                button_lora_a_model_file.click(
                    get_file_path,
                    inputs=[lora_a_model, lora_ext, lora_ext_name],
                    outputs=lora_a_model,
                    show_progress=False,
                )

                lora_b_model = gr.Dropdown(
                    label='LoRA model "B" (path to the LoRA B model)',
                    interactive=True,
                    choices=[""] + list_b_models(current_b_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    lora_b_model,
                    lambda: None,
                    lambda: {"choices": list_b_models(current_b_model_dir)},
                    "open_folder_small",
                )
                button_lora_b_model_file = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                button_lora_b_model_file.click(
                    get_file_path,
                    inputs=[lora_b_model, lora_ext, lora_ext_name],
                    outputs=lora_b_model,
                    show_progress=False,
                )

                lora_a_model.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_a_models(path)),
                    inputs=lora_a_model,
                    outputs=lora_a_model,
                    show_progress=False,
                )
                lora_b_model.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_b_models(path)),
                    inputs=lora_b_model,
                    outputs=lora_b_model,
                    show_progress=False,
                )

            with gr.Row():
                ratio_a = gr.Slider(
                    label="Model A merge ratio (eg: 0.5 mean 50%)",
                    minimum=0,
                    maximum=1,
                    step=0.01,
                    value=0.0,
                    interactive=True,
                )

                ratio_b = gr.Slider(
                    label="Model B merge ratio (eg: 0.5 mean 50%)",
                    minimum=0,
                    maximum=1,
                    step=0.01,
                    value=0.0,
                    interactive=True,
                )

            with gr.Group(), gr.Row():
                lora_c_model = gr.Dropdown(
                    label='LoRA model "C" (path to the LoRA C model)',
                    interactive=True,
                    choices=[""] + list_c_models(current_c_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    lora_c_model,
                    lambda: None,
                    lambda: {"choices": list_c_models(current_c_model_dir)},
                    "open_folder_small",
                )
                button_lora_c_model_file = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                button_lora_c_model_file.click(
                    get_file_path,
                    inputs=[lora_c_model, lora_ext, lora_ext_name],
                    outputs=lora_c_model,
                    show_progress=False,
                )

                lora_d_model = gr.Dropdown(
                    label='LoRA model "D" (path to the LoRA D model)',
                    interactive=True,
                    choices=[""] + list_d_models(current_d_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    lora_d_model,
                    lambda: None,
                    lambda: {"choices": list_d_models(current_d_model_dir)},
                    "open_folder_small",
                )
                button_lora_d_model_file = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                button_lora_d_model_file.click(
                    get_file_path,
                    inputs=[lora_d_model, lora_ext, lora_ext_name],
                    outputs=lora_d_model,
                    show_progress=False,
                )
                lora_c_model.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_c_models(path)),
                    inputs=lora_c_model,
                    outputs=lora_c_model,
                    show_progress=False,
                )
                lora_d_model.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_d_models(path)),
                    inputs=lora_d_model,
                    outputs=lora_d_model,
                    show_progress=False,
                )

            with gr.Row():
                ratio_c = gr.Slider(
                    label="Model C merge ratio (eg: 0.5 mean 50%)",
                    minimum=0,
                    maximum=1,
                    step=0.01,
                    value=0.0,
                    interactive=True,
                )

                ratio_d = gr.Slider(
                    label="Model D merge ratio (eg: 0.5 mean 50%)",
                    minimum=0,
                    maximum=1,
                    step=0.01,
                    value=0.0,
                    interactive=True,
                )

            with gr.Group(), gr.Row():
                save_to = gr.Dropdown(
                    label="Save to (path for the file to save...)",
                    interactive=True,
                    choices=[""] + list_save_to(current_d_model_dir),
                    value="",
                    allow_custom_value=True,
                )
                create_refresh_button(
                    save_to,
                    lambda: None,
                    lambda: {"choices": list_save_to(current_save_dir)},
                    "open_folder_small",
                )
                button_save_to = gr.Button(
                    folder_symbol,
                    elem_id="open_folder_small",
                    elem_classes=["tool"],
                    visible=(not self.headless),
                )
                button_save_to.click(
                    get_saveasfilename_path,
                    inputs=[save_to, lora_ext, lora_ext_name],
                    outputs=save_to,
                    show_progress=False,
                )
                precision = gr.Radio(
                    label="Merge precision",
                    choices=["fp16", "bf16", "float"],
                    value="float",
                    interactive=True,
                )
                save_precision = gr.Radio(
                    label="Save precision",
                    choices=["fp16", "bf16", "float"],
                    value="fp16",
                    interactive=True,
                )

                save_to.change(
                    fn=lambda path: gr.Dropdown(choices=[""] + list_save_to(path)),
                    inputs=save_to,
                    outputs=save_to,
                    show_progress=False,
                )

            merge_button = gr.Button("Merge model")

            merge_button.click(
                self.merge_lora,
                inputs=[
                    sd_model,
                    sdxl_model,
                    lora_a_model,
                    lora_b_model,
                    lora_c_model,
                    lora_d_model,
                    ratio_a,
                    ratio_b,
                    ratio_c,
                    ratio_d,
                    save_to,
                    precision,
                    save_precision,
                ],
                show_progress=False,
            )

    def merge_lora(

        self,

        sd_model,

        sdxl_model,

        lora_a_model,

        lora_b_model,

        lora_c_model,

        lora_d_model,

        ratio_a,

        ratio_b,

        ratio_c,

        ratio_d,

        save_to,

        precision,

        save_precision,

    ):

        log.info("Merge model...")
        models = [
            sd_model,
            lora_a_model,
            lora_b_model,
            lora_c_model,
            lora_d_model,
        ]
        lora_models = models[1:]
        ratios = [ratio_a, ratio_b, ratio_c, ratio_d]

        if not verify_conditions(sd_model, lora_models):
            log.info(
                "Warning: Either provide at least one LoRa model along with the sd_model or at least two LoRa models if no sd_model is provided."
            )
            return

        for model in models:
            if not check_model(model):
                return

        if not sdxl_model:
            run_cmd = [rf"{PYTHON}", rf"{scriptdir}/sd-scripts/networks/merge_lora.py"]
        else:
            run_cmd = [
                rf"{PYTHON}",
                rf"{scriptdir}/sd-scripts/networks/sdxl_merge_lora.py",
            ]

        if sd_model:
            run_cmd.append("--sd_model")
            run_cmd.append(rf"{sd_model}")

        run_cmd.append("--save_precision")
        run_cmd.append(save_precision)
        run_cmd.append("--precision")
        run_cmd.append(precision)
        run_cmd.append("--save_to")
        run_cmd.append(rf"{save_to}")

        # Prepare model and ratios command as lists, including only non-empty models
        valid_models = [model for model in lora_models if model]
        valid_ratios = [ratios[i] for i, model in enumerate(lora_models) if model]

        if valid_models:
            run_cmd.append("--models")
            run_cmd.extend(valid_models)  # Each model is a separate argument
            run_cmd.append("--ratios")
            run_cmd.extend(
                map(str, valid_ratios)
            )  # Convert ratios to strings and include them as separate arguments

        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)

        log.info("Done merging...")