File size: 3,771 Bytes
66dbebd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# performance_optimizations.py
class MobilePerformance:
    """
    Performance optimizations specifically for mobile devices
    """
    
    def __init__(self):
        self.optimization_config = {
            "mobile_max_tokens": 800,
            "mobile_timeout": 15000,  # 15 seconds for mobile
            "cache_aggressive": True,
            "lazy_loading": True
        }
    
    async def optimize_for_mobile(self, processing_function, *args, **kwargs):
        """
        Apply mobile-specific performance optimizations
        """
        # Reduce processing load for mobile
        kwargs.update({
            'max_tokens': self.optimization_config['mobile_max_tokens'],
            'timeout': self.optimization_config['mobile_timeout']
        })
        
        # Implement progressive loading for better perceived performance
        return await self._progressive_loading(processing_function, *args, **kwargs)
    
    async def _progressive_loading(self, processing_function, *args, **kwargs):
        """
        Stream responses progressively for better mobile UX
        """
        # This would integrate with streaming LLM responses
        async for chunk in processing_function(*args, **kwargs):
            yield chunk
    
    @staticmethod
    def get_mobile_optimized_css():
        """
        CSS optimizations for mobile performance
        """
        return """
        /* Hardware acceleration for mobile */
        .chatbot-container {
            transform: translateZ(0);
            -webkit-transform: translateZ(0);
        }
        
        /* Reduce animations for better performance */
        @media (prefers-reduced-motion: reduce) {
            * {
                animation-duration: 0.01ms !important;
                animation-iteration-count: 1 !important;
                transition-duration: 0.01ms !important;
            }
        }
        
        /* Optimize images and media */
        img {
            max-width: 100%;
            height: auto;
        }
        
        /* Touch device optimizations */
        @media (hover: none) and (pointer: coarse) {
            .gradio-button:hover {
                background-color: initial !important;
            }
        }
        """
    
    def is_mobile_device(self, user_agent: str) -> bool:
        """
        Detect if request is from mobile device
        """
        mobile_keywords = ['mobile', 'android', 'iphone', 'ipad', 'ipod', 'blackberry', 'windows phone']
        user_agent_lower = user_agent.lower()
        return any(keyword in user_agent_lower for keyword in mobile_keywords)
    
    def get_optimization_params(self, user_agent: str) -> dict:
        """
        Get optimization parameters based on device type
        """
        if self.is_mobile_device(user_agent):
            return {
                'max_tokens': self.optimization_config['mobile_max_tokens'],
                'timeout': self.optimization_config['mobile_timeout'],
                'cache_mode': 'aggressive' if self.optimization_config['cache_aggressive'] else 'normal',
                'lazy_load': self.optimization_config['lazy_loading']
            }
        else:
            return {
                'max_tokens': 2000,  # Desktop gets more tokens
                'timeout': 30000,  # 30 seconds for desktop
                'cache_mode': 'normal',
                'lazy_load': False
            }
    
    def enable_aggressive_caching(self):
        """
        Enable aggressive caching for improved performance
        """
        self.optimization_config['cache_aggressive'] = True
    
    def disable_aggressive_caching(self):
        """
        Disable aggressive caching
        """
        self.optimization_config['cache_aggressive'] = False