File size: 11,377 Bytes
d0c8d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Style Model Module

Handles caption styling using Groq API with fallback mechanisms.
Applies different writing styles to generated captions.
"""

import time
from typing import Optional
from groq import Groq
import requests

from config import groq_config, style_config


class StyleModelError(Exception):
    """Custom exception for style model errors"""
    pass


class StyleModel:
    """
    Caption styling using Groq LLM API
    
    Features:
    - Multiple style options
    - Automatic retry logic
    - Fallback to rule-based styling
    - Rate limiting handling
    """
    
    def __init__(self, api_key: Optional[str] = None):
        """
        Initialize style model
        
        Args:
            api_key: Groq API key (uses config if not provided)
        """
        self.api_key = api_key or groq_config.API_KEY
        self.model_name = groq_config.MODEL_NAME
        self.max_tokens = groq_config.MAX_TOKENS
        self.temperature = groq_config.TEMPERATURE
        self.timeout = groq_config.TIMEOUT_SECONDS
        
        # Initialize Groq client
        if self.api_key:
            try:
                self.client = Groq(
                    api_key=self.api_key
                )
                self._api_available = True
                _ = self.client.models.list()
            except Exception as e:
                print(f"Warning: Groq client initialization failed: {e}")
                print(f"Attempting alternative initialization...")
                try:
                    # Alternative: Create client without extra params
                    import groq
                    self.client = groq.Client(api_key=self.api_key)
                    self._api_available = True
                except Exception as e2:
                    print(f"Alternative initialization also failed: {e2}")
                    self.client = None
                    self._api_available = False
        else:
            print("Warning: No Groq API key provided")
            self.client = None
            self._api_available = False
            
        # Retry configuration
        self.max_retries = groq_config.MAX_RETRIES
        self.retry_delay = groq_config.RETRY_DELAY_SECONDS
    
    def style_caption(
        self,
        caption: str,
        style: str = "Professional"
    ) -> str:
        """
        Apply style to caption
        
        Args:
            caption: Original caption
            style: Style to apply
            
        Returns:
            str: Styled caption
        """
        # If "None" style or no API, return original
        if style == "None" or not self._api_available:
            if style != "None":
                # Use fallback styling if API unavailable
                return self._fallback_style(caption, style)
            return caption
        
        # Try API styling with retries
        for attempt in range(self.max_retries):
            try:
                styled_caption = self._style_with_api(caption, style)
                return styled_caption
            
            except Exception as e:
                print(f"API styling attempt {attempt + 1} failed: {e}")
                
                # If last attempt, use fallback
                if attempt == self.max_retries - 1:
                    print(f"Using fallback styling for: {style}")
                    return self._fallback_style(caption, style)
                
                # Wait before retry
                time.sleep(self.retry_delay)
        
        # Fallback if all retries failed
        return self._fallback_style(caption, style)
    
    def _style_with_api(self, caption: str, style: str) -> str:
        """
        Style caption using Groq API
        
        Args:
            caption: Original caption
            style: Style to apply
            
        Returns:
            str: Styled caption
            
        Raises:
            StyleModelError: If API call fails
        """
        if not self._api_available:
            raise StyleModelError("API not available")
        
        # Get style prompt
        style_prompt = style_config.STYLES.get(
            style,
            style_config.STYLES[style_config.DEFAULT_STYLE]
        )
        
        # Construct messages
        messages = [
            {
                "role": "system",
                "content": "You are an expert at rewriting image captions in different styles. Keep the core meaning but adapt the tone and style as requested. Be concise."
            },
            {
                "role": "user",
                "content": f"{style_prompt}\n\nOriginal caption: {caption}\n\nStyled caption:"
            }
        ]
        
        try:
            # Make API call
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=messages,
                max_tokens=self.max_tokens,
                temperature=self.temperature,
                top_p=groq_config.TOP_P,
                timeout=self.timeout
            )
            
            # Extract styled caption
            styled_caption = response.choices[0].message.content.strip()
            
            # Clean up common artifacts
            styled_caption = self._clean_response(styled_caption)
            
            return styled_caption
            
        except requests.exceptions.Timeout:
            raise StyleModelError("API request timed out")
        except requests.exceptions.RequestException as e:
            raise StyleModelError(f"API request failed: {e}")
        except Exception as e:
            raise StyleModelError(f"Unexpected error: {e}")
    
    def _fallback_style(self, caption: str, style: str) -> str:
        """
        Apply rule-based styling as fallback
        
        Args:
            caption: Original caption
            style: Style to apply
            
        Returns:
            str: Styled caption using templates
        """
        template = style_config.FALLBACK_TEMPLATES.get(
            style,
            style_config.FALLBACK_TEMPLATES["Professional"]
        )
        
        return template.format(caption=caption)
    
    def _clean_response(self, text: str) -> str:
        """
        Clean up API response
        
        Args:
            text: Raw response text
            
        Returns:
            str: Cleaned text
        """
        # Remove common prefixes
        prefixes = [
            "Styled caption:",
            "Caption:",
            "Here's the styled caption:",
            "Here is the caption:",
        ]
        
        for prefix in prefixes:
            if text.lower().startswith(prefix.lower()):
                text = text[len(prefix):].strip()
        
        # Remove quotes if the entire text is quoted
        if (text.startswith('"') and text.endswith('"')) or \
           (text.startswith("'") and text.endswith("'")):
            text = text[1:-1]
        
        return text.strip()
    
    def batch_style_captions(
        self,
        captions: dict,
        style: str = "Professional"
    ) -> dict:
        """
        Style multiple captions at once
        
        Args:
            captions: Dictionary of {model_name: caption}
            style: Style to apply
            
        Returns:
            dict: Dictionary of {model_name: styled_caption}
        """
        styled_captions = {}
        
        for model_name, caption in captions.items():
            try:
                styled_caption = self.style_caption(caption, style)
                styled_captions[model_name] = styled_caption
            except Exception as e:
                print(f"Error styling {model_name} caption: {e}")
                # Use original caption on error
                styled_captions[model_name] = caption
        
        return styled_captions
    
    def is_api_available(self) -> bool:
        """Check if API is available"""
        return self._api_available
    
    def test_connection(self) -> bool:
        """
        Test API connection
        
        Returns:
            bool: True if API is working
        """
        if not self._api_available:
            return False
        
        try:
            # Simple test call
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=[
                    {"role": "user", "content": "Hello"}
                ],
                max_tokens=10,
                timeout=5
            )
            return True
        except Exception as e:
            print(f"API connection test failed: {e}")
            return False
    
    def get_available_styles(self) -> list:
        """Get list of available styles"""
        return list(style_config.STYLES.keys())
    
    def get_info(self) -> dict:
        """Get model information"""
        return {
            "model_name": self.model_name,
            "api_available": self._api_available,
            "max_tokens": self.max_tokens,
            "temperature": self.temperature,
            "available_styles": self.get_available_styles()
        }


# Singleton instance
_style_model = None


def get_style_model() -> StyleModel:
    """Get singleton StyleModel instance"""
    global _style_model
    if _style_model is None:
        _style_model = StyleModel()
    return _style_model


if __name__ == "__main__":
    # Test the style model
    print("=" * 60)
    print("STYLE MODEL - TEST MODE")
    print("=" * 60)
    
    # Initialize model
    style_model = StyleModel()
    
    print(f"\n✓ Style model initialized")
    print(f"  API Available: {style_model.is_api_available()}")
    print(f"  Model: {style_model.model_name}")
    
    # Get info
    print("\nModel Info:")
    info = style_model.get_info()
    for key, value in info.items():
        if isinstance(value, list):
            print(f"  {key}:")
            for item in value:
                print(f"    - {item}")
        else:
            print(f"  {key}: {value}")
    
    # Test connection if API available
    if style_model.is_api_available():
        print("\nTesting API connection...")
        connection_ok = style_model.test_connection()
        print(f"  Connection: {'✓ Success' if connection_ok else '✗ Failed'}")
        
        if connection_ok:
            # Test styling
            print("\nTesting caption styling:")
            test_caption = "A cat sitting on a windowsill looking outside"
            
            for style in ["Professional", "Creative", "Social Media"]:
                print(f"\n  {style}:")
                try:
                    styled = style_model.style_caption(test_caption, style)
                    print(f"    Original: {test_caption}")
                    print(f"    Styled: {styled}")
                except Exception as e:
                    print(f"    Error: {e}")
    else:
        print("\n⚠️  API not available, testing fallback styling:")
        test_caption = "A cat sitting on a windowsill looking outside"
        
        for style in ["Professional", "Creative", "Social Media"]:
            styled = style_model.style_caption(test_caption, style)
            print(f"\n  {style}: {styled}")
    
    print("\n" + "=" * 60)
    print("✓ Style model test complete")
    print("=" * 60)