|
|
|
|
|
class MobilePerformance: |
|
|
""" |
|
|
Performance optimizations specifically for mobile devices |
|
|
""" |
|
|
|
|
|
def __init__(self): |
|
|
self.optimization_config = { |
|
|
"mobile_max_tokens": 800, |
|
|
"mobile_timeout": 15000, |
|
|
"cache_aggressive": True, |
|
|
"lazy_loading": True |
|
|
} |
|
|
|
|
|
async def optimize_for_mobile(self, processing_function, *args, **kwargs): |
|
|
""" |
|
|
Apply mobile-specific performance optimizations |
|
|
""" |
|
|
|
|
|
kwargs.update({ |
|
|
'max_tokens': self.optimization_config['mobile_max_tokens'], |
|
|
'timeout': self.optimization_config['mobile_timeout'] |
|
|
}) |
|
|
|
|
|
|
|
|
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 |
|
|
""" |
|
|
|
|
|
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, |
|
|
'timeout': 30000, |
|
|
'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 |
|
|
|
|
|
|