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
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
import sys
import os
import torch
from torch import nn
from transformers import (
    AutoModel,
    AutoProcessor,
    AutoTokenizer,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    AutoModelForCausalLM,
    BitsAndBytesConfig,
)
from PIL import Image
import torchvision.transforms.functional as TVF
import contextlib
from typing import Union, List
from pathlib import Path

from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLabel,
    QPushButton,
    QFileDialog,
    QLineEdit,
    QTextEdit,
    QComboBox,
    QVBoxLayout,
    QHBoxLayout,
    QCheckBox,
    QListWidget,
    QListWidgetItem,
    QMessageBox,
    QSizePolicy,
)
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtCore import Qt

# Constants and Mappings
CLIP_PATH = "google/siglip-so400m-patch14-384"
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.",
    ],
}

EXTRA_OPTIONS_LIST = [
    "If there is a person/character in the image you must refer to them as {name}.",
    "Do NOT include information about people/characters that cannot be changed (like ethnicity, gender, etc), but do still include changeable attributes (like hair style).",
    "Include information about lighting.",
    "Include information about camera angle.",
    "Include information about whether there is a watermark or not.",
    "Include information about whether there are JPEG artifacts or not.",
    "If it is a photo you MUST include information about what camera was likely used and details such as aperture, shutter speed, ISO, etc.",
    "Do NOT include anything sexual; keep it PG.",
    "Do NOT mention the image's resolution.",
    "You MUST include information about the subjective aesthetic quality of the image from low to very high.",
    "Include information on the image's composition style, such as leading lines, rule of thirds, or symmetry.",
    "Do NOT mention any text that is in the image.",
    "Specify the depth of field and whether the background is in focus or blurred.",
    "If applicable, mention the likely use of artificial or natural lighting sources.",
    "Do NOT use any ambiguous language.",
    "Include whether the image is sfw, suggestive, or nsfw.",
    "ONLY describe the most important elements of the image.",
]

CAPTION_LENGTH_CHOICES = (
    ["any", "very short", "short", "medium-length", "long", "very long"]
    + [str(i) for i in range(20, 261, 10)]
)

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

# Determine the device to use (GPU if available, else CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if device.type == "cuda":
    torch_dtype = torch.bfloat16  # or torch.float16 based on compatibility
else:
    torch_dtype = torch.float32

# Update autocast usage
if device.type == "cuda":
    autocast = lambda: torch.amp.autocast(device_type='cuda', dtype=torch_dtype)
else:
    autocast = contextlib.nullcontext  # No autocasting on CPU

class ImageAdapter(nn.Module):
    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))
        )

        # Other tokens (<|image_start|>, <|image_end|>, <|eot_id|>)
        self.other_tokens = nn.Embedding(3, output_features)
        self.other_tokens.weight.data.normal_(
            mean=0.0, std=0.02
        )  # Matches HF's implementation of llama3

    def forward(self, vision_outputs: torch.Tensor):
        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)}"  # batch, tokens, features
            assert (
                x.shape[-1] == vision_outputs[-2].shape[-1] * 5
            ), f"Expected {vision_outputs[-2].shape[-1] * 5}, 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)

        # <|image_start|>, IMAGE, <|image_end|>
        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):
        return self.other_tokens(torch.tensor([2], device=self.other_tokens.weight.device)).squeeze(0)

def load_models(CHECKPOINT_PATH):
    # Load CLIP
    print("Loading CLIP")
    clip_processor = AutoProcessor.from_pretrained(CLIP_PATH)
    clip_model = AutoModel.from_pretrained(CLIP_PATH)
    clip_model = clip_model.vision_model

    assert (
        CHECKPOINT_PATH / "clip_model.pt"
    ).exists(), f"clip_model.pt not found in {CHECKPOINT_PATH}"
    print("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()}
    clip_model.load_state_dict(checkpoint)
    del checkpoint

    clip_model.eval()
    clip_model.requires_grad_(False)
    clip_model.to(device)

    # Tokenizer
    print("Loading tokenizer")
    tokenizer = AutoTokenizer.from_pretrained(
        CHECKPOINT_PATH / "text_model", use_fast=True
    )
    assert isinstance(tokenizer, (PreTrainedTokenizer, PreTrainedTokenizerFast)), f"Tokenizer is of type {type(tokenizer)}"

    # Add special tokens to the tokenizer
    special_tokens_dict = {'additional_special_tokens': ['<|system|>', '<|user|>', '<|end|>', '<|eot_id|>']}
    num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
    print(f"Added {num_added_toks} special tokens.")

    # LLM with 4-bit quantization
    print("Loading LLM with 4-bit quantization")
    text_model = AutoModelForCausalLM.from_pretrained(
        CHECKPOINT_PATH / "text_model",
        device_map="auto",
        quantization_config=BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_use_double_quant=True,
            bnb_4bit_quant_type='nf4',
            bnb_4bit_compute_dtype=torch.float16
        )
    )
    text_model.eval()
    # Removed text_model.to(device)

    # Resize token embeddings if new tokens were added
    if num_added_toks > 0:
        text_model.resize_token_embeddings(len(tokenizer))

    # Image Adapter
    print("Loading image adapter")
    image_adapter = ImageAdapter(
        clip_model.config.hidden_size, text_model.config.hidden_size, False, False, 38, False
    )
    image_adapter.load_state_dict(
        torch.load(CHECKPOINT_PATH / "image_adapter.pt", map_location="cpu")
    )
    image_adapter.eval()
    image_adapter.to(device)  # image_adapter is not quantized, so it's okay

    return clip_processor, clip_model, tokenizer, text_model, image_adapter

@torch.no_grad()
def generate_caption(
    input_image: Image.Image,
    caption_type: str,
    caption_length: Union[str, int],
    extra_options: List[str],
    name_input: str,
    custom_prompt: str,
    clip_model,
    tokenizer,
    text_model,
    image_adapter,
) -> tuple:
    if device.type == "cuda":
        torch.cuda.empty_cache()

    # If a custom prompt is provided, use it directly
    if custom_prompt.strip() != "":
        prompt_str = custom_prompt.strip()
    else:
        # 'any' means no length specified
        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}")

        prompt_str = CAPTION_TYPE_MAP[caption_type][map_idx]

        # Add extra options
        if len(extra_options) > 0:
            prompt_str += " " + " ".join(extra_options)

        # Add name, length, word_count
        prompt_str = prompt_str.format(name=name_input, length=caption_length, word_count=caption_length)

    # For debugging
    print(f"Prompt: {prompt_str}")

    # Preprocess image
    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])
    pixel_values = pixel_values.to(device)

    # Embed image
    # This results in Batch x Image Tokens x Features
    with autocast():
        vision_outputs = clip_model(pixel_values=pixel_values, output_hidden_states=True)
        embedded_images = image_adapter(vision_outputs.hidden_states)
        embedded_images = embedded_images.to(device)

    # Build the conversation
    convo = [
        {
            "role": "system",
            "content": "You are a helpful image captioner.",
        },
        {
            "role": "user",
            "content": prompt_str,
        },
    ]

    # Format the conversation
    # The apply_chat_template method might not be available; handle accordingly
    if hasattr(tokenizer, "apply_chat_template"):
        convo_string = tokenizer.apply_chat_template(
            convo, tokenize=False, add_generation_prompt=True
        )
    else:
        # Simple concatenation if apply_chat_template is not available
        convo_string = (
            "<|system|>\n" + convo[0]["content"] + "\n<|end|>\n<|user|>\n" + convo[1]["content"] + "\n<|end|>\n"
        )

    assert isinstance(convo_string, str)

    # Tokenize the conversation
    # prompt_str is tokenized separately so we can do the calculations below
    convo_tokens = tokenizer.encode(
        convo_string, return_tensors="pt", add_special_tokens=False, truncation=False
    ).to(device)
    prompt_tokens = tokenizer.encode(
        prompt_str, return_tensors="pt", add_special_tokens=False, truncation=False
    ).to(device)
    assert isinstance(convo_tokens, torch.Tensor) and isinstance(prompt_tokens, torch.Tensor)
    convo_tokens = convo_tokens.squeeze(0)  # Squeeze just to make the following easier
    prompt_tokens = prompt_tokens.squeeze(0)

    # Calculate where to inject the image
    # Use the indices of the special tokens
    end_token_id = tokenizer.convert_tokens_to_ids("<|end|>")

    # Ensure end_token_id is valid
    if end_token_id is None:
        raise ValueError("The tokenizer does not recognize the '<|end|>' token. Please ensure special tokens are added.")

    end_token_indices = (convo_tokens == end_token_id).nonzero(as_tuple=True)[0].tolist()
    if len(end_token_indices) >= 2:
        # The image is to be injected between the system message and the user prompt
        preamble_len = end_token_indices[0] + 1  # Position after the first <|end|>
    else:
        preamble_len = 0  # Fallback to the start if tokens are missing

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

    # Construct the input
    input_embeds = torch.cat(
        [
            convo_embeds[:, :preamble_len],  # Part before the prompt
            embedded_images.to(dtype=convo_embeds.dtype),  # Image embeddings
            convo_embeds[:, preamble_len:],  # The prompt and anything after it
        ],
        dim=1,
    ).to(device)

    input_ids = torch.cat(
        [
            convo_tokens[:preamble_len].unsqueeze(0),
            torch.full((1, embedded_images.shape[1]), tokenizer.pad_token_id, dtype=torch.long, device=device),  # Dummy tokens for the image
            convo_tokens[preamble_len:].unsqueeze(0),
        ],
        dim=1,
    ).to(device)
    attention_mask = torch.ones_like(input_ids).to(device)

    # Debugging
    print(f"Input to model: {repr(tokenizer.decode(input_ids[0]))}")

    # Generate the caption
    generate_ids = text_model.generate(
        input_ids=input_ids,
        inputs_embeds=input_embeds,
        attention_mask=attention_mask,
        max_new_tokens=300,
        do_sample=True,
        temperature=0.6,
        top_p=0.9,
        suppress_tokens=None,
    )

    # Trim off the prompt
    generate_ids = generate_ids[:, input_ids.shape[1]:]
    if generate_ids[0][-1] in [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|end|>")]:
        generate_ids = generate_ids[:, :-1]

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

    return prompt_str, caption.strip()

class CaptionApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("JoyCaption Alpha Two")
        self.setGeometry(100, 100, 1200, 1200)
        
        # Set minimum size to maintain GUI consistency
        self.setMinimumSize(1000, 700)
        
        self.initUI()

        # Initialize model variables
        self.clip_processor = None
        self.clip_model = None
        self.tokenizer = None
        self.text_model = None
        self.image_adapter = None

        # Initialize variables for selected images
        self.input_dir = None
        self.single_image_path = None
        self.selected_image_path = None

        # Theme variables
        self.dark_mode = False

    def initUI(self):
        main_layout = QHBoxLayout()

        # Left panel for parameters
        left_panel = QVBoxLayout()

        # Input directory selection
        self.input_dir_button = QPushButton("Select Input Directory")
        self.input_dir_button.clicked.connect(self.select_input_directory)
        self.input_dir_label = QLabel("No directory selected")
        left_panel.addWidget(self.input_dir_button)
        left_panel.addWidget(self.input_dir_label)

        # Single image selection
        self.single_image_button = QPushButton("Select Single Image")
        self.single_image_button.clicked.connect(self.select_single_image)
        self.single_image_label = QLabel("No image selected")
        left_panel.addWidget(self.single_image_button)
        left_panel.addWidget(self.single_image_label)

        # Caption Type
        self.caption_type_combo = QComboBox()
        self.caption_type_combo.addItems(CAPTION_TYPE_MAP.keys())
        self.caption_type_combo.setCurrentText("Descriptive")
        left_panel.addWidget(QLabel("Caption Type:"))
        left_panel.addWidget(self.caption_type_combo)

        # Caption Length
        self.caption_length_combo = QComboBox()
        self.caption_length_combo.addItems(CAPTION_LENGTH_CHOICES)
        self.caption_length_combo.setCurrentText("long")
        left_panel.addWidget(QLabel("Caption Length:"))
        left_panel.addWidget(self.caption_length_combo)

        # Extra Options
        left_panel.addWidget(QLabel("Extra Options:"))
        self.extra_options_checkboxes = []
        for option in EXTRA_OPTIONS_LIST:
            checkbox = QCheckBox(option)
            self.extra_options_checkboxes.append(checkbox)
            left_panel.addWidget(checkbox)

        # Name Input
        self.name_input_line = QLineEdit()
        left_panel.addWidget(QLabel("Person/Character Name (if applicable):"))
        left_panel.addWidget(self.name_input_line)

        # Custom Prompt
        self.custom_prompt_text = QTextEdit()
        left_panel.addWidget(QLabel("Custom Prompt (optional):"))
        left_panel.addWidget(self.custom_prompt_text)

        # Checkpoint Path
        self.checkpoint_path_line = QLineEdit()
        self.checkpoint_path_line.setText("cgrkzexw-599808")  # Update this path accordingly
        left_panel.addWidget(QLabel("Checkpoint Path:"))
        left_panel.addWidget(self.checkpoint_path_line)

        # Load Models Button
        self.load_models_button = QPushButton("Load Models")
        self.load_models_button.clicked.connect(self.load_models)
        left_panel.addWidget(self.load_models_button)

        # Run Buttons
        self.run_button = QPushButton("Generate Captions for All Images")
        self.run_button.clicked.connect(self.generate_captions)
        left_panel.addWidget(self.run_button)

        self.caption_selected_button = QPushButton("Caption Selected Image")
        self.caption_selected_button.clicked.connect(self.caption_selected_image)
        self.caption_selected_button.setEnabled(False)  # Disabled until an image is selected
        left_panel.addWidget(self.caption_selected_button)

        self.caption_single_button = QPushButton("Caption Single Image")
        self.caption_single_button.clicked.connect(self.caption_single_image)
        self.caption_single_button.setEnabled(False)  # Disabled until a single image is selected
        left_panel.addWidget(self.caption_single_button)

        # Theme Toggle Button
        self.toggle_theme_button = QPushButton("Toggle Dark Mode")
        self.toggle_theme_button.clicked.connect(self.toggle_theme)
        left_panel.addWidget(self.toggle_theme_button)

        # Right panel for image display and captions
        right_panel = QVBoxLayout()

        # List widget for images
        self.image_list_widget = QListWidget()
        self.image_list_widget.itemClicked.connect(self.display_selected_image)
        right_panel.addWidget(QLabel("Images:"))
        right_panel.addWidget(self.image_list_widget)

        # Label to display the selected image
        self.selected_image_label = QLabel()
        self.selected_image_label.setAlignment(Qt.AlignCenter)
        
        # Set size policy to expanding to utilize available space
        self.selected_image_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.selected_image_label.setMinimumSize(400, 400)  # Set a reasonable minimum size

        right_panel.addWidget(QLabel("Selected Image:"))
        right_panel.addWidget(self.selected_image_label)

        # Adjust stretch factors to allocate more space to the image label
        main_layout.addLayout(left_panel, 2)
        main_layout.addLayout(right_panel, 5)  # Increased stretch factor for right_panel
        self.setLayout(main_layout)

    def toggle_theme(self):
        if self.dark_mode:
            self.setStyleSheet("")  # Reset to default
            self.dark_mode = False
        else:
            # Apply dark theme stylesheet with adjusted properties
            self.setStyleSheet("""
                QWidget {
                    background-color: #2E2E2E;
                    color: #FFFFFF;
                    font-family: Arial, sans-serif;
                    /* Removed font-size to prevent resizing */
                }
                QPushButton {
                    background-color: #3A3A3A;
                    color: #FFFFFF;
                    border: none;
                    padding: 5px;  /* Keep padding minimal */
                }
                QPushButton:hover {
                    background-color: #555555;
                }
                QLabel {
                    color: #FFFFFF;
                }
                QLineEdit, QTextEdit, QComboBox {
                    background-color: #3A3A3A;
                    color: #FFFFFF;
                    border: 1px solid #555555;
                    padding: 5px;  /* Keep padding minimal */
                }
                QListWidget {
                    background-color: #3A3A3A;
                    color: #FFFFFF;
                    border: 1px solid #555555;
                }
                QCheckBox {
                    color: #FFFFFF;
                }
            """)
            self.dark_mode = True

    def select_input_directory(self):
        directory = QFileDialog.getExistingDirectory(self, "Select Input Directory")
        if directory:
            self.input_dir = Path(directory)
            self.input_dir_label.setText(str(self.input_dir))
            self.load_images()
        else:
            self.input_dir_label.setText("No directory selected")
            self.input_dir = None

    def select_single_image(self):
        file_filter = "Image Files (*.jpg *.jpeg *.png *.bmp *.gif *.tiff)"
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Single Image", "", file_filter)
        if file_path:
            self.single_image_path = Path(file_path)
            self.single_image_label.setText(str(self.single_image_path.name))
            self.display_image(self.single_image_path)
            self.caption_single_button.setEnabled(True)
        else:
            self.single_image_label.setText("No image selected")
            self.single_image_path = None
            self.caption_single_button.setEnabled(False)

    def load_images(self):
        # List of image file extensions
        image_extensions = [".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"]

        # Collect all image files in the directory
        self.image_files = [f for f in self.input_dir.iterdir() if f.suffix.lower() in image_extensions]

        if not self.image_files:
            QMessageBox.warning(self, "No Images", "No image files found in the selected directory.")
            return

        self.image_list_widget.clear()
        for image_path in self.image_files:
            item = QListWidgetItem(str(image_path.name))
            pixmap = QPixmap(str(image_path))
            if not pixmap.isNull():
                # Increase thumbnail size
                scaled_pixmap = pixmap.scaled(150, 150, Qt.KeepAspectRatio, Qt.SmoothTransformation)
                icon = QIcon(scaled_pixmap)
                item.setIcon(icon)
            self.image_list_widget.addItem(item)

    def display_selected_image(self, item):
        # Get the selected image path
        image_name = item.text()
        image_path = self.input_dir / image_name
        pixmap = QPixmap(str(image_path))
        if not pixmap.isNull():
            # Scale the pixmap to fit the label while preserving aspect ratio
            scaled_pixmap = pixmap.scaled(
                self.selected_image_label.size(),
                Qt.KeepAspectRatio,
                Qt.SmoothTransformation
            )
            self.selected_image_label.setPixmap(scaled_pixmap)
            self.caption_selected_button.setEnabled(True)
            self.selected_image_path = image_path
        else:
            self.selected_image_label.clear()
            self.caption_selected_button.setEnabled(False)
            self.selected_image_path = None

    def display_image(self, image_path):
        pixmap = QPixmap(str(image_path))
        if not pixmap.isNull():
            # Scale the pixmap to fit the label while preserving aspect ratio
            scaled_pixmap = pixmap.scaled(
                self.selected_image_label.size(),
                Qt.KeepAspectRatio,
                Qt.SmoothTransformation
            )
            self.selected_image_label.setPixmap(scaled_pixmap)
        else:
            self.selected_image_label.clear()

    def load_models(self):
        checkpoint_path = Path(self.checkpoint_path_line.text())
        if not checkpoint_path.exists():
            QMessageBox.warning(self, "Checkpoint Error", f"Checkpoint path does not exist: {checkpoint_path}")
            return

        try:
            (
                self.clip_processor,
                self.clip_model,
                self.tokenizer,
                self.text_model,
                self.image_adapter,
            ) = load_models(checkpoint_path)
            QMessageBox.information(self, "Models Loaded", "Models have been loaded successfully.")
        except Exception as e:
            QMessageBox.critical(self, "Model Loading Error", f"An error occurred while loading models: {e}")

    def collect_parameters(self):
        # Collect parameters for caption generation
        caption_type = self.caption_type_combo.currentText()
        caption_length = self.caption_length_combo.currentText()
        extra_options = [checkbox.text() for checkbox in self.extra_options_checkboxes if checkbox.isChecked()]
        name_input = self.name_input_line.text()
        custom_prompt = self.custom_prompt_text.toPlainText()

        return caption_type, caption_length, extra_options, name_input, custom_prompt

    def generate_captions(self):
        # Determine which images to process
        if hasattr(self, 'input_image_path') and self.input_image_path is not None:
            image_paths = [self.input_image_path]
        elif hasattr(self, 'image_files') and self.image_files:
            image_paths = self.image_files
        else:
            QMessageBox.warning(self, "No Images", "Please select an image or directory containing images.")
            return

        if not all([self.clip_processor, self.clip_model, self.tokenizer, self.text_model, self.image_adapter]):
            QMessageBox.warning(self, "Models Not Loaded", "Please load the models before generating captions.")
            return

        # Collect parameters
        caption_type, caption_length, extra_options, name_input, custom_prompt = self.collect_parameters()

        # Process each image
        for image_path in image_paths:
            print(f"\nProcessing image: {image_path}")
            input_image = Image.open(image_path).convert("RGB")

            try:
                prompt_str, caption = generate_caption(
                    input_image,
                    caption_type,
                    caption_length,
                    extra_options,
                    name_input,
                    custom_prompt,
                    self.clip_model,
                    self.tokenizer,
                    self.text_model,
                    self.image_adapter,
                )

                # Save the caption in a text file with the same name as the image
                caption_file = image_path.with_suffix('.txt')
                with open(caption_file, 'w', encoding='utf-8') as f:
                    # Just write the caption
                    f.write(f"{caption}\n")

                print(f"Caption saved to {caption_file}")

            except Exception as e:
                print(f"Error processing image {image_path}: {e}")
                continue

        QMessageBox.information(self, "Captions Generated", "Captions have been generated and saved.")

    def caption_selected_image(self):
        if not self.selected_image_path:
            QMessageBox.warning(self, "No Image Selected", "Please select an image from the list.")
            return

        if not all([self.clip_processor, self.clip_model, self.tokenizer, self.text_model, self.image_adapter]):
            QMessageBox.warning(self, "Models Not Loaded", "Please load the models before generating captions.")
            return

        caption_type, caption_length, extra_options, name_input, custom_prompt = self.collect_parameters()

        print(f"\nProcessing image: {self.selected_image_path}")
        input_image = Image.open(self.selected_image_path).convert("RGB")

        try:
            prompt_str, caption = generate_caption(
                input_image,
                caption_type,
                caption_length,
                extra_options,
                name_input,
                custom_prompt,
                self.clip_model,
                self.tokenizer,
                self.text_model,
                self.image_adapter,
            )

            # Save the caption in a text file with the same name as the image
            caption_file = self.selected_image_path.with_suffix('.txt')
            with open(caption_file, 'w', encoding='utf-8') as f:
                # Just write the caption
                f.write(f"{caption}\n")

            print(f"Caption saved to {caption_file}")

        except Exception as e:
            print(f"Error processing image {self.selected_image_path}: {e}")
            QMessageBox.critical(self, "Error", f"An error occurred: {e}")
            return

        QMessageBox.information(self, "Caption Generated", f"Caption has been generated and saved for {self.selected_image_path.name}.")

    def caption_single_image(self):
        if not self.single_image_path:
            QMessageBox.warning(self, "No Image Selected", "Please select a single image.")
            return

        if not all([self.clip_processor, self.clip_model, self.tokenizer, self.text_model, self.image_adapter]):
            QMessageBox.warning(self, "Models Not Loaded", "Please load the models before generating captions.")
            return

        caption_type, caption_length, extra_options, name_input, custom_prompt = self.collect_parameters()

        print(f"\nProcessing image: {self.single_image_path}")
        input_image = Image.open(self.single_image_path).convert("RGB")

        try:
            prompt_str, caption = generate_caption(
                input_image,
                caption_type,
                caption_length,
                extra_options,
                name_input,
                custom_prompt,
                self.clip_model,
                self.tokenizer,
                self.text_model,
                self.image_adapter,
            )

            # Save the caption in a text file with the same name as the image
            caption_file = self.single_image_path.with_suffix('.txt')
            with open(caption_file, 'w', encoding='utf-8') as f:
                # Just write the caption
                f.write(f"{caption}\n")

            print(f"Caption saved to {caption_file}")

        except Exception as e:
            print(f"Error processing image {self.single_image_path}: {e}")
            QMessageBox.critical(self, "Error", f"An error occurred: {e}")
            return

        QMessageBox.information(self, "Caption Generated", f"Caption has been generated and saved for {self.single_image_path.name}.")

    def resizeEvent(self, event):
        super().resizeEvent(event)
        if self.selected_image_path and self.selected_image_label.pixmap():
            pixmap = QPixmap(str(self.selected_image_path))
            if not pixmap.isNull():
                # Rescale the pixmap to fit the label size
                scaled_pixmap = pixmap.scaled(
                    self.selected_image_label.size(),
                    Qt.KeepAspectRatio,
                    Qt.SmoothTransformation
                )
                self.selected_image_label.setPixmap(scaled_pixmap)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = CaptionApp()
    window.show()
    sys.exit(app.exec_())