File size: 2,394 Bytes
1fc3adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Leaderboard Screen for TraceMind-AI
Displays evaluation runs with MCP-powered insights
"""

import pandas as pd
import gradio as gr
from typing import Optional, Tuple


def prepare_leaderboard_data(df: pd.DataFrame) -> pd.DataFrame:
    """
    Prepare leaderboard dataframe for display

    Args:
        df: Raw leaderboard dataframe from HuggingFace

    Returns:
        Formatted dataframe for display
    """
    if df.empty:
        return pd.DataFrame()

    # Select and reorder columns for display
    display_columns = [
        'model', 'agent_type', 'provider', 'success_rate',
        'total_tests', 'avg_duration_ms', 'total_cost_usd',
        'co2_emissions_g', 'gpu_utilization_avg', 'submitted_by', 'timestamp'
    ]

    # Only include columns that exist
    available_columns = [col for col in display_columns if col in df.columns]
    display_df = df[available_columns].copy()

    # Round numeric columns
    if 'success_rate' in display_df.columns:
        display_df['success_rate'] = display_df['success_rate'].round(1)
    if 'avg_duration_ms' in display_df.columns:
        display_df['avg_duration_ms'] = display_df['avg_duration_ms'].round(0)
    if 'total_cost_usd' in display_df.columns:
        display_df['total_cost_usd'] = display_df['total_cost_usd'].round(4)
    if 'co2_emissions_g' in display_df.columns:
        display_df['co2_emissions_g'] = display_df['co2_emissions_g'].round(2)
    if 'gpu_utilization_avg' in display_df.columns:
        display_df['gpu_utilization_avg'] = display_df['gpu_utilization_avg'].round(1)

    # Sort by success rate descending by default
    if 'success_rate' in display_df.columns:
        display_df = display_df.sort_values('success_rate', ascending=False)

    return display_df


def get_run_id_from_selection(
    df: pd.DataFrame,
    evt: gr.SelectData
) -> Optional[str]:
    """
    Extract run_id from a selected row in the dataframe

    Args:
        df: Full leaderboard dataframe (with run_id column)
        evt: Gradio SelectData event from dataframe click

    Returns:
        run_id string or None
    """
    if df.empty or evt is None:
        return None

    try:
        row_index = evt.index[0]  # evt.index is (row, col)
        if row_index < len(df):
            return df.iloc[row_index]['run_id']
    except (IndexError, KeyError, AttributeError):
        return None

    return None