|
| 1 | +from dataclasses import dataclass |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from datasets import Dataset, DatasetDict, IterableDataset, load_dataset |
| 6 | + |
| 7 | +ARENA_HARD_HF_REPO_ID = "lmarena-ai/arena-hard-auto" |
| 8 | + |
| 9 | + |
| 10 | +@dataclass(frozen=True) |
| 11 | +class ArenaHardSpec: |
| 12 | + hf_variant: str |
| 13 | + baseline_model: str |
| 14 | + |
| 15 | + |
| 16 | +ARENA_HARD_DATASETS: dict[str, ArenaHardSpec] = { |
| 17 | + "arena-hard-v0.1": ArenaHardSpec( |
| 18 | + hf_variant="arena-hard-v0.1", |
| 19 | + baseline_model="gpt-4-0314", |
| 20 | + ), |
| 21 | + "arena-hard-v2.0": ArenaHardSpec( |
| 22 | + hf_variant="arena-hard-v2.0", |
| 23 | + baseline_model="o3-mini-2025-01-31", |
| 24 | + ), |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +def resolve_arena_hard_spec(dataset: str) -> ArenaHardSpec | None: |
| 29 | + return ARENA_HARD_DATASETS.get(dataset) |
| 30 | + |
| 31 | + |
| 32 | +def is_arena_hard_dataset(dataset: str) -> bool: |
| 33 | + return resolve_arena_hard_spec(dataset) is not None |
| 34 | + |
| 35 | + |
| 36 | +def arena_hard_baseline_model(dataset: str) -> str | None: |
| 37 | + spec = resolve_arena_hard_spec(dataset) |
| 38 | + if spec is None: |
| 39 | + return None |
| 40 | + return spec.baseline_model |
| 41 | + |
| 42 | + |
| 43 | +def _load_official_arena_hard_dataset(spec: ArenaHardSpec) -> pd.DataFrame: |
| 44 | + data = load_dataset( |
| 45 | + path=ARENA_HARD_HF_REPO_ID, |
| 46 | + data_dir=f"data/{spec.hf_variant}", |
| 47 | + ) |
| 48 | + return _dataset_like_to_dataframe(data) |
| 49 | + |
| 50 | + |
| 51 | +def _dataset_like_to_dataframe( |
| 52 | + data: Dataset | DatasetDict | IterableDataset, |
| 53 | +) -> pd.DataFrame: |
| 54 | + if isinstance(data, DatasetDict): |
| 55 | + if "train" in data: |
| 56 | + return data["train"].to_pandas() |
| 57 | + first_split = next(iter(data.keys())) |
| 58 | + return data[first_split].to_pandas() |
| 59 | + if isinstance(data, Dataset): |
| 60 | + return data.to_pandas() |
| 61 | + if isinstance(data, IterableDataset): |
| 62 | + return pd.DataFrame(list(data)) |
| 63 | + raise TypeError(f"Unsupported dataset object type: {type(data)}") |
| 64 | + |
| 65 | + |
| 66 | +def normalize_official_arena_hard( |
| 67 | + raw_df: pd.DataFrame, dataset: str |
| 68 | +) -> tuple[pd.DataFrame, pd.DataFrame | None]: |
| 69 | + spec = resolve_arena_hard_spec(dataset) |
| 70 | + if spec is None: |
| 71 | + raise ValueError(f"Unsupported Arena-Hard dataset: {dataset}") |
| 72 | + |
| 73 | + instruction_index = _pick_instruction_index(raw_df) |
| 74 | + instruction = _pick_instruction(raw_df) |
| 75 | + df_instructions = pd.DataFrame( |
| 76 | + { |
| 77 | + "instruction_index": instruction_index, |
| 78 | + "instruction": instruction, |
| 79 | + } |
| 80 | + ) |
| 81 | + df_instructions = df_instructions.dropna( |
| 82 | + subset=["instruction_index", "instruction"] |
| 83 | + ) |
| 84 | + df_instructions = df_instructions.drop_duplicates(subset=["instruction_index"]) |
| 85 | + df_instructions = df_instructions.sort_values("instruction_index") |
| 86 | + |
| 87 | + df_model_outputs = _build_model_outputs(raw_df) |
| 88 | + return df_instructions, df_model_outputs |
| 89 | + |
| 90 | + |
| 91 | +def download_arena_hard(dataset: str, local_tables_path: Path) -> None: |
| 92 | + """Load Arena-Hard from the Hub if instruction and model-output files are missing.""" |
| 93 | + spec = resolve_arena_hard_spec(dataset) |
| 94 | + if spec is None: |
| 95 | + return |
| 96 | + instructions_path = local_tables_path / "instructions" / f"{dataset}.csv" |
| 97 | + model_outputs_path = local_tables_path / "model_outputs" / f"{dataset}.csv.zip" |
| 98 | + if instructions_path.exists() and model_outputs_path.exists(): |
| 99 | + return |
| 100 | + |
| 101 | + raw_df = _load_official_arena_hard_dataset(spec) |
| 102 | + df_instructions, df_model_outputs = normalize_official_arena_hard( |
| 103 | + raw_df=raw_df, dataset=dataset |
| 104 | + ) |
| 105 | + instructions_path.parent.mkdir(parents=True, exist_ok=True) |
| 106 | + model_outputs_path.parent.mkdir(parents=True, exist_ok=True) |
| 107 | + df_instructions.to_csv(instructions_path, index=False) |
| 108 | + if df_model_outputs is not None: |
| 109 | + df_model_outputs.to_csv(model_outputs_path, index=False) |
| 110 | + |
| 111 | + |
| 112 | +def _pick_instruction_index(raw_df: pd.DataFrame) -> pd.Series: |
| 113 | + for col in ["instruction_index", "question_id", "id"]: |
| 114 | + if col in raw_df.columns: |
| 115 | + return raw_df[col].astype(str) |
| 116 | + return pd.Series(range(len(raw_df)), dtype=str) |
| 117 | + |
| 118 | + |
| 119 | +def _pick_instruction(raw_df: pd.DataFrame) -> pd.Series: |
| 120 | + for col in ["instruction", "prompt", "question", "turns"]: |
| 121 | + if col in raw_df.columns: |
| 122 | + if col == "turns": |
| 123 | + return raw_df[col].apply(_turns_to_text) |
| 124 | + return raw_df[col].astype(str) |
| 125 | + raise ValueError( |
| 126 | + f"Unable to infer instruction text column from Arena-Hard data. Available columns: {raw_df.columns.tolist()}" |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def _turns_to_text(turns_value) -> str: |
| 131 | + if isinstance(turns_value, list): |
| 132 | + if not turns_value: |
| 133 | + return "" |
| 134 | + first = turns_value[0] |
| 135 | + if isinstance(first, dict): |
| 136 | + for key in ["content", "text", "prompt"]: |
| 137 | + if key in first: |
| 138 | + return str(first[key]) |
| 139 | + return str(first) |
| 140 | + if isinstance(turns_value, dict): |
| 141 | + for key in ["content", "text", "prompt"]: |
| 142 | + if key in turns_value: |
| 143 | + return str(turns_value[key]) |
| 144 | + return str(turns_value) |
| 145 | + |
| 146 | + |
| 147 | +def _build_model_outputs(raw_df: pd.DataFrame) -> pd.DataFrame | None: |
| 148 | + if not {"model", "output"}.issubset(raw_df.columns): |
| 149 | + return None |
| 150 | + instruction_index = _pick_instruction_index(raw_df) |
| 151 | + df_outputs = pd.DataFrame( |
| 152 | + { |
| 153 | + "instruction_index": instruction_index, |
| 154 | + "model": raw_df["model"].astype(str), |
| 155 | + "output": raw_df["output"].fillna("").astype(str), |
| 156 | + } |
| 157 | + ) |
| 158 | + return df_outputs |
0 commit comments