JatsTheAIGen commited on
Commit
1b4b874
Β·
1 Parent(s): b3aba24

Improve orchestrator initialization error handling and diagnostics

Browse files

- Add comprehensive GatedRepoError handling during initialization
- Implement automatic fallback to API-only mode when local model loading fails
- Add detailed error logging with step-by-step diagnostics
- Improve 503 error responses with helpful messages
- Add per-component initialization logging for better debugging
- Handle gated repository errors gracefully with fallback to API mode

Files changed (1) hide show
  1. flask_api_standalone.py +100 -12
flask_api_standalone.py CHANGED
@@ -164,36 +164,122 @@ def initialize_orchestrator():
164
  hf_token = os.getenv('HF_TOKEN', '')
165
  if not hf_token:
166
  logger.warning("HF_TOKEN not set - API fallback will be used if local models fail")
 
 
 
 
 
 
 
 
167
 
168
  # Initialize LLM Router with local model loading enabled
169
  logger.info("Initializing LLM Router with local GPU model loading...")
170
- llm_router = LLMRouter(hf_token, use_local_models=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  logger.info("Initializing Agents...")
173
- agents = {
174
- 'intent_recognition': create_intent_agent(llm_router),
175
- 'response_synthesis': create_synthesis_agent(llm_router),
176
- 'safety_check': create_safety_agent(llm_router),
177
- 'skills_identification': create_skills_identification_agent(llm_router)
178
- }
 
 
 
 
 
179
 
180
  logger.info("Initializing Context Manager...")
181
- context_manager = EfficientContextManager(llm_router=llm_router)
 
 
 
 
 
182
 
183
  logger.info("Initializing Orchestrator...")
184
- orchestrator = MVPOrchestrator(llm_router, context_manager, agents)
 
 
 
 
 
185
 
186
  orchestrator_available = True
187
  logger.info("=" * 60)
188
  logger.info("βœ“ AI ORCHESTRATOR READY")
189
- logger.info(" - Local GPU models enabled")
190
  logger.info(" - MAX_WORKERS: 4")
191
  logger.info("=" * 60)
192
 
193
  return True
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  except Exception as e:
196
- logger.error(f"Failed to initialize: {e}", exc_info=True)
 
 
 
 
 
 
197
  orchestrator_available = False
198
  return False
199
 
@@ -296,10 +382,12 @@ def chat():
296
  logger.info(f"Message length: {len(message)} chars, preview: {message[:100]}...")
297
 
298
  if not orchestrator_available or orchestrator is None:
 
299
  return jsonify({
300
  'success': False,
301
  'error': 'Orchestrator not ready',
302
- 'message': 'AI system is initializing. Please try again in a moment.'
 
303
  }), 503
304
 
305
  # Process with orchestrator (async method)
 
164
  hf_token = os.getenv('HF_TOKEN', '')
165
  if not hf_token:
166
  logger.warning("HF_TOKEN not set - API fallback will be used if local models fail")
167
+ else:
168
+ logger.info(f"HF_TOKEN available (length: {len(hf_token)})")
169
+
170
+ # Import GatedRepoError for better error handling
171
+ try:
172
+ from huggingface_hub.exceptions import GatedRepoError
173
+ except ImportError:
174
+ GatedRepoError = Exception
175
 
176
  # Initialize LLM Router with local model loading enabled
177
  logger.info("Initializing LLM Router with local GPU model loading...")
178
+ try:
179
+ llm_router = LLMRouter(hf_token, use_local_models=True)
180
+ logger.info("βœ“ LLM Router initialized")
181
+ except GatedRepoError as e:
182
+ logger.error(f"❌ Gated Repository Error during router initialization: {e}")
183
+ logger.error(" Falling back to API-only mode (local models disabled)")
184
+ # Try again without local models
185
+ llm_router = LLMRouter(hf_token, use_local_models=False)
186
+ logger.warning("⚠️ LLM Router initialized in API-only mode")
187
+ except Exception as e:
188
+ logger.error(f"❌ Failed to initialize LLM Router: {e}", exc_info=True)
189
+ logger.error(" Falling back to API-only mode")
190
+ try:
191
+ llm_router = LLMRouter(hf_token, use_local_models=False)
192
+ logger.warning("⚠️ LLM Router initialized in API-only mode after error")
193
+ except Exception as fallback_error:
194
+ logger.error(f"❌ Failed to initialize LLM Router even in API mode: {fallback_error}", exc_info=True)
195
+ raise
196
 
197
  logger.info("Initializing Agents...")
198
+ try:
199
+ agents = {
200
+ 'intent_recognition': create_intent_agent(llm_router),
201
+ 'response_synthesis': create_synthesis_agent(llm_router),
202
+ 'safety_check': create_safety_agent(llm_router),
203
+ 'skills_identification': create_skills_identification_agent(llm_router)
204
+ }
205
+ logger.info("βœ“ All agents initialized")
206
+ except Exception as e:
207
+ logger.error(f"❌ Failed to initialize agents: {e}", exc_info=True)
208
+ raise
209
 
210
  logger.info("Initializing Context Manager...")
211
+ try:
212
+ context_manager = EfficientContextManager(llm_router=llm_router)
213
+ logger.info("βœ“ Context Manager initialized")
214
+ except Exception as e:
215
+ logger.error(f"❌ Failed to initialize Context Manager: {e}", exc_info=True)
216
+ raise
217
 
218
  logger.info("Initializing Orchestrator...")
219
+ try:
220
+ orchestrator = MVPOrchestrator(llm_router, context_manager, agents)
221
+ logger.info("βœ“ Orchestrator initialized")
222
+ except Exception as e:
223
+ logger.error(f"❌ Failed to initialize Orchestrator: {e}", exc_info=True)
224
+ raise
225
 
226
  orchestrator_available = True
227
  logger.info("=" * 60)
228
  logger.info("βœ“ AI ORCHESTRATOR READY")
229
+ logger.info(" - Local GPU models enabled" if llm_router.use_local_models else " - API-only mode (local models disabled)")
230
  logger.info(" - MAX_WORKERS: 4")
231
  logger.info("=" * 60)
232
 
233
  return True
234
 
235
+ except GatedRepoError as e:
236
+ logger.error("=" * 60)
237
+ logger.error("❌ GATED REPOSITORY ERROR DURING INITIALIZATION")
238
+ logger.error("=" * 60)
239
+ logger.error(f"Error: {e}")
240
+ logger.error("")
241
+ logger.error("SOLUTION:")
242
+ logger.error("1. Visit the model repository on Hugging Face")
243
+ logger.error("2. Click 'Agree and access repository'")
244
+ logger.error("3. Wait for approval (usually instant)")
245
+ logger.error("4. Ensure HF_TOKEN is set with your access token")
246
+ logger.error("=" * 60)
247
+ logger.warning("⚠️ Attempting to initialize in API-only mode...")
248
+ try:
249
+ # Try to initialize without local models
250
+ hf_token = os.getenv('HF_TOKEN', '')
251
+ from src.llm_router import LLMRouter
252
+ from src.agents.intent_agent import create_intent_agent
253
+ from src.agents.synthesis_agent import create_synthesis_agent
254
+ from src.agents.safety_agent import create_safety_agent
255
+ from src.agents.skills_identification_agent import create_skills_identification_agent
256
+ from src.orchestrator_engine import MVPOrchestrator
257
+ from src.context_manager import EfficientContextManager
258
+
259
+ llm_router = LLMRouter(hf_token, use_local_models=False)
260
+ agents = {
261
+ 'intent_recognition': create_intent_agent(llm_router),
262
+ 'response_synthesis': create_synthesis_agent(llm_router),
263
+ 'safety_check': create_safety_agent(llm_router),
264
+ 'skills_identification': create_skills_identification_agent(llm_router)
265
+ }
266
+ context_manager = EfficientContextManager(llm_router=llm_router)
267
+ orchestrator = MVPOrchestrator(llm_router, context_manager, agents)
268
+ orchestrator_available = True
269
+ logger.info("βœ“ Orchestrator initialized in API-only mode")
270
+ return True
271
+ except Exception as fallback_error:
272
+ logger.error(f"❌ Failed to initialize in API-only mode: {fallback_error}", exc_info=True)
273
+ orchestrator_available = False
274
+ return False
275
  except Exception as e:
276
+ logger.error("=" * 60)
277
+ logger.error("❌ FAILED TO INITIALIZE ORCHESTRATOR")
278
+ logger.error("=" * 60)
279
+ logger.error(f"Error type: {type(e).__name__}")
280
+ logger.error(f"Error message: {str(e)}")
281
+ logger.error("=" * 60)
282
+ logger.error("Full traceback:", exc_info=True)
283
  orchestrator_available = False
284
  return False
285
 
 
382
  logger.info(f"Message length: {len(message)} chars, preview: {message[:100]}...")
383
 
384
  if not orchestrator_available or orchestrator is None:
385
+ logger.warning("Chat request received but orchestrator not ready")
386
  return jsonify({
387
  'success': False,
388
  'error': 'Orchestrator not ready',
389
+ 'message': 'AI system is initializing. Please try again in a moment.',
390
+ 'help': 'If this persists, check logs for initialization errors or try POST /api/initialize'
391
  }), 503
392
 
393
  # Process with orchestrator (async method)