Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -116,36 +116,68 @@ class ChatInterface:
|
|
| 116 |
) -> Tuple[str, List[List[str]]]:
|
| 117 |
"""Process incoming chat message."""
|
| 118 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Check network before processing
|
| 120 |
if not check_network():
|
| 121 |
return "Network connectivity issues detected. Some features may be limited.", history
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# Convert UnifiedResult to dict if needed
|
| 127 |
-
intent_type = getattr(intent, "type", None) or intent.get("type", "general")
|
| 128 |
-
|
| 129 |
-
if intent_type == "query":
|
| 130 |
-
response = await self._handle_query(message)
|
| 131 |
-
elif intent_type == "objective":
|
| 132 |
-
response = await self._handle_objective(message)
|
| 133 |
-
elif intent_type == "status":
|
| 134 |
-
response = await self._handle_status_request(message)
|
| 135 |
-
else:
|
| 136 |
-
response = await self._handle_general_chat(message)
|
| 137 |
|
| 138 |
# Update chat history
|
|
|
|
|
|
|
| 139 |
history.append([message, response])
|
| 140 |
|
| 141 |
return response, history
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
logger.error(f"Error processing message: {str(e)}")
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
async def _analyze_intent(self, message: str) -> Dict[str, Any]:
|
| 148 |
-
"""Analyze user message intent."""
|
| 149 |
try:
|
| 150 |
# Use reasoning engine to analyze intent
|
| 151 |
result = await self.orchestrator.reasoning_engine.reason(
|
|
@@ -156,17 +188,19 @@ class ChatInterface:
|
|
| 156 |
}
|
| 157 |
)
|
| 158 |
|
| 159 |
-
#
|
| 160 |
if hasattr(result, "to_dict"):
|
| 161 |
return result.to_dict()
|
| 162 |
elif hasattr(result, "__dict__"):
|
| 163 |
return result.__dict__
|
|
|
|
|
|
|
| 164 |
else:
|
| 165 |
-
return {"type": "general"}
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
logger.error(f"Error analyzing intent: {str(e)}")
|
| 169 |
-
return {"type": "general"}
|
| 170 |
|
| 171 |
async def _handle_query(self, message: str) -> str:
|
| 172 |
"""Handle information queries."""
|
|
@@ -231,17 +265,28 @@ class ChatInterface:
|
|
| 231 |
return self._format_status_response(system_status, team_status, objective_status)
|
| 232 |
|
| 233 |
async def _handle_general_chat(self, message: str) -> str:
|
| 234 |
-
"""Handle general chat interactions."""
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
async def _get_team_response(self, team_type: TeamType, query: str) -> Dict[str, Any]:
|
| 247 |
"""Get response from a specific team."""
|
|
|
|
| 116 |
) -> Tuple[str, List[List[str]]]:
|
| 117 |
"""Process incoming chat message."""
|
| 118 |
try:
|
| 119 |
+
# Validate input
|
| 120 |
+
if not message or not message.strip():
|
| 121 |
+
return "Please provide a message to process.", history
|
| 122 |
+
|
| 123 |
# Check network before processing
|
| 124 |
if not check_network():
|
| 125 |
return "Network connectivity issues detected. Some features may be limited.", history
|
| 126 |
|
| 127 |
+
# Process the message
|
| 128 |
+
response = await self._handle_message(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# Update chat history
|
| 131 |
+
if isinstance(response, tuple):
|
| 132 |
+
response = response[0] # Extract message if tuple returned
|
| 133 |
history.append([message, response])
|
| 134 |
|
| 135 |
return response, history
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
logger.error(f"Error processing message: {str(e)}")
|
| 139 |
+
error_msg = "I apologize, but I encountered a temporary error. Please try again or rephrase your request."
|
| 140 |
+
history.append([message, error_msg])
|
| 141 |
+
return error_msg, history
|
| 142 |
+
|
| 143 |
+
async def _handle_message(self, message: str) -> str:
|
| 144 |
+
"""Handle message processing with error recovery."""
|
| 145 |
+
try:
|
| 146 |
+
# Analyze message intent
|
| 147 |
+
intent = await self._analyze_intent(message)
|
| 148 |
+
intent_type = self._get_intent_type(intent)
|
| 149 |
+
|
| 150 |
+
# Process based on intent
|
| 151 |
+
if intent_type == "query":
|
| 152 |
+
return await self._handle_query(message)
|
| 153 |
+
elif intent_type == "objective":
|
| 154 |
+
return await self._handle_objective(message)
|
| 155 |
+
elif intent_type == "status":
|
| 156 |
+
return await self._handle_status_request(message)
|
| 157 |
+
else:
|
| 158 |
+
return await self._handle_general_chat(message)
|
| 159 |
+
|
| 160 |
+
except Exception as e:
|
| 161 |
+
logger.error(f"Error in message handler: {str(e)}")
|
| 162 |
+
raise
|
| 163 |
+
|
| 164 |
+
def _get_intent_type(self, intent) -> str:
|
| 165 |
+
"""Safely extract intent type from various result formats."""
|
| 166 |
+
try:
|
| 167 |
+
if hasattr(intent, "type"):
|
| 168 |
+
return intent.type
|
| 169 |
+
elif hasattr(intent, "intent_type"):
|
| 170 |
+
return intent.intent_type
|
| 171 |
+
elif isinstance(intent, dict):
|
| 172 |
+
return intent.get("type") or intent.get("intent_type", "general")
|
| 173 |
+
else:
|
| 174 |
+
return "general"
|
| 175 |
+
except Exception as e:
|
| 176 |
+
logger.error(f"Error getting intent type: {str(e)}")
|
| 177 |
+
return "general"
|
| 178 |
|
| 179 |
async def _analyze_intent(self, message: str) -> Dict[str, Any]:
|
| 180 |
+
"""Analyze user message intent with error handling."""
|
| 181 |
try:
|
| 182 |
# Use reasoning engine to analyze intent
|
| 183 |
result = await self.orchestrator.reasoning_engine.reason(
|
|
|
|
| 188 |
}
|
| 189 |
)
|
| 190 |
|
| 191 |
+
# Handle different result types
|
| 192 |
if hasattr(result, "to_dict"):
|
| 193 |
return result.to_dict()
|
| 194 |
elif hasattr(result, "__dict__"):
|
| 195 |
return result.__dict__
|
| 196 |
+
elif isinstance(result, dict):
|
| 197 |
+
return result
|
| 198 |
else:
|
| 199 |
+
return {"type": "general", "confidence": 0.5}
|
| 200 |
|
| 201 |
except Exception as e:
|
| 202 |
logger.error(f"Error analyzing intent: {str(e)}")
|
| 203 |
+
return {"type": "general", "confidence": 0.5}
|
| 204 |
|
| 205 |
async def _handle_query(self, message: str) -> str:
|
| 206 |
"""Handle information queries."""
|
|
|
|
| 265 |
return self._format_status_response(system_status, team_status, objective_status)
|
| 266 |
|
| 267 |
async def _handle_general_chat(self, message: str) -> str:
|
| 268 |
+
"""Handle general chat interactions with error recovery."""
|
| 269 |
+
try:
|
| 270 |
+
# Get responses from all teams
|
| 271 |
+
responses = []
|
| 272 |
+
for team_type in self.team_manager.teams:
|
| 273 |
+
try:
|
| 274 |
+
response = await self._get_team_response(team_type, message)
|
| 275 |
+
if response:
|
| 276 |
+
responses.append(response)
|
| 277 |
+
except Exception as e:
|
| 278 |
+
logger.error(f"Error getting response from team {team_type}: {str(e)}")
|
| 279 |
+
continue
|
| 280 |
+
|
| 281 |
+
if not responses:
|
| 282 |
+
return "I'm having trouble processing that request. Could you please rephrase it?"
|
| 283 |
+
|
| 284 |
+
# Combine responses
|
| 285 |
+
return self._format_team_responses(responses)
|
| 286 |
+
|
| 287 |
+
except Exception as e:
|
| 288 |
+
logger.error(f"Error in general chat handler: {str(e)}")
|
| 289 |
+
return "I apologize, but I'm having trouble understanding that. Could you try asking in a different way?"
|
| 290 |
|
| 291 |
async def _get_team_response(self, team_type: TeamType, query: str) -> Dict[str, Any]:
|
| 292 |
"""Get response from a specific team."""
|