import pandas as pd from pathlib import Path from ..styles import highlight_color abs_path = Path(__file__).parent.parent.parent def replace_models_names(model_name): if "gpt" in model_name: return model_name replaces = {'meta-llama': 'meta_llama', 'epfl-llm':'epfl_llm', '01-ai':'01_ai'} new_name = model_name.replace('model-', '') for k, v in replaces.items(): if new_name.startswith(k): new_name = new_name.replace(k, v) new_name = new_name.replace('-','/',1) new_name = new_name.replace('_','-',1) new_name = f"[{new_name}](https://huggingface.co/{new_name})" return new_name def generate_order_list_and_data_types(json_data): order_list = ["model_name", "overall_accuracy"] data_types = ["markdown", "number"] for key in json_data.keys(): if key not in ["model_name", "overall_accuracy"]: order_list.append(key) data_types.append("number") order_list[2:] = sorted(order_list[2:]) return order_list, data_types def filter_data(selected_columns, search_query): df = PES_ACCS[selected_columns] if search_query: df = df[df['model_name'].str.contains(search_query, case=False, na=False)] return df def filter_columns(column_choices): selected_columns = [col for col in ORDER_LIST if col in column_choices] filtered_df = PES_ACCS[selected_columns] return filtered_df.style.highlight_max(color=highlight_color, subset=filtered_df.columns[1:]).format(precision=2) def load_json_data(file_path, order_list): PES_ACCS = pd.read_json(file_path) for column in PES_ACCS.columns: if PES_ACCS[column].apply(type).eq(dict).any(): PES_ACCS[column] = PES_ACCS[column].apply(str) PES_ACCS["model_name"] = PES_ACCS["model_name"].apply( lambda name: replace_models_names(name) ) for column in PES_ACCS.select_dtypes(include='number').columns: PES_ACCS[column] = PES_ACCS[column].round(2) ordered_columns = [col for col in order_list if col in PES_ACCS.columns] PES_ACCS = PES_ACCS[ordered_columns] if "overall_accuracy" in PES_ACCS.columns: PES_ACCS = PES_ACCS.sort_values(by="overall_accuracy", ascending=False) return PES_ACCS # file_path = str(abs_path / "leaderboards/pes_accuracy.json") file_path = str(abs_path / "leaderboards/pes_accs.json") with open(file_path, 'r', encoding='utf-8') as file: sample_data = pd.read_json(file_path).iloc[0].to_dict() # Load the first row as a dict ORDER_LIST, DATA_TYPES = generate_order_list_and_data_types(sample_data) PES_ACCS = load_json_data(file_path, ORDER_LIST) STYLED_PES_ACCS = PES_ACCS.style.highlight_max( color = highlight_color, subset=PES_ACCS.columns[1:]).format(precision=2) COLUMN_HEADERS = ORDER_LIST print('test')