|
import pandas as pd |
|
import plotly.express as px |
|
from plotly.graph_objs import Figure |
|
import pickle |
|
from datetime import datetime, timezone |
|
from typing import List, Dict, Tuple, Any |
|
from src.get_model_info.hardocded_metadata.flags import FLAGGED_MODELS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HUMAN_BASELINES = { |
|
"Average ⬆️": 0.897 * 100, |
|
"ARC": 0.80 * 100, |
|
"HellaSwag": 0.95 * 100, |
|
"MMLU": 0.898 * 100, |
|
"TruthfulQA": 0.94 * 100, |
|
} |
|
|
|
|
|
def to_datetime(model_info: Tuple[str, Any]) -> datetime: |
|
""" |
|
Converts the lastModified attribute of the object to datetime. |
|
|
|
:param model_info: A tuple containing the name and object. |
|
The object must have a lastModified attribute |
|
with a string representing the date and time. |
|
:return: A datetime object converted from the lastModified attribute of the input object. |
|
""" |
|
name, obj = model_info |
|
return datetime.strptime(obj.lastModified, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc) |
|
|
|
|
|
def join_model_info_with_results(results_df: pd.DataFrame) -> pd.DataFrame: |
|
""" |
|
Integrates model information with the results DataFrame by matching 'Model sha'. |
|
:param results_df: A DataFrame containing results information including 'Model sha' column. |
|
:return: A DataFrame with updated 'Results Date' columns, which are synchronized with model information. |
|
""" |
|
|
|
df = results_df.copy(deep=True) |
|
|
|
|
|
df = df[~df["model_name_for_query"].isin(FLAGGED_MODELS.keys())].reset_index(drop=True) |
|
|
|
|
|
try: |
|
with open("model_info_cache.pkl", "rb") as f: |
|
model_info_cache = pickle.load(f) |
|
except (EOFError, FileNotFoundError): |
|
model_info_cache = {} |
|
|
|
|
|
sorted_dates = sorted(list(model_info_cache.items()), key=to_datetime, reverse=True) |
|
df["Results Date"] = datetime.now().replace(tzinfo=timezone.utc) |
|
|
|
|
|
date_format = "%Y-%m-%dT%H:%M:%S.%fZ" |
|
|
|
|
|
for name, obj in sorted_dates: |
|
|
|
last_modified_datetime = datetime.strptime(obj.lastModified, date_format).replace(tzinfo=timezone.utc) |
|
|
|
|
|
df.loc[df["Model sha"] == obj.sha, "Results Date"] = last_modified_datetime |
|
return df |
|
|
|
|
|
def create_scores_df(results_df: pd.DataFrame) -> pd.DataFrame: |
|
""" |
|
Generates a DataFrame containing the maximum scores until each result date. |
|
|
|
:param results_df: A DataFrame containing result information including metric scores and result dates. |
|
:return: A new DataFrame containing the maximum scores until each result date for every metric. |
|
""" |
|
|
|
results_df["Results Date"] = pd.to_datetime(results_df["Results Date"]) |
|
results_df.sort_values(by="Results Date", inplace=True) |
|
|
|
|
|
scores = { |
|
"Average ⬆️": [], |
|
"ARC": [], |
|
"HellaSwag": [], |
|
"MMLU": [], |
|
"TruthfulQA": [], |
|
"Result Date": [], |
|
"Model Name": [], |
|
} |
|
|
|
|
|
for i, row in results_df.iterrows(): |
|
date = row["Results Date"] |
|
for column in scores.keys(): |
|
if column == "Result Date": |
|
if not scores[column] or scores[column][-1] <= date: |
|
scores[column].append(date) |
|
continue |
|
if column == "Model Name": |
|
scores[column].append(row["model_name_for_query"]) |
|
continue |
|
current_max = scores[column][-1] if scores[column] else float("-inf") |
|
scores[column].append(max(current_max, row[column])) |
|
|
|
|
|
return pd.DataFrame(scores) |
|
|
|
|
|
def create_plot_df(scores_df: pd.DataFrame) -> pd.DataFrame: |
|
""" |
|
Transforms the scores DataFrame into a new format suitable for plotting. |
|
|
|
:param scores_df: A DataFrame containing metric scores and result dates. |
|
:return: A new DataFrame reshaped for plotting purposes. |
|
""" |
|
|
|
cols = ["Average ⬆️", "ARC", "HellaSwag", "MMLU", "TruthfulQA"] |
|
|
|
|
|
dfs = [] |
|
|
|
|
|
for col in cols: |
|
d = scores_df[[col, "Model Name", "Result Date"]].copy().reset_index(drop=True) |
|
d["Metric Name"] = col |
|
d.rename(columns={col: "Metric Value"}, inplace=True) |
|
dfs.append(d) |
|
|
|
|
|
concat_df = pd.concat(dfs, ignore_index=True) |
|
|
|
|
|
concat_df.sort_values(by="Result Date", inplace=True) |
|
concat_df.reset_index(drop=True, inplace=True) |
|
|
|
|
|
concat_df.drop_duplicates(subset=["Metric Name", "Metric Value"], keep="first", inplace=True) |
|
|
|
concat_df.reset_index(drop=True, inplace=True) |
|
return concat_df |
|
|
|
|
|
def create_metric_plot_obj( |
|
df: pd.DataFrame, metrics: List[str], human_baselines: Dict[str, float], title: str |
|
) -> Figure: |
|
""" |
|
Create a Plotly figure object with lines representing different metrics |
|
and horizontal dotted lines representing human baselines. |
|
|
|
:param df: The DataFrame containing the metric values, names, and dates. |
|
:param metrics: A list of strings representing the names of the metrics |
|
to be included in the plot. |
|
:param human_baselines: A dictionary where keys are metric names |
|
and values are human baseline values for the metrics. |
|
:param title: A string representing the title of the plot. |
|
:return: A Plotly figure object with lines representing metrics and |
|
horizontal dotted lines representing human baselines. |
|
""" |
|
|
|
|
|
df = df[df["Metric Name"].isin(metrics)] |
|
|
|
|
|
filtered_human_baselines = {k: v for k, v in human_baselines.items() if k in metrics} |
|
|
|
|
|
fig = px.line( |
|
df, |
|
x="Result Date", |
|
y="Metric Value", |
|
color="Metric Name", |
|
markers=True, |
|
custom_data=["Metric Name", "Metric Value", "Model Name"], |
|
title=title, |
|
) |
|
|
|
|
|
fig.update_traces( |
|
hovertemplate="<br>".join( |
|
[ |
|
"Model Name: %{customdata[2]}", |
|
"Metric Name: %{customdata[0]}", |
|
"Date: %{x}", |
|
"Metric Value: %{y}", |
|
] |
|
) |
|
) |
|
|
|
|
|
fig.update_layout(yaxis_range=[0, 100]) |
|
|
|
|
|
metric_color_mapping = {} |
|
|
|
|
|
for trace in fig.data: |
|
metric_color_mapping[trace.name] = trace.line.color |
|
|
|
|
|
for metric, value in filtered_human_baselines.items(): |
|
color = metric_color_mapping.get(metric, "blue") |
|
location = "top left" if metric == "HellaSwag" else "bottom left" |
|
|
|
fig.add_hline( |
|
y=value, |
|
line_dash="dot", |
|
annotation_text=f"{metric} human baseline", |
|
annotation_position=location, |
|
annotation_font_size=10, |
|
annotation_font_color=color, |
|
line_color=color, |
|
) |
|
|
|
return fig |
|
|
|
|
|
|
|
|
|
|