joy: don't load model when there is no work to do
Browse files
joy
CHANGED
@@ -716,6 +716,21 @@ def main():
|
|
716 |
|
717 |
setup_logging(args.verbose)
|
718 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
719 |
# Validate random-tags usage
|
720 |
if args.random_tags is not None and args.feed_from_tags is None:
|
721 |
parser.error("--random-tags can only be used when --feed-from-tags is enabled")
|
@@ -731,61 +746,52 @@ def main():
|
|
731 |
else:
|
732 |
logging.info("Running in dry-run mode. Models will not be loaded.")
|
733 |
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
caption_file = image_path.with_suffix(".caption")
|
738 |
-
|
739 |
-
# Skip if the caption file already exists
|
740 |
-
if caption_file.exists():
|
741 |
-
logging.info(f"Skipping {image_path}: Caption file already exists.")
|
742 |
-
continue
|
743 |
|
744 |
-
|
745 |
-
|
|
|
|
|
746 |
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
751 |
|
752 |
-
|
753 |
-
raise ValueError(
|
754 |
-
"feed-from-tags and artist-from-folder can't be used together"
|
755 |
-
)
|
756 |
-
if args.feed_from_tags is not None:
|
757 |
-
prompt = prompt_from_tags(args, image_path, tagset_normalizer, prompt)
|
758 |
-
elif args.artist_from_folder:
|
759 |
-
prompt = prompt_from_folder(prompt, image_path.resolve())
|
760 |
-
|
761 |
-
if args.dry_run:
|
762 |
-
logging.info(
|
763 |
-
f"Dry run: Skipping caption generation for {image_path} with prompt:\n\t{prompt}"
|
764 |
-
)
|
765 |
-
continue
|
766 |
-
else:
|
767 |
-
logging.info(f"Prompt for {image_path}:\n\t{prompt}")
|
768 |
-
|
769 |
-
caption = joy_caption_model.generate_valid_caption(input_image, prompt)
|
770 |
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
|
780 |
-
|
781 |
-
|
782 |
|
783 |
-
|
784 |
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
|
790 |
|
791 |
RE_PARENS_SUFFIX = re.compile(r"_\([^)]+\)$")
|
|
|
716 |
|
717 |
setup_logging(args.verbose)
|
718 |
|
719 |
+
tasks = []
|
720 |
+
image_extensions = {".webp", ".png", ".jpeg", ".jpg", ".jxl"}
|
721 |
+
for image_path in Path(args.directory).rglob("*"):
|
722 |
+
if image_path.suffix.lower() in image_extensions:
|
723 |
+
caption_file = image_path.with_suffix(".caption")
|
724 |
+
# Skip if the caption file already exists
|
725 |
+
if caption_file.exists():
|
726 |
+
logging.info(f"Skipping {image_path}: Caption file already exists.")
|
727 |
+
continue
|
728 |
+
tasks.append((image_path, caption_file))
|
729 |
+
|
730 |
+
if not tasks:
|
731 |
+
logging.error('No input file found.')
|
732 |
+
return
|
733 |
+
|
734 |
# Validate random-tags usage
|
735 |
if args.random_tags is not None and args.feed_from_tags is None:
|
736 |
parser.error("--random-tags can only be used when --feed-from-tags is enabled")
|
|
|
746 |
else:
|
747 |
logging.info("Running in dry-run mode. Models will not be loaded.")
|
748 |
|
749 |
+
for image_path, caption_file in tasks:
|
750 |
+
if not args.dry_run:
|
751 |
+
input_image = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
752 |
|
753 |
+
# Use custom prompt if specified
|
754 |
+
prompt = args.custom_prompt or JoyCaptionModel.get_prompt_string(
|
755 |
+
args.caption_type, args.caption_length
|
756 |
+
)
|
757 |
|
758 |
+
if args.feed_from_tags is not None and args.artist_from_folder:
|
759 |
+
raise ValueError(
|
760 |
+
"feed-from-tags and artist-from-folder can't be used together"
|
761 |
)
|
762 |
+
if args.feed_from_tags is not None:
|
763 |
+
prompt = prompt_from_tags(args, image_path, tagset_normalizer, prompt)
|
764 |
+
elif args.artist_from_folder:
|
765 |
+
prompt = prompt_from_folder(prompt, image_path.resolve())
|
766 |
+
|
767 |
+
if args.dry_run:
|
768 |
+
logging.info(
|
769 |
+
f"Dry run: Skipping caption generation for {image_path} with prompt:\n\t{prompt}"
|
770 |
+
)
|
771 |
+
continue
|
772 |
+
else:
|
773 |
+
logging.info(f"Prompt for {image_path}:\n\t{prompt}")
|
774 |
|
775 |
+
caption = joy_caption_model.generate_valid_caption(input_image, prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
776 |
|
777 |
+
# Strip commas if the --dont-strip-commas flag is not set
|
778 |
+
if not args.dont_strip_commas:
|
779 |
+
# Existing comma stripping logic
|
780 |
+
caption = re.sub(r",\s*([^\d])", r" \1", caption)
|
781 |
|
782 |
+
# New feature: Add commas after periods if specified
|
783 |
+
if args.add_commas_to_sentence_ends:
|
784 |
+
caption = re.sub(r"(\.)(\s+)([A-Z])", r"\1,\2\3", caption)
|
785 |
|
786 |
+
# Remove all newline characters
|
787 |
+
caption = caption.replace("\n", " ")
|
788 |
|
789 |
+
logging.info(f"Caption for {image_path}:\n\t{caption}\n\n")
|
790 |
|
791 |
+
# Save the caption to a .caption file
|
792 |
+
with open(caption_file, "w", encoding="utf-8") as f:
|
793 |
+
f.write(caption)
|
794 |
+
logging.info(f"Caption saved to {caption_file}")
|
795 |
|
796 |
|
797 |
RE_PARENS_SUFFIX = re.compile(r"_\([^)]+\)$")
|