""" Report Cards Component Generate downloadable summary cards for leaderboard and runs """ import pandas as pd from datetime import datetime from typing import Optional import base64 from pathlib import Path def _get_logo_base64(): """Load and encode TraceMind logo as base64""" try: # Try local file first (for development and GitHub) logo_path = Path(__file__).parent.parent / "Logo.png" if logo_path.exists(): with open(logo_path, "rb") as f: return base64.b64encode(f.read()).decode() # Fallback: fetch from GitHub assets branch (for HuggingFace Spaces) # Logo.png is hosted on assets branch to avoid binary file issues on HF import urllib.request github_logo_url = "https://raw.githubusercontent.com/Mandark-droid/TraceMind-AI/assets/Logo.png" with urllib.request.urlopen(github_logo_url, timeout=5) as response: return base64.b64encode(response.read()).decode() except Exception as e: print(f"Warning: Could not load logo: {e}") return None def generate_leaderboard_summary_card(df: pd.DataFrame, top_n: int = 3) -> str: """ Generate HTML for leaderboard summary card Args: df: Leaderboard DataFrame top_n: Number of top performers to show Returns: HTML string for summary card """ if df.empty: return _create_empty_card_html("No leaderboard data available") # Get top performers by success rate top_models = df.nlargest(top_n, 'success_rate') if 'success_rate' in df.columns else df.head(top_n) # Get logo logo_base64 = _get_logo_base64() # Card header html = f"""
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M')}
{timestamp}
{message}
{datetime.now().strftime('%Y-%m-%d %H:%M')}
{f"Run {overall_winner} ({model_a if overall_winner == 'A' else model_b}) is recommended for most use cases" if overall_winner != "Tie" else "Both runs are evenly matched - choose based on your specific priorities"}