File size: 5,876 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 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 |
# mobile_handlers.py
import gradio as gr
class MobileUXHandlers:
def __init__(self, orchestrator):
self.orchestrator = orchestrator
self.mobile_state = {}
async def handle_mobile_submit(self, message, chat_history, session_id,
show_reasoning, show_agent_trace, request: gr.Request):
"""
Mobile-optimized submission handler with enhanced UX
"""
# Get mobile device info
user_agent = request.headers.get("user-agent", "").lower()
is_mobile = any(device in user_agent for device in ['mobile', 'android', 'iphone'])
# Mobile-specific optimizations
if is_mobile:
return await self._mobile_optimized_processing(
message, chat_history, session_id, show_reasoning, show_agent_trace
)
else:
return await self._desktop_processing(
message, chat_history, session_id, show_reasoning, show_agent_trace
)
async def _mobile_optimized_processing(self, message, chat_history, session_id,
show_reasoning, show_agent_trace):
"""
Mobile-specific processing with enhanced UX feedback
"""
try:
# Immediate feedback for mobile users
yield {
"chatbot": chat_history + [[message, "Thinking..."]],
"message_input": "",
"reasoning_display": {"status": "processing"},
"performance_display": {"status": "processing"}
}
# Process with mobile-optimized parameters
result = await self.orchestrator.process_request(
session_id=session_id,
user_input=message,
mobile_optimized=True, # Special flag for mobile
max_tokens=800 # Shorter responses for mobile
)
# Format for mobile display
formatted_response = self._format_for_mobile(
result['final_response'],
show_reasoning and result.get('reasoning_chain'),
show_agent_trace and result.get('agent_trace')
)
# Update chat history
updated_history = chat_history + [[message, formatted_response]]
yield {
"chatbot": updated_history,
"message_input": "",
"reasoning_display": result.get('reasoning_chain', {}),
"performance_display": result.get('performance_metrics', {})
}
except Exception as e:
# Mobile-friendly error handling
error_response = self._get_mobile_friendly_error(e)
yield {
"chatbot": chat_history + [[message, error_response]],
"message_input": message, # Keep message for retry
"reasoning_display": {"error": "Processing failed"},
"performance_display": {"error": str(e)}
}
def _format_for_mobile(self, response, reasoning_chain, agent_trace):
"""
Format response for optimal mobile readability
"""
# Split long responses for mobile
if len(response) > 400:
paragraphs = self._split_into_paragraphs(response, max_length=300)
response = "\n\n".join(paragraphs)
# Add mobile-optimized formatting
formatted = f"""
<div class="mobile-response">
{response}
</div>
"""
# Add reasoning if requested
if reasoning_chain:
formatted += f"""
<div class="reasoning-mobile" style="margin-top: 15px; padding: 10px; background: #f5f5f5; border-radius: 8px; font-size: 14px;">
<strong>Reasoning:</strong> {reasoning_chain[:200]}...
</div>
"""
return formatted
def _get_mobile_friendly_error(self, error):
"""
User-friendly error messages for mobile
"""
error_messages = {
"timeout": "⏱️ Taking longer than expected. Please try a simpler question.",
"network": "📡 Connection issue. Check your internet and try again.",
"rate_limit": "🚦 Too many requests. Please wait a moment.",
"default": "❌ Something went wrong. Please try again."
}
error_type = "default"
if "timeout" in str(error).lower():
error_type = "timeout"
elif "network" in str(error).lower() or "connection" in str(error).lower():
error_type = "network"
elif "rate" in str(error).lower():
error_type = "rate_limit"
return error_messages[error_type]
async def _desktop_processing(self, message, chat_history, session_id,
show_reasoning, show_agent_trace):
"""
Desktop processing without mobile optimizations
"""
# TODO: Implement desktop-specific processing
return {
"chatbot": chat_history,
"message_input": "",
"reasoning_display": {},
"performance_display": {}
}
def _split_into_paragraphs(self, text, max_length=300):
"""
Split text into mobile-friendly paragraphs
"""
# TODO: Implement intelligent paragraph splitting
words = text.split()
paragraphs = []
current_para = []
for word in words:
current_para.append(word)
if len(' '.join(current_para)) > max_length:
paragraphs.append(' '.join(current_para[:-1]))
current_para = [current_para[-1]]
if current_para:
paragraphs.append(' '.join(current_para))
return paragraphs
|