File size: 6,288 Bytes
d83a64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e74285c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def model_hyperlink(link, model_name):
    return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'


def make_clickable_model(model_name):
    link = f"https://huggingface.co/{model_name}"
    return model_hyperlink(link, model_name)


def styled_error(error):
    return f"<p style='color: red; font-size: 20px; text-align: center;'>{error}</p>"


def styled_warning(warn):
    return f"<p style='color: orange; font-size: 20px; text-align: center;'>{warn}</p>"


def styled_message(message):
    return f"<p style='color: green; font-size: 20px; text-align: center;'>{message}</p>"


def has_no_nan_values(df, columns):
    return df[columns].notna().all(axis=1)


def has_nan_values(df, columns):
    return df[columns].isna().any(axis=1)

def get_score_bar(score):
    """
    Generate HTML for a score bar with gradient styling.
    Expects score in the range 0-100.
    """
    width = max(0, min(score, 100))  # Clamp to [0, 100]
    return f"""
        <div class="score-bar">
            <div class="score-bar-track">
                <div class="score-bar-fill" style="width: {width}%;"></div>
            </div>
            <span class="score-bar-value">{score:.3f}</span>
        </div>
    """

def render_leaderboard_html(df, overall_col="average"):
    """
    Render a DataFrame as an HTML table, replacing the overall_col with a gauge bar.
    """
    from .formatting import get_score_bar
    from src.display.css_html_js import get_rank_badge

    def get_type_badge(model_type):
        if model_type == "open":
            return '<span class="badge badge-open">open</span>'
        else:
            return '<span class="badge badge-closed">closed</span>'

    def get_output_badge(output_form):
        if output_form == "reasoning":
            return '<span class="badge badge-reasoning">reasoning</span>'
        else:
            return '<span class="badge badge-normal">normal</span>'

    # ์ˆจ๊ธธ ์ปฌ๋Ÿผ
    hidden_cols = ["Model", "Model Type", "Output Form", "Rank"]

    # Build table header
    def get_sort_arrow(col, sort_col, sort_asc):
        # "Model Name", "Group" ์ปฌ๋Ÿผ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ปฌ๋Ÿผ์— ์ •๋ ฌ ๋ฒ„ํŠผ ๋…ธ์ถœ
        if col in {"Model Name", "Group"}:
            return ""
        # ํ•˜๋‚˜์˜ ๋ฒ„ํŠผ(โ–ฒ ๋˜๋Š” โ–ผ)๋งŒ ๋…ธ์ถœ, ํด๋ฆญ ์‹œ asc๊ฐ€ ๋ฐ˜์ „๋จ
        if col == sort_col:
            # ํ˜„์žฌ ์ •๋ ฌ ์ƒํƒœ์— ๋”ฐ๋ผ ์•„์ด์ฝ˜๊ณผ data-asc๋ฅผ ๋ฐ˜์ „
            if sort_asc:
                # ์˜ค๋ฆ„์ฐจ์ˆœ ์ƒํƒœ: โ–ผ ์•„์ด์ฝ˜, ํด๋ฆญ ์‹œ ๋‚ด๋ฆผ์ฐจ์ˆœ
                svg = (
                    '<svg width="14" height="14" viewBox="0 0 14 14" style="vertical-align:middle">'
                    '<polygon points="3,5 11,5 7,11" fill="currentColor"/></svg>'
                )
                return (
                    f'<span class="sort-arrow active" data-col="{col}" data-asc="false" aria-label="๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ">{svg}</span>'
                )
            else:
                # ๋‚ด๋ฆผ์ฐจ์ˆœ ์ƒํƒœ: โ–ฒ ์•„์ด์ฝ˜, ํด๋ฆญ ์‹œ ์˜ค๋ฆ„์ฐจ์ˆœ
                svg = (
                    '<svg width="14" height="14" viewBox="0 0 14 14" style="vertical-align:middle">'
                    '<polygon points="7,3 11,9 3,9" fill="currentColor"/></svg>'
                )
                return (
                    f'<span class="sort-arrow active" data-col="{col}" data-asc="true" aria-label="์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ">{svg}</span>'
                )
        else:
            # ์ •๋ ฌ ์ค‘์ด ์•„๋‹Œ ์ปฌ๋Ÿผ: โ–ฒ(์˜ค๋ฆ„์ฐจ์ˆœ) ์•„์ด์ฝ˜, ํด๋ฆญ ์‹œ ์˜ค๋ฆ„์ฐจ์ˆœ
            svg = (
                '<svg width="14" height="14" viewBox="0 0 14 14" style="vertical-align:middle">'
                '<polygon points="7,3 11,9 3,9" fill="currentColor"/></svg>'
            )
            return (
                f'<span class="sort-arrow" data-col="{col}" data-asc="true" aria-label="์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ">{svg}</span>'
            )

    # ์ •๋ ฌ ์ƒํƒœ ์ถ”์ถœ (State์—์„œ ์ „๋‹ฌ๋ฐ›๊ฑฐ๋‚˜ ๊ธฐ๋ณธ๊ฐ’)
    sort_col = getattr(df, "_sort_col", None) or (df.columns[0] if len(df.columns) > 0 else None)
    sort_asc = getattr(df, "_sort_asc", None)
    if sort_asc is None:
        sort_asc = True

    html = '<table class="pretty-leaderboard-table">\n<thead><tr>'
    for col in df.columns:
        if col in hidden_cols:
            continue
        html += f'<th>{col}{get_sort_arrow(col, sort_col, sort_asc)}</th>'
    html += '</tr></thead>\n<tbody>\n'

    # Build table rows
    for idx, row in df.iterrows():
        html += '<tr>'
        for col in df.columns:
            if col in hidden_cols:
                continue
            cell = row[col]
            if col == overall_col:
                try:
                    cell_html = get_score_bar(float(cell))
                except Exception:
                    cell_html = str(cell)
                html += f'<td>{cell_html}</td>'
            elif col in ["Model Name"]:
                # 1~3์œ„ ํ•˜์ด๋ผ์ดํŠธ + 4๋“ฑ ์ดํ›„ ํฐ์ƒ‰ + ๋ฑƒ์ง€ ํ•ญ์ƒ ํ‘œ์‹œ
                rank = row.get("Rank", None)
                model_type = row.get("Model Type", None) or row.get("Model_Type", None)
                output_form = row.get("Output Form", None) or row.get("Output_Form", None)
                highlight_style = ""
                if rank == 1 or rank == "1":
                    highlight_style = "color: #ffd700; font-weight: bold; text-shadow: 0 0 4px #fff2;"
                elif rank == 2 or rank == "2":
                    highlight_style = "color: #b0b0b0; font-weight: bold;"
                elif rank == 3 or rank == "3":
                    highlight_style = "color: #cd7f32; font-weight: bold;"
                else:
                    highlight_style = "color: #fff; font-weight: 600;"
                badge_html = ""
                if model_type:
                    badge_html += " " + get_type_badge(model_type)
                if output_form:
                    badge_html += " " + get_output_badge(output_form)
                html += f'<td><span style="{highlight_style}">{cell}</span>{badge_html}</td>'
            else:
                html += f'<td>{cell}</td>'
        html += '</tr>\n'
    html += '</tbody></table>'
    return html