Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -286,7 +286,23 @@ def captioning(img):
|
|
286 |
)
|
287 |
|
288 |
generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
generated_text = generated_text.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
|
|
|
290 |
output_prompt.append(generated_text)
|
291 |
print(generated_text)
|
292 |
# Loop through prompts array:
|
@@ -331,7 +347,7 @@ def captioning(img):
|
|
331 |
output_prompt.append(response_text)
|
332 |
'''
|
333 |
output_prompt = " ".join(output_prompt)
|
334 |
-
return output_prompt
|
335 |
|
336 |
def flatten_and_stringify(data):
|
337 |
return [str(item) for sublist in data if isinstance(sublist, list) for item in flatten_and_stringify(sublist) ] + [str(item) for item in data if not isinstance(item, list)]
|
@@ -433,19 +449,22 @@ def generate_30(
|
|
433 |
image_paths=[]
|
434 |
seed = random.randint(0, MAX_SEED)
|
435 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
|
|
436 |
if latent_file is not None: # Check if a latent file is provided
|
437 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
438 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
439 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
440 |
caption=[]
|
441 |
#caption.append(captioning(sd_image_a))
|
442 |
-
prompt1=captioning(sd_image_a)
|
|
|
443 |
if latent_file_2 is not None: # Check if a latent file is provided
|
444 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
445 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
446 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
447 |
#caption.append(captioning(sd_image_b))
|
448 |
-
prompt2=captioning(sd_image_b)
|
|
|
449 |
else:
|
450 |
sd_image_b = None
|
451 |
if latent_file_3 is not None: # Check if a latent file is provided
|
@@ -453,7 +472,8 @@ def generate_30(
|
|
453 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
454 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
455 |
#caption.append(captioning(sd_image_c))
|
456 |
-
prompt3=captioning(sd_image_c)
|
|
|
457 |
else:
|
458 |
sd_image_c = None
|
459 |
if latent_file_4 is not None: # Check if a latent file is provided
|
@@ -461,7 +481,8 @@ def generate_30(
|
|
461 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
462 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
463 |
#caption.append(captioning(sd_image_d))
|
464 |
-
prompt4=captioning(sd_image_d)
|
|
|
465 |
else:
|
466 |
sd_image_d = None
|
467 |
if latent_file_5 is not None: # Check if a latent file is provided
|
@@ -469,7 +490,8 @@ def generate_30(
|
|
469 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
470 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
471 |
#caption.append(captioning(sd_image_e))
|
472 |
-
prompt5=captioning(sd_image_e)
|
|
|
473 |
else:
|
474 |
sd_image_e = None
|
475 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -481,6 +503,7 @@ def generate_30(
|
|
481 |
print("-- CURRENT PROMPT AFTER .join --")
|
482 |
print(prompt)
|
483 |
captions = " ".join(caption)
|
|
|
484 |
print(captions)
|
485 |
print("-- not generating further caption --")
|
486 |
global model5
|
@@ -494,7 +517,7 @@ def generate_30(
|
|
494 |
new_prompt = prompt + ' ' + captions
|
495 |
print("-- ------------ --")
|
496 |
print("-- FINAL PROMPT --")
|
497 |
-
print(
|
498 |
print("-- FINAL PROMPT --")
|
499 |
print("-- ------------ --")
|
500 |
gc.collect()
|
@@ -577,19 +600,22 @@ def generate_60(
|
|
577 |
image_paths=[]
|
578 |
seed = random.randint(0, MAX_SEED)
|
579 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
|
|
580 |
if latent_file is not None: # Check if a latent file is provided
|
581 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
582 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
583 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
584 |
caption=[]
|
585 |
#caption.append(captioning(sd_image_a))
|
586 |
-
prompt1=captioning(sd_image_a)
|
|
|
587 |
if latent_file_2 is not None: # Check if a latent file is provided
|
588 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
589 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
590 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
591 |
#caption.append(captioning(sd_image_b))
|
592 |
-
prompt2=captioning(sd_image_b)
|
|
|
593 |
else:
|
594 |
sd_image_b = None
|
595 |
if latent_file_3 is not None: # Check if a latent file is provided
|
@@ -597,7 +623,8 @@ def generate_60(
|
|
597 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
598 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
599 |
#caption.append(captioning(sd_image_c))
|
600 |
-
prompt3=captioning(sd_image_c)
|
|
|
601 |
else:
|
602 |
sd_image_c = None
|
603 |
if latent_file_4 is not None: # Check if a latent file is provided
|
@@ -605,7 +632,8 @@ def generate_60(
|
|
605 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
606 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
607 |
#caption.append(captioning(sd_image_d))
|
608 |
-
prompt4=captioning(sd_image_d)
|
|
|
609 |
else:
|
610 |
sd_image_d = None
|
611 |
if latent_file_5 is not None: # Check if a latent file is provided
|
@@ -613,7 +641,8 @@ def generate_60(
|
|
613 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
614 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
615 |
#caption.append(captioning(sd_image_e))
|
616 |
-
prompt5=captioning(sd_image_e)
|
|
|
617 |
else:
|
618 |
sd_image_e = None
|
619 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -625,6 +654,7 @@ def generate_60(
|
|
625 |
print("-- CURRENT PROMPT AFTER .join --")
|
626 |
print(prompt)
|
627 |
captions = " ".join(caption)
|
|
|
628 |
print(captions)
|
629 |
print("-- not generating further caption --")
|
630 |
global model5
|
@@ -638,7 +668,7 @@ def generate_60(
|
|
638 |
new_prompt = prompt + ' ' + captions
|
639 |
print("-- ------------ --")
|
640 |
print("-- FINAL PROMPT --")
|
641 |
-
print(
|
642 |
print("-- FINAL PROMPT --")
|
643 |
print("-- ------------ --")
|
644 |
gc.collect()
|
@@ -721,19 +751,22 @@ def generate_90(
|
|
721 |
image_paths=[]
|
722 |
seed = random.randint(0, MAX_SEED)
|
723 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
|
|
724 |
if latent_file is not None: # Check if a latent file is provided
|
725 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
726 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
727 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
728 |
caption=[]
|
729 |
#caption.append(captioning(sd_image_a))
|
730 |
-
prompt1=captioning(sd_image_a)
|
|
|
731 |
if latent_file_2 is not None: # Check if a latent file is provided
|
732 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
733 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
734 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
735 |
#caption.append(captioning(sd_image_b))
|
736 |
-
prompt2=captioning(sd_image_b)
|
|
|
737 |
else:
|
738 |
sd_image_b = None
|
739 |
if latent_file_3 is not None: # Check if a latent file is provided
|
@@ -741,7 +774,8 @@ def generate_90(
|
|
741 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
742 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
743 |
#caption.append(captioning(sd_image_c))
|
744 |
-
prompt3=captioning(sd_image_c)
|
|
|
745 |
else:
|
746 |
sd_image_c = None
|
747 |
if latent_file_4 is not None: # Check if a latent file is provided
|
@@ -749,7 +783,8 @@ def generate_90(
|
|
749 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
750 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
751 |
#caption.append(captioning(sd_image_d))
|
752 |
-
prompt4=captioning(sd_image_d)
|
|
|
753 |
else:
|
754 |
sd_image_d = None
|
755 |
if latent_file_5 is not None: # Check if a latent file is provided
|
@@ -757,7 +792,8 @@ def generate_90(
|
|
757 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
758 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
759 |
#caption.append(captioning(sd_image_e))
|
760 |
-
prompt5=captioning(sd_image_e)
|
|
|
761 |
else:
|
762 |
sd_image_e = None
|
763 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -769,6 +805,7 @@ def generate_90(
|
|
769 |
print("-- CURRENT PROMPT AFTER .join --")
|
770 |
print(prompt)
|
771 |
captions = " ".join(caption)
|
|
|
772 |
print(captions)
|
773 |
print("-- not generating further caption --")
|
774 |
global model5
|
@@ -782,7 +819,7 @@ def generate_90(
|
|
782 |
new_prompt = prompt + ' ' + captions
|
783 |
print("-- ------------ --")
|
784 |
print("-- FINAL PROMPT --")
|
785 |
-
print(
|
786 |
print("-- FINAL PROMPT --")
|
787 |
print("-- ------------ --")
|
788 |
gc.collect()
|
|
|
286 |
)
|
287 |
|
288 |
generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
289 |
+
|
290 |
+
generated_idsb = model5.generate(
|
291 |
+
**inputsa,
|
292 |
+
do_sample=True,
|
293 |
+
num_beams=1,
|
294 |
+
max_length=16,
|
295 |
+
min_length=12,
|
296 |
+
top_p=0.9,
|
297 |
+
repetition_penalty=1.0,
|
298 |
+
length_penalty=2.0,
|
299 |
+
temperature=0.5,
|
300 |
+
)
|
301 |
+
|
302 |
+
generated_textb = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
303 |
+
|
304 |
generated_text = generated_text.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
|
305 |
+
generated_textb = generated_textb.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
|
306 |
output_prompt.append(generated_text)
|
307 |
print(generated_text)
|
308 |
# Loop through prompts array:
|
|
|
347 |
output_prompt.append(response_text)
|
348 |
'''
|
349 |
output_prompt = " ".join(output_prompt)
|
350 |
+
return output_prompt, generated_textb
|
351 |
|
352 |
def flatten_and_stringify(data):
|
353 |
return [str(item) for sublist in data if isinstance(sublist, list) for item in flatten_and_stringify(sublist) ] + [str(item) for item in data if not isinstance(item, list)]
|
|
|
449 |
image_paths=[]
|
450 |
seed = random.randint(0, MAX_SEED)
|
451 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
452 |
+
capt=[]
|
453 |
if latent_file is not None: # Check if a latent file is provided
|
454 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
455 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
456 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
457 |
caption=[]
|
458 |
#caption.append(captioning(sd_image_a))
|
459 |
+
prompt1, cap=captioning(sd_image_a)
|
460 |
+
capt.append(cap)
|
461 |
if latent_file_2 is not None: # Check if a latent file is provided
|
462 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
463 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
464 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
465 |
#caption.append(captioning(sd_image_b))
|
466 |
+
prompt2, cap=captioning(sd_image_b)
|
467 |
+
capt.append(cap)
|
468 |
else:
|
469 |
sd_image_b = None
|
470 |
if latent_file_3 is not None: # Check if a latent file is provided
|
|
|
472 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
473 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
474 |
#caption.append(captioning(sd_image_c))
|
475 |
+
prompt3, cap=captioning(sd_image_c)
|
476 |
+
capt.append(cap)
|
477 |
else:
|
478 |
sd_image_c = None
|
479 |
if latent_file_4 is not None: # Check if a latent file is provided
|
|
|
481 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
482 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
483 |
#caption.append(captioning(sd_image_d))
|
484 |
+
prompt4, cap=captioning(sd_image_d)
|
485 |
+
capt.append(cap)
|
486 |
else:
|
487 |
sd_image_d = None
|
488 |
if latent_file_5 is not None: # Check if a latent file is provided
|
|
|
490 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
491 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
492 |
#caption.append(captioning(sd_image_e))
|
493 |
+
prompt5, cap=captioning(sd_image_e)
|
494 |
+
capt.append(cap)
|
495 |
else:
|
496 |
sd_image_e = None
|
497 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
503 |
print("-- CURRENT PROMPT AFTER .join --")
|
504 |
print(prompt)
|
505 |
captions = " ".join(caption)
|
506 |
+
capt = " ".join(capt)
|
507 |
print(captions)
|
508 |
print("-- not generating further caption --")
|
509 |
global model5
|
|
|
517 |
new_prompt = prompt + ' ' + captions
|
518 |
print("-- ------------ --")
|
519 |
print("-- FINAL PROMPT --")
|
520 |
+
print(capt)
|
521 |
print("-- FINAL PROMPT --")
|
522 |
print("-- ------------ --")
|
523 |
gc.collect()
|
|
|
600 |
image_paths=[]
|
601 |
seed = random.randint(0, MAX_SEED)
|
602 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
603 |
+
capt=[]
|
604 |
if latent_file is not None: # Check if a latent file is provided
|
605 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
606 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
607 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
608 |
caption=[]
|
609 |
#caption.append(captioning(sd_image_a))
|
610 |
+
prompt1, cap=captioning(sd_image_a)
|
611 |
+
capt.append(cap)
|
612 |
if latent_file_2 is not None: # Check if a latent file is provided
|
613 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
614 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
615 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
616 |
#caption.append(captioning(sd_image_b))
|
617 |
+
prompt2, cap=captioning(sd_image_b)
|
618 |
+
capt.append(cap)
|
619 |
else:
|
620 |
sd_image_b = None
|
621 |
if latent_file_3 is not None: # Check if a latent file is provided
|
|
|
623 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
624 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
625 |
#caption.append(captioning(sd_image_c))
|
626 |
+
prompt3, cap=captioning(sd_image_c)
|
627 |
+
capt.append(cap)
|
628 |
else:
|
629 |
sd_image_c = None
|
630 |
if latent_file_4 is not None: # Check if a latent file is provided
|
|
|
632 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
633 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
634 |
#caption.append(captioning(sd_image_d))
|
635 |
+
prompt4, cap=captioning(sd_image_d)
|
636 |
+
capt.append(cap)
|
637 |
else:
|
638 |
sd_image_d = None
|
639 |
if latent_file_5 is not None: # Check if a latent file is provided
|
|
|
641 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
642 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
643 |
#caption.append(captioning(sd_image_e))
|
644 |
+
prompt5, cap=captioning(sd_image_e)
|
645 |
+
capt.append(cap)
|
646 |
else:
|
647 |
sd_image_e = None
|
648 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
654 |
print("-- CURRENT PROMPT AFTER .join --")
|
655 |
print(prompt)
|
656 |
captions = " ".join(caption)
|
657 |
+
capt = " ".join(capt)
|
658 |
print(captions)
|
659 |
print("-- not generating further caption --")
|
660 |
global model5
|
|
|
668 |
new_prompt = prompt + ' ' + captions
|
669 |
print("-- ------------ --")
|
670 |
print("-- FINAL PROMPT --")
|
671 |
+
print(capt)
|
672 |
print("-- FINAL PROMPT --")
|
673 |
print("-- ------------ --")
|
674 |
gc.collect()
|
|
|
751 |
image_paths=[]
|
752 |
seed = random.randint(0, MAX_SEED)
|
753 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
754 |
+
capt=[]
|
755 |
if latent_file is not None: # Check if a latent file is provided
|
756 |
sd_image_a = Image.open(latent_file.name).convert('RGB')
|
757 |
sd_image_a.resize((768,768), Image.LANCZOS)
|
758 |
#sd_image_a.resize((height,width), Image.LANCZOS)
|
759 |
caption=[]
|
760 |
#caption.append(captioning(sd_image_a))
|
761 |
+
prompt1, cap=captioning(sd_image_a)
|
762 |
+
capt.append(cap)
|
763 |
if latent_file_2 is not None: # Check if a latent file is provided
|
764 |
sd_image_b = Image.open(latent_file_2.name).convert('RGB')
|
765 |
#sd_image_b.resize((height,width), Image.LANCZOS)
|
766 |
sd_image_b.resize((768,768), Image.LANCZOS)
|
767 |
#caption.append(captioning(sd_image_b))
|
768 |
+
prompt2, cap=captioning(sd_image_b)
|
769 |
+
capt.append(cap)
|
770 |
else:
|
771 |
sd_image_b = None
|
772 |
if latent_file_3 is not None: # Check if a latent file is provided
|
|
|
774 |
#sd_image_c.resize((height,width), Image.LANCZOS)
|
775 |
sd_image_c.resize((768,768), Image.LANCZOS)
|
776 |
#caption.append(captioning(sd_image_c))
|
777 |
+
prompt3, cap=captioning(sd_image_c)
|
778 |
+
capt.append(cap)
|
779 |
else:
|
780 |
sd_image_c = None
|
781 |
if latent_file_4 is not None: # Check if a latent file is provided
|
|
|
783 |
#sd_image_d.resize((height,width), Image.LANCZOS)
|
784 |
sd_image_d.resize((768,768), Image.LANCZOS)
|
785 |
#caption.append(captioning(sd_image_d))
|
786 |
+
prompt4, cap=captioning(sd_image_d)
|
787 |
+
capt.append(cap)
|
788 |
else:
|
789 |
sd_image_d = None
|
790 |
if latent_file_5 is not None: # Check if a latent file is provided
|
|
|
792 |
#sd_image_e.resize((height,width), Image.LANCZOS)
|
793 |
sd_image_e.resize((768,768), Image.LANCZOS)
|
794 |
#caption.append(captioning(sd_image_e))
|
795 |
+
prompt5, cap=captioning(sd_image_e)
|
796 |
+
capt.append(cap)
|
797 |
else:
|
798 |
sd_image_e = None
|
799 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
805 |
print("-- CURRENT PROMPT AFTER .join --")
|
806 |
print(prompt)
|
807 |
captions = " ".join(caption)
|
808 |
+
capt = " ".join(capt)
|
809 |
print(captions)
|
810 |
print("-- not generating further caption --")
|
811 |
global model5
|
|
|
819 |
new_prompt = prompt + ' ' + captions
|
820 |
print("-- ------------ --")
|
821 |
print("-- FINAL PROMPT --")
|
822 |
+
print(capt)
|
823 |
print("-- FINAL PROMPT --")
|
824 |
print("-- ------------ --")
|
825 |
gc.collect()
|