File size: 37,253 Bytes
25e2f7f
d34ac72
 
 
 
 
 
0a6a405
de8ac53
d34ac72
 
 
de8ac53
5302530
c60bd9d
f0c3651
f1d6334
cd39c08
 
 
 
 
 
5302530
 
 
d0d2198
5302530
f0c3651
 
5302530
 
 
 
 
f390e2a
5302530
f0c3651
 
5302530
 
f2884b5
d0d2198
5302530
d0d2198
5302530
d0d2198
5302530
 
f2884b5
5302530
f2884b5
5302530
8ac6201
d0d2198
5302530
 
d0d2198
5302530
 
f2884b5
5302530
8ac6201
d0d2198
5302530
 
d0d2198
 
5302530
 
 
d34ac72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5302530
d0d2198
 
 
 
 
 
 
 
 
 
f0c3651
 
 
 
 
 
 
 
 
 
 
 
 
d0d2198
 
f0c3651
 
 
 
 
 
 
 
 
 
 
 
d0d2198
 
 
d34ac72
 
 
f1d6334
d0d2198
cd39c08
09fa6ac
b397bfd
fd8a02a
d34ac72
de8ac53
cd39c08
 
 
 
de8ac53
0139658
d34ac72
 
 
de8ac53
 
 
 
 
 
 
 
 
 
cd39c08
 
 
 
 
 
d34ac72
 
 
 
 
 
d0d2198
635b226
f1d6334
0792a15
 
 
635b226
0792a15
d0d2198
 
09fa6ac
d34ac72
 
de8ac53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34ac72
 
 
 
 
 
 
 
de8ac53
 
 
 
d34ac72
de8ac53
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ac6201
cd39c08
8ca7ab7
c60bd9d
cd39c08
 
 
 
 
 
 
 
 
 
 
 
de8ac53
 
 
 
 
f0c3651
cd39c08
a138792
0a6a405
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
de8ac53
 
 
 
 
 
 
 
 
 
 
c533c6b
 
 
82bcda0
c533c6b
 
 
 
 
 
 
 
 
de8ac53
cd39c08
de8ac53
 
c60bd9d
c533c6b
 
 
82bcda0
de8ac53
cd39c08
de8ac53
 
 
82bcda0
f1d6334
 
 
 
 
d0d2198
f1d6334
 
c533c6b
 
 
 
 
 
 
 
 
 
 
f1d6334
d34ac72
 
 
d0d2198
 
 
d34ac72
de8ac53
 
 
d0d2198
 
 
de8ac53
 
 
d0d2198
 
 
de8ac53
d34ac72
 
db65c96
f1d6334
d0d2198
 
 
db65c96
d34ac72
 
f1d6334
d0d2198
 
 
d34ac72
 
d0d2198
f1d6334
 
 
 
d0d2198
 
 
f1d6334
d0d2198
d34ac72
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
d0d2198
635b226
 
 
 
 
 
 
 
 
cd39c08
 
 
 
635b226
cd39c08
 
 
 
 
 
f0c3651
f1d6334
 
 
 
 
 
 
 
d0d2198
 
635b226
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5404b87
cd39c08
5404b87
 
 
cd39c08
 
 
 
 
 
 
 
 
 
5404b87
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
5404b87
 
cd39c08
 
5404b87
cd39c08
 
 
 
 
 
 
 
 
5404b87
cd39c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5404b87
cd39c08
 
455cf48
cd39c08
455cf48
 
 
 
cd39c08
455cf48
 
 
 
 
cd39c08
455cf48
 
 
 
 
 
5404b87
455cf48
 
 
 
 
 
 
 
c6c9f0f
455cf48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5404b87
cd39c08
 
455cf48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34ac72
 
d6802e8
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
import spaces
import gradio as gr
import json
import logging
import torch
from PIL import Image
from diffusers import DiffusionPipeline
from diffusers import FluxControlNetPipeline, FluxControlNetModel, FluxMultiControlNetModel
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
import copy
import random
import time

from mod import (models, clear_cache, get_repo_safetensors, is_repo_name, is_repo_exists,
                 description_ui, num_loras, compose_lora_json, is_valid_lora, fuse_loras,
                 get_trigger_word, enhance_prompt, deselect_lora, num_cns, set_control_union_image,
                 get_control_union_mode, set_control_union_mode, get_control_params)
from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
                  download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
                  update_loras)
from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
from tagger.fl2flux import predict_tags_fl2_flux

# Initialize the base model
base_model = models[0]
controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union'
#controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union-alpha'
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
controlnet_union = None
controlnet = None
last_model = models[0]
last_cn_on = False

# https://huggingface.co/InstantX/FLUX.1-dev-Controlnet-Union
# https://huggingface.co/spaces/jiuface/FLUX.1-dev-Controlnet-Union
def change_base_model(repo_id: str, cn_on: bool):
    global pipe
    global controlnet_union
    global controlnet
    global last_model
    global last_cn_on
    dtype = torch.bfloat16
    #dtype = torch.float8_e4m3fn
    try:
        if (repo_id == last_model and cn_on is last_cn_on) or not is_repo_name(repo_id) or not is_repo_exists(repo_id): return gr.update(visible=True)
        if cn_on:
            #progress(0, desc=f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
            print(f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
            clear_cache()
            controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union_repo, torch_dtype=dtype)
            controlnet = FluxMultiControlNetModel([controlnet_union])
            pipe = FluxControlNetPipeline.from_pretrained(repo_id, controlnet=controlnet, torch_dtype=dtype)
            last_model = repo_id
            last_cn_on = cn_on
            #progress(1, desc=f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
            print(f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
        else:
            #progress(0, desc=f"Loading model: {repo_id}")
            print(f"Loading model: {repo_id}")
            clear_cache()
            pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=dtype)
            last_model = repo_id
            last_cn_on = cn_on
            #progress(1, desc=f"Model loaded: {repo_id}")
            print(f"Model loaded: {repo_id}")
    except Exception as e:
        print(f"Model load Error: {e}")
        raise gr.Error(f"Model load Error: {e}")
    return gr.update(visible=True)

change_base_model.zerogpu = True

# Load LoRAs from JSON file
with open('loras.json', 'r') as f:
    loras = json.load(f)

MAX_SEED = 2**32-1

class calculateDuration:
    def __init__(self, activity_name=""):
        self.activity_name = activity_name

    def __enter__(self):
        self.start_time = time.time()
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        self.end_time = time.time()
        self.elapsed_time = self.end_time - self.start_time
        if self.activity_name:
            print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
        else:
            print(f"Elapsed time: {self.elapsed_time:.6f} seconds")

def update_selection(evt: gr.SelectData, width, height):
    selected_lora = loras[evt.index]
    new_placeholder = f"Type a prompt for {selected_lora['title']}"
    lora_repo = selected_lora["repo"]
    updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
    if "aspect" in selected_lora:
        if selected_lora["aspect"] == "portrait":
            width = 768
            height = 1024
        elif selected_lora["aspect"] == "landscape":
            width = 1024
            height = 768
    return (
        gr.update(placeholder=new_placeholder),
        updated_text,
        evt.index,
        width,
        height,
    )

@spaces.GPU(duration=70)
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress=gr.Progress(track_tqdm=True)):
    global pipe
    global controlnet
    global controlnet_union
    try:
        pipe.to("cuda")
        generator = torch.Generator(device="cuda").manual_seed(seed)
        
        with calculateDuration("Generating image"):
            # Generate image
            modes, images, scales = get_control_params()
            if not cn_on or len(modes) == 0:
                progress(0, desc="Start Inference.")
                image = pipe(
                    prompt=prompt_mash,
                    num_inference_steps=steps,
                    guidance_scale=cfg_scale,
                    width=width,
                    height=height,
                    generator=generator,
                    joint_attention_kwargs={"scale": lora_scale},
                ).images[0]
            else:
                progress(0, desc="Start Inference with ControlNet.")
                if controlnet is not None: controlnet.to("cuda")
                if controlnet_union is not None: controlnet_union.to("cuda")
                image = pipe(
                    prompt=prompt_mash,
                    control_image=images,
                    control_mode=modes,
                    num_inference_steps=steps,
                    guidance_scale=cfg_scale,
                    width=width,
                    height=height,
                    controlnet_conditioning_scale=scales,
                    generator=generator,
                    joint_attention_kwargs={"scale": lora_scale},
                ).images[0]
    except Exception as e:
        print(e)
        raise gr.Error(f"Inference Error: {e}")
    return image

def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,

              lora_scale, lora_json, cn_on, progress=gr.Progress(track_tqdm=True)):
    global pipe
    if selected_index is None and not is_valid_lora(lora_json):
        gr.Info("LoRA isn't selected.")
    #    raise gr.Error("You must select a LoRA before proceeding.")
    progress(0, desc="Preparing Inference.")

    prompt_mash = prompt
    if is_valid_lora(lora_json):
        with calculateDuration("Loading LoRA weights"):
            fuse_loras(pipe, lora_json)
            trigger_word = get_trigger_word(lora_json)
            prompt_mash = f"{prompt} {trigger_word}"
    if selected_index is not None:
        selected_lora = loras[selected_index]
        lora_path = selected_lora["repo"]
        trigger_word = selected_lora["trigger_word"]
        if(trigger_word):
            if "trigger_position" in selected_lora:
                if selected_lora["trigger_position"] == "prepend":
                    prompt_mash = f"{trigger_word} {prompt}"
                else:
                    prompt_mash = f"{prompt} {trigger_word}"
            else:
                prompt_mash = f"{trigger_word} {prompt}"
        else:
            prompt_mash = prompt
        # Load LoRA weights
        with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
            if "weights" in selected_lora:
                pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
            else:
                pipe.load_lora_weights(lora_path)
        
    # Set random seed for reproducibility
    with calculateDuration("Randomizing seed"):
        if randomize_seed:
            seed = random.randint(0, MAX_SEED)
    
    progress(0, desc="Running Inference.")

    image = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress)
    if is_valid_lora(lora_json):
        pipe.unfuse_lora()
        pipe.unload_lora_weights()
    if selected_index is not None: pipe.unload_lora_weights()
    pipe.to("cpu")
    if controlnet is not None: controlnet.to("cpu")
    if controlnet_union is not None: controlnet_union.to("cpu")
    clear_cache()
    return image, seed  

def get_huggingface_safetensors(link):
  split_link = link.split("/")
  if(len(split_link) == 2):
            model_card = ModelCard.load(link)
            base_model = model_card.data.get("base_model")
            print(base_model)
            if((base_model != "black-forest-labs/FLUX.1-dev") and (base_model != "black-forest-labs/FLUX.1-schnell")):
                raise Exception("Not a FLUX LoRA!")
            image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
            trigger_word = model_card.data.get("instance_prompt", "")
            image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
            fs = HfFileSystem()
            try:
                list_of_files = fs.ls(link, detail=False)
                for file in list_of_files:
                    if(file.endswith(".safetensors")):
                        safetensors_name = file.split("/")[-1]
                    if (not image_url and file.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))):
                      image_elements = file.split("/")
                      image_url = f"https://huggingface.co/{link}/resolve/main/{image_elements[-1]}"
            except Exception as e:
              print(e)
              gr.Warning(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
              raise Exception(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
            return split_link[1], link, safetensors_name, trigger_word, image_url

def check_custom_model(link):
    if(link.startswith("https://")):
        if(link.startswith("https://huggingface.co") or link.startswith("https://www.huggingface.co")):
            link_split = link.split("huggingface.co/")
            return get_huggingface_safetensors(link_split[1])
    else: 
        return get_huggingface_safetensors(link)

def add_custom_lora(custom_lora):
    global loras
    if(custom_lora):
        try:
            title, repo, path, trigger_word, image = check_custom_model(custom_lora)
            print(f"Loaded custom LoRA: {repo}")
            card = f'''

            <div class="custom_lora_card">

              <span>Loaded custom LoRA:</span>

              <div class="card_internal">

                <img src="{image}" />

                <div>

                    <h3>{title}</h3>

                    <small>{"Using: <code><b>"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>

                </div>

              </div>

            </div>

            '''
            existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
            if(not existing_item_index):
                new_item = {
                    "image": image,
                    "title": title,
                    "repo": repo,
                    "weights": path,
                    "trigger_word": trigger_word
                }
                print(new_item)
                existing_item_index = len(loras)
                loras.append(new_item)
        
            return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
        except Exception as e:
            gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
            return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-FLUX LoRA"), gr.update(visible=True), gr.update(), "", None, ""
    else:
        return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""

def remove_custom_lora():
    return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""

run_lora.zerogpu = True

css = '''

#gen_btn{height: 100%}

#title{text-align: center}

#title h1{font-size: 3em; display:inline-flex; align-items:center}

#title img{width: 100px; margin-right: 0.5em}

#gallery .grid-wrap{height: 10vh}

#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}

.card_internal{display: flex;height: 100px;margin-top: .5em}

.card_internal img{margin-right: 1em}

.styler{--form-gap-width: 0px !important}

'''
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
    with gr.Tab("FLUX LoRA the Explorer"):
        title = gr.HTML(
            """<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA">FLUX LoRA the Explorer Mod</h1>""",
            elem_id="title",
        )
        selected_index = gr.State(None)
        with gr.Row():
            with gr.Column(scale=3):
                with gr.Group():
                    with gr.Accordion("Generate Prompt from Image", open=False):
                        tagger_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
                        with gr.Accordion(label="Advanced options", open=False):
                            tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
                            tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
                            neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="", visible=False)
                            v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2, visible=False)
                            v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2, visible=False)
                            v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False, visible=False)
                        tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-Flux"], label="Algorithms", value=["Use WD Tagger"])
                        tagger_generate_from_image = gr.Button(value="Generate Prompt from Image")
                    prompt = gr.Textbox(label="Prompt", lines=1, max_lines=8, placeholder="Type a prompt")
                    prompt_enhance = gr.Button(value="Enhance your prompt", variant="secondary")
            with gr.Column(scale=1, elem_id="gen_column"):
                generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
        with gr.Row():
            with gr.Column(scale=3):
                selected_info = gr.Markdown("")
                gallery = gr.Gallery(
                    [(item["image"], item["title"]) for item in loras],
                    label="LoRA Gallery",
                    allow_preview=False,
                    columns=3,
                    elem_id="gallery"
                )
                with gr.Group():
                    custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="multimodalart/vintage-ads-flux")
                    gr.Markdown("[Check the list of FLUX LoRas](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
                custom_lora_info = gr.HTML(visible=False)
                custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
                deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
            with gr.Column(scale=4):
                result = gr.Image(label="Generated Image", format="png", show_share_button=False)
        model_name = gr.Dropdown(label="Base Model", info="You can enter a huggingface model repo_id to want to use.", choices=models, value=models[0], allow_custom_value=True)
        with gr.Row():
            with gr.Accordion("Advanced Settings", open=False):
                with gr.Column():
                    with gr.Row():
                        cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
                        steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
                    with gr.Row():
                        width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
                        height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
                    with gr.Row():
                        randomize_seed = gr.Checkbox(True, label="Randomize seed")
                        seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
                        lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=0.95)
                    with gr.Accordion("External LoRA", open=True):
                        with gr.Column():
                            lora_repo_json = gr.JSON(value=[{}] * num_loras, visible=False)
                            lora_repo = [None] * num_loras
                            lora_weights = [None] * num_loras
                            lora_trigger = [None] * num_loras
                            lora_wt = [None] * num_loras
                            lora_info = [None] * num_loras
                            lora_copy = [None] * num_loras
                            lora_md = [None] * num_loras
                            lora_num = [None] * num_loras
                            with gr.Row():
                                for i in range(num_loras):
                                    with gr.Column():
                                        lora_repo[i] = gr.Dropdown(label=f"LoRA {int(i+1)} Repo", choices=get_all_lora_tupled_list(), info="Input LoRA Repo ID", value="", allow_custom_value=True)
                                        with gr.Row():
                                            lora_weights[i] = gr.Dropdown(label=f"LoRA {int(i+1)} Filename", choices=[], info="Optional", value="", allow_custom_value=True)
                                            lora_trigger[i] = gr.Textbox(label=f"LoRA {int(i+1)} Trigger Prompt", lines=1, max_lines=4, value="")
                                            lora_wt[i] = gr.Slider(label=f"LoRA {int(i+1)} Scale", minimum=-2, maximum=2, step=0.01, value=1.00)
                                        with gr.Row():
                                            lora_info[i] = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                            lora_copy[i] = gr.Button(value="Copy example to prompt", visible=False)
                                            lora_md[i] = gr.Markdown(value="", visible=False)
                                            lora_num[i] = gr.Number(i, visible=False)
                            with gr.Accordion("From URL", open=True, visible=True):
                                with gr.Row():
                                    lora_search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
                                    lora_search_civitai_submit = gr.Button("Search on Civitai")
                                    lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Flux.1 D", "Flux.1 S"], value=["Flux.1 D", "Flux.1 S"])
                                with gr.Row():
                                    lora_search_civitai_json = gr.JSON(value={}, visible=False)
                                    lora_search_civitai_desc = gr.Markdown(value="", visible=False)
                                lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
                                lora_download_url = gr.Textbox(label="URL", placeholder="http://...my_lora_url.safetensors", lines=1)
                                with gr.Row():
                                    lora_download = [None] * num_loras
                                    for i in range(num_loras):
                                        lora_download[i] = gr.Button(f"Get and set LoRA to {int(i+1)}")
                    with gr.Accordion("ControlNet (🚧Under construction...🚧)", open=False):
                        with gr.Column():
                            cn_on = gr.Checkbox(False, label="Use ControlNet")
                            cn_mode = [None] * num_cns
                            cn_scale = [None] * num_cns
                            cn_image = [None] * num_cns
                            cn_image_ref = [None] * num_cns
                            cn_res = [None] * num_cns
                            cn_num = [None] * num_cns
                            with gr.Row():
                                for i in range(num_cns):
                                    with gr.Column():
                                        with gr.Row():
                                            cn_mode[i] = gr.Dropdown(label=f"ControlNet {int(i+1)} Mode", choices=get_control_union_mode(), value=get_control_union_mode()[0], allow_custom_value=False)
                                            cn_scale[i] = gr.Slider(label=f"ControlNet {int(i+1)} Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.75)
                                            cn_res[i] = gr.Slider(label=f"ControlNet {int(i+1)} Preprocess resolution", minimum=128, maximum=512, value=384, step=1)
                                            cn_num[i] = gr.Number(i, visible=False)
                                        with gr.Row():
                                            cn_image_ref[i] = gr.Image(label="Image Reference", type="pil", format="png", height=256, sources=["upload", "clipboard"], show_share_button=False)
                                            cn_image[i] = gr.Image(label="Control Image", type="pil", format="png", height=256, show_share_button=False, interactive=False)

    gallery.select(
        update_selection,
        inputs=[width, height],
        outputs=[prompt, selected_info, selected_index, width, height],
        queue=False,
        show_api=False,
    )
    custom_lora.input(
        add_custom_lora,
        inputs=[custom_lora],
        outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt],
        queue=False,
        show_api=False,
    )
    custom_lora_button.click(
        remove_custom_lora,
        outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora],
        queue=False,
        show_api=False,
    )
    gr.on(
        triggers=[generate_button.click, prompt.submit],
        fn=change_base_model,
        inputs=[model_name, cn_on],
        outputs=[result],
        queue=False,
        show_api=False,
    ).success(
        fn=run_lora,
        inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
                 lora_scale, lora_repo_json, cn_on], 
        outputs=[result, seed],
        queue=True,
        show_api=True,
    )

    deselect_lora_button.click(deselect_lora, None, [prompt, selected_info, selected_index, width, height], queue=False, show_api=False)
    gr.on(
        triggers=[model_name.change, cn_on.change],
        fn=change_base_model,
        inputs=[model_name, cn_on],
        outputs=[result],
        queue=True,
        show_api=False,
    )
    prompt_enhance.click(enhance_prompt, [prompt], [prompt], queue=False, show_api=False)

    gr.on(
        triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit],
        fn=search_civitai_lora,
        inputs=[lora_search_civitai_query, lora_search_civitai_basemodel],
        outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
        scroll_to_output=True,
        queue=True,
        show_api=False,
    )
    lora_search_civitai_json.change(search_civitai_lora_json, [lora_search_civitai_query, lora_search_civitai_basemodel], [lora_search_civitai_json], queue=True, show_api=True)  # fn for api
    lora_search_civitai_result.change(select_civitai_lora, [lora_search_civitai_result], [lora_download_url, lora_search_civitai_desc], scroll_to_output=True, queue=False, show_api=False)

    for i, l in enumerate(lora_repo):
        deselect_lora_button.click(lambda: ("", 1.0), None, [lora_repo[i], lora_wt[i]], queue=False, show_api=False)
        gr.on(
            triggers=[lora_download[i].click],
            fn=download_my_lora,
            inputs=[lora_download_url, lora_repo[i]],
            outputs=[lora_repo[i]],
            scroll_to_output=True,
            queue=True,
            show_api=False,
        )
        gr.on(
            triggers=[lora_repo[i].change, lora_wt[i].change],
            fn=update_loras,
            inputs=[prompt, lora_repo[i], lora_wt[i]],
            outputs=[prompt, lora_repo[i], lora_wt[i], lora_info[i], lora_md[i]],
            queue=False,
            trigger_mode="once",
            show_api=False,
        ).success(get_repo_safetensors, [lora_repo[i]], [lora_weights[i]], queue=False, show_api=False
        ).success(apply_lora_prompt, [lora_info[i]], [lora_trigger[i]], queue=False, show_api=False
        ).success(compose_lora_json, [lora_repo_json, lora_num[i], lora_repo[i], lora_wt[i], lora_weights[i], lora_trigger[i]], [lora_repo_json], queue=False, show_api=False)
        
    for i, m in enumerate(cn_mode):
        gr.on(
            triggers=[cn_mode[i].change, cn_scale[i].change],
            fn=set_control_union_mode,
            inputs=[cn_num[i], cn_mode[i], cn_scale[i]],
            outputs=[cn_on],
            queue=True,
            show_api=False,
        ).success(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)
        cn_image_ref[i].upload(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)

    tagger_generate_from_image.click(
            lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
    ).success(
        predict_tags_wd,
        [tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
        [v2_series, v2_character, prompt, v2_copy],
        show_api=False,
    ).success(
        predict_tags_fl2_flux, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
    ).success(
        compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False,
    )

    with gr.Tab("FLUX Prompt Generator"):
        from prompt import (PromptGenerator, HuggingFaceInferenceNode, florence_caption,
            ARTFORM, PHOTO_TYPE, ROLES, HAIRSTYLES, LIGHTING, COMPOSITION, POSE, BACKGROUND,
            PHOTOGRAPHY_STYLES, DEVICE, PHOTOGRAPHER, ARTIST, DIGITAL_ARTFORM, PLACE,
            FEMALE_DEFAULT_TAGS, MALE_DEFAULT_TAGS, FEMALE_BODY_TYPES, MALE_BODY_TYPES,
            FEMALE_CLOTHING, MALE_CLOTHING, FEMALE_ADDITIONAL_DETAILS, MALE_ADDITIONAL_DETAILS, pg_title)

        prompt_generator = PromptGenerator()
        huggingface_node = HuggingFaceInferenceNode()

        gr.HTML(pg_title)

        with gr.Row():
            with gr.Column(scale=2):
                with gr.Accordion("Basic Settings"):
                    pg_custom = gr.Textbox(label="Custom Input Prompt (optional)")
                    pg_subject = gr.Textbox(label="Subject (optional)")
                    pg_gender = gr.Radio(["female", "male"], label="Gender", value="female")
                    
                    # Add the radio button for global option selection
                    pg_global_option = gr.Radio(
                        ["Disabled", "Random", "No Figure Rand"],
                        label="Set all options to:",
                        value="Disabled"
                    )
                
                with gr.Accordion("Artform and Photo Type", open=False):
                    pg_artform = gr.Dropdown(["disabled", "random"] + ARTFORM, label="Artform", value="disabled")
                    pg_photo_type = gr.Dropdown(["disabled", "random"] + PHOTO_TYPE, label="Photo Type", value="disabled")
            
                with gr.Accordion("Character Details", open=False):
                    pg_body_types = gr.Dropdown(["disabled", "random"] + FEMALE_BODY_TYPES + MALE_BODY_TYPES, label="Body Types", value="disabled")
                    pg_default_tags = gr.Dropdown(["disabled", "random"] + FEMALE_DEFAULT_TAGS + MALE_DEFAULT_TAGS, label="Default Tags", value="disabled")
                    pg_roles = gr.Dropdown(["disabled", "random"] + ROLES, label="Roles", value="disabled")
                    pg_hairstyles = gr.Dropdown(["disabled", "random"] + HAIRSTYLES, label="Hairstyles", value="disabled")
                    pg_clothing = gr.Dropdown(["disabled", "random"] + FEMALE_CLOTHING + MALE_CLOTHING, label="Clothing", value="disabled")
            
                with gr.Accordion("Scene Details", open=False):
                    pg_place = gr.Dropdown(["disabled", "random"] + PLACE, label="Place", value="disabled")
                    pg_lighting = gr.Dropdown(["disabled", "random"] + LIGHTING, label="Lighting", value="disabled")
                    pg_composition = gr.Dropdown(["disabled", "random"] + COMPOSITION, label="Composition", value="disabled")
                    pg_pose = gr.Dropdown(["disabled", "random"] + POSE, label="Pose", value="disabled")
                    pg_background = gr.Dropdown(["disabled", "random"] + BACKGROUND, label="Background", value="disabled")
            
                with gr.Accordion("Style and Artist", open=False):
                    pg_additional_details = gr.Dropdown(["disabled", "random"] + FEMALE_ADDITIONAL_DETAILS + MALE_ADDITIONAL_DETAILS, label="Additional Details", value="disabled")
                    pg_photography_styles = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHY_STYLES, label="Photography Styles", value="disabled")
                    pg_device = gr.Dropdown(["disabled", "random"] + DEVICE, label="Device", value="disabled")
                    pg_photographer = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHER, label="Photographer", value="disabled")
                    pg_artist = gr.Dropdown(["disabled", "random"] + ARTIST, label="Artist", value="disabled")
                    pg_digital_artform = gr.Dropdown(["disabled", "random"] + DIGITAL_ARTFORM, label="Digital Artform", value="disabled")
                
                pg_generate_button = gr.Button("Generate Prompt")

            with gr.Column(scale=2):
                with gr.Accordion("Image and Caption", open=False):
                    pg_input_image = gr.Image(label="Input Image (optional)")
                    pg_caption_output = gr.Textbox(label="Generated Caption", lines=3)
                    pg_create_caption_button = gr.Button("Create Caption")
                    pg_add_caption_button = gr.Button("Add Caption to Prompt")

                with gr.Accordion("Prompt Generation", open=True):
                    pg_output = gr.Textbox(label="Generated Prompt / Input Text", lines=4)
                    pg_t5xxl_output = gr.Textbox(label="T5XXL Output", visible=True)
                    pg_clip_l_output = gr.Textbox(label="CLIP L Output", visible=True)
                    pg_clip_g_output = gr.Textbox(label="CLIP G Output", visible=True)
            
            with gr.Column(scale=2):
                with gr.Accordion("Prompt Generation with LLM", open=False):
                    pg_happy_talk = gr.Checkbox(label="Happy Talk", value=True)
                    pg_compress = gr.Checkbox(label="Compress", value=True)
                    pg_compression_level = gr.Radio(["soft", "medium", "hard"], label="Compression Level", value="hard")
                    pg_poster = gr.Checkbox(label="Poster", value=False)
                    pg_custom_base_prompt = gr.Textbox(label="Custom Base Prompt", lines=5)
                pg_generate_text_button = gr.Button("Generate Prompt with LLM (Llama 3.1 70B)")
                pg_text_output = gr.Textbox(label="Generated Text", lines=10)

    description_ui()

    def create_caption(image):
        if image is not None:
            return florence_caption(image)
        return ""

    pg_create_caption_button.click(
        create_caption,
        inputs=[pg_input_image],
        outputs=[pg_caption_output]
    )

    def generate_prompt_with_dynamic_seed(*args):
        # Generate a new random seed
        dynamic_seed = random.randint(0, 1000000)
        
        # Call the generate_prompt function with the dynamic seed
        result = prompt_generator.generate_prompt(dynamic_seed, *args)
        
        # Return the result along with the used seed
        return [dynamic_seed] + list(result)

    pg_generate_button.click(
        generate_prompt_with_dynamic_seed,
        inputs=[pg_custom, pg_subject, pg_gender, pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles,
                pg_additional_details, pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform,
                pg_place, pg_lighting, pg_clothing, pg_composition, pg_pose, pg_background, pg_input_image],
        outputs=[gr.Number(label="Used Seed", visible=False), pg_output, gr.Number(visible=False), pg_t5xxl_output, pg_clip_l_output, pg_clip_g_output]
    )

    pg_add_caption_button.click(
        prompt_generator.add_caption_to_prompt,
        inputs=[pg_output, pg_caption_output],
        outputs=[pg_output]
    )

    pg_generate_text_button.click(
        huggingface_node.generate,
        inputs=[pg_output, pg_happy_talk, pg_compress, pg_compression_level, pg_poster, pg_custom_base_prompt],
        outputs=pg_text_output
    )

    def update_all_options(choice):
        updates = {}
        if choice == "Disabled":
            for dropdown in [
                pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
                pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
                pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
            ]:
                updates[dropdown] = gr.update(value="disabled")
        elif choice == "Random":
            for dropdown in [
                pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
                pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
                pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
            ]:
                updates[dropdown] = gr.update(value="random")
        else:  # No Figure Random
            for dropdown in [pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing, pg_pose, pg_additional_details]:
                updates[dropdown] = gr.update(value="disabled")
            for dropdown in [pg_artform, pg_place, pg_lighting, pg_composition, pg_background, pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform]:
                updates[dropdown] = gr.update(value="random")
        return updates
    
    pg_global_option.change(
        update_all_options,
        inputs=[pg_global_option],
        outputs=[
            pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
            pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
            pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
        ]
    )

app.queue()
app.launch()