From e9edf2c5f21ee73a32351cbb45d250c1fcaada85 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Fri, 24 Oct 2025 23:13:28 +0300 Subject: [PATCH] fix async model loading --- ANNOTATE/web/app.py | 76 ++++++++- .../templates/components/annotation_list.html | 152 +++++++++--------- .../templates/components/training_panel.html | 61 +++++-- 3 files changed, 197 insertions(+), 92 deletions(-) diff --git a/ANNOTATE/web/app.py b/ANNOTATE/web/app.py index 441f8c9..7297bb9 100644 --- a/ANNOTATE/web/app.py +++ b/ANNOTATE/web/app.py @@ -147,14 +147,19 @@ class AnnotationDashboard: if self.data_provider: self._enable_unified_storage_async() - # ANNOTATE doesn't need orchestrator - skip ML model loading for fast startup + # ANNOTATE doesn't need orchestrator immediately - load async for fast startup self.orchestrator = None + self.models_loading = True + self.available_models = [] # Initialize ANNOTATE components self.annotation_manager = AnnotationManager() # Use REAL training adapter - NO SIMULATION! self.training_adapter = RealTrainingAdapter(None, self.data_provider) + # Start async model loading in background + self._start_async_model_loading() + # Initialize data loader with existing DataProvider self.data_loader = HistoricalDataLoader(self.data_provider) if self.data_provider else None self.time_range_manager = TimeRangeManager(self.data_loader) if self.data_loader else None @@ -168,6 +173,60 @@ class AnnotationDashboard: logger.info("Annotation Dashboard initialized") + def _start_async_model_loading(self): + """Load ML models asynchronously in background thread""" + import threading + + def load_models(): + try: + logger.info("🔄 Starting async model loading...") + + # Initialize orchestrator with models + if TradingOrchestrator: + self.orchestrator = TradingOrchestrator( + data_provider=self.data_provider, + enhanced_rl_training=True + ) + + # Initialize ML models + logger.info("Initializing ML models...") + self.orchestrator._initialize_ml_models() + + # Update training adapter with orchestrator + self.training_adapter.orchestrator = self.orchestrator + + # Get available models from orchestrator + available = [] + if hasattr(self.orchestrator, 'rl_agent') and self.orchestrator.rl_agent: + available.append('DQN') + if hasattr(self.orchestrator, 'cnn_model') and self.orchestrator.cnn_model: + available.append('CNN') + if hasattr(self.orchestrator, 'transformer_model') and self.orchestrator.transformer_model: + available.append('Transformer') + + self.available_models = available + + if available: + logger.info(f"✅ Models loaded: {', '.join(available)}") + else: + logger.warning("⚠️ No models were initialized") + + self.models_loading = False + logger.info("✅ Async model loading complete") + else: + logger.warning("⚠️ TradingOrchestrator not available") + self.models_loading = False + + except Exception as e: + logger.error(f"❌ Error loading models: {e}") + self.models_loading = False + self.available_models = [] + + # Start loading in background thread + thread = threading.Thread(target=load_models, daemon=True) + thread.start() + logger.info("🚀 Model loading started in background (UI remains responsive)") + def _enable_unified_storage_async(self): """Enable unified storage system in background thread""" def enable_storage(): @@ -967,21 +1026,33 @@ class AnnotationDashboard: @self.server.route('/api/available-models', methods=['GET']) def get_available_models(): - """Get list of available models""" + """Get list of available models with loading status""" try: if not self.training_adapter: return jsonify({ 'success': False, + 'loading': False, 'error': { 'code': 'TRAINING_UNAVAILABLE', 'message': 'Real training adapter not available' } }) + # Check if models are still loading + if self.models_loading: + return jsonify({ + 'success': True, + 'loading': True, + 'models': [], + 'message': 'Models are loading in background...' + }) + + # Models loaded - get the list models = self.training_adapter.get_available_models() return jsonify({ 'success': True, + 'loading': False, 'models': models }) @@ -989,6 +1060,7 @@ class AnnotationDashboard: logger.error(f"Error getting available models: {e}") return jsonify({ 'success': False, + 'loading': False, 'error': { 'code': 'MODEL_LIST_ERROR', 'message': str(e) diff --git a/ANNOTATE/web/templates/components/annotation_list.html b/ANNOTATE/web/templates/components/annotation_list.html index b2e0e94..ec42ce9 100644 --- a/ANNOTATE/web/templates/components/annotation_list.html +++ b/ANNOTATE/web/templates/components/annotation_list.html @@ -27,102 +27,102 @@ + \ No newline at end of file diff --git a/ANNOTATE/web/templates/components/training_panel.html b/ANNOTATE/web/templates/components/training_panel.html index 6e4d733..0b186ef 100644 --- a/ANNOTATE/web/templates/components/training_panel.html +++ b/ANNOTATE/web/templates/components/training_panel.html @@ -102,32 +102,65 @@