File size: 38,048 Bytes
f1a2ec8
 
 
 
913d039
f1a2ec8
 
 
 
 
 
 
 
 
 
 
 
 
 
0f0ad40
ee41534
f1a2ec8
56166a4
f1a2ec8
 
 
 
 
 
 
 
 
 
 
 
2e83eb5
913d039
f1a2ec8
 
 
dc933c5
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
913d039
3461363
 
 
f1a2ec8
 
 
 
 
2e83eb5
f1a2ec8
 
 
 
 
 
 
 
 
56166a4
 
 
 
 
 
 
 
 
 
 
 
f1a2ec8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e83eb5
 
 
 
f1a2ec8
 
 
 
 
 
 
 
 
 
56166a4
 
f1a2ec8
 
 
 
 
2e83eb5
 
 
 
 
 
 
 
 
f1a2ec8
2e83eb5
56166a4
2e83eb5
56166a4
 
f1a2ec8
 
 
 
 
 
2e83eb5
 
 
f1a2ec8
 
 
 
 
 
 
ab50172
 
 
f1a2ec8
2e83eb5
 
 
 
 
f1a2ec8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e83eb5
913d039
 
 
 
 
ee41534
2e83eb5
f1a2ec8
 
56166a4
 
 
 
 
 
f1a2ec8
 
 
 
 
 
 
 
 
 
 
3461363
56166a4
 
f1a2ec8
 
 
 
 
 
 
 
 
 
 
 
913d039
f1a2ec8
 
 
 
913d039
2e83eb5
 
 
 
ab50172
2e83eb5
f1a2ec8
 
 
 
 
 
 
913d039
56166a4
3f4436c
56166a4
 
 
 
f1a2ec8
913d039
f1a2ec8
913d039
f1a2ec8
ab50172
f1a2ec8
 
 
2e83eb5
f1a2ec8
 
 
 
913d039
f1a2ec8
 
 
 
 
 
2e83eb5
f1a2ec8
 
ab50172
f1a2ec8
 
 
 
 
2e83eb5
 
 
913d039
b54f417
f1a2ec8
b54f417
 
ee41534
 
b54f417
f1a2ec8
 
 
 
 
 
2e83eb5
3461363
2e83eb5
f1a2ec8
ee41534
f1a2ec8
 
ee41534
 
 
 
b54f417
f1a2ec8
56166a4
 
 
913d039
ee41534
 
 
 
 
ab50172
56166a4
 
ee41534
 
 
 
913d039
b54f417
ee41534
b54f417
 
ee41534
 
 
b54f417
 
 
 
 
 
 
 
56166a4
 
913d039
ab50172
 
 
 
 
 
 
913d039
ab50172
 
 
913d039
ab50172
 
 
 
 
 
 
 
 
 
 
913d039
ab50172
 
 
 
 
 
 
 
 
 
 
 
913d039
ab50172
 
ee41534
913d039
ab50172
 
ee41534
913d039
ab50172
 
ee41534
56166a4
 
913d039
 
f1a2ec8
 
 
 
 
 
 
 
3461363
 
 
 
 
 
 
 
 
f1a2ec8
913d039
3461363
 
f1a2ec8
3461363
913d039
f1a2ec8
 
913d039
 
3461363
 
 
 
 
 
 
 
 
f1a2ec8
 
 
3461363
f1a2ec8
3461363
 
 
 
 
 
f1a2ec8
3461363
 
 
2e83eb5
 
 
 
3461363
 
f1a2ec8
3461363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e83eb5
913d039
3461363
 
 
 
 
2e83eb5
3461363
 
 
 
 
 
 
 
 
 
 
 
 
2e83eb5
3461363
 
 
f1a2ec8
 
3461363
 
2e83eb5
3461363
 
 
2e83eb5
 
3461363
f1a2ec8
2e83eb5
 
3461363
ab50172
3461363
2e83eb5
 
 
3461363
f1a2ec8
 
3461363
f1a2ec8
 
 
 
 
 
 
3f4436c
f1a2ec8
2e83eb5
f300b48
f1a2ec8
 
 
 
ab50172
f1a2ec8
ab50172
 
 
f1a2ec8
 
 
ab50172
f1a2ec8
 
 
ee41534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1a2ec8
913d039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1a2ec8
 
56166a4
 
 
 
f1a2ec8
56166a4
 
 
 
f1a2ec8
2e83eb5
 
 
f1a2ec8
 
 
 
913d039
2e83eb5
f1a2ec8
 
ab50172
f1a2ec8
 
 
 
ab50172
f1a2ec8
 
 
 
ab50172
f1a2ec8
 
2e83eb5
 
 
f1a2ec8
 
2e83eb5
e77eb40
2e83eb5
e77eb40
56166a4
 
 
 
 
f1a2ec8
6f8fd26
 
 
 
 
0f0ad40
2e83eb5
0f0ad40
56166a4
 
 
 
0f0ad40
913d039
 
 
 
 
 
 
 
 
 
 
 
f1a2ec8
 
 
913d039
f1a2ec8
a27421b
f1a2ec8
 
 
2e83eb5
f1a2ec8
 
a27421b
f1a2ec8
a27421b
 
 
fc8f518
a27421b
 
0f0ad40
 
ab50172
0f0ad40
4505cc5
fc8f518
4505cc5
913d039
 
 
f1a2ec8
913d039
 
 
 
 
 
f1a2ec8
a27421b
 
f1a2ec8
 
a27421b
 
 
 
3821594
a27421b
 
 
 
3821594
a27421b
 
 
3821594
a27421b
 
 
3821594
a27421b
f1a2ec8
fc8f518
 
 
 
 
 
f1a2ec8
a27421b
 
 
 
f1a2ec8
a27421b
 
 
f1a2ec8
a27421b
 
b7d52fe
a27421b
f1a2ec8
a27421b
 
 
 
f1a2ec8
3821594
 
 
 
2e83eb5
3821594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56166a4
 
 
 
 
3821594
 
56166a4
 
 
 
 
 
3821594
2e83eb5
 
 
 
 
 
3821594
2e83eb5
3461363
 
 
 
 
913d039
 
3461363
2e83eb5
 
 
 
 
56166a4
 
 
 
2e83eb5
913d039
3821594
 
913d039
 
3821594
2e83eb5
 
3821594
913d039
3821594
 
 
913d039
 
2e83eb5
ab50172
2e83eb5
 
 
913d039
 
3821594
 
 
2e83eb5
3821594
913d039
56166a4
 
3821594
 
 
 
 
 
913d039
3821594
 
 
913d039
3821594
913d039
ab50172
3821594
913d039
2e83eb5
913d039
2e83eb5
 
 
56166a4
913d039
3821594
2e83eb5
913d039
2e83eb5
3821594
913d039
3821594
 
 
2e83eb5
 
56166a4
 
2e83eb5
 
 
3821594
 
2e83eb5
3821594
913d039
3821594
 
ab50172
2e83eb5
3821594
2e83eb5
56166a4
3821594
 
d3c5f55
 
3821594
2e83eb5
56166a4
3821594
 
56166a4
ab50172
56166a4
d3c5f55
3821594
 
2e83eb5
913d039
2e83eb5
3821594
2e83eb5
56166a4
3821594
 
2e83eb5
d3c5f55
3821594
2e83eb5
913d039
 
d3c5f55
3461363
913d039
3461363
 
 
 
 
 
 
913d039
 
 
 
 
 
 
 
d3c5f55
3f4436c
913d039
 
 
 
3821594
 
2e83eb5
f1a2ec8
 
 
 
 
 
2e83eb5
f1a2ec8
 
 
 
 
2e83eb5
f1a2ec8
 
13ab065
f1a2ec8
 
 
 
 
2e83eb5
6f8fd26
 
 
 
 
 
 
f1a2ec8
 
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
JoyCaption Alpha Two

This module provides functionality for generating captions for images using a
combination of CLIP, LLM, and custom image adapters. It supports various
caption types, tones, and lengths.

The main components include:
- Loading and initializing models (CLIP, LLM, image adapter)
- Processing images and generating captions
- Command-line interface for batch processing images in a directory
"""

import os
import argparse
import re
import random
import math
from pathlib import Path
from typing import List, Tuple, Dict
from PIL import Image
import pillow_jxl
import torch
import torchvision.transforms.functional as TVF
from transformers import (
    AutoModel,
    AutoTokenizer,
    AutoModelForCausalLM,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
)
from torch import nn
from e6db_reader import TagSetNormalizer, tag_category2id, tag_rank_to_freq
import logging

CLIP_PATH = "google/siglip-so400m-patch14-384"
MODEL_PATH = "meta-llama/Meta-Llama-3.1-8B"
CHECKPOINT_PATH = Path(__file__).resolve().parent / "cgrkzexw-599808"
CAPTION_TYPE_MAP = {
    "descriptive": [
        "Write a descriptive caption for this image in a formal tone.",
        "Write a descriptive caption for this image in a formal tone within {word_count} words.",
        "Write a {length} descriptive caption for this image in a formal tone.",
    ],
    "descriptive (informal)": [
        "Write a descriptive caption for this image in a casual tone.",
        "Write a descriptive caption for this image in a casual tone within {word_count} words.",
        "Write a {length} descriptive caption for this image in a casual tone.",
    ],
    "training prompt": [
        "Write a stable diffusion prompt for this image.",
        "Write a stable diffusion prompt for this image within {word_count} words.",
        "Write a {length} stable diffusion prompt for this image.",
    ],
    "midjourney": [
        "Write a MidJourney prompt for this image.",
        "Write a MidJourney prompt for this image within {word_count} words.",
        "Write a {length} MidJourney prompt for this image.",
    ],
    "booru tag list": [
        "Write a list of Booru tags for this image.",
        "Write a list of Booru tags for this image within {word_count} words.",
        "Write a {length} list of Booru tags for this image.",
    ],
    "booru-like tag list": [
        "Write a list of Booru-like tags for this image.",
        "Write a list of Booru-like tags for this image within {word_count} words.",
        "Write a {length} list of Booru-like tags for this image.",
    ],
    "art critic": [
        "Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc.",
        "Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc. Keep it within {word_count} words.",
        "Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc. Keep it {length}.",
    ],
    "product listing": [
        "Write a caption for this image as though it were a product listing.",
        "Write a caption for this image as though it were a product listing. Keep it under {word_count} words.",
        "Write a {length} caption for this image as though it were a product listing.",
    ],
    "social media post": [
        "Write a caption for this image as if it were being used for a social media post.",
        "Write a caption for this image as if it were being used for a social media post. Limit the caption to {word_count} words.",
        "Write a {length} caption for this image as if it were being used for a social media post.",
    ],
}

HF_TOKEN = os.environ.get("HF_TOKEN", None)


class ImageAdapter(nn.Module):
    """
    Custom image adapter module for processing CLIP vision outputs.

    This module adapts the output of a CLIP vision model to be compatible with
    a text model. It supports optional layer normalization, positional
    embeddings, and deep feature extraction.

    Args:
        input_features (int):
            Number of input features from the vision model.
        output_features (int):
            Number of output features to match the text model.
        ln1 (bool):
            Whether to use layer normalization.
        pos_emb (bool):
            Whether to use positional embeddings.
        num_image_tokens (int):
            Number of image tokens.
        deep_extract (bool):
            Whether to use deep feature extraction.
    """

    def __init__(
        self,
        input_features: int,
        output_features: int,
        ln1: bool,
        pos_emb: bool,
        num_image_tokens: int,
        deep_extract: bool,
    ):
        super().__init__()
        self.deep_extract = deep_extract

        if self.deep_extract:
            input_features = input_features * 5

        self.linear1 = nn.Linear(input_features, output_features)
        self.activation = nn.GELU()
        self.linear2 = nn.Linear(output_features, output_features)
        self.ln1 = nn.Identity() if not ln1 else nn.LayerNorm(input_features)
        self.pos_emb = (
            None
            if not pos_emb
            else nn.Parameter(torch.zeros(num_image_tokens, input_features))
        )

        self.other_tokens = nn.Embedding(3, output_features)
        self.other_tokens.weight.data.normal_(mean=0.0, std=0.02)

    def forward(self, vision_outputs: torch.Tensor):
        """
        Forward pass of the image adapter.

        Args:
            vision_outputs (torch.Tensor):
                Output tensor from the CLIP vision model.

        Returns:
            torch.Tensor: Adapted image features.
        """
        if self.deep_extract:
            x = torch.concat(
                (
                    vision_outputs[-2],
                    vision_outputs[3],
                    vision_outputs[7],
                    vision_outputs[13],
                    vision_outputs[20],
                ),
                dim=-1,
            )
            assert len(x.shape) == 3, f"Expected 3, got {len(x.shape)}"
            expected_shape = vision_outputs[-2].shape[-1] * 5
            assert (
                x.shape[-1] == expected_shape
            ), f"Expected {expected_shape}, got {x.shape[-1]}"
        else:
            x = vision_outputs[-2]

        x = self.ln1(x)

        if self.pos_emb is not None:
            assert (
                x.shape[-2:] == self.pos_emb.shape
            ), f"Expected {self.pos_emb.shape}, got {x.shape[-2:]}"
            x = x + self.pos_emb

        x = self.linear1(x)
        x = self.activation(x)
        x = self.linear2(x)

        other_tokens = self.other_tokens(
            torch.tensor([0, 1], device=self.other_tokens.weight.device).expand(
                x.shape[0], -1
            )
        )
        assert other_tokens.shape == (
            x.shape[0],
            2,
            x.shape[2],
        ), f"Expected {(x.shape[0], 2, x.shape[2])}, got {other_tokens.shape}"
        x = torch.cat((other_tokens[:, 0:1], x, other_tokens[:, 1:2]), dim=1)

        return x

    def get_eot_embedding(self):
        """
        Get the end-of-text embedding.

        Returns:
            torch.Tensor: The end-of-text embedding.
        """
        return self.other_tokens(
            torch.tensor([2], device=self.other_tokens.weight.device)
        ).squeeze(0)


STOP_WORDS: set[str] = set(
    "i'll if we'd can't you'd shouldn't i'd only doesn't further isn't didn't has more aren't during do than were he's too here you against could few for ought won't we until weren't i've they're same up she but are how here's their over can under mustn't while on by had and an each he'd he about she'd am was she'll where's did out or that's it they'd a let's shall what's the to don't when below no any some from is hadn't all they i'm must in before who's own where you've that very them this not because it's shan't wasn't you'll when's most off i at other hasn't nor been such again we'll down above will so should into she's once have these why's be we've as being why those then with after may you're would haven't both wouldn't there cannot they've couldn't how's between does we're through he'll of there's they'll might".split(
        " "
    )
)


class JoyCaptionModel:
    """
    A class for generating captions for images using CLIP, LLM,
    and custom image adapters.

    This class encapsulates the functionality to load and initialize
    various models (CLIP, LLM, image adapter) and use them to process
    images and generate captions.

    It supports different caption types, tones, and lengths.

    Attributes:
        clip_model: The CLIP vision model for processing images.
        text_model: The language model for generating captions.
        image_adapter: Custom adapter for processing CLIP vision outputs.
        tokenizer: Tokenizer for the language model.

    Methods:
        load_models(): Load and initialize all required models.
        process_image(input_image, caption_type, caption_length):
            Process an input image and generate a caption
            based on specified parameters.
    """

    def __init__(self):
        self.clip_model = None
        self.text_model = None
        self.image_adapter = None
        self.tokenizer = None

    def load_models(self):
        """
        Load and initialize all required models (CLIP, LLM, image adapter).
        """
        logging.info("Loading CLIP")
        self.clip_model = AutoModel.from_pretrained(CLIP_PATH)
        self.clip_model = self.clip_model.vision_model

        if (CHECKPOINT_PATH / "clip_model.pt").exists():
            logging.info("Loading VLM's custom vision model")
            checkpoint = torch.load(
                CHECKPOINT_PATH / "clip_model.pt", map_location="cpu"
            )
            checkpoint = {
                k.replace("_orig_mod.module.", ""): v for k, v in checkpoint.items()
            }
            self.clip_model.load_state_dict(checkpoint)
            del checkpoint

        self.clip_model.eval()
        self.clip_model.requires_grad_(False)
        self.clip_model.to("cuda")

        logging.info("Loading tokenizer")
        self.tokenizer = AutoTokenizer.from_pretrained(
            CHECKPOINT_PATH / "text_model", use_fast=True
        )
        assert isinstance(
            self.tokenizer, (PreTrainedTokenizer, PreTrainedTokenizerFast)
        )

        logging.info("Loading LLM")
        if (CHECKPOINT_PATH / "text_model").exists():
            logging.info("Loading VLM's custom text model")
            self.text_model = AutoModelForCausalLM.from_pretrained(
                CHECKPOINT_PATH / "text_model", device_map=0, torch_dtype=torch.bfloat16
            )
        else:
            self.text_model = AutoModelForCausalLM.from_pretrained(
                MODEL_PATH, device_map="auto", torch_dtype=torch.bfloat16
            )

        self.text_model.eval()

        logging.info("Loading image adapter")
        self.image_adapter = ImageAdapter(
            self.clip_model.config.hidden_size,
            self.text_model.config.hidden_size,
            False,
            False,
            38,
            False,
        )
        self.image_adapter.load_state_dict(
            torch.load(CHECKPOINT_PATH / "image_adapter.pt", map_location="cpu")
        )
        self.image_adapter.eval()
        self.image_adapter.to("cuda")

    @torch.no_grad()
    def process_image(
        self,
        input_image: Image.Image,
        prompt_str: str,
    ) -> Tuple[str, float]:
        """
        Process an input image and generate a caption based on specified parameters.
        Also calculates the entropy of the generated caption.

        Returns:
            Tuple[str, float]: The generated caption and its entropy.
        """
        torch.cuda.empty_cache()

        pixel_values = self._preprocess_image(input_image)

        embedded_images = self._embed_image(pixel_values)
        inputs_embeds, input_ids, attention_mask = self._construct_inputs(
            embedded_images, prompt_str
        )

        generate_ids = self._generate_caption(inputs_embeds, input_ids, attention_mask)
        caption = self._decode_caption(generate_ids, input_ids)

        # Calculate entropy
        token_ids = generate_ids[0].tolist()
        entropy = self._calculate_entropy(token_ids)

        return caption.strip(), entropy

    def generate_valid_caption(
        self,
        input_image: Image.Image,
        prompt: str,
        *,
        limited_words: Dict[str, int] = {"fluffy": 2},
        min_sentence_count: int = 3,
        max_word_repetitions: int = 5,
        min_entropy: float = 1.75,
        stop_words: set[str] = STOP_WORDS,
    ) -> str:
        """
        Generate a valid caption, retrying if certain conditions are not met.

        Args:
            input_image (Image.Image): The input image to caption.
            prompt (str | None): Prompt for caption generation.
            limited_words (Dict[str, int]): Dictionary of words with their maximum allowed occurrences. Default is {"fluffy": 1}.
            min_sentence_count (int): Minimum required number of sentences. Default is 3.
            max_word_repetitions (int): Maximum allowed repetitions for words longer than 4 characters. Default is 15.
            min_entropy (float): Minimum required entropy of the caption. Default is 2.3.

        Returns:
            str: A valid caption meeting all specified criteria.

        The method retries caption generation if:
        - The caption contains only special characters
        - The caption does not end with a period, exclamation mark, or question mark
        - Any word in limited_words appears more than its specified maximum times
        - Any word longer than 4 characters is repeated more than max_word_repetitions times
        - The caption contains fewer than min_sentence_count sentences
        - The entropy of the caption is below min_entropy
        """
        while True:
            caption, entropy = self.process_image(input_image, prompt)
            words = re.findall(r"\b\w+\b", caption.lower())
            word_counts = {
                word: words.count(word) for word in set(words) if word not in stop_words
            }
            sentence_count = len(re.findall(r"[.!?]", caption))

            if not re.search(r"\w", caption):
                logging.info(
                    f"Retrying: Caption contains only special characters.\nCaption: {caption!r}"
                )
            elif caption[-1] not in {".", "!", "?"}:
                logging.info(
                    f"Retrying: Caption does not end with proper punctuation.\nCaption: {caption!r}"
                )
            elif any(
                caption.lower().count(word) > max_count
                for word, max_count in limited_words.items()
            ):
                exceeded_words = [
                    f"{word} ({caption.lower().count(word)}/{max_count})"
                    for word, max_count in limited_words.items()
                    if caption.lower().count(word) > max_count
                ]
                logging.info(
                    f"Retrying: Limited words exceeded: {', '.join(exceeded_words)}.\nCaption: {caption!r}"
                )
            elif any(
                count > max_word_repetitions
                for word, count in word_counts.items()
                if len(word) > 4
            ):
                repeated_words = [
                    word
                    for word, count in word_counts.items()
                    if count > max_word_repetitions and len(word) > 4
                ]
                logging.info(
                    f"Retrying: Words repeated more than {max_word_repetitions} times: {', '.join(repeated_words)}.\nCaption: {caption!r}"
                )
            elif sentence_count < min_sentence_count:
                logging.info(
                    f"Retrying: Only {sentence_count} sentences (min: {min_sentence_count}).\nCaption: {caption!r}"
                )
            elif entropy < min_entropy:
                logging.info(
                    f"Retrying: Low entropy ({entropy:.2f} < {min_entropy}).\nCaption: {caption!r}"
                )
            else:
                return caption

    @staticmethod
    def get_prompt_string(caption_type, caption_length):
        length = None if caption_length == "any" else caption_length

        if isinstance(length, str):
            try:
                length = int(length)
            except ValueError:
                pass

        # Build prompt
        if length is None:
            map_idx = 0
        elif isinstance(length, int):
            map_idx = 1
        elif isinstance(length, str):
            map_idx = 2
        else:
            raise ValueError(f"Invalid caption length: {length}")

        caption_type = caption_type.lower()
        if caption_type not in CAPTION_TYPE_MAP:
            raise ValueError(f"Invalid caption type: {caption_type}")

        prompt_str = CAPTION_TYPE_MAP[caption_type][map_idx]
        prompt_str = prompt_str.format(length=caption_length, word_count=caption_length)
        return prompt_str

    @staticmethod
    def _preprocess_image(input_image: Image.Image) -> torch.Tensor:
        """
        Preprocess the input image for the CLIP model.

        Args:
            input_image (Image.Image): The input PIL image.

        Returns:
            torch.Tensor: Preprocessed image tensor.
        """
        image = input_image.resize((384, 384), Image.LANCZOS)
        pixel_values = TVF.pil_to_tensor(image).unsqueeze(0) / 255.0
        pixel_values = TVF.normalize(pixel_values, [0.5], [0.5])
        return pixel_values.to("cuda")

    def _embed_image(self, pixel_values: torch.Tensor) -> torch.Tensor:
        """
        Embed the preprocessed image using CLIP and the image adapter.

        Args:
            pixel_values (torch.Tensor): Preprocessed image tensor.

        Returns:
            torch.Tensor: Embedded image tensor.
        """
        with torch.amp.autocast_mode.autocast("cuda", enabled=True):
            vision_outputs = self.clip_model(
                pixel_values=pixel_values, output_hidden_states=True
            )
            embedded_images = self.image_adapter(vision_outputs.hidden_states)
        return embedded_images.to("cuda")

    def _construct_inputs(
        self, embedded_images: torch.Tensor, prompt_str: str
    ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Construct the inputs for the text model's generate method.

        Args:
            embedded_images (torch.Tensor): Embedded image tensor.
            prompt_str (str): The prompt string for captioning.

        Returns:
            tuple: (input_embeds, input_ids, attention_mask)
        """
        # Build the conversation
        convo = [
            {"role": "system", "content": "You are a helpful image captioner."},
            {"role": "user", "content": prompt_str},
        ]

        # Format and tokenize the conversation
        convo_string = self.tokenizer.apply_chat_template(
            convo, tokenize=False, add_generation_prompt=True
        )
        logging.debug(f"Convo:\n{convo_string}")
        convo_tokens = self.tokenizer.encode(
            convo_string,
            return_tensors="pt",
            add_special_tokens=False,
            truncation=False,
        )
        prompt_tokens = self.tokenizer.encode(
            prompt_str, return_tensors="pt", add_special_tokens=False, truncation=False
        )
        convo_tokens = convo_tokens.squeeze(0)
        prompt_tokens = prompt_tokens.squeeze(0)

        # Calculate where to inject the image
        eot_id_indices = (
            (convo_tokens == self.tokenizer.convert_tokens_to_ids("<|eot_id|>"))
            .nonzero(as_tuple=True)[0]
            .tolist()
        )
        preamble_len = eot_id_indices[1] - prompt_tokens.shape[0]

        # Embed the tokens
        convo_embeds = self.text_model.model.embed_tokens(
            convo_tokens.unsqueeze(0).to("cuda")
        )

        # Construct the input
        input_embeds = torch.cat(
            [
                convo_embeds[:, :preamble_len],
                embedded_images.to(dtype=convo_embeds.dtype),
                convo_embeds[:, preamble_len:],
            ],
            dim=1,
        ).to("cuda")

        input_ids = torch.cat(
            [
                convo_tokens[:preamble_len].unsqueeze(0),
                torch.zeros((1, embedded_images.shape[1]), dtype=torch.long),
                convo_tokens[preamble_len:].unsqueeze(0),
            ],
            dim=1,
        ).to("cuda")

        attention_mask = torch.ones_like(input_ids)

        return input_embeds, input_ids, attention_mask

    def _generate_caption(self, inputs_embeds, input_ids, attention_mask):
        generate_ids = self.text_model.generate(
            input_ids,
            inputs_embeds=inputs_embeds,
            attention_mask=attention_mask,
            max_new_tokens=300,
            # max_length=4096,
            do_sample=True,
            suppress_tokens=None,
            repetition_penalty=1.2,
        )
        return generate_ids

    def _decode_caption(self, generate_ids, input_ids):
        generate_ids = generate_ids[:, input_ids.shape[1] :]

        if generate_ids[0][-1] == self.tokenizer.eos_token_id or generate_ids[0][
            -1
        ] == self.tokenizer.convert_tokens_to_ids("<|eot_id|>"):
            generate_ids = generate_ids[:, :-1]

        caption = self.tokenizer.batch_decode(
            generate_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False
        )[0]
        return caption

    def _calculate_entropy(self, token_ids: List[int]) -> float:
        """
        Calculate the entropy of a sequence of token IDs.

        Args:
            token_ids (List[int]): List of token IDs.

        Returns:
            float: Entropy of the token sequence.
        """
        token_counts = {}
        total_tokens = len(token_ids)

        for token_id in token_ids:
            token_counts[token_id] = token_counts.get(token_id, 0) + 1

        entropy = 0
        for count in token_counts.values():
            probability = count / total_tokens
            entropy -= probability * math.log2(probability)

        return entropy


class ColoredFormatter(logging.Formatter):
    COLORS = {
        "DEBUG": "\033[36m",  # Cyan
        "INFO": "\033[32m",  # Green
        "WARNING": "\033[33m",  # Yellow
        "ERROR": "\033[31m",  # Red
        "CRITICAL": "\033[31;1m",  # Bright Red
    }
    RESET = "\033[0m"

    def format(self, record):
        log_message = super().format(record)
        return f"{self.COLORS.get(record.levelname, '')}{log_message}{self.RESET}"


def setup_logging(verbosity):
    if verbosity == 0:
        log_level = logging.INFO
    elif verbosity == 1:
        log_level = logging.DEBUG

    handler = logging.StreamHandler()
    formatter = ColoredFormatter(
        fmt="%(asctime)s | %(levelname)-8s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
    )
    handler.setFormatter(formatter)

    logger = logging.getLogger()
    logger.setLevel(log_level)
    logger.addHandler(handler)


def main():
    """
    Generate captions for images in a directory
    and save them as .caption files.
    """
    parser = argparse.ArgumentParser(
        description=(
            "Generate captions for images in a directory and save them as "
            ".caption files."
        )
    )
    parser.add_argument(
        "directory", type=str, help="Target directory containing images."
    )
    parser.add_argument(
        "--caption_type",
        type=str,
        default="descriptive",
        choices=CAPTION_TYPE_MAP.keys(),
        help="Type of caption to generate.",
    )
    parser.add_argument(
        "--caption_length", type=str, default="any", help="Length of the caption."
    )
    parser.add_argument(
        "--dont-strip-commas",
        action="store_true",
        help=("If set, commas will not be stripped from the generated captions."),
    )
    parser.add_argument(
        "--custom_prompt",
        type=str,
        help=("Custom prompt for the captioner. " "Use with --caption_type custom."),
    )
    parser.add_argument(
        "--add-commas-to-sentence-ends",
        action="store_true",
        help="Add commas after periods in sentences",
    )
    parser.add_argument(
        "--feed-from-tags",
        type=int,
        nargs="?",
        const=-1,
        help=(
            "Use .txt files with the same base filename "
            "as the images as input to the captioner. "
            "Optionally specify the number of tags to use."
        ),
    )
    parser.add_argument(
        "--artist-from-folder",
        action="store_true",
        help="Get the artist name from the parent folder",
    )
    parser.add_argument(
        "--random-tags",
        type=int,
        help=(
            "Randomly select n number of tags. "
            "Only works if --feed-from-tags is enabled."
        ),
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Run in dry-run mode without loading models or generating captions.",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="count",
        default=0,
        help="Increase output verbosity (can be repeated)",
    )

    args = parser.parse_args()

    setup_logging(args.verbose)

    tasks = []
    image_extensions = {".webp", ".png", ".jpeg", ".jpg", ".jxl"}
    for image_path in Path(args.directory).rglob("*"):
        if image_path.suffix.lower() in image_extensions:
            caption_file = image_path.with_suffix(".caption")
            # Skip if the caption file already exists
            if caption_file.exists():
                logging.info(f"Skipping {image_path}: Caption file already exists.")
                continue
            tasks.append((image_path, caption_file))

    if not tasks:
        logging.error("No input file found.")
        return

    # Validate random-tags usage
    if args.random_tags is not None and args.feed_from_tags is None:
        parser.error("--random-tags can only be used when --feed-from-tags is enabled")

    if args.feed_from_tags is not None and args.artist_from_folder:
        raise ValueError("feed-from-tags and artist-from-folder can't be used together")

    if args.feed_from_tags is not None:
        logging.info("Loading e621 tag data")
        tagset_normalizer = make_tagset_normalizer()

    # Initialize and load models only if not in dry-run mode
    if not args.dry_run:
        joy_caption_model = JoyCaptionModel()
        joy_caption_model.load_models()
    else:
        logging.info("Running in dry-run mode. Models will not be loaded.")

    for image_path, caption_file in tasks:
        if not args.dry_run:
            input_image = Image.open(image_path).convert("RGB")

        # Use custom prompt if specified
        prompt = args.custom_prompt or JoyCaptionModel.get_prompt_string(
            args.caption_type, args.caption_length
        )

        if args.feed_from_tags is not None:
            prompt = prompt_from_tags(args, image_path, tagset_normalizer, prompt)
        elif args.artist_from_folder:
            prompt = prompt_from_folder(prompt, image_path.resolve())

        if args.dry_run:
            logging.info(
                f"Dry run: Skipping caption generation for {image_path} with prompt:\n\t{prompt}"
            )
            continue
        else:
            logging.info(f"Prompt for {image_path}:\n\t{prompt}")

        caption = joy_caption_model.generate_valid_caption(input_image, prompt)

        # Replace multiple spaces with a single space
        caption = " ".join(caption.split())
        # Replace multiple newlines with a single newline
        caption = "\n".join(
            line for line in (line.strip() for line in caption.split("\n")) if line
        )

        # Strip commas if the --dont-strip-commas flag is not set
        if not args.dont_strip_commas:
            # Existing comma stripping logic
            caption = re.sub(r",\s*([^\d])", r" \1", caption)

            # New feature: Add commas after periods if specified
            if args.add_commas_to_sentence_ends:
                caption = re.sub(r"(\.)(\s+)([A-Z])", r"\1,\2\3", caption)

        # Remove all newline characters
        caption = caption.replace("\n", " ")

        logging.info(f"Caption for {image_path}:\n\t{caption}\n\n")

        # Save the caption to a .caption file
        with open(caption_file, "w", encoding="utf-8") as f:
            f.write(caption)
        logging.info(f"Caption saved to {caption_file}")


RE_PARENS_SUFFIX = re.compile(r"_\([^)]+\)$")
E6DB_DATA = Path(__file__).resolve().parent / "data"


def make_tagset_normalizer():
    """
    Create a TagSetNormalizer for encoding/decoding tags to and from integers.
    Configures it based on the provided config.
    """
    # This loads all the aliases and implications
    tagset_normalizer = TagSetNormalizer(E6DB_DATA)

    tagid2cat = tagset_normalizer.tag_normalizer.tag_categories
    cat_artist = tag_category2id["artist"]
    cat2suffix = {
        tag_category2id["character"]: "_(character)",
        tag_category2id["lore"]: "_(lore)",
        tag_category2id["species"]: "_(species)",
        tag_category2id["copyright"]: "_(copyright)",
    }

    # Create additional aliases for tags using simple rules
    def input_map(tag, tid):
        # Make an alias without parentheses, it might conflict but we'll handle
        # it depending on `on_alias_conflict` config value.
        without_suffix = RE_PARENS_SUFFIX.sub("", tag)
        had_suffix = tag != without_suffix
        if had_suffix:
            yield without_suffix

        # Add an alias with the suffix (special case for artist)
        cat = tagid2cat[tid] if tid is not None else -1
        if cat == cat_artist:
            artist = without_suffix.removeprefix("by_")
            if artist != without_suffix:
                yield artist
                if not had_suffix:
                    yield f"{artist}_(artist)"
            else:
                yield f"by_{artist}"
                if not had_suffix:
                    yield f"by_{artist}_(artist)"
        elif not had_suffix:
            suffix = cat2suffix.get(cat)
            if suffix is not None:
                yield f"{without_suffix}{suffix}"

        # Recognize tags where ':' were replaced by a space (aspect ratio)
        if ":" in tag:
            yield tag.replace(":", "_")

    return tagset_normalizer.map_inputs(input_map, on_conflict="ignore")


def format_nl_list(word_list):
    """
    Takes a list of words and generates a natural language output.
    """
    n = len(word_list)
    assert n > 0
    if n == 1:
        return word_list[0]
    if n == 2:
        return f"{word_list[0]} and {word_list[1]}"
    # n > 2
    *head, last = word_list
    return ", ".join(head) + ", and " + last


TAG_SPECIES = tag_category2id["species"]
TAG_CHARACTER = tag_category2id["character"]
TAG_ARTIST = tag_category2id["artist"]
TAG_COPYRIGHT = tag_category2id["copyright"]
TAG_META = tag_category2id["meta"]


def prompt_from_tags(
    args,
    image_path: Path,
    tagset_normalizer: TagSetNormalizer,
    base_prompt: str = "Write a descriptive caption for this image in a formal tone.",
    tag_freq_threshold: int = 0,
    tag_string_prefix: str = "Use these tags to construct your caption:",
):
    """
    Generates a prompt from tags associated with the given image.

    Args:
        args: Additional arguments for the function.
        image_path (Path):
            The path to the image file.
        tagset_normalizer (TagSetNormalizer):
            An instance to normalize the tag set.
    """
    # Find and read the corresponding tag file
    tag_file = find_tag_file(image_path)
    if tag_file is None:
        logging.warning(f"No tag file found for {image_path}")
        return base_prompt

    with open(tag_file, "r", encoding="utf-8") as f:
        tags = f.read().lower().split(",")

    # Get helper functions from the tagset_normalizer
    tag_id_to_cat_id = tagset_normalizer.tag_normalizer.tag_categories
    encode = tagset_normalizer.tag_normalizer.encode

    # Initialize dictionaries and lists to store categorized tags
    # These lists will contain tuples (freq, tag, tag_id)
    tag_by_category: Dict[int, List[Tuple[int, str, int]]] = {
        cat: [] for cat in [TAG_ARTIST, TAG_CHARACTER, TAG_COPYRIGHT, TAG_SPECIES]
    }
    other_tags: List[Tuple[int, str, int]] = []
    implied: set = set()

    # Process each tag
    for tag in tags:
        tag = tag.strip()
        # Encode the tag into a numerical id
        tag_id = encode(tag.replace(" ", "_"))
        if tag_id is None:
            # If tag is not recognized, add it to other_tags
            other_tags.append((0, tag, 0))
            implied.update(tagset_normalizer.implications_rej.get(0, ()))
            continue
        # Get the category of the tag
        cat_id = tag_id_to_cat_id[tag_id]
        # Skip meta tags
        if cat_id == TAG_META:
            continue
        # Update implied tags
        implied.update(tagset_normalizer.implications.get(tag_id, ()))
        # Get the frequency of the tag
        freq = tag_rank_to_freq(tag_id)
        if freq < tag_freq_threshold:
            continue
        # Add the tag to its category, or other_tags
        tag_by_category.get(cat_id, other_tags).append((int(freq), tag, tag_id))

    # Sort other_tags by frequency (descending) and filter out implied tags
    other_tags = sorted(
        (-freq, tag, tag_id)
        for freq, tag, tag_id in other_tags
        if tag_id not in implied
    )

    # Sort tags within each category, prefering non implied tags
    for cat_id, cat_list in tag_by_category.items():
        tag_by_category[cat_id] = sorted(
            ((tag_id in implied, -freq), tag, tag_id) for freq, tag, tag_id in cat_list
        )

    # Handle random tag selection or tag limit if specified
    if args.random_tags is not None:
        # Randomly select tags if --random-tags is specified
        num_tags = min(args.random_tags, len(other_tags))
        other_tags = random.sample(
            [
                (i, tag, 0)
                for i, tag in enumerate(tags[: round(args.random_tags * 1.5)])
            ],
            num_tags,
        )
    elif args.feed_from_tags > 0:
        # Use specified number of tags if --feed-from-tags has a positive value
        other_tags = other_tags[: args.feed_from_tags]

    # Prepare sentence pieces for each category
    artist_tag = tag_by_category[TAG_ARTIST]
    if artist_tag:
        artist_list = [str(tp[1]).removeprefix("by ") for tp in artist_tag[:4]]
        artist_txt = f"by {format_nl_list(artist_list)}"
    else:
        artist_txt = ""

    character_tag = tag_by_category[TAG_CHARACTER]
    if character_tag:
        tags = [tag for _, tag, _ in character_tag[:4]]
        character_txt = f"named {format_nl_list(tags)}"
    else:
        character_txt = ""

    species_tag = tag_by_category[TAG_SPECIES]
    if species_tag:
        species_txt = (
            "of a " if len(character_tag) <= 1 and len(species_tag) <= 1 else "of "
        )
        species_txt += format_nl_list([tp[1] for tp in species_tag[:4]])
    else:
        if character_tag:
            species_txt = (
                "of a character" if len(character_tag) <= 1 else "of characters"
            )
        else:
            species_txt = ""

    copyright_tag = tag_by_category[TAG_COPYRIGHT]
    if copyright_tag:
        tags = [tag for _, tag, *_ in copyright_tag[:4]]
        copyright_txt = f"from {format_nl_list(tags)}"
    else:
        copyright_txt = ""

    # Prepare the remaining tags as a string
    tag_string = ", ".join(tp[1] for tp in other_tags)

    # Extract the prefix and suffix around the word "image" from the base prompt
    image_pos = base_prompt.find("image")
    if image_pos < 0:
        raise ValueError("Base prompt must contain the word 'image'")
    image_pos += len("image")
    base_prompt_prefix = base_prompt[:image_pos].rstrip()
    base_prompt_suffix = base_prompt[image_pos:].lstrip()

    pieces = [
        base_prompt_prefix,
        artist_txt,
        species_txt,
        character_txt,
        copyright_txt,
        base_prompt_suffix,
        tag_string_prefix,
        tag_string,
        ".",
    ]
    logging.debug("Prompt pieces: %r", pieces)
    custom_prompt = " ".join(p for p in pieces if p)
    custom_prompt = custom_prompt.replace(" .", ".").replace(" ,", ",")
    return custom_prompt


def find_tag_file(image_path):
    """
    Find the corresponding .txt file for the given image path.
    Handles cases where the image has a -(number) suffix.
    """
    base_name = image_path.stem
    tag_file = image_path.with_suffix(".txt")

    if tag_file.exists():
        return tag_file

    # Handle -(number) suffix
    match = re.match(r"(.+)-\d+$", base_name)
    if match:
        base_name = match.group(1)
        tag_file = image_path.with_name(base_name).with_suffix(".tags")
        if tag_file.exists():
            return tag_file

    return None


def prompt_from_folder(prompt, path):
    artist = (
        path.parent.name.replace("_", " ").replace("-", " ").replace(".", " ").title()
    )
    return prompt.replace("image", f"image by {artist}")


if __name__ == "__main__":
    main()