MedIRT / src /display /formatting.py
Qian, Xusheng
feat: add leaderboard component
4dd69b3
Raw History Blame Contribute Delete
9.08 kB
# ****************************
# HTML HELPERS
# ****************************
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>"
# ****************************
# DATA VALIDATION
# ****************************
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)
# ****************************
# RANK & SCORE VISUALIZATION
# ****************************
def get_rank_badge(rank: int) -> str:
if rank == 1:
return '<span style="font-size:1.5em;" title="1st">🥇</span>'
elif rank == 2:
return '<span style="font-size:1.5em;" title="2nd">🥈</span>'
elif rank == 3:
return '<span style="font-size:1.5em;" title="3rd">🥉</span>'
else:
return f'<span style="font-size:1.2em;color:#a1a1aa;font-weight:500;">{rank}</span>'
def get_score_gauge(score: float) -> str:
"""Progress bar visualization (UNUSED)"""
try:
score = float(score)
except (TypeError, ValueError):
score = 0.0
if score is None or score != score:
percent = 0.0
display_score = 0.0
elif score <= 1.0:
percent = score * 100
display_score = percent
else:
percent = score
display_score = score
if percent >= 95:
percent = 98 + (min(percent, 100) - 95) * 0.4
percent = min(max(percent, 0), 100)
display_score = min(max(display_score, 0), 100)
return f'''
<div class="score-bar" style="margin: 0.5em 0;">
<div class="score-bar-track">
<div class="score-bar-fill" style="width: {percent}%;"></div>
</div>
<span class="score-bar-value">{display_score:.3f}</span>
</div>
'''
def get_score_stars(score: float, unique_id: str = None) -> str:
"""Star rating with numeric score display"""
try:
score = float(score)
except (TypeError, ValueError):
score = 0.0
if score <= 1.0:
stars = score * 5
display_score = f"{score:.3f}"
else:
stars = (score / 100) * 5
display_score = f"{score:.1f}"
full_stars = int(stars)
partial_star = stars - full_stars
empty_stars = 5 - full_stars - (1 if partial_star > 0 else 0)
star_html = []
for i in range(full_stars):
star_html.append('<span style="color: #ffd700; font-size: 1.2em;">★</span>')
if partial_star > 0:
star_html.append(f'<span style="color: #ffd700; font-size: 1.2em; opacity: {partial_star:.1f};">★</span>')
for i in range(empty_stars):
star_html.append('<span style="color: #ddd; font-size: 1.2em;">☆</span>')
stars_str = ''.join(star_html)
return f'<div style="display: flex; align-items: center; gap: 6px; min-width: 140px;"><span title="Score: {display_score}">{stars_str}</span><span style="color: #455a64; font-family: \'SF Mono\', monospace; font-weight: 600; font-size: 14px; min-width: 50px;">{display_score}</span></div>'
# ****************************
# BADGE SYSTEM (UNUSED)
# ****************************
def get_type_badge(type_value: str) -> str:
if not type_value or str(type_value).lower() == 'nan':
return ''
type_lower = str(type_value).lower()
if 'open' in type_lower:
return '<span class="badge badge-type-open">Open</span>'
elif 'proprietary' in type_lower:
return '<span class="badge badge-type-proprietary">Proprietary</span>'
else:
return f'<span class="badge">{type_value}</span>'
def get_model_type_badge(model_type: str) -> str:
if not model_type or str(model_type).lower() == 'nan':
return ''
model_type_lower = str(model_type).lower()
if 'instruct' in model_type_lower:
return '<span class="badge badge-modeltype-instruct">Instruct</span>'
elif 'think' in model_type_lower:
return '<span class="badge badge-modeltype-think">Think</span>'
elif 'hybrid' in model_type_lower:
return '<span class="badge badge-modeltype-hybrid">Hybrid</span>'
else:
return f'<span class="badge">{model_type}</span>'
def get_output_badge(think_value: str) -> str:
if not think_value or str(think_value).lower() == 'nan':
return ''
think_lower = str(think_value).lower()
if think_lower in ['yes', 'true', 'on', '1']:
return '<span class="badge badge-think-on">Yes</span>'
elif think_lower in ['no', 'false', 'off', '0']:
return '<span class="badge badge-think-off">No</span>'
else:
return f'<span class="badge">{think_value}</span>'
# ****************************
# MODEL NAME & CELL FORMATTING
# ****************************
def get_display_model_name(model_name: str) -> str:
if not model_name:
return ""
display_name = str(model_name)
if "/" in display_name:
display_name = display_name.split("/")[-1]
if len(display_name) > 40:
display_name = display_name[:37] + "..."
return display_name
def format_leaderboard_cell(cell_value, column_name: str, key: str = "Category") -> str:
if cell_value is None or (isinstance(cell_value, float) and cell_value != cell_value):
return '<span style="color: #888;">—</span>'
try:
num_value = float(cell_value)
if column_name.lower() in ['total_correct', 'total_attempts', 'questions_attempted']:
return f"{int(num_value)}"
else:
return f"{num_value:.3f}"
except (TypeError, ValueError):
return str(cell_value)
# ****************************
# MAIN TABLE GENERATOR
# ****************************
def get_leaderboard_table_html(df, key="Category") -> str:
"""Generate HTML for medical leaderboard table with sticky columns and top-3 highlighting"""
import pandas as pd
html = ['<table class="pretty-leaderboard-table">']
html.append("<thead><tr>")
for col in df.columns:
if col == "accuracy":
html.append(f'<th style="min-width: 160px; width: 160px;">{col}</th>')
else:
html.append(f"<th>{col}</th>")
html.append("</tr></thead>")
html.append("<tbody>")
for idx, row in df.iterrows():
html.append("<tr>")
for col in df.columns:
cell = row[col]
if col == "rank":
badge = get_rank_badge(cell)
html.append(f"<td>{badge}</td>")
elif col == "model_id":
rank = row.get("rank", None)
highlight_style = ""
if rank == 1:
highlight_style = "color: #b8860b; font-weight: 900; font-size: 1.15em; text-shadow: 0 2px 4px rgba(255,215,0,0.4); background: linear-gradient(135deg, #ffd700 0%, #ffed4a 100%); padding: 4px 8px; border-radius: 6px; border-left: 4px solid #ffd700;"
elif rank == 2:
highlight_style = "color: #4a5568; font-weight: 800; font-size: 1.1em; text-shadow: 0 2px 3px rgba(192,192,192,0.3); background: linear-gradient(135deg, #c0c0c0 0%, #e2e8f0 100%); padding: 4px 8px; border-radius: 6px; border-left: 4px solid #c0c0c0;"
elif rank == 3:
highlight_style = "color: #b45309; font-weight: 800; font-size: 1.05em; text-shadow: 0 2px 3px rgba(205,127,50,0.3); background: linear-gradient(135deg, #cd7f32 0%, #d69e2e 100%); padding: 4px 8px; border-radius: 6px; border-left: 4px solid #cd7f32;"
else:
highlight_style = "color: #37474f; font-weight: 600;"
display_name = get_display_model_name(str(cell))
html.append(f'<td><span style="{highlight_style}">{display_name}</span></td>')
elif col == "accuracy":
try:
unique_id = str(row["model_id"]).replace(" ", "_").replace("-", "_").replace("(", "_").replace(")", "_")
cell_html = get_score_stars(float(cell), unique_id=unique_id)
except Exception:
cell_html = str(cell)
html.append(f'<td class="score-stars-cell" style="min-width: 160px; width: 160px;">{cell_html}</td>')
else:
html.append(f"<td>{format_leaderboard_cell(cell, col, key)}</td>")
html.append("</tr>")
html.append("</tbody></table>")
table_html = "\n".join(html)
return f'<div class="leaderboard-table-container" style="max-height:900px;overflow-y:auto;">{table_html}</div>'