Spaces:
Sleeping
Sleeping
File size: 9,654 Bytes
d4d998a 346c3c5 d4d998a 346c3c5 5565a34 d4d998a 346c3c5 d4d998a 5565a34 346c3c5 5565a34 346c3c5 5565a34 346c3c5 5565a34 346c3c5 5565a34 d4d998a 3c01baa d4d998a 3c01baa d4d998a 3c01baa d4d998a 3c01baa d4d998a 3c01baa d4d998a 3c01baa 346c3c5 d4d998a 935d31d 3c01baa b1cb07d 346c3c5 d4d998a 3c01baa f990f50 3c01baa 346c3c5 3c01baa d4d998a 3c01baa 346c3c5 d4d998a b1cb07d 346c3c5 5565a34 346c3c5 5565a34 346c3c5 5565a34 346c3c5 5565a34 d4d998a 3c01baa d4d998a 3c01baa 346c3c5 3c01baa d4d998a 3c01baa d4d998a 3c01baa d4d998a 3c01baa d4d998a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
"""
Process CodeReview Bench leaderboard data and submissions.
"""
import json
import os
import pandas as pd
from datetime import datetime
from typing import Dict, List, Tuple, Optional
import numpy as np
from src.display.utils import (
CODEREVIEW_COLUMN, DISPLAY_COLS, CATEGORIES, COMMENT_LANGUAGES, EXAMPLE_CATEGORIES,
MULTIMETRIC_METRICS, EXACT_MATCH_METRICS
)
def process_jsonl_submission(file_path: str) -> Tuple[List[Dict], str]:
"""
Process a JSONL submission file for CodeReview Bench.
Args:
file_path: Path to the JSONL submission file
Returns:
Tuple of (entries_list, message)
"""
try:
entries = []
with open(file_path, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
# Validate required fields
required_fields = ['model_name', 'programming_language', 'comment_language']
missing_fields = [field for field in required_fields if field not in entry]
if missing_fields:
return [], f"Missing required fields {missing_fields} in line {line_num}"
# Validate metrics exist
has_multimetric = any(metric in entry for metric in MULTIMETRIC_METRICS)
has_exact_match = any(metric in entry for metric in EXACT_MATCH_METRICS)
if not has_multimetric and not has_exact_match:
return [], f"No valid metrics found in line {line_num}. Required: {MULTIMETRIC_METRICS + EXACT_MATCH_METRICS}"
entries.append(entry)
except json.JSONDecodeError as e:
return [], f"Invalid JSON in line {line_num}: {e}"
if not entries:
return [], "No valid entries found in submission file"
return entries, f"Successfully processed {len(entries)} entries"
except Exception as e:
return [], f"Error processing submission: {e}"
def calculate_overall_score(entry: Dict) -> float:
"""
Calculate overall score for a CodeReview Bench entry.
Args:
entry: Dictionary containing model evaluation results
Returns:
Overall score as float
"""
# Calculate multimetric average
multimetric_scores = []
for metric in MULTIMETRIC_METRICS:
if metric in entry and isinstance(entry[metric], (int, float)):
multimetric_scores.append(entry[metric])
multimetric_avg = np.mean(multimetric_scores) if multimetric_scores else 0
# Calculate exact match average
exact_match_scores = []
for metric in EXACT_MATCH_METRICS:
if metric in entry and isinstance(entry[metric], (int, float)):
exact_match_scores.append(entry[metric])
exact_match_avg = np.mean(exact_match_scores) if exact_match_scores else 0
# Weighted combination (can be adjusted based on requirements)
overall_score = (multimetric_avg * 0.7) + (exact_match_avg * 0.3)
return overall_score
def load_leaderboard_data(file_path: str) -> Dict:
"""
Load the leaderboard data from a JSON file.
"""
if not os.path.exists(file_path):
version = "v0"
if "_v" in file_path:
version = file_path.split("_")[-1].split(".")[0]
return {"entries": [], "last_updated": datetime.now().isoformat(), "version": version}
with open(file_path, 'r') as f:
data = json.load(f)
# Ensure version field exists
if "version" not in data:
version = "v0"
if "_v" in file_path:
version = file_path.split("_")[-1].split(".")[0]
data["version"] = version
return data
def save_leaderboard_data(data: Dict, file_path: str) -> None:
"""
Save the leaderboard data to a JSON file.
"""
# Ensure the directory exists
os.makedirs(os.path.dirname(file_path), exist_ok=True)
# Update the last_updated timestamp
data["last_updated"] = datetime.now().isoformat()
# Ensure version is set
if "version" not in data:
version = "v0"
if "_v" in file_path:
version = file_path.split("_")[-1].split(".")[0]
data["version"] = version
with open(file_path, 'w') as f:
json.dump(data, f, indent=2)
def leaderboard_to_dataframe(leaderboard_data: Dict) -> pd.DataFrame:
"""
Convert leaderboard data to a pandas DataFrame for display.
"""
rows = []
for entry in leaderboard_data.get("entries", []):
model_name = entry.get("model_name", "Unknown Model")
# Extract basic metadata
row = {
"model_name": model_name,
"model_type": entry.get("model_type", "Unknown"),
"mode": entry.get("mode", "Strict"),
"submission_date": entry.get("submission_date", ""),
"version": entry.get("version", "v0"),
"review_model_type": entry.get("review_model_type", "custom").lower()
}
# Add additional metadata fields if present
for key in ["base_model", "revision", "precision", "weight_type", "topic", "programming_language", "comment_language"]:
if key in entry:
row[key] = entry[key]
# Add multimetric scores
for metric in MULTIMETRIC_METRICS:
if metric in entry:
row[metric] = entry[metric]
else:
row[metric] = pd.NA
# Add exact match metrics
for metric in EXACT_MATCH_METRICS:
if metric in entry:
row[metric] = entry[metric]
else:
row[metric] = pd.NA
# Calculate aggregated metrics
multimetric_scores = [entry.get(metric, 0) for metric in MULTIMETRIC_METRICS if metric in entry and pd.notna(entry[metric])]
exact_match_scores = [entry.get(metric, 0) for metric in EXACT_MATCH_METRICS if metric in entry and pd.notna(entry[metric])]
if multimetric_scores:
row["multimetric_average"] = np.mean(multimetric_scores)
else:
row["multimetric_average"] = pd.NA
if exact_match_scores:
row["exact_match_average"] = np.mean(exact_match_scores)
else:
row["exact_match_average"] = pd.NA
# Calculate overall score
row["overall_score"] = calculate_overall_score(entry)
# Add language-specific metrics if available
for lang in COMMENT_LANGUAGES:
for metric in ["readability", "relevance", "overall_score"]:
lang_key = f"{lang}_{metric}"
if lang_key in entry:
row[lang_key] = entry[lang_key]
else:
row[lang_key] = pd.NA
# Add evaluation count
row["total_evaluations"] = entry.get("total_evaluations", entry.get("evaluation_count", pd.NA))
rows.append(row)
# Create DataFrame and sort by overall score
df = pd.DataFrame(rows)
# Ensure all expected columns exist
for metric in MULTIMETRIC_METRICS + EXACT_MATCH_METRICS:
if metric not in df.columns:
df[metric] = pd.NA
# Sort by overall score (descending)
if not df.empty:
df = df.sort_values(by="overall_score", ascending=False, na_position='last')
# Ensure summary columns exist
summary_cols = ["overall_score", "multimetric_average", "exact_match_average", "total_evaluations"]
for col in summary_cols:
if col not in df.columns:
df[col] = pd.NA
return df
def add_entries_to_leaderboard(leaderboard_data: Dict, new_entries: List[Dict]) -> Dict:
"""
Add new entries to the leaderboard, replacing any with the same model name.
"""
# Create a mapping of existing entries by model name and version
existing_entries = {
(entry["model_name"], entry.get("version", "v0")): i
for i, entry in enumerate(leaderboard_data.get("entries", []))
}
# Process each new entry
for new_entry in new_entries:
model_name = new_entry.get("model_name")
version = new_entry.get("version", "v0")
# Add calculated metrics
new_entry["overall_score"] = calculate_overall_score(new_entry)
# Calculate averages
multimetric_scores = [new_entry.get(metric) for metric in MULTIMETRIC_METRICS if metric in new_entry and pd.notna(new_entry[metric])]
exact_match_scores = [new_entry.get(metric) for metric in EXACT_MATCH_METRICS if metric in new_entry and pd.notna(new_entry[metric])]
if multimetric_scores:
new_entry["multimetric_average"] = np.mean(multimetric_scores)
if exact_match_scores:
new_entry["exact_match_average"] = np.mean(exact_match_scores)
if (model_name, version) in existing_entries:
# Replace existing entry
leaderboard_data["entries"][existing_entries[(model_name, version)]] = new_entry
else:
# Add new entry
if "entries" not in leaderboard_data:
leaderboard_data["entries"] = []
leaderboard_data["entries"].append(new_entry)
# Update the last_updated timestamp
leaderboard_data["last_updated"] = datetime.now().isoformat()
return leaderboard_data
|