File size: 2,903 Bytes
063b292
 
 
c194fdd
30cee80
f5e64be
 
932a21a
063b292
f5e64be
0d4e1e9
30cee80
e90a37e
0bb5a0b
 
063b292
 
 
 
f8a1403
063b292
 
 
0c0c579
c194fdd
063b292
 
 
13c46fd
 
063b292
 
 
 
 
 
c194fdd
063b292
c194fdd
063b292
 
 
 
 
 
c194fdd
063b292
 
 
 
 
 
 
fe9f7af
063b292
c194fdd
063b292
fe9f7af
 
 
6d17160
9dfbb14
fe9f7af
 
 
 
 
0c0c579
fe9f7af
0c0c579
 
9dfbb14
fe9f7af
6d17160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import time

models = [
    "mann-e/Mann-E_Dreams",
    "Yntec/ChickFlick",
    "John6666/ultimate-realistic-mix-v2-sdxl",
    "Yntec/CrystalReality",
    "John6666/9527-detail-realistic-xl-v55mix-sdxl",
    "John6666/epicrealism-xl-v8kiss-sdxl",
    "John6666/wai-real-mix-v8-sdxl",
    "John6666/real-vis-xl-v40-sdxl",
    "Yntec/DegreesOfFreedom",
]

model_functions = {}
model_idx = 1
for model_path in models:
    try:
        model_functions[model_idx] = gr.Interface.load(f"models/{model_path}")
    except Exception as error:
        def the_fn(txt):
            return None
        model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=gr.Textbox(), outputs=gr.Image())
    model_idx += 1

def send_it_idx(idx):
    def send_it_fn(prompt):
        model = model_functions.get(idx, model_functions.get(1))
        return model.predict(prompt)
    return send_it_fn

def get_prompts(prompt_text):
    return prompt_text

def clear_it(val):
    return 0

def all_task_end(cnt, t_stamp):
    to = t_stamp + 360
    et = time.time()
    if et > to and t_stamp != 0:
        d = gr.update(value=0)
        tog = gr.update(value=1)
    else:
        d = gr.update(value=et) if cnt != 0 else gr.update(value=0)
        tog = gr.update(value=0)
    return d, tog

def all_task_start():
    t_stamp = time.time()
    return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)

def clear_fn(*args):
    nn = len(models)
    return (None, *[None for _ in range(nn)])

def run_model(prompt, model_idx):
    return send_it_idx(model_idx)(prompt)

with gr.Blocks(title="SD Models") as my_interface:
    primary_prompt = gr.Textbox(label="Prompt", value="")
    run = gr.Button("Run")
    clear_btn = gr.Button("Clear")

    sd_outputs = {}
    for idx, model_path in enumerate(models, start=1):
        sd_outputs[idx] = gr.Image(label=model_path)

    start_box = gr.Number(visible=False)
    end_box = gr.Number(visible=False)
    tog_box = gr.Textbox(value=0, visible=False)

    def main_function(prompt):
        return {idx: run_model(prompt, idx) for idx in range(1, len(models) + 1)}

    run.click(
        fn=main_function,
        inputs=[primary_prompt],
        outputs=list(sd_outputs.values()),
        concurrency_limit=10
    )

    clear_btn.click(
        fn=clear_fn,
        inputs=None,
        outputs=[primary_prompt] + list(sd_outputs.values()),
        concurrency_limit=10
    )

    start_box.change(
        fn=all_task_end,
        inputs=[start_box, end_box],
        outputs=[start_box, tog_box],
        every=1
    )

    primary_prompt.submit(all_task_start, inputs=None, outputs=[start_box, end_box, tog_box])
    run.click(all_task_start, inputs=None, outputs=[start_box, end_box, tog_box])
    tog_box.change(
        fn=clear_it,
        inputs=tog_box,
        outputs=tog_box
    )

    my_interface.launch(max_threads=10)