File size: 2,417 Bytes
16c2a22
 
 
 
20548ac
16c2a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20548ac
16c2a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20548ac
16c2a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Helper functions for common operations."""

import os
import logging
from typing import Optional, Tuple, List, Dict, Any

from app.utils.constants import HF_TOKEN_VARS, FRENCH_PHRASES, FRENCH_CHARS, FRENCH_PATTERNS


logger = logging.getLogger(__name__)


def get_hf_token() -> Tuple[Optional[str], str]:
    """
    Get Hugging Face token from environment variables.
    
    Returns:
        Tuple of (token, token_source_name)
    """
    for var_name in HF_TOKEN_VARS:
        token = os.getenv(var_name)
        if token:
            return token, var_name
    return None, "none"


def is_french_request(messages: List[Dict[str, Any]]) -> bool:
    """
    Detect if the request is in French based on user messages.
    
    Args:
        messages: List of message dictionaries with 'role' and 'content'
        
    Returns:
        True if French is detected, False otherwise
    """
    user_messages = [msg for msg in messages if msg.get("role") == "user"]
    
    for msg in user_messages:
        content = msg.get("content", "")
        content_lower = content.lower()
        
        # Check for explicit French language request
        if any(phrase in content_lower for phrase in FRENCH_PHRASES):
            return True
        
        # Check for French characters
        if any(char in content for char in FRENCH_CHARS):
            return True
        
        # Check for French question patterns
        if any(pattern in content_lower for pattern in FRENCH_PATTERNS):
            return True
    
    return False


def has_french_system_prompt(messages: List[Dict[str, Any]]) -> bool:
    """Check if messages already contain a French system prompt."""
    return any(
        "français" in msg.get("content", "").lower()
        for msg in messages
        if msg.get("role") == "system"
    )


def log_info(message: str, print_output: bool = False):
    """Log info message, optionally also print."""
    logger.info(message)
    if print_output:
        print(message)


def log_warning(message: str, print_output: bool = False):
    """Log warning message, optionally also print."""
    logger.warning(message)
    if print_output:
        print(message)


def log_error(message: str, exc_info: bool = False, print_output: bool = False):
    """Log error message, optionally also print."""
    logger.error(message, exc_info=exc_info)
    if print_output:
        print(message)