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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
"""
Caption Model Module

Manages BLIP and GIT models for image caption generation.
Handles model loading, inference, and memory management.
"""

import torch
from PIL import Image
from typing import Optional, Dict, Tuple
from transformers import (
    BlipProcessor, 
    BlipForConditionalGeneration,
    AutoProcessor,
    AutoModelForCausalLM
)
import gc

from config import model_config


class CaptionModelError(Exception):
    """Custom exception for caption model errors"""
    pass


class CaptionModel:
    """
    Base class for caption generation models
    
    Provides common interface for BLIP and GIT models
    """
    
    def __init__(self, model_name: str, device: str = "cuda"):
        """
        Initialize caption model
        
        Args:
            model_name: HuggingFace model identifier
            device: Device to load model on (cuda/cpu)
        """
        self.model_name = model_name
        self.device = self._get_device(device)
        self.processor = None
        self.model = None
        self._is_loaded = False
    
    def _get_device(self, requested_device: str) -> str:
        """
        Determine available device
        
        Args:
            requested_device: Requested device (cuda/cpu)
            
        Returns:
            str: Available device
        """
        if requested_device == "cuda" and torch.cuda.is_available():
            return "cuda"
        return "cpu"
    
    def load(self) -> bool:
        """
        Load model into memory
        
        Returns:
            bool: True if successful
        """
        raise NotImplementedError("Subclass must implement load()")
    
    def generate_caption(
        self, 
        image: Image.Image,
        max_length: int = 50,
        num_beams: int = 3
    ) -> str:
        """
        Generate caption for image
        
        Args:
            image: PIL Image
            max_length: Maximum caption length
            num_beams: Number of beams for beam search
            
        Returns:
            str: Generated caption
        """
        raise NotImplementedError("Subclass must implement generate_caption()")
    
    def unload(self) -> None:
        """Unload model from memory"""
        if self.model is not None:
            del self.model
            self.model = None
        if self.processor is not None:
            del self.processor
            self.processor = None
        
        gc.collect()
        if self.device == "cuda":
            torch.cuda.empty_cache()
        
        self._is_loaded = False
    
    def is_loaded(self) -> bool:
        """Check if model is loaded"""
        return self._is_loaded
    
    def get_info(self) -> dict:
        """Get model information"""
        return {
            "model_name": self.model_name,
            "device": self.device,
            "is_loaded": self._is_loaded
        }


class BLIPModel(CaptionModel):
    """
    BLIP (Bootstrapping Language-Image Pre-training) model
    
    Fast and efficient model for image captioning
    """
    
    def __init__(self, device: str = "cuda"):
        """Initialize BLIP model"""
        super().__init__(model_config.BLIP_MODEL_NAME, device)
        self.max_length = model_config.BLIP_MAX_LENGTH
        self.num_beams = model_config.BLIP_NUM_BEAMS
    
    def load(self) -> bool:
        """
        Load BLIP model and processor
        
        Returns:
            bool: True if successful
        """
        try:
            print(f"Loading BLIP model on {self.device}...")
            
            # Load processor
            self.processor = BlipProcessor.from_pretrained(
                self.model_name,
                cache_dir=model_config.MODEL_CACHE_DIR
            )
            
            # Load model
            self.model = BlipForConditionalGeneration.from_pretrained(
                self.model_name,
                cache_dir=model_config.MODEL_CACHE_DIR,
                torch_dtype=torch.float16 if self.device == "cuda" else torch.float32
            ).to(self.device)
            
            # Set to evaluation mode
            self.model.eval()
            
            self._is_loaded = True
            print(f"βœ“ BLIP model loaded successfully on {self.device}")
            return True
            
        except Exception as e:
            print(f"Error loading BLIP model: {e}")
            self._is_loaded = False
            return False
    
    def generate_caption(
        self,
        image: Image.Image,
        max_length: Optional[int] = None,
        num_beams: Optional[int] = None
    ) -> str:
        """
        Generate caption using BLIP
        
        Args:
            image: PIL Image
            max_length: Maximum caption length
            num_beams: Number of beams for beam search
            
        Returns:
            str: Generated caption
            
        Raises:
            CaptionModelError: If generation fails
        """
        if not self._is_loaded:
            raise CaptionModelError("BLIP model not loaded")
        
        try:
            # Use default values if not provided
            max_length = max_length or self.max_length
            num_beams = num_beams or self.num_beams
            
            # Preprocess image
            inputs = self.processor(
                images=image,
                return_tensors="pt"
            ).to(self.device)
            
            # Generate caption
            with torch.no_grad():
                output_ids = self.model.generate(
                    **inputs,
                    max_length=max_length,
                    num_beams=num_beams,
                    early_stopping=True
                )
            
            # Decode caption
            caption = self.processor.decode(
                output_ids[0],
                skip_special_tokens=True
            )
            
            return caption.strip()
            
        except Exception as e:
            raise CaptionModelError(f"BLIP caption generation failed: {e}")


class GITModel(CaptionModel):
    """
    GIT (Generative Image-to-text Transformer) model
    
    More detailed and accurate captions compared to BLIP
    """
    
    def __init__(self, device: str = "cuda"):
        """Initialize GIT model"""
        super().__init__(model_config.GIT_MODEL_NAME, device)
        self.max_length = model_config.GIT_MAX_LENGTH
        self.num_beams = model_config.GIT_NUM_BEAMS
    
    def load(self) -> bool:
        """
        Load GIT model and processor
        
        Returns:
            bool: True if successful
        """
        try:
            print(f"Loading GIT model on {self.device}...")
            
            # Load processor
            self.processor = AutoProcessor.from_pretrained(
                self.model_name,
                cache_dir=model_config.MODEL_CACHE_DIR
            )
            
            # Load model
            self.model = AutoModelForCausalLM.from_pretrained(
                self.model_name,
                cache_dir=model_config.MODEL_CACHE_DIR,
                torch_dtype=torch.float16 if self.device == "cuda" else torch.float32
            ).to(self.device)
            
            # Set to evaluation mode
            self.model.eval()
            
            self._is_loaded = True
            print(f"βœ“ GIT model loaded successfully on {self.device}")
            return True
            
        except Exception as e:
            print(f"Error loading GIT model: {e}")
            self._is_loaded = False
            return False
    
    def generate_caption(
        self,
        image: Image.Image,
        max_length: Optional[int] = None,
        num_beams: Optional[int] = None
    ) -> str:
        """
        Generate caption using GIT
        
        Args:
            image: PIL Image
            max_length: Maximum caption length
            num_beams: Number of beams for beam search
            
        Returns:
            str: Generated caption
            
        Raises:
            CaptionModelError: If generation fails
        """
        if not self._is_loaded:
            raise CaptionModelError("GIT model not loaded")
        
        try:
            # Use default values if not provided
            max_length = max_length or self.max_length
            num_beams = num_beams or self.num_beams
            
            # Preprocess image
            inputs = self.processor(
                images=image,
                return_tensors="pt"
            ).to(self.device)
            
            # Generate caption
            with torch.no_grad():
                output_ids = self.model.generate(
                    pixel_values=inputs.pixel_values,
                    max_length=max_length,
                    num_beams=num_beams,
                    early_stopping=True
                )
            
            # Decode caption
            caption = self.processor.batch_decode(
                output_ids,
                skip_special_tokens=True
            )[0]
            
            return caption.strip()
            
        except Exception as e:
            raise CaptionModelError(f"GIT caption generation failed: {e}")


class CaptionModelManager:
    """
    Manager for both BLIP and GIT models
    
    Provides unified interface and handles model lifecycle
    """
    
    def __init__(self, device: Optional[str] = None):
        """
        Initialize model manager
        
        Args:
            device: Device to use (cuda/cpu), auto-detects if None
        """
        self.device = device or model_config.DEVICE
        
        # Initialize models
        self.blip_model = BLIPModel(self.device)
        self.git_model = GITModel(self.device)
        
        # Track which models are loaded
        self._loaded_models = set()
    
    def load_all_models(self) -> Tuple[bool, bool]:
        """
        Load both models
        
        Returns:
            Tuple[bool, bool]: (blip_success, git_success)
        """
        blip_success = self.blip_model.load()
        if blip_success:
            self._loaded_models.add("blip")
        
        git_success = self.git_model.load()
        if git_success:
            self._loaded_models.add("git")
        
        return blip_success, git_success
    
    def load_model(self, model_name: str) -> bool:
        """
        Load specific model
        
        Args:
            model_name: Model to load ("blip" or "git")
            
        Returns:
            bool: True if successful
        """
        if model_name.lower() == "blip":
            success = self.blip_model.load()
            if success:
                self._loaded_models.add("blip")
            return success
        elif model_name.lower() == "git":
            success = self.git_model.load()
            if success:
                self._loaded_models.add("git")
            return success
        else:
            raise ValueError(f"Unknown model: {model_name}")
    
    def generate_captions(
        self,
        image: Image.Image
    ) -> Dict[str, str]:
        """
        Generate captions from all loaded models
        
        Args:
            image: PIL Image
            
        Returns:
            Dict[str, str]: Captions from each model
        """
        captions = {}
        
        if "blip" in self._loaded_models:
            try:
                captions["blip"] = self.blip_model.generate_caption(image)
            except Exception as e:
                captions["blip"] = f"Error: {str(e)}"
        
        if "git" in self._loaded_models:
            try:
                captions["git"] = self.git_model.generate_caption(image)
            except Exception as e:
                captions["git"] = f"Error: {str(e)}"
        
        return captions
    
    def unload_all_models(self) -> None:
        """Unload all models from memory"""
        self.blip_model.unload()
        self.git_model.unload()
        self._loaded_models.clear()
    
    def get_status(self) -> dict:
        """Get status of all models"""
        return {
            "device": self.device,
            "blip": {
                "loaded": self.blip_model.is_loaded(),
                "info": self.blip_model.get_info()
            },
            "git": {
                "loaded": self.git_model.is_loaded(),
                "info": self.git_model.get_info()
            },
            "loaded_models": list(self._loaded_models)
        }


# Singleton instance
_model_manager = None


def get_model_manager() -> CaptionModelManager:
    """Get singleton CaptionModelManager instance"""
    global _model_manager
    if _model_manager is None:
        _model_manager = CaptionModelManager()
    return _model_manager


if __name__ == "__main__":
    # Test the caption models
    print("=" * 60)
    print("CAPTION MODELS - TEST MODE")
    print("=" * 60)
    
    # Initialize manager
    manager = CaptionModelManager()
    print(f"\nβœ“ Model manager initialized")
    print(f"  Device: {manager.device}")
    
    print("\n" + "=" * 60)
    print("Loading models (this may take a few minutes)...")
    print("=" * 60)
    
    # Load models
    blip_success, git_success = manager.load_all_models()
    
    print(f"\nBLIP: {'βœ“ Loaded' if blip_success else 'βœ— Failed'}")
    print(f"GIT: {'βœ“ Loaded' if git_success else 'βœ— Failed'}")
    
    print("\n" + "=" * 60)
    print("Model Status:")
    print("=" * 60)
    status = manager.get_status()
    for key, value in status.items():
        if isinstance(value, dict):
            print(f"{key}:")
            for k, v in value.items():
                print(f"  {k}: {v}")
        else:
            print(f"{key}: {value}")
    
    print("\n" + "=" * 60)
    print("βœ“ Caption models test complete")
    print("=" * 60)
    print("\nTo test caption generation, provide a test image:")
    print("  from PIL import Image")
    print("  img = Image.open('your_image.jpg')")
    print("  captions = manager.generate_captions(img)")
    print("  print(captions)")