import pandas as pd from constants import ( NUMERIC_COLS_CATEGORY, NUMERIC_INT_COLS_CATEGORY, NUMERIC_COLS_LANGUAGE, NUMERIC_INT_COLS_LANGUAGE ) def format_leaderboard_cell(cell, col, key="Category"): """ Apply integer/two-decimal formatting to numeric columns. key: "Category" or "Language" """ if key == "Language": numeric_cols = NUMERIC_COLS_LANGUAGE int_cols = NUMERIC_INT_COLS_LANGUAGE else: numeric_cols = NUMERIC_COLS_CATEGORY int_cols = NUMERIC_INT_COLS_CATEGORY if pd.isna(cell) or (isinstance(cell, str) and cell.strip() == ""): return "" try: if col in int_cols: return str(int(round(float(cell)))) elif col in numeric_cols: return "{:.2f}".format(float(cell)) else: return str(cell) except Exception: return "" def styled_error(error): return f"
{error}
" def styled_warning(warn): return f"{warn}
" def styled_message(message): return f"{message}
" 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_display_model_name(full_model_name: str) -> str: """ Removes text within parentheses from the model name for display purposes. Example: "Model (v1)" -> "Model" """ import re return re.sub(r'\s*\(.*?\)', '', full_model_name) def get_score_stars(score, unique_id=None): """ Generate HTML for a 5-star rating visualization. Args: score (float or int): Overall score, can be in 0~1 or 0~100 range. - If 0~1, it will be automatically scaled to 0~100. - If None, NaN, or negative, treated as 0. unique_id (optional): Unique identifier for SVG gradient. Returns: str: HTML string with 5-star visualization, filled in proportion to score. """ # Robust normalization: 0~1 -> 0~100, None/NaN/negative -> 0 max_stars = 5 full_stars = int(score // 20) partial = (score % 20) / 20 # 0.0 ~ 0.999 stars_html = "" star_size = 18 # px # If unique_id is not provided, use "default" uid = str(unique_id) if unique_id is not None else "default" def star_svg(fill_ratio, idx): # fill_ratio: 0.0 (empty) ~ 1.0 (full) # White fill, gray background grad_id = f"star-grad-{uid}-{idx}" return f''' ''' # Full stars for i in range(full_stars): stars_html += star_svg(1.0, i) # Partial star (if needed) if full_stars < max_stars: if partial > 0: stars_html += star_svg(partial, full_stars) empty_stars = max_stars - full_stars - 1 start_empty = full_stars + 1 else: empty_stars = max_stars - full_stars start_empty = full_stars else: empty_stars = 0 start_empty = max_stars # Empty stars for i in range(start_empty, start_empty + empty_stars): stars_html += star_svg(0.0, i) # Score text score_text = f'{score:.2f}' return f'''{col}' ' | ' ) elif col == "Med. Len.": html += ( f' ' f'{get_sort_arrow()}{col}' ' | ' ) elif col == "Med. Resp. Len.": html += ( f' ' f'{get_sort_arrow()}{col}' ' | ' ) elif col == overall_col: html += f' ' f'{get_sort_arrow()}{col}{get_sort_arrow()} | ' else: html += f'{col}{get_sort_arrow()} | ' html += '||
---|---|---|---|---|---|---|
{cell_html} | ' elif col == "Rank": # For 1st, 2nd, and 3rd place, emphasize with medal emoji and color medal = "" style = "color: #fff; font-weight: 600;" if cell == 1 or cell == "1": medal = "🥇" style = "color: #ffd700; font-weight: bold; text-shadow: 0 0 4px #fff2;" elif cell == 2 or cell == "2": medal = "🥈" style = "color: #b0b0b0; font-weight: bold;" elif cell == 3 or cell == "3": medal = "🥉" style = "color: #cd7f32; font-weight: bold;" html += f'{medal if medal else cell} | ' elif col in ["Model Name"]: # Only highlight top 1~3, do not apply badge rank = row.get("Rank", 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;" display_name = get_display_model_name(str(cell)) # --- Start of new logic for tooltip --- comment_value = "" # Check if 'Comment' column exists and the value is not NaN/empty if "Comment" in row and pd.notna(row["Comment"]) and str(row["Comment"]).strip() != "": comment_value = str(row["Comment"]).strip() title_attribute = f' title="{comment_value}"' if comment_value else "" # --- End of new logic for tooltip --- # Link logic link_value = row["Link"] if "Link" in row and pd.notna(row["Link"]) and str(row["Link"]).strip() != "" else None if link_value: clickable_name = f'{display_name}' else: clickable_name = display_name html += f'{clickable_name} | ' elif col == "Type": html += f'{get_type_badge(row.get("Type", ""))} | ' elif col == "Model Type": html += f'{get_model_type_badge(row.get("Model Type", ""))} | ' elif col == "Think": html += f'{get_think_badge(row.get("Think", ""))} | ' else: html += f'{format_leaderboard_cell(cell, col, key)} | ' html += '