File size: 19,120 Bytes
9708d87
6fef025
89c5f18
9027be6
 
53b0019
9027be6
 
 
80dc43c
 
9027be6
 
80dc43c
0318f31
9027be6
 
 
 
 
 
 
15a21d3
16cf1b4
90c3818
 
16cf1b4
90aa79e
16cf1b4
 
 
 
90c3818
f0bb78c
 
16cf1b4
 
fbd4f9f
7e3e984
 
 
fbd4f9f
7e3e984
fbd4f9f
 
7e3e984
fbd4f9f
7e3e984
 
 
 
9027be6
7e3e984
 
22eb359
 
 
 
 
 
 
 
 
 
 
 
 
 
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15a21d3
53b0019
 
0318f31
c469318
9027be6
0318f31
b21c027
9027be6
 
f5b8400
07e4c35
 
f5b8400
07e4c35
c8c1456
 
a5da923
 
f5b8400
 
 
 
 
d6c665c
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b8400
6fef025
9448672
 
9027be6
b412bc1
4918b69
 
d7b9a12
6fef025
f5b8400
4a46f7b
 
6fef025
c94d160
 
 
 
f5b8400
 
9027be6
6fef025
b62315c
9027be6
b62315c
04cf984
9027be6
 
af5a0dc
9027be6
4a24514
04cf984
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a24514
9027be6
 
 
 
 
 
 
 
 
4a24514
3dcdf22
ace2323
8ec162c
9027be6
5b55c81
f54cd26
9027be6
b62315c
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b8400
 
25a1729
1a35139
 
 
fb67142
9027be6
796babb
1a35139
 
6fef025
9027be6
80dc43c
 
9027be6
 
80dc43c
 
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdcf524
132d5bb
 
21c4e58
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8f343
1871b0f
 
b93810d
9027be6
4a46f7b
 
9027be6
 
 
 
 
25ced86
9027be6
 
 
 
b26345f
0116e27
9027be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0116e27
9027be6
556ec8a
9027be6
 
0318f31
 
 
 
 
2944f54
 
8831d66
2944f54
8831d66
 
2944f54
 
 
9027be6
bb307df
e5beca8
8fc64ff
26b4194
9027be6
 
 
 
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
import os
import gradio as gr
from random import randint
from operator import itemgetter
from all_models import tags_plus_models,models,models_plus_tags
from datetime import datetime
from externalmod import gr_Interface_load
import asyncio
import os
from threading import RLock
lock = RLock()
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.


now2 = 0
inference_timeout = 300
MAX_SEED = 2**32-1


nb_rep=2
nb_mod_dif=20
nb_models=nb_mod_dif*nb_rep

def split_models(models,nb_models):
    models_temp=[]
    models_lis_temp=[]
    i=0
    for m in models:
        models_temp.append(m)
        i=i+1
        if i%nb_models==0:
            models_lis_temp.append(models_temp)
            models_temp=[]
    if len(models_temp)>1:
        models_lis_temp.append(models_temp)
    return models_lis_temp

def split_models_axb(models,a,b):
    models_temp=[]
    models_lis_temp=[]
    i=0
    nb_models=b
    for m in models:
        for j in range(a):
            models_temp.append(m)
        i=i+1
        if i%nb_models==0:
            models_lis_temp.append(models_temp)
            models_temp=[]
    if len(models_temp)>1:
        models_lis_temp.append(models_temp)
    return models_lis_temp 

def split_models_8x3(models,nb_models):
    models_temp=[]
    models_lis_temp=[]
    i=0
    nb_models_x3=8
    for m in models:
        models_temp.append(m)
        i=i+1
        if i%nb_models_x3==0:
            models_lis_temp.append(models_temp+models_temp+models_temp)
            models_temp=[]
    if len(models_temp)>1:
        models_lis_temp.append(models_temp+models_temp+models_temp)
    return models_lis_temp

def construct_list_models(tags_plus_models,nb_rep,nb_mod_dif):
    list_temp=[]
    output=[]
    for tag_plus_models in tags_plus_models:
        list_temp=split_models_axb(tag_plus_models[2],nb_rep,nb_mod_dif)
        list_temp2=[]
        i=0
        for elem in list_temp:
            list_temp2.append([tag_plus_models[0]+"_"+str(i)+" : "+elem[0]+" - "+elem[len(elem)-1] ,elem])
            i+=1
        output.append([tag_plus_models[0] + " (" + str(tag_plus_models[1]) + ")",list_temp2])
    return output

models_test = []
models_test = construct_list_models(tags_plus_models,nb_rep,nb_mod_dif)

def get_current_time():
    now = datetime.now()
    now2 = now
    current_time = now2.strftime("%Y-%m-%d %H:%M:%S")
    kii = "" # ?
    ki = f'{kii} {current_time}'
    return ki

def load_fn_original(models):
    global models_load
    global num_models
    global default_models
    models_load = {}
    num_models = len(models)
    if num_models!=0:
        default_models = models[:num_models]
    else:
        default_models = {}
    for model in models:
        if model not in models_load.keys():
            try:
                m = gr.load(f'models/{model}')
            except Exception as error:
                m = gr.Interface(lambda txt: None, ['text'], ['image'])
                print(error)
            models_load.update({model: m})

def load_fn(models):
    global models_load
    global num_models
    global default_models
    models_load = {}
    num_models = len(models)
    i=0
    if num_models!=0:
        default_models = models[:num_models]
    else:
        default_models = {}
    for model in models:
        i+=1
        if i%50==0:
            print("\n\n\n-------"+str(i)+'/'+str(len(models))+"-------\n\n\n")
        if model not in models_load.keys():
            try:
                m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
            except Exception as error:
                m = gr.Interface(lambda txt: None, ['text'], ['image'])
                print(error)
            models_load.update({model: m})


"""models = models_test[1]"""
#load_fn_original
load_fn(models)
"""models = {}
load_fn(models)"""
    

def extend_choices(choices):
    return choices + (nb_models - len(choices)) * ['NA']
    """return choices + (num_models - len(choices)) * ['NA']"""

def extend_choices_b(choices):
    choices_plus = extend_choices(choices)
    return [gr.Textbox(m, visible=False) for m in choices_plus]

def update_imgbox(choices):
    choices_plus = extend_choices(choices)
    return [gr.Image(None, label=m,interactive=False, visible=(m != 'NA')) for m in choices_plus]

def choice_group_a(group_model_choice):
    return group_model_choice

def choice_group_b(group_model_choice):
    choiceTemp =choice_group_a(group_model_choice)
    choiceTemp = extend_choices(choiceTemp)
    """return [gr.Image(label=m, min_width=170, height=170) for m in choice]"""
    return [gr.Image(None, label=m,interactive=False, visible=(m != 'NA')) for m in choiceTemp]

def choice_group_c(group_model_choice):
    choiceTemp=choice_group_a(group_model_choice)
    choiceTemp = extend_choices(choiceTemp)
    return [gr.Textbox(m, visible=False) for m in choiceTemp]


def cutStrg(longStrg,start,end):
    shortStrg=''
    for i in range(end-start):
        shortStrg+=longStrg[start+i]
    return shortStrg
    
def aff_models_perso(txt_list_perso,nb_models=nb_models,models=models):
    list_perso=[]
    t1=True
    start=txt_list_perso.find('\"')
    if start!=-1:
        while t1:
            start+=1
            end=txt_list_perso.find('\"',start)
            if end != -1:
                txtTemp=cutStrg(txt_list_perso,start,end)
                if txtTemp in models:
                    list_perso.append(cutStrg(txt_list_perso,start,end))
            else :
                t1=False
            start=txt_list_perso.find('\"',end+1)
            if start==-1:
                t1=False
            if len(list_perso)>=nb_models:
                t1=False
    return list_perso

def aff_models_perso_b(txt_list_perso):
    return choice_group_b(aff_models_perso(txt_list_perso))

def aff_models_perso_c(txt_list_perso):
    return choice_group_c(aff_models_perso(txt_list_perso))
            
            
def tag_choice(group_tag_choice):
    return gr.Dropdown(label="List of Models with the chosen Tag", show_label=True, choices=list(group_tag_choice) , interactive = True , filterable = False)

def test_pass(test):
    if test==os.getenv('p'):
        print("ok")
        return gr.Dropdown(label="Lists Tags", show_label=True, choices=list(models_test) , interactive = True)
    else:
        print("nop")
        return gr.Dropdown(label="Lists Tags", show_label=True, choices=list([]) , interactive = True)

def test_pass_aff(test):
    if test==os.getenv('p'):
        return gr.Accordion( open=True, visible=True)
    else:
        return gr.Accordion( open=True, visible=False)


# https://huggingface.co/docs/api-inference/detailed_parameters
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
async def infer(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1, timeout=inference_timeout):
    from pathlib import Path
    kwargs = {}
    if height is not None and height >= 256: kwargs["height"] = height
    if width is not None and width >= 256: kwargs["width"] = width
    if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
    if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
    noise = ""
    if seed >= 0: kwargs["seed"] = seed
    else:
        rand = randint(1, 500)
        for i in range(rand):
            noise += " "
    task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
                               prompt=f'{prompt} {noise}', negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
    await asyncio.sleep(0)
    try:
        result = await asyncio.wait_for(task, timeout=timeout)
    except (Exception, asyncio.TimeoutError) as e:
        print(e)
        print(f"Task timed out: {model_str}")
        if not task.done(): task.cancel()
        result = None
    if task.done() and result is not None:
        with lock:
            png_path = "image.png"
            result.save(png_path)
            image = str(Path(png_path).resolve())
        return image
    return None

def gen_fn(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1):
    if model_str == 'NA':
        return None
    try:
        loop = asyncio.new_event_loop()
        result = loop.run_until_complete(infer(model_str, prompt, nprompt,
                                         height, width, steps, cfg, seed, inference_timeout))
    except (Exception, asyncio.CancelledError) as e:
        print(e)
        print(f"Task aborted: {model_str}")
        result = None
    finally:
        loop.close()
    return result

def gen_fn_original(model_str, prompt):
    if model_str == 'NA':
        return None
    noise = str(randint(0, 9999))
    try :
        m=models_load[model_str](f'{prompt} {noise}')
    except Exception as error : 
        print("error : " + model_str)
        print(error)
        m=False
    
    return m

   
def add_gallery(image, model_str, gallery):
    if gallery is None: gallery = []
    #with lock:
    if image is not None: gallery.append((image, model_str))
    return gallery

def reset_gallery(gallery):
    return add_gallery(None,"",[])

def load_gallery(gallery):
    gallery = reset_gallery(gallery)
    for c in cache_image:
        gallery=add_gallery(c[0],c[1],gallery)
    return gallery
def load_gallery_actu(gallery):
    gallery = reset_gallery(gallery)
    #for c in cache_image_actu:
    for c in sorted(cache_image_actu, key=itemgetter(1)):
        gallery=add_gallery(c[0],c[1],gallery)
    return gallery

def add_cache_image(o,m):
    cache_image.append((o,m))
    #cache_image=sorted(cache_image, key=itemgetter(1))
    return 
def add_cache_image_actu(o,m):
    cache_image_actu.append((o,m))
    #cache_image_actu=sorted(cache_image_actu, key=itemgetter(1))
    return
def reset_cache_image():
    cache_image.clear()
    return 
def reset_cache_image_actu():
    cache_image_actu.clear()
    return 

def disp_models(group_model_choice,nb_rep=nb_rep):
    listTemp=[]
    strTemp='\n'
    i=0
    for m in group_model_choice:
        if m not in listTemp:
            listTemp.append(m)
    for m in listTemp:
        i+=1
        strTemp+="\"" + m + "\",\n"
        if i%(8/nb_rep)==0:
            strTemp+="\n"
    return gr.Textbox(label="models",value=strTemp)

def search_models(str_search,tags_plus_models=tags_plus_models):
    output1="\n"
    output2=""
    for m in tags_plus_models[0][2]:
        if m.find(str_search)!=-1:
            output1+="\"" + m + "\",\n"
    outputPlus="\n From tags : \n\n"
    for tag_plus_models in tags_plus_models:
        if str_search.lower() == tag_plus_models[0].lower() and str_search!="":
            for m in tag_plus_models[2]:
                output2+="\"" + m + "\",\n"
    if output2 != "":
        output=output1+outputPlus+output2
    else :
        output=output1
    return gr.Textbox(label="out",value=output)

def search_info(txt_search_info,models_plus_tags=models_plus_tags):
    outputList=[]
    if txt_search_info.find("\"")!=-1:
        start=txt_search_info.find("\"")+1
        end=txt_search_info.find("\"",start)
        m_name=cutStrg(txt_search_info,start,end)
    else :
        m_name = txt_search_info
    for m in models_plus_tags:
        if m_name == m[0]:
            outputList=m[1]
    if len(outputList)==0:
        outputList.append("Model Not Find")
    return gr.Textbox(label="out",value=outputList)


def make_me():
   # with gr.Tab('The Dream'): 
        with gr.Row():
            #txt_input = gr.Textbox(lines=3, width=300, max_height=100)
            #txt_input = gr.Textbox(label='Your prompt:', lines=3, width=300, max_height=100)
            with gr.Column(scale=4):
                with gr.Group():
                    txt_input = gr.Textbox(label='Your prompt:', lines=3)
                    with gr.Accordion("Advanced", open=False, visible=True):
                        neg_input = gr.Textbox(label='Negative prompt:', lines=1)
                        with gr.Row():
                            width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
                            height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
                        with gr.Row():
                            steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
                            cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
                            seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
                #gen_button = gr.Button('Generate images', width=150, height=30)
            #stop_button = gr.Button('Stop', variant='secondary', interactive=False, width=150, height=30)
            gen_button = gr.Button('Generate images', scale=3)
            stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)

            gen_button.click(lambda: gr.update(interactive=True), None, stop_button)
            #gr.HTML("""
            #<div style="text-align: center; max-width: 100%; margin: 0 auto;">
            #    <body>
            #    </body>
            #</div>
            #""")
        with gr.Row():
            """output = [gr.Image(label=m, min_width=170, height=170) for m in default_models]
            current_models = [gr.Textbox(m, visible=False) for m in default_models]"""
            """choices=[models_test[0][0]]"""
            choices=models_test[0][1][0][1]
            """output = [gr.Image(label=m, min_width=170, height=170) for m in choices]
            current_models = [gr.Textbox(m, visible=False) for m in choices]"""
            global output_g
            global current_models_g
            output_g = update_imgbox([choices[0]])
            current_models_g = extend_choices_b([choices[0]])

            
            for m, o in zip(current_models_g, output_g):
                gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
                                inputs=[m, txt_input, neg_input, height, width, steps, cfg, seed], outputs=[o])
                stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
            
        with gr.Row():
            txt_input_p = gr.Textbox(label="Pass", lines=1)
            test_button = gr.Button(' ')    

            
        with gr.Accordion( open=True, visible=False) as stuffs:
            with gr.Accordion("Gallery",open=False):
                with gr.Row():
                    global cache_image
                    global cache_image_actu
                    cache_image=[]
                    cache_image_actu=[]
                    with gr.Column():
                        b11 = gr.Button('Load Galerry Actu')
                        b12 = gr.Button('Load Galerry All')
                    gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
                                    interactive=False, show_share_button=True, container=True, format="png",
                                    preview=True, object_fit="cover",columns=4,rows=4) 
                    with gr.Column():
                        b21 = gr.Button('Reset Gallery')
                        b22 = gr.Button('Reset Gallery All')
                    b11.click(load_gallery_actu,gallery,gallery)
                    b12.click(load_gallery,gallery,gallery)
                    b21.click(reset_gallery,gallery,gallery)
                    b22.click(reset_cache_image,[],gallery)
                    for m, o in zip(current_models_g, output_g):
                        #o.change(add_gallery, [o, m, gallery], [gallery])
                        o.change(add_cache_image,[o,m],[])
                        o.change(add_cache_image_actu,[o,m],[])
                gen_button.click(reset_cache_image_actu, [], [])
                
            with gr.Group():
                with gr.Row():
                    group_tag_choice = gr.Dropdown(label="Lists Tags", show_label=True, choices=list([]) , interactive = True)
                    
                with gr.Row():
                    group_model_choice = gr.Dropdown(label="List of Models with the chosen Tag", show_label=True, choices=list([]) , interactive = True)
                    group_model_choice.change(choice_group_b,group_model_choice,output_g)
                    group_model_choice.change(choice_group_c,group_model_choice,current_models_g)
                    group_tag_choice.change(tag_choice,group_tag_choice,group_model_choice)
                
            with gr.Row():
                txt_list_models=gr.Textbox(label="Models Actu",value="")
                group_model_choice.change(disp_models,group_model_choice,txt_list_models)
                
            with gr.Row():
                txt_list_perso = gr.Textbox(label='List Models Perso')
            
                button_list_perso = gr.Button('Load')
                button_list_perso.click(aff_models_perso_b,txt_list_perso,output_g)
                button_list_perso.click(aff_models_perso_c,txt_list_perso,current_models_g)
    
            with gr.Row():
                txt_search = gr.Textbox(label='Search in')
                txt_output_search = gr.Textbox(label='Search out')
                button_search = gr.Button('Research')
                button_search.click(search_models,txt_search,txt_output_search)

            with gr.Row():
                txt_search_info = gr.Textbox(label='Search info in')
                txt_output_search_info = gr.Textbox(label='Search info out')
                button_search_info = gr.Button('Research info')
                button_search_info.click(search_info,txt_search_info,txt_output_search_info)
        
            
        with gr.Row():
            test_button.click(test_pass_aff,txt_input_p,stuffs)
            test_button.click(test_pass,txt_input_p,group_tag_choice)
            gr.HTML("""
                <div class="footer">
                <p> Based on the <a href="https://huggingface.co/spaces/derwahnsinn/TestGen">TestGen</a> Space by derwahnsinn, the <a href="https://huggingface.co/spaces/RdnUser77/SpacIO_v1">SpacIO</a> Space by RdnUser77 and Omnibus's Maximum Multiplier!
                </p>
            """)

js_code = """

    
    console.log('ghgh');

"""


with gr.Blocks(theme="Nymbo/Nymbo_Theme", fill_width=True, css="div.float.svelte-1mwvhlq {    position: absolute;    top: var(--block-label-margin);    left: var(--block-label-margin);    background: none;    border: none;}") as demo: 
    gr.Markdown("<script>" + js_code + "</script>")
    make_me()


# https://www.gradio.app/guides/setting-up-a-demo-for-maximum-performance
#demo.queue(concurrency_count=999) # concurrency_count is deprecated in 4.x
demo.queue(default_concurrency_limit=200, max_size=200)
demo.launch(max_threads=400)