Spaces:
Running
Running
File size: 10,242 Bytes
eae65f4 0380c4f 5f85cf4 eae65f4 0380c4f 5f85cf4 0380c4f 5f85cf4 0380c4f 5f85cf4 0380c4f 5f85cf4 0380c4f 5f85cf4 eae65f4 0380c4f 5f85cf4 0380c4f 5f85cf4 0380c4f 5f85cf4 eae65f4 0380c4f 5f85cf4 0380c4f 5f85cf4 0380c4f |
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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
import gradio as gr
import pandas as pd
import os
import shutil
import json
# Function to load leaderboard data from a CSV file
def load_leaderboard_data(csv_file_path):
try:
df = pd.read_csv(csv_file_path)
return df
except Exception as e:
print(f"Error loading CSV file: {e}")
return pd.DataFrame() # Return an empty DataFrame in case of error
# Function to process uploaded JSON file
def process_json_file(file):
if file is None:
return None, "Please upload a JSON file."
try:
with open(file.name, 'r') as f:
data = json.load(f)
return data, None
except Exception as e:
return None, f"Error reading JSON file: {str(e)}"
# Function to save the uploaded JSON file
def save_json_file(file):
if file is None:
return "No file uploaded."
# Define the directory to save uploaded files
save_dir = "uploaded_jsons"
os.makedirs(save_dir, exist_ok=True)
# Get the original filename
original_filename = os.path.basename(file.name)
# Define the path to save the file
save_path = os.path.join(save_dir, original_filename)
# Copy the uploaded file to the save directory
shutil.copy2(file.name, save_path)
return f"File saved to {save_path}"
# Load the leaderboard data
leaderboard1 = load_leaderboard_data("leaderboard_swe.csv")
leaderboard2 = load_leaderboard_data("leaderboard_gaia.csv")
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# 🥇 TRAIL: Trace Reasoning and Agentic Issue Localization Leaderboard")
with gr.Row():
with gr.Column():
gr.Markdown("## TRAIL-SWE Leaderboard")
gr.Dataframe(leaderboard1)
with gr.Column():
gr.Markdown("## TRAIL-GAIA Leaderboard")
gr.Dataframe(leaderboard2)
with gr.Blocks() as submit_page:
gr.Markdown("## Submit Your JSON File Here")
file_input = gr.File(label="Upload JSON File", file_types=['.json'])
json_preview = gr.JSON(label="JSON Preview")
submit_button = gr.Button("Submit", interactive=True)
output = gr.Textbox(label="Status")
def handle_submission(file):
if file is None:
return None, "Please upload a JSON file."
try:
# Process and preview the JSON
with open(file.name, 'r') as f:
data = json.load(f)
# Save the file
save_result = save_json_file(file)
return data, save_result
except Exception as e:
return None, f"Error: {str(e)}"
submit_button.click(
fn=handle_submission,
inputs=[file_input],
outputs=[json_preview, output]
)
if __name__ == "__main__":
demo.launch()
"""
import gradio as gr
import pandas as pd
import os
import json
import uuid
import hashlib
from datetime import datetime
from huggingface_hub import HfApi, login, HfFolder
# Configuration
LEADERBOARD_CSV = "leaderboard.csv"
SUBMISSIONS_FOLDER = "submissions"
CONFIG_FILE = "config.json"
DEFAULT_COLUMNS = ["rank", "submission_name", "score", "user", "timestamp"]
VERIFY_USERS = False # Set to True to enable HF authentication
# Default configuration
DEFAULT_CONFIG = {
"title": "Hugging Face Competition Leaderboard",
"description": "Submit your results for the competition",
"metric_name": "Score",
"higher_is_better": True,
"max_submissions_per_user": 5,
"allow_submission_edits": True
}
# Ensure submissions folder exists
os.makedirs(SUBMISSIONS_FOLDER, exist_ok=True)
# Load or create config
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
config = json.load(f)
else:
config = DEFAULT_CONFIG
with open(CONFIG_FILE, "w") as f:
json.dump(config, f, indent=2)
# Initialize leaderboard if it doesn't exist
if not os.path.exists(LEADERBOARD_CSV):
pd.DataFrame(columns=DEFAULT_COLUMNS).to_csv(LEADERBOARD_CSV, index=False)
def read_leaderboard():
#Read the current leaderboard
if os.path.exists(LEADERBOARD_CSV):
df = pd.read_csv(LEADERBOARD_CSV)
return df
return pd.DataFrame(columns=DEFAULT_COLUMNS)
def verify_user(username, token):
#Verify a user with their Hugging Face token
if not VERIFY_USERS:
return True
try:
api = HfApi(token=token)
user_info = api.whoami()
return user_info["name"] == username
except:
return False
def count_user_submissions(username):
#Count how many submissions a user already has
df = read_leaderboard()
return len(df[df["user"] == username])
def update_leaderboard():
#Update the leaderboard based on submissions
# Read all submissions
submissions = []
for filename in os.listdir(SUBMISSIONS_FOLDER):
if filename.endswith(".json"):
with open(os.path.join(SUBMISSIONS_FOLDER, filename), "r") as f:
try:
data = json.load(f)
submissions.append(data)
except json.JSONDecodeError:
print(f"Error decoding {filename}")
if not submissions:
return pd.DataFrame(columns=DEFAULT_COLUMNS)
# Create dataframe and sort by score
df = pd.DataFrame(submissions)
# Sort based on configuration (higher or lower is better)
ascending = not config.get("higher_is_better", True)
df = df.sort_values("score", ascending=ascending)
# Add rank
df["rank"] = range(1, len(df) + 1)
# Save updated leaderboard
df.to_csv(LEADERBOARD_CSV, index=False)
return df
def submit(submission_name, score, username, hf_token="", submission_details=None):
#Add a new submission to the leaderboard
if not submission_name or not username:
return "Submission name and username are required", None
try:
score = float(score)
except ValueError:
return "Score must be a valid number", None
# Verify user if enabled
if VERIFY_USERS and not verify_user(username, hf_token):
return "Invalid Hugging Face credentials", None
# Check submission limit
max_submissions = config.get("max_submissions_per_user", 5)
if count_user_submissions(username) >= max_submissions:
return f"You've reached the maximum of {max_submissions} submissions", None
# Create submission entry
submission_id = str(uuid.uuid4())[:8]
submission = {
"submission_id": submission_id,
"submission_name": submission_name,
"score": score,
"user": username,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# Add optional details
if submission_details:
submission["details"] = submission_details
# Save submission to file
filename = f"{username}_{submission_name.replace(' ', '_')}_{submission_id}.json"
with open(os.path.join(SUBMISSIONS_FOLDER, filename), "w") as f:
json.dump(submission, f)
# Update leaderboard
leaderboard = update_leaderboard()
return f"Submission '{submission_name}' added successfully!", leaderboard
def render_leaderboard():
#Display the current leaderboard
df = update_leaderboard()
if len(df) == 0:
return "No submissions yet."
# Format the dataframe for display
display_df = df[DEFAULT_COLUMNS].copy()
return display_df
# Create the Gradio interface
with gr.Blocks(title=config["title"]) as demo:
gr.Markdown(f"# {config['title']}")
gr.Markdown(f"{config['description']}")
with gr.Tab("Leaderboard"):
gr.Markdown("## Current Rankings")
metric_name = config.get("metric_name", "Score")
higher_better = "higher is better" if config.get("higher_is_better", True) else "lower is better"
gr.Markdown(f"*Ranked by {metric_name} ({higher_better})*")
leaderboard_output = gr.Dataframe(
headers=["Rank", "Submission", metric_name, "User", "Timestamp"],
datatype=["number", "str", "number", "str", "str"],
interactive=False
)
refresh_btn = gr.Button("Refresh Leaderboard")
refresh_btn.click(render_leaderboard, inputs=[], outputs=[leaderboard_output])
with gr.Tab("Submit"):
gr.Markdown("## Submit Your Results")
with gr.Row():
with gr.Column():
submission_name = gr.Textbox(label="Submission Name", placeholder="MyAwesomeModel v1.0")
score = gr.Number(label=metric_name, precision=4)
username = gr.Textbox(label="Username", placeholder="Your Hugging Face username")
# Only show token field if verification is enabled
if VERIFY_USERS:
hf_token = gr.Textbox(
label="Hugging Face Token",
placeholder="hf_...",
type="password"
)
else:
hf_token = gr.Textbox(visible=False)
submission_details = gr.Textbox(
label="Additional Details (optional)",
placeholder="Model details, training info, etc.",
lines=5
)
submit_btn = gr.Button("Submit to Leaderboard")
submit_output = gr.Markdown()
submission_leaderboard = gr.Dataframe(
headers=["Rank", "Submission", metric_name, "User", "Timestamp"],
datatype=["number", "str", "number", "str", "str"],
interactive=False
)
submit_btn.click(
submit,
inputs=[submission_name, score, username, hf_token, submission_details],
outputs=[submit_output, submission_leaderboard]
)
# Add admin tab if desired
with gr.Tab("About"):
gr.Markdown("## About This Leaderboard")
# Initialize the leaderboard on load
demo.load(render_leaderboard, inputs=[], outputs=[leaderboard_output])
if __name__ == "__main__":
demo.launch()
""" |