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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from random import randint
|
4 |
+
from operator import itemgetter
|
5 |
+
import bisect
|
6 |
+
from all_models import tags_plus_models,models,models_plus_tags
|
7 |
+
from datetime import datetime
|
8 |
+
from externalmod import gr_Interface_load
|
9 |
+
import asyncio
|
10 |
+
import os
|
11 |
+
from threading import RLock
|
12 |
+
lock = RLock()
|
13 |
+
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.
|
14 |
+
|
15 |
+
|
16 |
+
now2 = 0
|
17 |
+
inference_timeout = 300
|
18 |
+
MAX_SEED = 2**32-1
|
19 |
+
|
20 |
+
|
21 |
+
nb_rep=2
|
22 |
+
nb_mod_dif=20
|
23 |
+
nb_models=nb_mod_dif*nb_rep
|
24 |
+
|
25 |
+
cache_image={}
|
26 |
+
cache_image_actu={}
|
27 |
+
|
28 |
+
def split_models(models,nb_models):
|
29 |
+
models_temp=[]
|
30 |
+
models_lis_temp=[]
|
31 |
+
i=0
|
32 |
+
for m in models:
|
33 |
+
models_temp.append(m)
|
34 |
+
i=i+1
|
35 |
+
if i%nb_models==0:
|
36 |
+
models_lis_temp.append(models_temp)
|
37 |
+
models_temp=[]
|
38 |
+
if len(models_temp)>1:
|
39 |
+
models_lis_temp.append(models_temp)
|
40 |
+
return models_lis_temp
|
41 |
+
|
42 |
+
def split_models_axb(models,a,b):
|
43 |
+
models_temp=[]
|
44 |
+
models_lis_temp=[]
|
45 |
+
i=0
|
46 |
+
nb_models=b
|
47 |
+
for m in models:
|
48 |
+
for j in range(a):
|
49 |
+
models_temp.append(m)
|
50 |
+
i=i+1
|
51 |
+
if i%nb_models==0:
|
52 |
+
models_lis_temp.append(models_temp)
|
53 |
+
models_temp=[]
|
54 |
+
if len(models_temp)>1:
|
55 |
+
models_lis_temp.append(models_temp)
|
56 |
+
return models_lis_temp
|
57 |
+
|
58 |
+
def split_models_8x3(models,nb_models):
|
59 |
+
models_temp=[]
|
60 |
+
models_lis_temp=[]
|
61 |
+
i=0
|
62 |
+
nb_models_x3=8
|
63 |
+
for m in models:
|
64 |
+
models_temp.append(m)
|
65 |
+
i=i+1
|
66 |
+
if i%nb_models_x3==0:
|
67 |
+
models_lis_temp.append(models_temp+models_temp+models_temp)
|
68 |
+
models_temp=[]
|
69 |
+
if len(models_temp)>1:
|
70 |
+
models_lis_temp.append(models_temp+models_temp+models_temp)
|
71 |
+
return models_lis_temp
|
72 |
+
|
73 |
+
def construct_list_models(tags_plus_models,nb_rep,nb_mod_dif):
|
74 |
+
list_temp=[]
|
75 |
+
output=[]
|
76 |
+
for tag_plus_models in tags_plus_models:
|
77 |
+
list_temp=split_models_axb(tag_plus_models[2],nb_rep,nb_mod_dif)
|
78 |
+
list_temp2=[]
|
79 |
+
i=0
|
80 |
+
for elem in list_temp:
|
81 |
+
list_temp2.append([f"{tag_plus_models[0]}_{i+1}/{len(list_temp)} ({len(elem)}) : {elem[0]} - {elem[len(elem)-1]}" ,elem])
|
82 |
+
i+=1
|
83 |
+
output.append([f"{tag_plus_models[0]} ({tag_plus_models[1]})",list_temp2])
|
84 |
+
tag_plus_models[0]=f"{tag_plus_models[0]} ({tag_plus_models[1]})"
|
85 |
+
return output
|
86 |
+
|
87 |
+
models_test = []
|
88 |
+
models_test = construct_list_models(tags_plus_models,nb_rep,nb_mod_dif)
|
89 |
+
|
90 |
+
def get_current_time():
|
91 |
+
now = datetime.now()
|
92 |
+
now2 = now
|
93 |
+
current_time = now2.strftime("%Y-%m-%d %H:%M:%S")
|
94 |
+
kii = "" # ?
|
95 |
+
ki = f'{kii} {current_time}'
|
96 |
+
return ki
|
97 |
+
|
98 |
+
def load_fn_original(models):
|
99 |
+
global models_load
|
100 |
+
global num_models
|
101 |
+
global default_models
|
102 |
+
models_load = {}
|
103 |
+
num_models = len(models)
|
104 |
+
if num_models!=0:
|
105 |
+
default_models = models[:num_models]
|
106 |
+
else:
|
107 |
+
default_models = {}
|
108 |
+
for model in models:
|
109 |
+
if model not in models_load.keys():
|
110 |
+
try:
|
111 |
+
m = gr.load(f'models/{model}')
|
112 |
+
except Exception as error:
|
113 |
+
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
114 |
+
print(error)
|
115 |
+
models_load.update({model: m})
|
116 |
+
|
117 |
+
def load_fn(models):
|
118 |
+
global models_load
|
119 |
+
global num_models
|
120 |
+
global default_models
|
121 |
+
models_load = {}
|
122 |
+
num_models = len(models)
|
123 |
+
i=0
|
124 |
+
if num_models!=0:
|
125 |
+
default_models = models[:num_models]
|
126 |
+
else:
|
127 |
+
default_models = {}
|
128 |
+
for model in models:
|
129 |
+
i+=1
|
130 |
+
if i%50==0:
|
131 |
+
print("\n\n\n-------"+str(i)+'/'+str(len(models))+"-------\n\n\n")
|
132 |
+
if model not in models_load.keys():
|
133 |
+
try:
|
134 |
+
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
135 |
+
except Exception as error:
|
136 |
+
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
137 |
+
print(error)
|
138 |
+
models_load.update({model: m})
|
139 |
+
|
140 |
+
|
141 |
+
"""models = models_test[1]"""
|
142 |
+
#load_fn_original
|
143 |
+
load_fn(models)
|
144 |
+
"""models = {}
|
145 |
+
load_fn(models)"""
|
146 |
+
|
147 |
+
|
148 |
+
def extend_choices(choices):
|
149 |
+
return choices + (nb_models - len(choices)) * ['NA']
|
150 |
+
"""return choices + (num_models - len(choices)) * ['NA']"""
|
151 |
+
|
152 |
+
def extend_choices_b(choices):
|
153 |
+
choices_plus = extend_choices(choices)
|
154 |
+
return [gr.Textbox(m, visible=False) for m in choices_plus]
|
155 |
+
|
156 |
+
def update_imgbox(choices):
|
157 |
+
choices_plus = extend_choices(choices)
|
158 |
+
return [gr.Image(None, label=m,interactive=False, visible=(m != 'NA'),show_share_button=False) for m in choices_plus]
|
159 |
+
|
160 |
+
def choice_group_a(group_model_choice):
|
161 |
+
return group_model_choice
|
162 |
+
|
163 |
+
def choice_group_b(group_model_choice):
|
164 |
+
choiceTemp =choice_group_a(group_model_choice)
|
165 |
+
choiceTemp = extend_choices(choiceTemp)
|
166 |
+
"""return [gr.Image(label=m, min_width=170, height=170) for m in choice]"""
|
167 |
+
return [gr.Image(None, label=m,interactive=False, visible=(m != 'NA'),show_share_button=False) for m in choiceTemp]
|
168 |
+
|
169 |
+
def choice_group_c(group_model_choice):
|
170 |
+
choiceTemp=choice_group_a(group_model_choice)
|
171 |
+
choiceTemp = extend_choices(choiceTemp)
|
172 |
+
return [gr.Textbox(m) for m in choiceTemp]
|
173 |
+
|
174 |
+
def choice_group_d(group_model_choice):
|
175 |
+
choiceTemp=choice_group_a(group_model_choice)
|
176 |
+
choiceTemp = extend_choices(choiceTemp)
|
177 |
+
return [gr.Textbox(choiceTemp[i*nb_rep], visible=(choiceTemp[i*nb_rep] != 'NA'),show_label=False) for i in range(nb_mod_dif)]
|
178 |
+
def choice_group_e(group_model_choice):
|
179 |
+
choiceTemp=choice_group_a(group_model_choice)
|
180 |
+
choiceTemp = extend_choices(choiceTemp)
|
181 |
+
return [gr.Column(visible=(choiceTemp[i*nb_rep] != 'NA')) for i in range(nb_mod_dif)]
|
182 |
+
|
183 |
+
def cutStrg(longStrg,start,end):
|
184 |
+
shortStrg=''
|
185 |
+
for i in range(end-start):
|
186 |
+
shortStrg+=longStrg[start+i]
|
187 |
+
return shortStrg
|
188 |
+
|
189 |
+
def aff_models_perso(txt_list_perso,nb_models=nb_models,models=models):
|
190 |
+
list_perso=[]
|
191 |
+
t1=True
|
192 |
+
start=txt_list_perso.find('\"')
|
193 |
+
if start!=-1:
|
194 |
+
while t1:
|
195 |
+
start+=1
|
196 |
+
end=txt_list_perso.find('\"',start)
|
197 |
+
if end != -1:
|
198 |
+
txtTemp=cutStrg(txt_list_perso,start,end)
|
199 |
+
if txtTemp in models:
|
200 |
+
list_perso.append(cutStrg(txt_list_perso,start,end))
|
201 |
+
else :
|
202 |
+
t1=False
|
203 |
+
start=txt_list_perso.find('\"',end+1)
|
204 |
+
if start==-1:
|
205 |
+
t1=False
|
206 |
+
if len(list_perso)>=nb_models:
|
207 |
+
t1=False
|
208 |
+
return list_perso
|
209 |
+
|
210 |
+
def aff_models_perso_b(txt_list_perso):
|
211 |
+
return choice_group_b(aff_models_perso(txt_list_perso))
|
212 |
+
|
213 |
+
def aff_models_perso_c(txt_list_perso):
|
214 |
+
return choice_group_c(aff_models_perso(txt_list_perso))
|
215 |
+
|
216 |
+
|
217 |
+
def tag_choice(group_tag_choice):
|
218 |
+
return gr.Dropdown(label="List of Models with the chosen Tag", show_label=True, choices=list(group_tag_choice) , interactive = True , filterable = False)
|
219 |
+
|
220 |
+
def test_pass(test):
|
221 |
+
if test==os.getenv('p'):
|
222 |
+
print("ok")
|
223 |
+
return gr.Dropdown(label="Lists Tags", show_label=True, choices=list(models_test) , interactive = True)
|
224 |
+
else:
|
225 |
+
print("nop")
|
226 |
+
return gr.Dropdown(label="Lists Tags", show_label=True, choices=list([]) , interactive = True)
|
227 |
+
|
228 |
+
def test_pass_aff(test):
|
229 |
+
if test==os.getenv('p'):
|
230 |
+
return gr.Accordion( open=True, visible=True) ,gr.Row(visible=False)
|
231 |
+
else:
|
232 |
+
return gr.Accordion( open=True, visible=False) , gr.Row()
|
233 |
+
|
234 |
+
|
235 |
+
# https://huggingface.co/docs/api-inference/detailed_parameters
|
236 |
+
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
237 |
+
async def infer(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1, timeout=inference_timeout):
|
238 |
+
from pathlib import Path
|
239 |
+
kwargs = {}
|
240 |
+
if height is not None and height >= 256: kwargs["height"] = height
|
241 |
+
if width is not None and width >= 256: kwargs["width"] = width
|
242 |
+
if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
|
243 |
+
if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
244 |
+
noise = ""
|
245 |
+
if seed >= 0: kwargs["seed"] = seed
|
246 |
+
else:
|
247 |
+
rand = randint(1, 500)
|
248 |
+
for i in range(rand):
|
249 |
+
noise += " "
|
250 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
251 |
+
prompt=f'{prompt} {noise}', negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
|
252 |
+
await asyncio.sleep(0)
|
253 |
+
try:
|
254 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
255 |
+
except (Exception, asyncio.TimeoutError) as e:
|
256 |
+
print(e)
|
257 |
+
print(f"Task timed out: {model_str}")
|
258 |
+
if not task.done(): task.cancel()
|
259 |
+
result = None
|
260 |
+
if task.done() and result is not None:
|
261 |
+
with lock:
|
262 |
+
png_path = "image.png"
|
263 |
+
result.save(png_path)
|
264 |
+
image = str(Path(png_path).resolve())
|
265 |
+
return image
|
266 |
+
return None
|
267 |
+
|
268 |
+
def gen_fn(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1):
|
269 |
+
if model_str == 'NA':
|
270 |
+
return None
|
271 |
+
try:
|
272 |
+
loop = asyncio.new_event_loop()
|
273 |
+
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
|
274 |
+
height, width, steps, cfg, seed, inference_timeout))
|
275 |
+
except (Exception, asyncio.CancelledError) as e:
|
276 |
+
print(e)
|
277 |
+
print(f"Task aborted: {model_str}")
|
278 |
+
result = None
|
279 |
+
finally:
|
280 |
+
loop.close()
|
281 |
+
return result
|
282 |
+
|
283 |
+
def gen_fn_original(model_str, prompt):
|
284 |
+
if model_str == 'NA':
|
285 |
+
return None
|
286 |
+
noise = str(randint(0, 9999))
|
287 |
+
try :
|
288 |
+
m=models_load[model_str](f'{prompt} {noise}')
|
289 |
+
except Exception as error :
|
290 |
+
print("error : " + model_str)
|
291 |
+
print(error)
|
292 |
+
m=False
|
293 |
+
|
294 |
+
return m
|
295 |
+
|
296 |
+
|
297 |
+
def add_gallery(image, model_str, gallery):
|
298 |
+
if gallery is None: gallery = []
|
299 |
+
#with lock:
|
300 |
+
if image is not None: gallery.append((image, model_str))
|
301 |
+
return gallery
|
302 |
+
|
303 |
+
def reset_gallery(gallery):
|
304 |
+
return add_gallery(None,"",[])
|
305 |
+
|
306 |
+
def load_gallery(gallery,id):
|
307 |
+
gallery = reset_gallery(gallery)
|
308 |
+
for c in cache_image[f"{id}"]:
|
309 |
+
gallery=add_gallery(c[0],c[1],gallery)
|
310 |
+
return gallery
|
311 |
+
def load_gallery_sorted(gallery,id):
|
312 |
+
gallery = reset_gallery(gallery)
|
313 |
+
for c in sorted(cache_image[f"{id}"], key=itemgetter(1)):
|
314 |
+
gallery=add_gallery(c[0],c[1],gallery)
|
315 |
+
return gallery
|
316 |
+
def load_gallery_actu(gallery,id):
|
317 |
+
gallery = reset_gallery(gallery)
|
318 |
+
for c in cache_image_actu[f"{id}"]:
|
319 |
+
gallery=add_gallery(c[0],c[1],gallery)
|
320 |
+
return gallery
|
321 |
+
|
322 |
+
def add_cache_image(image, model_str,id,cache_image=cache_image):
|
323 |
+
if image is not None:
|
324 |
+
cache_image[f"{id}"].append((image,model_str))
|
325 |
+
#cache_image=sorted(cache_image, key=itemgetter(1))
|
326 |
+
return
|
327 |
+
def add_cache_image_actu(image, model_str,id,cache_image_actu=cache_image_actu):
|
328 |
+
if image is not None:
|
329 |
+
bisect.insort(cache_image_actu[f"{id}"],(image, model_str), key=itemgetter(1))
|
330 |
+
#cache_image_actu=sorted(cache_image_actu, key=itemgetter(1))
|
331 |
+
return
|
332 |
+
def reset_cache_image(id,cache_image=cache_image):
|
333 |
+
cache_image[f"{id}"].clear()
|
334 |
+
return
|
335 |
+
def reset_cache_image_actu(id,cache_image_actu=cache_image_actu):
|
336 |
+
cache_image_actu[f"{id}"].clear()
|
337 |
+
return
|
338 |
+
def reset_cache_image_all_sessions(cache_image=cache_image,cache_image_actu=cache_image_actu):
|
339 |
+
for key, listT in cache_image.items():
|
340 |
+
listT.clear()
|
341 |
+
for key, listT in cache_image_actu.items():
|
342 |
+
listT.clear()
|
343 |
+
return
|
344 |
+
|
345 |
+
def set_session(id):
|
346 |
+
if id==0:
|
347 |
+
randTemp=randint(1,MAX_SEED)
|
348 |
+
cache_image[f"{randTemp}"]=[]
|
349 |
+
cache_image_actu[f"{randTemp}"]=[]
|
350 |
+
return gr.Number(visible=False,value=randTemp)
|
351 |
+
else :
|
352 |
+
return id
|
353 |
+
def print_info_sessions():
|
354 |
+
lenTot=0
|
355 |
+
print("###################################")
|
356 |
+
print("number of sessions : "+str(len(cache_image)))
|
357 |
+
for key, listT in cache_image.items():
|
358 |
+
print("session "+key+" : "+str(len(listT)))
|
359 |
+
lenTot+=len(listT)
|
360 |
+
print("images total = "+str(lenTot))
|
361 |
+
print("###################################")
|
362 |
+
return
|
363 |
+
|
364 |
+
def disp_models(group_model_choice,nb_rep=nb_rep):
|
365 |
+
listTemp=[]
|
366 |
+
strTemp='\n'
|
367 |
+
i=0
|
368 |
+
for m in group_model_choice:
|
369 |
+
if m not in listTemp:
|
370 |
+
listTemp.append(m)
|
371 |
+
for m in listTemp:
|
372 |
+
i+=1
|
373 |
+
strTemp+="\"" + m + "\",\n"
|
374 |
+
if i%(8/nb_rep)==0:
|
375 |
+
strTemp+="\n"
|
376 |
+
return gr.Textbox(label="models",value=strTemp)
|
377 |
+
|
378 |
+
def search_models(str_search,tags_plus_models=tags_plus_models):
|
379 |
+
output1="\n"
|
380 |
+
output2=""
|
381 |
+
for m in tags_plus_models[0][2]:
|
382 |
+
if m.find(str_search)!=-1:
|
383 |
+
output1+="\"" + m + "\",\n"
|
384 |
+
outputPlus="\n From tags : \n\n"
|
385 |
+
for tag_plus_models in tags_plus_models:
|
386 |
+
if str_search.lower() == tag_plus_models[0].lower() and str_search!="":
|
387 |
+
for m in tag_plus_models[2]:
|
388 |
+
output2+="\"" + m + "\",\n"
|
389 |
+
if output2 != "":
|
390 |
+
output=output1+outputPlus+output2
|
391 |
+
else :
|
392 |
+
output=output1
|
393 |
+
return gr.Textbox(label="out",value=output)
|
394 |
+
|
395 |
+
def search_info(txt_search_info,models_plus_tags=models_plus_tags):
|
396 |
+
outputList=[]
|
397 |
+
if txt_search_info.find("\"")!=-1:
|
398 |
+
start=txt_search_info.find("\"")+1
|
399 |
+
end=txt_search_info.find("\"",start)
|
400 |
+
m_name=cutStrg(txt_search_info,start,end)
|
401 |
+
else :
|
402 |
+
m_name = txt_search_info
|
403 |
+
for m in models_plus_tags:
|
404 |
+
if m_name == m[0]:
|
405 |
+
outputList=m[1]
|
406 |
+
if len(outputList)==0:
|
407 |
+
outputList.append("Model Not Find")
|
408 |
+
return gr.Textbox(label="out",value=outputList)
|
409 |
+
|
410 |
+
def add_in_blacklist(bl,model):
|
411 |
+
return gr.Textbox(bl+(f"\"{model}\",\n"))
|
412 |
+
def add_in_fav(fav,model):
|
413 |
+
return gr.Textbox(fav+(f"\"{model}\",\n"))
|
414 |
+
def rand_from_all_all_models():
|
415 |
+
if len(tags_plus_models[0][2])<nb_mod_dif:
|
416 |
+
return choice_group_c(tags_plus_models[0][2])
|
417 |
+
else:
|
418 |
+
result=[]
|
419 |
+
list_index_temp=[]
|
420 |
+
for i in range(len(tags_plus_models[0][2])):
|
421 |
+
list_index_temp.append(i)
|
422 |
+
for i in range(nb_mod_dif):
|
423 |
+
index_temp=randint(1,len(list_index_temp))-1
|
424 |
+
for j in range(nb_rep):
|
425 |
+
result.append(gr.Textbox(tags_plus_models[0][2][list_index_temp[index_temp]]))
|
426 |
+
list_index_temp.remove(list_index_temp[index_temp])
|
427 |
+
return result
|
428 |
+
def rand_from_tag_all_models(index):
|
429 |
+
if len(tags_plus_models[index][2])<nb_mod_dif:
|
430 |
+
return choice_group_c(models_test[index][1][0][1])
|
431 |
+
else:
|
432 |
+
result=[]
|
433 |
+
list_index_temp=[]
|
434 |
+
for i in range(len(tags_plus_models[index][2])):
|
435 |
+
list_index_temp.append(i)
|
436 |
+
for i in range(nb_mod_dif):
|
437 |
+
index_temp=randint(1,len(list_index_temp))-1
|
438 |
+
for j in range(nb_rep):
|
439 |
+
result.append(gr.Textbox(tags_plus_models[index][2][list_index_temp[index_temp]]))
|
440 |
+
list_index_temp.remove(list_index_temp[index_temp])
|
441 |
+
return result
|
442 |
+
|
443 |
+
def find_index_tag(group_tag_choice):
|
444 |
+
for i in (range(len(models_test)-1)):
|
445 |
+
if models_test[i][1]==group_tag_choice:
|
446 |
+
return gr.Number(i)
|
447 |
+
return gr.Number(0)
|
448 |
+
|
449 |
+
def ratio_chosen(choice_ratio,width,height):
|
450 |
+
if choice_ratio == [None,None]:
|
451 |
+
return width , height
|
452 |
+
else :
|
453 |
+
return gr.Slider(label="Width", info="If 0, the default value is used.", maximum=2024, step=32, value=choice_ratio[0]), gr.Slider(label="Height", info="If 0, the default value is used.", maximum=2024, step=32, value=choice_ratio[1])
|
454 |
+
|
455 |
+
list_ratios=[["None",[None,None]],
|
456 |
+
["4:1 (2048 x 512)",[2048,512]],
|
457 |
+
["12:5 (1536 x 640)",[1536,640]],
|
458 |
+
["~16:9 (1344 x 768)",[1344,768]],
|
459 |
+
["~3:2 (1216 x 832)",[1216,832]],
|
460 |
+
["~4:3 (1152 x 896)",[1152,896]],
|
461 |
+
["1:1 (1024 x 1024)",[1024,1024]],
|
462 |
+
["~3:4 (896 x 1152)",[896,1152]],
|
463 |
+
["~2:3 (832 x 1216)",[832,1216]],
|
464 |
+
["~9:16 (768 x 1344)",[768,1344]],
|
465 |
+
["5:12 (640 x 1536)",[640,1536]],
|
466 |
+
["1:4 (512 x 2048)",[512,2048]]]
|
467 |
+
|
468 |
+
def make_me():
|
469 |
+
# with gr.Tab('The Dream'):
|
470 |
+
with gr.Row():
|
471 |
+
#txt_input = gr.Textbox(lines=3, width=300, max_height=100)
|
472 |
+
#txt_input = gr.Textbox(label='Your prompt:', lines=3, width=300, max_height=100)
|
473 |
+
with gr.Column(scale=4):
|
474 |
+
with gr.Group():
|
475 |
+
txt_input = gr.Textbox(label='Your prompt:', lines=3)
|
476 |
+
with gr.Accordion("Advanced", open=False, visible=True):
|
477 |
+
neg_input = gr.Textbox(label='Negative prompt:', lines=1)
|
478 |
+
with gr.Row():
|
479 |
+
width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
480 |
+
height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
481 |
+
with gr.Row():
|
482 |
+
choice_ratio = gr.Dropdown(label="Ratio Width/Height",
|
483 |
+
info="OverWrite Width and Height (W*H<1024*1024)",
|
484 |
+
show_label=True, choices=list(list_ratios) , interactive = True, value=list_ratios[0][1])
|
485 |
+
choice_ratio.change(ratio_chosen,[choice_ratio,width,height],[width,height])
|
486 |
+
with gr.Row():
|
487 |
+
steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
488 |
+
cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
489 |
+
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
490 |
+
#gen_button = gr.Button('Generate images', width=150, height=30)
|
491 |
+
#stop_button = gr.Button('Stop', variant='secondary', interactive=False, width=150, height=30)
|
492 |
+
gen_button = gr.Button('Generate images', scale=3)
|
493 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
|
494 |
+
|
495 |
+
gen_button.click(lambda: gr.update(interactive=True), None, stop_button)
|
496 |
+
#gr.HTML("""
|
497 |
+
#<div style="text-align: center; max-width: 100%; margin: 0 auto;">
|
498 |
+
# <body>
|
499 |
+
# </body>
|
500 |
+
#</div>
|
501 |
+
#""")
|
502 |
+
with gr.Row() as block_images:
|
503 |
+
choices=[models_test[0][1][0][1][0]]
|
504 |
+
output = []
|
505 |
+
current_models = []
|
506 |
+
#text_disp_models = []
|
507 |
+
block_images_liste = []
|
508 |
+
block_images_options_liste = []
|
509 |
+
button_rand_from_tag=[]
|
510 |
+
button_rand_from_all=[]
|
511 |
+
button_rand_from_fav=[]
|
512 |
+
button_blacklisted=[]
|
513 |
+
button_favorites=[]
|
514 |
+
choices_plus = extend_choices(choices)
|
515 |
+
for i in range(nb_mod_dif):
|
516 |
+
with gr.Column(visible=(choices_plus[i*nb_rep] != 'NA')) as block_Temp :
|
517 |
+
block_images_liste.append(block_Temp)
|
518 |
+
with gr.Group():
|
519 |
+
with gr.Row():
|
520 |
+
for j in range(nb_rep):
|
521 |
+
output.append(gr.Image(None, label=choices_plus[i*nb_rep+j],interactive=False,
|
522 |
+
visible=(choices_plus[i*nb_rep+j] != 'NA'),show_label=False,show_share_button=False))
|
523 |
+
for j in range(nb_rep):
|
524 |
+
current_models.append(gr.Textbox(choices_plus[i*nb_rep+j], visible=(j==0),show_label=False))
|
525 |
+
#text_disp_models.append(gr.Textbox(choices_plus[i*nb_rep], visible=(choices_plus[i*nb_rep] != 'NA'),show_label=False))
|
526 |
+
with gr.Row(visible=False) as block_Temp:
|
527 |
+
block_images_options_liste.append(block_Temp)
|
528 |
+
button_rand_from_tag.append(gr.Button("Random\nfrom tag"))
|
529 |
+
button_rand_from_all.append(gr.Button("Random\nfrom all"))
|
530 |
+
button_rand_from_fav.append(gr.Button("Random\nfrom fav"))
|
531 |
+
button_blacklisted.append(gr.Button("put in\nblacklist"))
|
532 |
+
button_favorites.append(gr.Button("put in\nfavorites"))
|
533 |
+
|
534 |
+
|
535 |
+
#output = update_imgbox([choices[0]])
|
536 |
+
#current_models = extend_choices_b([choices[0]])
|
537 |
+
|
538 |
+
for m, o in zip(current_models, output):
|
539 |
+
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
|
540 |
+
inputs=[m, txt_input, neg_input, height, width, steps, cfg, seed], outputs=[o])
|
541 |
+
stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
|
542 |
+
|
543 |
+
with gr.Row() as blockPass:
|
544 |
+
txt_input_p = gr.Textbox(label="Pass", lines=1)
|
545 |
+
test_button = gr.Button(' ')
|
546 |
+
|
547 |
+
|
548 |
+
with gr.Accordion( open=True, visible=False) as stuffs:
|
549 |
+
with gr.Accordion("Advanced",open=False):
|
550 |
+
images_options=gr.Checkbox(False,label="Images Options")
|
551 |
+
images_options.change(lambda x:[gr.Row(visible=x) for b in range(nb_mod_dif)],[images_options],block_images_options_liste)
|
552 |
+
blacklist_perso=gr.Textbox(label="Blacklist perso")
|
553 |
+
fav_perso=gr.Textbox(label="Fav perso")
|
554 |
+
button_rand_from_tag_all_models=gr.Button("Random all models from tag")
|
555 |
+
button_rand_from_all_all_models=gr.Button("Random all models from all")
|
556 |
+
button_rand_from_fav_all_models=gr.Button("Random all models from fav")
|
557 |
+
|
558 |
+
|
559 |
+
with gr.Accordion("Gallery",open=False):
|
560 |
+
with gr.Row():
|
561 |
+
#global cache_image
|
562 |
+
#global cache_image_actu
|
563 |
+
id_session=gr.Number(visible=False,value=0)
|
564 |
+
gen_button.click(set_session, id_session, id_session)
|
565 |
+
cache_image[f"{id_session.value}"]=[]
|
566 |
+
cache_image_actu[f"{id_session.value}"]=[]
|
567 |
+
with gr.Column():
|
568 |
+
b11 = gr.Button('Load Galerry Actu')
|
569 |
+
b12 = gr.Button('Load Galerry All')
|
570 |
+
b13 = gr.Button('Load Galerry All (sorted)')
|
571 |
+
gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
|
572 |
+
interactive=False, show_share_button=True, container=True, format="png",
|
573 |
+
preview=True, object_fit="cover",columns=4,rows=4)
|
574 |
+
with gr.Column():
|
575 |
+
b21 = gr.Button('Reset Gallery')
|
576 |
+
b22 = gr.Button('Reset Gallery All')
|
577 |
+
b23 = gr.Button('Reset All Sessions')
|
578 |
+
b24 = gr.Button('print info sessions')
|
579 |
+
b11.click(load_gallery_actu,[gallery,id_session],gallery)
|
580 |
+
b12.click(load_gallery,[gallery,id_session],gallery)
|
581 |
+
b13.click(load_gallery_sorted,[gallery,id_session],gallery)
|
582 |
+
b21.click(reset_gallery,[gallery],gallery)
|
583 |
+
b22.click(reset_cache_image,[id_session],gallery)
|
584 |
+
b23.click(reset_cache_image_all_sessions,[],[])
|
585 |
+
b24.click(print_info_sessions,[],[])
|
586 |
+
for m, o in zip(current_models, output):
|
587 |
+
#o.change(add_gallery, [o, m, gallery], [gallery])
|
588 |
+
o.change(add_cache_image,[o,m,id_session],[])
|
589 |
+
o.change(add_cache_image_actu,[o,m,id_session],[])
|
590 |
+
gen_button.click(reset_cache_image_actu, [id_session], [])
|
591 |
+
gen_button.click(lambda id:gr.Button('Load Galerry All ('+str(len(cache_image[f"{id}"]))+")"), [id_session], [b12])
|
592 |
+
|
593 |
+
with gr.Group():
|
594 |
+
with gr.Row():
|
595 |
+
#group_tag_choice = gr.Dropdown(label="Lists Tags", show_label=True, choices=list([]) , interactive = True)
|
596 |
+
group_tag_choice = gr.Dropdown(label="Lists Tags", show_label=True, choices=list(models_test), interactive = True,value=models_test[0][1])
|
597 |
+
#group_tag_choice = gr.Dropdown(label="Lists Tags", show_label=True, choices=list(models_test), interactive = True)
|
598 |
+
index_tag=gr.Number(0,visible=False)
|
599 |
+
|
600 |
+
with gr.Row():
|
601 |
+
group_model_choice = gr.Dropdown(label="List of Models with the chosen Tag", show_label=True, choices=list([]), interactive = True)
|
602 |
+
group_model_choice.change(choice_group_b,group_model_choice,output)
|
603 |
+
group_model_choice.change(choice_group_c,group_model_choice,current_models)
|
604 |
+
#group_model_choice.change(choice_group_d,group_model_choice,text_disp_models)
|
605 |
+
group_model_choice.change(choice_group_e,group_model_choice,block_images_liste)
|
606 |
+
group_tag_choice.change(tag_choice,group_tag_choice,group_model_choice)
|
607 |
+
group_tag_choice.change(find_index_tag,group_tag_choice,index_tag)
|
608 |
+
|
609 |
+
with gr.Accordion("Display/Load Models") :
|
610 |
+
with gr.Row():
|
611 |
+
txt_list_models=gr.Textbox(label="Models Actu",value="")
|
612 |
+
group_model_choice.change(disp_models,group_model_choice,txt_list_models)
|
613 |
+
|
614 |
+
with gr.Column():
|
615 |
+
txt_list_perso = gr.Textbox(label='List Models Perso to Load')
|
616 |
+
|
617 |
+
button_list_perso = gr.Button('Load')
|
618 |
+
button_list_perso.click(aff_models_perso_b,txt_list_perso,output)
|
619 |
+
button_list_perso.click(aff_models_perso_c,txt_list_perso,current_models)
|
620 |
+
|
621 |
+
with gr.Row():
|
622 |
+
txt_search = gr.Textbox(label='Search in')
|
623 |
+
txt_output_search = gr.Textbox(label='Search out')
|
624 |
+
button_search = gr.Button('Research')
|
625 |
+
button_search.click(search_models,txt_search,txt_output_search)
|
626 |
+
|
627 |
+
with gr.Row():
|
628 |
+
txt_search_info = gr.Textbox(label='Search info in')
|
629 |
+
txt_output_search_info = gr.Textbox(label='Search info out')
|
630 |
+
button_search_info = gr.Button('Research info')
|
631 |
+
button_search_info.click(search_info,txt_search_info,txt_output_search_info)
|
632 |
+
|
633 |
+
|
634 |
+
with gr.Row():
|
635 |
+
test_button.click(test_pass_aff,txt_input_p,[stuffs,blockPass])
|
636 |
+
#test_button.click(test_pass,txt_input_p,group_tag_choice)
|
637 |
+
|
638 |
+
#text_disp_models = []
|
639 |
+
#button_rand_from_tag=[]
|
640 |
+
#button_rand_from_all=[]
|
641 |
+
button_rand_from_all_all_models.click(rand_from_all_all_models,[],current_models)
|
642 |
+
button_rand_from_tag_all_models.click(rand_from_tag_all_models,index_tag,current_models)
|
643 |
+
for i in range(nb_mod_dif):
|
644 |
+
#######################################################################################################################
|
645 |
+
#button_rand_from_tag.click()
|
646 |
+
#button_rand_from_all.click()
|
647 |
+
#button_rand_from_fav.click()
|
648 |
+
button_blacklisted[i].click(add_in_blacklist,[blacklist_perso,current_models[i*nb_rep]],blacklist_perso)
|
649 |
+
button_favorites[i].click(add_in_fav,[fav_perso,current_models[i*nb_rep]],fav_perso)
|
650 |
+
|
651 |
+
|
652 |
+
|
653 |
+
gr.HTML("""
|
654 |
+
<div class="footer">
|
655 |
+
<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!
|
656 |
+
</p>
|
657 |
+
""")
|
658 |
+
|
659 |
+
js_code = """
|
660 |
+
|
661 |
+
console.log('ghgh');
|
662 |
+
"""
|
663 |
+
|
664 |
+
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:
|
665 |
+
gr.Markdown("<script>" + js_code + "</script>")
|
666 |
+
make_me()
|
667 |
+
|
668 |
+
|
669 |
+
# https://www.gradio.app/guides/setting-up-a-demo-for-maximum-performance
|
670 |
+
#demo.queue(concurrency_count=999) # concurrency_count is deprecated in 4.x
|
671 |
+
demo.queue(default_concurrency_limit=200, max_size=200)
|
672 |
+
demo.launch(max_threads=400)
|