Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -364,18 +364,22 @@ def crea_list_task(id_session,list_param,list_models_to_gen,nb_images_by_prompt)
|
|
364 |
dict_temp={}
|
365 |
dict_temp["model"]=m
|
366 |
dict_temp["id_module"]=-1
|
|
|
367 |
dict_temp["task"]=[]
|
368 |
list_progress.append(0)
|
|
|
369 |
for p in list_param:
|
370 |
for i in range(nb_images_by_prompt):
|
371 |
-
dict_temp["task"].append({"prompt":p[0],"nprompt":p[1],"width":p[2],"height":p[3],"steps":p[4],"cfg":p[5],"seed":p[6]})
|
|
|
372 |
cache_list_task[f"{id_session}"].append(dict_temp)
|
373 |
|
374 |
cache_text_actu[f"{id_session}"]={"nb_modules_use":nb_req_simult,"stop":False,"nb_fail":0,"warm":False,
|
375 |
"nb_models_to_do":len(list_models_to_gen) ,"nb_models_tot":len(list_models_to_gen) ,
|
376 |
"nb_tasks_to_do":len(list_models_to_gen)*len(list_param)*nb_images_by_prompt ,
|
377 |
"nb_tasks_tot":len(list_models_to_gen)*len(list_param)*nb_images_by_prompt,
|
378 |
-
"progress":list_progress,'nb_tasks_by_model': nb_images_by_prompt*len(list_param)
|
|
|
379 |
|
380 |
def fonc_update_actu(text_actu,id):
|
381 |
s=""
|
@@ -413,11 +417,12 @@ def fonc_update_actu_2(id):
|
|
413 |
s+="\n"
|
414 |
|
415 |
s+="\n"
|
416 |
-
s+=f"modules: {cache_text_actu[str(id)]['nb_modules_use']}/{nb_req_simult} ({
|
417 |
s+=f"models remaining: {cache_text_actu[str(id)]['nb_models_to_do']}/{cache_text_actu[str(id)]['nb_models_tot']}\n"
|
418 |
|
419 |
s+=f"images remaining(done): {cache_text_actu[str(id)]['nb_tasks_to_do']}({cache_text_actu[str(id)]['nb_tasks_tot']-cache_text_actu[str(id)]['nb_tasks_to_do']})/{cache_text_actu[str(id)]['nb_tasks_tot']}\n"
|
420 |
s+=f"fail attempt: {cache_text_actu[str(id)]['nb_fail']}\n"
|
|
|
421 |
#s+=f"{tempo_update_actu*incr_update_actu[str(id)]} s"
|
422 |
#incr_update_actu[str(id)]+=1
|
423 |
s+=f"{randint(1,MAX_SEED)}"
|
@@ -464,11 +469,15 @@ def fonc_load_gallery(id_session,gallery):
|
|
464 |
|
465 |
def fonc_move_gallery_by_model(id_session,gallery,index_g,models,index_m,direction):
|
466 |
delta=int((nb_gallery_model-1)/2)
|
|
|
467 |
if index_g==(index_m+(delta*direction))%nb_gallery_model :
|
468 |
gallery = reset_gallery(gallery)
|
469 |
for i in range(len(cache_image[f"{id_session}"])):
|
470 |
if cache_image[f"{id_session}"][i]["model"]==models[(index_m+(delta*direction))%len(models)]:
|
471 |
-
|
|
|
|
|
|
|
472 |
if index_g==(index_m-direction)%nb_gallery_model:
|
473 |
return gr.Gallery(gallery,visible=False)
|
474 |
#return gr.Gallery(gallery,visible=True)
|
@@ -485,21 +494,49 @@ def fonc_start(id_session,id_module,s,cont,list_models_to_gen):
|
|
485 |
return None,gr.Textbox(s),gr.Number(randint(1,MAX_SEED))
|
486 |
task_actu={}
|
487 |
model_actu=""
|
|
|
488 |
print(f"in fonc : id module={id_module}\n")
|
|
|
|
|
|
|
489 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
490 |
-
if
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
|
496 |
if model_actu=="":
|
497 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
498 |
if model_plus_tasks["id_module"]==-1 :
|
499 |
if model_actu=="":
|
500 |
-
|
|
|
|
|
|
|
|
|
501 |
model_actu=model_plus_tasks["model"]
|
502 |
-
|
|
|
503 |
print(f"module num {id_module} take {model_actu}\n")
|
504 |
i=0
|
505 |
for model in list_models_to_gen:
|
@@ -514,22 +551,25 @@ def fonc_start(id_session,id_module,s,cont,list_models_to_gen):
|
|
514 |
print("begin gen image:")
|
515 |
print(model_actu)
|
516 |
print(task_actu)
|
517 |
-
|
|
|
|
|
|
|
518 |
print("reception")
|
519 |
if result!=None:
|
520 |
#result=gr.Image(result)
|
521 |
id_image=len(cache_image[f"{id_session}"])
|
522 |
i=0
|
523 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
524 |
-
if model_plus_tasks["
|
525 |
-
model_plus_tasks["
|
526 |
cache_text_actu[f"{id_session}"]["nb_tasks_to_do"]-=1
|
527 |
i=0
|
528 |
for model in list_models_to_gen:
|
529 |
if model_actu==model:
|
530 |
-
cache_text_actu[f"{id_session}"]['progress'][i]=int(((1-(len(model_plus_tasks["task"])/cache_text_actu[f"{id_session}"]["nb_tasks_by_model"]))*7)//1)+2
|
531 |
i+=1
|
532 |
-
if len(model_plus_tasks["task"])==0:
|
533 |
cache_list_task[f"{id_session}"].remove(model_plus_tasks)
|
534 |
cache_text_actu[f"{id_session}"]["nb_models_to_do"]-=1
|
535 |
|
@@ -542,12 +582,15 @@ def fonc_start(id_session,id_module,s,cont,list_models_to_gen):
|
|
542 |
cache_image[f"{id_session}"].append(task_actu)
|
543 |
print("image saved\n")
|
544 |
else:
|
|
|
545 |
cache_text_actu[f"{id_session}"]["nb_fail"]+=1
|
546 |
print("fail to generate\n")
|
547 |
num_task_to_do=0
|
548 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
549 |
for task in model_plus_tasks["task"]:
|
550 |
num_task_to_do+=1
|
|
|
|
|
551 |
print(f"\n {num_task_to_do} tasks to do\n")
|
552 |
return result , gr.Textbox(s+"1"),gr.Number(randint(1,MAX_SEED))
|
553 |
|
@@ -557,9 +600,13 @@ def fonc_init(s):
|
|
557 |
def fonc_load_gallery_by_model(id_session,gallery,models,index_g,index_m,gallery_all):
|
558 |
delta=int((nb_gallery_model-1)/2)
|
559 |
gallery = reset_gallery(gallery)
|
|
|
560 |
for i in range(len(cache_image[f"{id_session}"])):
|
561 |
if cache_image[f"{id_session}"][i]["model"]==models[((index_m+index_g+delta)%nb_gallery_model)-delta]:
|
562 |
-
|
|
|
|
|
|
|
563 |
return gr.Gallery(gallery,visible=(index_g==(index_m%nb_gallery_model))), gr.Gallery(gallery_all,visible=False)
|
564 |
|
565 |
def load_gallery_by_prompt(id_session,gallery,index_p,list_p):
|
|
|
364 |
dict_temp={}
|
365 |
dict_temp["model"]=m
|
366 |
dict_temp["id_module"]=-1
|
367 |
+
dict_temp["pending_task"]=0
|
368 |
dict_temp["task"]=[]
|
369 |
list_progress.append(0)
|
370 |
+
index_prompt=0
|
371 |
for p in list_param:
|
372 |
for i in range(nb_images_by_prompt):
|
373 |
+
dict_temp["task"].append({"prompt":p[0],"nprompt":p[1],"width":p[2],"height":p[3],"steps":p[4],"cfg":p[5],"seed":p[6],"index_prompt":index_prompt})
|
374 |
+
index_prompt+=1
|
375 |
cache_list_task[f"{id_session}"].append(dict_temp)
|
376 |
|
377 |
cache_text_actu[f"{id_session}"]={"nb_modules_use":nb_req_simult,"stop":False,"nb_fail":0,"warm":False,
|
378 |
"nb_models_to_do":len(list_models_to_gen) ,"nb_models_tot":len(list_models_to_gen) ,
|
379 |
"nb_tasks_to_do":len(list_models_to_gen)*len(list_param)*nb_images_by_prompt ,
|
380 |
"nb_tasks_tot":len(list_models_to_gen)*len(list_param)*nb_images_by_prompt,
|
381 |
+
"progress":list_progress,'nb_tasks_by_model': nb_images_by_prompt*len(list_param),
|
382 |
+
"nb_warm_in_use":0}
|
383 |
|
384 |
def fonc_update_actu(text_actu,id):
|
385 |
s=""
|
|
|
417 |
s+="\n"
|
418 |
|
419 |
s+="\n"
|
420 |
+
s+=f"modules: {cache_text_actu[str(id)]['nb_modules_use']}/{nb_req_simult} ({nb_ones}/{cache_text_actu[str(id)]['nb_modules_use']})\n"
|
421 |
s+=f"models remaining: {cache_text_actu[str(id)]['nb_models_to_do']}/{cache_text_actu[str(id)]['nb_models_tot']}\n"
|
422 |
|
423 |
s+=f"images remaining(done): {cache_text_actu[str(id)]['nb_tasks_to_do']}({cache_text_actu[str(id)]['nb_tasks_tot']-cache_text_actu[str(id)]['nb_tasks_to_do']})/{cache_text_actu[str(id)]['nb_tasks_tot']}\n"
|
424 |
s+=f"fail attempt: {cache_text_actu[str(id)]['nb_fail']}\n"
|
425 |
+
s+=f"warm task pending = {cache_text_actu[str(id)]['nb_warm_in_use']}\n"
|
426 |
#s+=f"{tempo_update_actu*incr_update_actu[str(id)]} s"
|
427 |
#incr_update_actu[str(id)]+=1
|
428 |
s+=f"{randint(1,MAX_SEED)}"
|
|
|
469 |
|
470 |
def fonc_move_gallery_by_model(id_session,gallery,index_g,models,index_m,direction):
|
471 |
delta=int((nb_gallery_model-1)/2)
|
472 |
+
list_image_temp=[]
|
473 |
if index_g==(index_m+(delta*direction))%nb_gallery_model :
|
474 |
gallery = reset_gallery(gallery)
|
475 |
for i in range(len(cache_image[f"{id_session}"])):
|
476 |
if cache_image[f"{id_session}"][i]["model"]==models[(index_m+(delta*direction))%len(models)]:
|
477 |
+
list_image_temp.append([cache_image[f"{id_session}"][i]["image"],cache_image[f"{id_session}"][i]["model"],cache_image[f"{id_session}"][i]["index_prompt"]])
|
478 |
+
|
479 |
+
for temp in sorted(list_image_temp,key=itemgetter(2)):
|
480 |
+
gallery=add_gallery(temp[0],temp[1],gallery)
|
481 |
if index_g==(index_m-direction)%nb_gallery_model:
|
482 |
return gr.Gallery(gallery,visible=False)
|
483 |
#return gr.Gallery(gallery,visible=True)
|
|
|
494 |
return None,gr.Textbox(s),gr.Number(randint(1,MAX_SEED))
|
495 |
task_actu={}
|
496 |
model_actu=""
|
497 |
+
use_warm_model=False
|
498 |
print(f"in fonc : id module={id_module}\n")
|
499 |
+
|
500 |
+
warm_models , models_plus_tags_temp = find_warm_model_list("John6666", ["stable-diffusion-xl"], "", "last_modified", 10000)
|
501 |
+
|
502 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
503 |
+
if model_actu == "":
|
504 |
+
if model_plus_tasks["model"] in warm_models:
|
505 |
+
try:
|
506 |
+
task_actu=model_plus_tasks["task"].pop()
|
507 |
+
except:
|
508 |
+
continue
|
509 |
+
model_actu=model_plus_tasks["model"]
|
510 |
+
model_plus_tasks["pending_task"]+=1
|
511 |
+
use_warm_model=True
|
512 |
+
cache_text_actu[f"{id_session}"]["nb_warm_in_use"]+=1
|
513 |
+
print(f"warm model : {model_actu}\n")
|
514 |
+
break
|
515 |
+
|
516 |
+
if model_actu == "":
|
517 |
+
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
518 |
+
if model_plus_tasks["id_module"]==id_module:
|
519 |
+
try:
|
520 |
+
task_actu=model_plus_tasks["task"].pop()
|
521 |
+
except:
|
522 |
+
continue
|
523 |
+
model_actu=model_plus_tasks["model"]
|
524 |
+
model_plus_tasks["pending_task"]+=1
|
525 |
+
print(f"find model : {model_actu}\n")
|
526 |
+
break
|
527 |
|
528 |
if model_actu=="":
|
529 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
530 |
if model_plus_tasks["id_module"]==-1 :
|
531 |
if model_actu=="":
|
532 |
+
|
533 |
+
try:
|
534 |
+
task_actu=model_plus_tasks["task"].pop()
|
535 |
+
except:
|
536 |
+
continue
|
537 |
model_actu=model_plus_tasks["model"]
|
538 |
+
model_plus_tasks["pending_task"]+=1
|
539 |
+
model_plus_tasks["id_module"]=id_module
|
540 |
print(f"module num {id_module} take {model_actu}\n")
|
541 |
i=0
|
542 |
for model in list_models_to_gen:
|
|
|
551 |
print("begin gen image:")
|
552 |
print(model_actu)
|
553 |
print(task_actu)
|
554 |
+
if use_warm_model:
|
555 |
+
result=gen_fn(model_actu, task_actu["prompt"], task_actu["nprompt"], task_actu["height"], task_actu["width"], task_actu["steps"], task_actu["cfg"], task_actu["seed"],inference_timeout_w)
|
556 |
+
else:
|
557 |
+
result=gen_fn(model_actu, task_actu["prompt"], task_actu["nprompt"], task_actu["height"], task_actu["width"], task_actu["steps"], task_actu["cfg"], task_actu["seed"])
|
558 |
print("reception")
|
559 |
if result!=None:
|
560 |
#result=gr.Image(result)
|
561 |
id_image=len(cache_image[f"{id_session}"])
|
562 |
i=0
|
563 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
564 |
+
if model_plus_tasks["model"]==model_actu:
|
565 |
+
model_plus_tasks["pending_task"]-=1
|
566 |
cache_text_actu[f"{id_session}"]["nb_tasks_to_do"]-=1
|
567 |
i=0
|
568 |
for model in list_models_to_gen:
|
569 |
if model_actu==model:
|
570 |
+
cache_text_actu[f"{id_session}"]['progress'][i]=int(((1-((len(model_plus_tasks["task"])+model_plus_tasks["pending_task"])/cache_text_actu[f"{id_session}"]["nb_tasks_by_model"]))*7)//1)+2
|
571 |
i+=1
|
572 |
+
if len(model_plus_tasks["task"])+model_plus_tasks["pending_task"]==0:
|
573 |
cache_list_task[f"{id_session}"].remove(model_plus_tasks)
|
574 |
cache_text_actu[f"{id_session}"]["nb_models_to_do"]-=1
|
575 |
|
|
|
582 |
cache_image[f"{id_session}"].append(task_actu)
|
583 |
print("image saved\n")
|
584 |
else:
|
585 |
+
model_plus_tasks["task"].append(task_actu)
|
586 |
cache_text_actu[f"{id_session}"]["nb_fail"]+=1
|
587 |
print("fail to generate\n")
|
588 |
num_task_to_do=0
|
589 |
for model_plus_tasks in cache_list_task[f"{id_session}"]:
|
590 |
for task in model_plus_tasks["task"]:
|
591 |
num_task_to_do+=1
|
592 |
+
if use_warm_model:
|
593 |
+
cache_text_actu[f"{id_session}"]["nb_warm_in_use"]-=1
|
594 |
print(f"\n {num_task_to_do} tasks to do\n")
|
595 |
return result , gr.Textbox(s+"1"),gr.Number(randint(1,MAX_SEED))
|
596 |
|
|
|
600 |
def fonc_load_gallery_by_model(id_session,gallery,models,index_g,index_m,gallery_all):
|
601 |
delta=int((nb_gallery_model-1)/2)
|
602 |
gallery = reset_gallery(gallery)
|
603 |
+
list_image_temp=[]
|
604 |
for i in range(len(cache_image[f"{id_session}"])):
|
605 |
if cache_image[f"{id_session}"][i]["model"]==models[((index_m+index_g+delta)%nb_gallery_model)-delta]:
|
606 |
+
list_image_temp.append([cache_image[f"{id_session}"][i]["image"],cache_image[f"{id_session}"][i]["model"],cache_image[f"{id_session}"][i]["index_prompt"]])
|
607 |
+
|
608 |
+
for temp in sorted(list_image_temp,key=itemgetter(2)):
|
609 |
+
gallery=add_gallery(temp[0],temp[1],gallery)
|
610 |
return gr.Gallery(gallery,visible=(index_g==(index_m%nb_gallery_model))), gr.Gallery(gallery_all,visible=False)
|
611 |
|
612 |
def load_gallery_by_prompt(id_session,gallery,index_p,list_p):
|