File size: 27,340 Bytes
54e3410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
import gradio as gr
from core.settings import VAE_DIR, LORA_DIR, EMBEDDING_DIR, ARCHITECTURES_CONFIG
from utils.app_utils import save_uploaded_file_with_hash
from ui.shared.ui_components import (
    MAX_CONTROLNETS,
    MAX_IPADAPTERS,
    MAX_EMBEDDINGS,
    MAX_CONDITIONINGS,
    MAX_LORAS
)
from .config_loaders import (
    load_controlnet_config,
    load_anima_controlnet_lllite_config,
    load_diffsynth_controlnet_config,
    load_ipadapter_config
)

def on_vae_upload(file_obj):
    if not file_obj:
        return gr.update(), gr.update(), None
    
    hashed_filename = save_uploaded_file_with_hash(file_obj, VAE_DIR)
    return hashed_filename, "File", file_obj


def on_lora_upload(file_obj):
    if not file_obj:
        return gr.update(), gr.update()
    
    hashed_filename = save_uploaded_file_with_hash(file_obj, LORA_DIR)
    return hashed_filename, "File"


def on_embedding_upload(file_obj):
    if not file_obj:
        return gr.update(), gr.update(), None
    
    hashed_filename = save_uploaded_file_with_hash(file_obj, EMBEDDING_DIR)
    return hashed_filename, "File", file_obj


def create_lora_event_handlers(prefix, ui_components):
    lora_rows = ui_components.get(f'lora_rows_{prefix}')
    if not lora_rows: return
    lora_ids = ui_components[f'lora_ids_{prefix}']
    lora_scales = ui_components[f'lora_scales_{prefix}']
    lora_uploads = ui_components[f'lora_uploads_{prefix}']
    count_state = ui_components[f'lora_count_state_{prefix}']
    add_button = ui_components[f'add_lora_button_{prefix}']
    del_button = ui_components[f'delete_lora_button_{prefix}']

    def add_lora_row(c):
        updates = {}
        if c < MAX_LORAS:
            c += 1
            updates[lora_rows[c - 1]] = gr.update(visible=True)
        
        updates[count_state] = c
        updates[add_button] = gr.update(visible=c < MAX_LORAS)
        updates[del_button] = gr.update(visible=c > 1)
        return updates

    def del_lora_row(c):
        updates = {}
        if c > 1:
            updates[lora_rows[c - 1]] = gr.update(visible=False)
            updates[lora_ids[c - 1]] = ""
            updates[lora_scales[c - 1]] = 0.0
            updates[lora_uploads[c - 1]] = None
            c -= 1

        updates[count_state] = c
        updates[add_button] = gr.update(visible=True)
        updates[del_button] = gr.update(visible=c > 1)
        return updates
    
    add_outputs = [count_state, add_button, del_button] + lora_rows
    del_outputs = [count_state, add_button, del_button] + lora_rows + lora_ids + lora_scales + lora_uploads

    add_button.click(add_lora_row, [count_state], add_outputs, show_progress=False)
    del_button.click(del_lora_row, [count_state], del_outputs, show_progress=False)


def create_controlnet_event_handlers(prefix, ui_components):
    cn_rows = ui_components.get(f'controlnet_rows_{prefix}')
    if not cn_rows: return
    cn_types = ui_components[f'controlnet_types_{prefix}']
    cn_series = ui_components[f'controlnet_series_{prefix}']
    cn_filepaths = ui_components[f'controlnet_filepaths_{prefix}']
    cn_images = ui_components[f'controlnet_images_{prefix}']
    cn_strengths = ui_components[f'controlnet_strengths_{prefix}']
    
    count_state = ui_components[f'controlnet_count_state_{prefix}']
    add_button = ui_components[f'add_controlnet_button_{prefix}']
    del_button = ui_components[f'delete_controlnet_button_{prefix}']
    accordion = ui_components[f'controlnet_accordion_{prefix}']

    base_model_comp = ui_components.get(f'base_model_{prefix}')
    actual_arch_comp = base_model_comp if base_model_comp else gr.State("SDXL")

    def add_cn_row(c):
        c += 1
        updates = {
            count_state: c,
            cn_rows[c-1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_CONTROLNETS),
            del_button: gr.update(visible=True)
        }
        return updates

    def del_cn_row(c):
        c -= 1
        updates = {
            count_state: c,
            cn_rows[c]: gr.update(visible=False),
            cn_images[c]: None,
            cn_strengths[c]: 1.0,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0)
        }
        return updates
        
    add_outputs = [count_state, add_button, del_button] + cn_rows
    del_outputs = [count_state, add_button, del_button] + cn_rows + cn_images + cn_strengths
    add_button.click(fn=add_cn_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_cn_row, inputs=[count_state], outputs=del_outputs, show_progress=False)
    
    def on_cn_type_change(selected_type, model_name):
        from core.settings import MODEL_TYPE_MAP
        m_type = MODEL_TYPE_MAP.get(model_name, "SDXL") if model_name else "SDXL"
        cn_full_config = load_controlnet_config()
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)

        cn_config = cn_full_config.get(controlnet_key, [])
        series_choices = []
        if selected_type:
            series_choices = sorted(list(set(
                model.get("Series", "Default") for model in cn_config
                if selected_type in model.get("Type", [])
            )))
        default_series = series_choices[0] if series_choices else None
        filepath = "None"
        if default_series:
            for model in cn_config:
                if model.get("Series") == default_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return gr.update(choices=series_choices, value=default_series), filepath

    def on_cn_series_change(selected_series, selected_type, model_name):
        from core.settings import MODEL_TYPE_MAP
        m_type = MODEL_TYPE_MAP.get(model_name, "SDXL") if model_name else "SDXL"
        cn_full_config = load_controlnet_config()
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)

        cn_config = cn_full_config.get(controlnet_key, [])
        filepath = "None"
        if selected_series and selected_type:
            for model in cn_config:
                if model.get("Series") == selected_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return filepath
        
    for i in range(MAX_CONTROLNETS):
        cn_types[i].change(
            fn=on_cn_type_change,
            inputs=[cn_types[i], actual_arch_comp],
            outputs=[cn_series[i], cn_filepaths[i]],
            show_progress=False
        )
        cn_series[i].change(
            fn=on_cn_series_change,
            inputs=[cn_series[i], cn_types[i], actual_arch_comp],
            outputs=[cn_filepaths[i]],
            show_progress=False
        )

    def on_accordion_expand(*images):
        return [gr.update() for _ in images]
    
    accordion.expand(
        fn=on_accordion_expand,
        inputs=cn_images,
        outputs=cn_images,
        show_progress=False
    )


def create_anima_controlnet_lllite_event_handlers(prefix, ui_components):
    cn_rows = ui_components.get(f'anima_controlnet_lllite_rows_{prefix}')
    if not cn_rows: return
    cn_types = ui_components[f'anima_controlnet_lllite_types_{prefix}']
    cn_series = ui_components[f'anima_controlnet_lllite_series_{prefix}']
    cn_filepaths = ui_components[f'anima_controlnet_lllite_filepaths_{prefix}']
    cn_images = ui_components[f'anima_controlnet_lllite_images_{prefix}']
    cn_strengths = ui_components[f'anima_controlnet_lllite_strengths_{prefix}']
    
    count_state = ui_components[f'anima_controlnet_lllite_count_state_{prefix}']
    add_button = ui_components[f'add_anima_controlnet_lllite_button_{prefix}']
    del_button = ui_components[f'delete_anima_controlnet_lllite_button_{prefix}']
    accordion = ui_components[f'anima_controlnet_lllite_accordion_{prefix}']

    def add_cn_row(c):
        c += 1
        updates = {
            count_state: c,
            cn_rows[c-1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_CONTROLNETS),
            del_button: gr.update(visible=True)
        }
        return updates

    def del_cn_row(c):
        c -= 1
        updates = {
            count_state: c,
            cn_rows[c]: gr.update(visible=False),
            cn_images[c]: None,
            cn_strengths[c]: 1.0,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0)
        }
        return updates
        
    add_outputs = [count_state, add_button, del_button] + cn_rows
    del_outputs = [count_state, add_button, del_button] + cn_rows + cn_images + cn_strengths
    add_button.click(fn=add_cn_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_cn_row, inputs=[count_state], outputs=del_outputs, show_progress=False)
    
    def on_cn_type_change(selected_type):
        cn_config = load_anima_controlnet_lllite_config()
        series_choices = []
        if selected_type:
            series_choices = sorted(list(set(
                model.get("Series", "Default") for model in cn_config
                if selected_type in model.get("Type", [])
            )))
        default_series = series_choices[0] if series_choices else None
        filepath = "None"
        if default_series:
            for model in cn_config:
                if model.get("Series") == default_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return gr.update(choices=series_choices, value=default_series), filepath

    def on_cn_series_change(selected_series, selected_type):
        cn_config = load_anima_controlnet_lllite_config()
        filepath = "None"
        if selected_series and selected_type:
            for model in cn_config:
                if model.get("Series") == selected_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return filepath
        
    for i in range(MAX_CONTROLNETS):
        cn_types[i].change(
            fn=on_cn_type_change,
            inputs=[cn_types[i]],
            outputs=[cn_series[i], cn_filepaths[i]],
            show_progress=False
        )
        cn_series[i].change(
            fn=on_cn_series_change,
            inputs=[cn_series[i], cn_types[i]],
            outputs=[cn_filepaths[i]],
            show_progress=False
        )

    def on_accordion_expand(*images):
        return [gr.update() for _ in images]
    
    accordion.expand(
        fn=on_accordion_expand,
        inputs=cn_images,
        outputs=cn_images,
        show_progress=False
    )


def create_diffsynth_controlnet_event_handlers(prefix, ui_components):
    cn_rows = ui_components.get(f'diffsynth_controlnet_rows_{prefix}')
    if not cn_rows: return
    cn_types = ui_components[f'diffsynth_controlnet_types_{prefix}']
    cn_series = ui_components[f'diffsynth_controlnet_series_{prefix}']
    cn_filepaths = ui_components[f'diffsynth_controlnet_filepaths_{prefix}']
    cn_images = ui_components[f'diffsynth_controlnet_images_{prefix}']
    cn_strengths = ui_components[f'diffsynth_controlnet_strengths_{prefix}']
    
    count_state = ui_components[f'diffsynth_controlnet_count_state_{prefix}']
    add_button = ui_components[f'add_diffsynth_controlnet_button_{prefix}']
    del_button = ui_components[f'delete_diffsynth_controlnet_button_{prefix}']
    accordion = ui_components[f'diffsynth_controlnet_accordion_{prefix}']

    base_model_comp = ui_components.get(f'base_model_{prefix}')
    actual_arch_comp = base_model_comp if base_model_comp else gr.State("SDXL")

    def add_cn_row(c):
        c += 1
        updates = {
            count_state: c,
            cn_rows[c-1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_CONTROLNETS),
            del_button: gr.update(visible=True)
        }
        return updates

    def del_cn_row(c):
        c -= 1
        updates = {
            count_state: c,
            cn_rows[c]: gr.update(visible=False),
            cn_images[c]: None,
            cn_strengths[c]: 1.0,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0)
        }
        return updates
        
    add_outputs = [count_state, add_button, del_button] + cn_rows
    del_outputs = [count_state, add_button, del_button] + cn_rows + cn_images + cn_strengths
    add_button.click(fn=add_cn_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_cn_row, inputs=[count_state], outputs=del_outputs, show_progress=False)
    
    def on_cn_type_change(selected_type, model_name):
        from core.settings import MODEL_TYPE_MAP
        m_type = MODEL_TYPE_MAP.get(model_name, "SDXL") if model_name else "SDXL"
        cn_full_config = load_diffsynth_controlnet_config()
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)

        cn_config = cn_full_config.get(controlnet_key, [])
        series_choices = []
        if selected_type:
            series_choices = sorted(list(set(
                model.get("Series", "Default") for model in cn_config
                if selected_type in model.get("Type", [])
            )))
        default_series = series_choices[0] if series_choices else None
        filepath = "None"
        if default_series:
            for model in cn_config:
                if model.get("Series") == default_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return gr.update(choices=series_choices, value=default_series), filepath

    def on_cn_series_change(selected_series, selected_type, model_name):
        from core.settings import MODEL_TYPE_MAP
        m_type = MODEL_TYPE_MAP.get(model_name, "SDXL") if model_name else "SDXL"
        cn_full_config = load_diffsynth_controlnet_config()
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)

        cn_config = cn_full_config.get(controlnet_key, [])
        filepath = "None"
        if selected_series and selected_type:
            for model in cn_config:
                if model.get("Series") == selected_series and selected_type in model.get("Type", []):
                    filepath = model.get("Filepath")
                    break
        return filepath
        
    for i in range(MAX_CONTROLNETS):
        cn_types[i].change(
            fn=on_cn_type_change,
            inputs=[cn_types[i], actual_arch_comp],
            outputs=[cn_series[i], cn_filepaths[i]],
            show_progress=False
        )
        cn_series[i].change(
            fn=on_cn_series_change,
            inputs=[cn_series[i], cn_types[i], actual_arch_comp],
            outputs=[cn_filepaths[i]],
            show_progress=False
        )

    def on_accordion_expand(*images):
        return [gr.update() for _ in images]
    
    accordion.expand(
        fn=on_accordion_expand,
        inputs=cn_images,
        outputs=cn_images,
        show_progress=False
    )


def create_flux1_ipadapter_event_handlers(prefix, ui_components):
    fipa_rows = ui_components.get(f'flux1_ipadapter_rows_{prefix}')
    if not fipa_rows: return
    count_state = ui_components[f'flux1_ipadapter_count_state_{prefix}']
    add_button = ui_components[f'add_flux1_ipadapter_button_{prefix}']
    del_button = ui_components[f'delete_flux1_ipadapter_button_{prefix}']

    def add_fipa_row(c):
        c += 1
        return {
            count_state: c,
            fipa_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_IPADAPTERS),
            del_button: gr.update(visible=True),
        }

    def del_fipa_row(c):
        c -= 1
        return {
            count_state: c,
            fipa_rows[c]: gr.update(visible=False),
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + fipa_rows
    del_outputs = [count_state, add_button, del_button] + fipa_rows
    add_button.click(fn=add_fipa_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_fipa_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_sd3_ipadapter_event_handlers(prefix, ui_components):
    ipa_rows = ui_components.get(f'sd3_ipadapter_rows_{prefix}')
    if not ipa_rows: return
    count_state = ui_components[f'sd3_ipadapter_count_state_{prefix}']
    add_button = ui_components[f'add_sd3_ipadapter_button_{prefix}']
    del_button = ui_components[f'delete_sd3_ipadapter_button_{prefix}']
    images = ui_components[f'sd3_ipadapter_images_{prefix}']
    weights = ui_components[f'sd3_ipadapter_weights_{prefix}']
    start_percents = ui_components[f'sd3_ipadapter_start_percents_{prefix}']
    end_percents = ui_components[f'sd3_ipadapter_end_percents_{prefix}']

    def add_ipa_row(c):
        c += 1
        return {
            count_state: c,
            ipa_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_IPADAPTERS),
            del_button: gr.update(visible=True),
        }

    def del_ipa_row(c):
        c -= 1
        return {
            count_state: c,
            ipa_rows[c]: gr.update(visible=False),
            images[c]: None,
            weights[c]: 0.5,
            start_percents[c]: 0.0,
            end_percents[c]: 1.0,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + ipa_rows
    del_outputs = [count_state, add_button, del_button] + ipa_rows + images + weights + start_percents + end_percents
    add_button.click(fn=add_ipa_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_ipa_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_style_event_handlers(prefix, ui_components):
    style_rows = ui_components.get(f'style_rows_{prefix}')
    if not style_rows: return
    count_state = ui_components[f'style_count_state_{prefix}']
    add_button = ui_components[f'add_style_button_{prefix}']
    del_button = ui_components[f'delete_style_button_{prefix}']

    def add_style_row(c):
        c += 1
        return {
            count_state: c,
            style_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < 5),
            del_button: gr.update(visible=True),
        }

    def del_style_row(c):
        c -= 1
        return {
            count_state: c,
            style_rows[c]: gr.update(visible=False),
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + style_rows
    del_outputs = [count_state, add_button, del_button] + style_rows
    add_button.click(fn=add_style_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_style_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_ipadapter_event_handlers(prefix, ui_components):
    ipa_rows = ui_components.get(f'ipadapter_rows_{prefix}')
    if not ipa_rows: return
    ipa_lora_strengths = ui_components[f'ipadapter_lora_strengths_{prefix}']
    ipa_final_preset = ui_components[f'ipadapter_final_preset_{prefix}']
    ipa_final_lora_strength = ui_components[f'ipadapter_final_lora_strength_{prefix}']
    count_state = ui_components[f'ipadapter_count_state_{prefix}']
    add_button = ui_components[f'add_ipadapter_button_{prefix}']
    del_button = ui_components[f'delete_ipadapter_button_{prefix}']
    accordion = ui_components[f'ipadapter_accordion_{prefix}']

    def add_ipa_row(c):
        c += 1
        return {
            count_state: c,
            ipa_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_IPADAPTERS),
            del_button: gr.update(visible=True),
        }

    def del_ipa_row(c):
        c -= 1
        return {
            count_state: c,
            ipa_rows[c]: gr.update(visible=False),
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + ipa_rows
    del_outputs = [count_state, add_button, del_button] + ipa_rows
    add_button.click(fn=add_ipa_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_ipa_row, inputs=[count_state], outputs=del_outputs, show_progress=False)

    def on_preset_change(preset_value):
        config = load_ipadapter_config()
        faceid_presets = []
        if config:
            faceid_presets.extend(config.get("IPAdapter_FaceID_presets", {}).get("SDXL", []))
            faceid_presets.extend(config.get("IPAdapter_FaceID_presets", {}).get("SD1.5", []))

        is_visible = preset_value in faceid_presets
        updates = [gr.update(visible=is_visible)] * (MAX_IPADAPTERS + 1)
        return updates

    all_lora_strength_sliders = [ipa_final_lora_strength] + ipa_lora_strengths
    ipa_final_preset.change(fn=on_preset_change, inputs=[ipa_final_preset], outputs=all_lora_strength_sliders, show_progress=False)
    
    accordion.expand(fn=lambda *imgs: [gr.update() for _ in imgs], inputs=ui_components[f'ipadapter_images_{prefix}'], outputs=ui_components[f'ipadapter_images_{prefix}'], show_progress=False)


def create_reference_latent_event_handlers(prefix, ui_components):
    ref_rows = ui_components.get(f'reference_latent_rows_{prefix}')
    if not ref_rows: return
    count_state = ui_components[f'reference_latent_count_state_{prefix}']
    add_button = ui_components[f'add_reference_latent_button_{prefix}']
    del_button = ui_components[f'delete_reference_latent_button_{prefix}']
    images = ui_components[f'reference_latent_images_{prefix}']

    def add_ref_row(c):
        c += 1
        return {
            count_state: c,
            ref_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < 10),
            del_button: gr.update(visible=True),
        }

    def del_ref_row(c):
        c -= 1
        return {
            count_state: c,
            ref_rows[c]: gr.update(visible=False),
            images[c]: None,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + ref_rows
    del_outputs = [count_state, add_button, del_button] + ref_rows + images
    add_button.click(fn=add_ref_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_ref_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_hidream_o1_reference_event_handlers(prefix, ui_components):
    ref_rows = ui_components.get(f'hidream_o1_reference_rows_{prefix}')
    if not ref_rows: return
    count_state = ui_components[f'hidream_o1_reference_count_state_{prefix}']
    add_button = ui_components[f'add_hidream_o1_reference_button_{prefix}']
    del_button = ui_components[f'delete_hidream_o1_reference_button_{prefix}']
    images = ui_components[f'hidream_o1_reference_images_{prefix}']

    def add_ref_row(c):
        c += 1
        return {
            count_state: c,
            ref_rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < 10),
            del_button: gr.update(visible=True),
        }

    def del_ref_row(c):
        c -= 1
        return {
            count_state: c,
            ref_rows[c]: gr.update(visible=False),
            images[c]: None,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + ref_rows
    del_outputs = [count_state, add_button, del_button] + ref_rows + images
    add_button.click(fn=add_ref_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_ref_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_embedding_event_handlers(prefix, ui_components):
    rows = ui_components.get(f'embedding_rows_{prefix}')
    if not rows: return
    ids = ui_components[f'embeddings_ids_{prefix}']
    files = ui_components[f'embeddings_files_{prefix}']
    count_state = ui_components[f'embedding_count_state_{prefix}']
    add_button = ui_components[f'add_embedding_button_{prefix}']
    del_button = ui_components[f'delete_embedding_button_{prefix}']

    def add_row(c):
        c += 1
        return {
            count_state: c,
            rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_EMBEDDINGS),
            del_button: gr.update(visible=True)
        }

    def del_row(c):
        c -= 1
        return {
            count_state: c,
            rows[c]: gr.update(visible=False),
            ids[c]: "",
            files[c]: None,
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0)
        }
    
    add_outputs = [count_state, add_button, del_button] + rows
    del_outputs = [count_state, add_button, del_button] + rows + ids + files
    add_button.click(fn=add_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_row, inputs=[count_state], outputs=del_outputs, show_progress=False)


def create_conditioning_event_handlers(prefix, ui_components):
    rows = ui_components.get(f'conditioning_rows_{prefix}')
    if not rows: return
    prompts = ui_components[f'conditioning_prompts_{prefix}']
    count_state = ui_components[f'conditioning_count_state_{prefix}']
    add_button = ui_components[f'add_conditioning_button_{prefix}']
    del_button = ui_components[f'delete_conditioning_button_{prefix}']
    
    def add_row(c):
        c += 1
        return {
            count_state: c,
            rows[c - 1]: gr.update(visible=True),
            add_button: gr.update(visible=c < MAX_CONDITIONINGS),
            del_button: gr.update(visible=True),
        }

    def del_row(c):
        c -= 1
        return {
            count_state: c,
            rows[c]: gr.update(visible=False),
            prompts[c]: "",
            add_button: gr.update(visible=True),
            del_button: gr.update(visible=c > 0),
        }

    add_outputs = [count_state, add_button, del_button] + rows
    del_outputs = [count_state, add_button, del_button] + rows + prompts
    add_button.click(fn=add_row, inputs=[count_state], outputs=add_outputs, show_progress=False)
    del_button.click(fn=del_row, inputs=[count_state], outputs=del_outputs, show_progress=False)