|
import asyncio |
|
|
|
import gradio as gr |
|
import pandas as pd |
|
from huggingface_hub import HfFileSystem |
|
|
|
from src.constants import SUBTASKS, DETAILS_DATASET_ID, DETAILS_FILENAME, TASK_DESCRIPTIONS |
|
from src.hub import load_details_file |
|
|
|
|
|
def update_task_description_component(task): |
|
return gr.Textbox( |
|
TASK_DESCRIPTIONS.get(task), |
|
label="Task Description", |
|
lines=3, |
|
visible=True, |
|
) |
|
|
|
|
|
def update_subtasks_component(task): |
|
return gr.Radio( |
|
SUBTASKS.get(task), |
|
info="Evaluation subtasks to be loaded", |
|
value=None, |
|
) |
|
|
|
|
|
def update_load_details_component(model_id_1, model_id_2, subtask): |
|
if (model_id_1 or model_id_2) and subtask: |
|
return gr.Button("Load Details", interactive=True) |
|
else: |
|
return gr.Button("Load Details", interactive=False) |
|
|
|
|
|
async def load_details_dataframe(model_id, subtask): |
|
fs = HfFileSystem() |
|
if not model_id or not subtask: |
|
return |
|
model_name_sanitized = model_id.replace("/", "__") |
|
paths = fs.glob( |
|
f"{DETAILS_DATASET_ID}/**/{DETAILS_FILENAME}".format( |
|
model_name_sanitized=model_name_sanitized, subtask=subtask |
|
) |
|
) |
|
if not paths: |
|
return |
|
path = max(paths) |
|
data = await load_details_file(path) |
|
df = pd.json_normalize(data) |
|
|
|
df["model_name"] = model_id |
|
return df |
|
|
|
|
|
|
|
async def load_details_dataframes(subtask, *model_ids): |
|
result = await asyncio.gather(*[load_details_dataframe(model_id, subtask) for model_id in model_ids]) |
|
return result |
|
|
|
|
|
def display_details(sample_idx, *dfs): |
|
rows = [df.iloc[sample_idx] for df in dfs if "model_name" in df.columns and sample_idx < len(df)] |
|
if not rows: |
|
return |
|
|
|
df = pd.concat([row.rename(row.pop("model_name")) for row in rows], axis="columns") |
|
|
|
df = df.apply(lambda x: x.str.wrap(140) if x.dtype == "object" else x) |
|
return ( |
|
df.style |
|
.format(na_rep="") |
|
|
|
.to_html() |
|
) |
|
|
|
|
|
def update_sample_idx_component(*dfs): |
|
maximum = max([len(df) - 1 for df in dfs]) |
|
return gr.Number( |
|
label="Sample Index", |
|
info="Index of the sample to be displayed", |
|
value=0, |
|
minimum=0, |
|
maximum=maximum, |
|
visible=True, |
|
) |
|
|
|
|
|
def clear_details(): |
|
|
|
return ( |
|
None, None, None, None, None, None, |
|
gr.Button("Load Details", interactive=False), |
|
gr.Number(label="Sample Index", info="Index of the sample to be displayed", value=0, minimum=0,visible=False), |
|
) |
|
|