model cards back

This commit is contained in:
Dobromir Popov
2025-07-29 23:14:00 +03:00
parent ab5784b890
commit a204362df2
2 changed files with 793 additions and 48 deletions

View File

@ -1374,65 +1374,86 @@ class CleanTradingDashboard:
Input('refresh-training-metrics-btn', 'n_clicks')] # Add manual refresh button
)
def update_training_metrics(slow_intervals, fast_intervals, n_clicks):
"""Update training metrics"""
"""Update training metrics using new clean panel implementation"""
logger.info(f"update_training_metrics callback triggered with slow_intervals={slow_intervals}, fast_intervals={fast_intervals}, n_clicks={n_clicks}")
try:
# Get toggle states from orchestrator
toggle_states = {}
if self.orchestrator:
# Get all available models dynamically
available_models = self._get_available_models()
logger.info(f"Available models: {list(available_models.keys())}")
for model_name in available_models.keys():
toggle_states[model_name] = self.orchestrator.get_model_toggle_state(model_name)
else:
# Fallback to dashboard dynamic state
toggle_states = {}
for model_name, state in self.model_toggle_states.items():
toggle_states[model_name] = state
# Now using slow-interval-component (10s) - no batching needed
# Import the new panel implementation
from web.models_training_panel import ModelsTrainingPanel
logger.info(f"Getting training metrics with toggle_states: {toggle_states}")
metrics_data = self._get_training_metrics(toggle_states)
logger.info(f"update_training_metrics callback: got metrics_data type={type(metrics_data)}")
if metrics_data and isinstance(metrics_data, dict):
logger.info(f"Metrics data keys: {list(metrics_data.keys())}")
if 'loaded_models' in metrics_data:
logger.info(f"Loaded models count: {len(metrics_data['loaded_models'])}")
logger.info(f"Loaded model names: {list(metrics_data['loaded_models'].keys())}")
else:
logger.warning("No 'loaded_models' key in metrics_data!")
else:
logger.warning(f"Invalid metrics_data: {metrics_data}")
# Create panel instance with orchestrator
panel = ModelsTrainingPanel(orchestrator=self.orchestrator)
# Generate the panel content
panel_content = panel.create_panel()
logger.info("Successfully created new training metrics panel")
return panel_content
logger.info("Formatting training metrics...")
formatted_metrics = self.component_manager.format_training_metrics(metrics_data)
logger.info(f"Formatted metrics type: {type(formatted_metrics)}, length: {len(formatted_metrics) if isinstance(formatted_metrics, list) else 'N/A'}")
return formatted_metrics
except PreventUpdate:
logger.info("PreventUpdate raised in training metrics callback")
raise
except Exception as e:
logger.error(f"Error updating training metrics: {e}")
logger.error(f"Error updating training metrics with new panel: {e}")
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
return [html.P(f"Error: {str(e)}", className="text-danger")]
return html.Div([
html.P("Error loading training panel", className="text-danger small"),
html.P(f"Details: {str(e)}", className="text-muted small")
], id="training-metrics")
# Test callback for training metrics (commented out - using real callback now)
# @self.app.callback(
# Output('training-metrics', 'children'),
# [Input('refresh-training-metrics-btn', 'n_clicks')],
# prevent_initial_call=False
# )
# def test_training_metrics_callback(n_clicks):
# """Test callback for training metrics"""
# logger.info(f"test_training_metrics_callback triggered with n_clicks={n_clicks}")
# try:
# # Return a simple test message
# return [html.P("Training metrics test - callback is working!", className="text-success")]
# except Exception as e:
# logger.error(f"Error in test callback: {e}")
# return [html.P(f"Error: {str(e)}", className="text-danger")]
# Universal model toggle callback using pattern matching
@self.app.callback(
[Output({'type': 'model-toggle', 'model': dash.ALL, 'toggle_type': dash.ALL}, 'value')],
[Input({'type': 'model-toggle', 'model': dash.ALL, 'toggle_type': dash.ALL}, 'value')],
prevent_initial_call=True
)
def handle_all_model_toggles(values):
"""Handle all model toggle switches using pattern matching"""
try:
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
# Get the triggered input
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
triggered_value = ctx.triggered[0]['value']
# Parse the component ID
import json
component_id = json.loads(triggered_id)
model_name = component_id['model']
toggle_type = component_id['toggle_type']
is_enabled = bool(triggered_value and len(triggered_value) > 0)
logger.info(f"Model toggle: {model_name} {toggle_type} = {is_enabled}")
if self.orchestrator and hasattr(self.orchestrator, 'set_model_toggle_state'):
# Map dashboard names to orchestrator names
model_mapping = {
'dqn_agent': 'dqn_agent',
'enhanced_cnn': 'enhanced_cnn',
'cob_rl_model': 'cob_rl_model',
'extrema_trainer': 'extrema_trainer',
'transformer': 'transformer',
'decision_fusion': 'decision_fusion'
}
orchestrator_name = model_mapping.get(model_name, model_name)
self.orchestrator.set_model_toggle_state(
orchestrator_name,
toggle_type + '_enabled',
is_enabled
)
logger.info(f"Updated {orchestrator_name} {toggle_type}_enabled = {is_enabled}")
# Return all current values (no change needed)
raise PreventUpdate
except PreventUpdate:
raise
except Exception as e:
logger.error(f"Error handling model toggles: {e}")
raise PreventUpdate
# Manual trading buttons
@self.app.callback(