Spaces:
Sleeping
Sleeping
Nicolas Wagner commited on
Commit ·
141f1e0
1
Parent(s): 48c818d
add basis of audio leaderboard
Browse files- app.py +178 -129
- background.png +3 -0
- logo.avif +3 -0
- requirements.txt +1 -0
- src/about.py +63 -57
- src/display/utils.py +18 -89
- src/envs.py +10 -10
- src/evaluation/__init__.py +0 -0
- src/evaluation/compute_metrics.py +35 -0
- src/evaluation/load_labels.py +53 -0
- src/leaderboard/read_team_results.py +52 -0
- src/populate.py +54 -45
- src/submission/submit.py +0 -119
- src/submission/submit_csv.py +148 -0
- src/submission/validate_csv.py +82 -0
- src/teams/__init__.py +0 -0
- src/teams/auth.py +7 -0
- src/teams/register.py +19 -0
- src/teams/storage.py +86 -0
app.py
CHANGED
|
@@ -1,192 +1,241 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
|
| 3 |
import pandas as pd
|
| 4 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
|
| 7 |
from src.about import (
|
| 8 |
CITATION_BUTTON_LABEL,
|
| 9 |
CITATION_BUTTON_TEXT,
|
| 10 |
-
EVALUATION_QUEUE_TEXT,
|
| 11 |
INTRODUCTION_TEXT,
|
| 12 |
LLM_BENCHMARKS_TEXT,
|
| 13 |
TITLE,
|
| 14 |
)
|
| 15 |
from src.display.css_html_js import custom_css
|
|
|
|
| 16 |
from src.display.utils import (
|
| 17 |
-
BENCHMARK_COLS,
|
| 18 |
COLS,
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
ModelType,
|
| 23 |
fields,
|
| 24 |
-
WeightType,
|
| 25 |
-
Precision
|
| 26 |
)
|
| 27 |
-
from src.envs import
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def restart_space():
|
| 33 |
API.restart_space(repo_id=REPO_ID)
|
| 34 |
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
-
print(EVAL_REQUESTS_PATH)
|
| 38 |
snapshot_download(
|
| 39 |
-
repo_id=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
except Exception:
|
| 42 |
-
|
|
|
|
| 43 |
try:
|
| 44 |
-
print(EVAL_RESULTS_PATH)
|
| 45 |
snapshot_download(
|
| 46 |
-
repo_id=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
except Exception:
|
| 49 |
-
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
-
LEADERBOARD_DF = get_leaderboard_df(
|
| 53 |
|
| 54 |
(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
) =
|
|
|
|
| 59 |
|
| 60 |
def init_leaderboard(dataframe):
|
| 61 |
if dataframe is None or dataframe.empty:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return Leaderboard(
|
| 64 |
value=dataframe,
|
| 65 |
-
datatype=[c.type for c in fields(
|
| 66 |
select_columns=SelectColumns(
|
| 67 |
-
default_selection=[c.name for c in fields(
|
| 68 |
-
cant_deselect=[c.name for c in fields(
|
| 69 |
label="Select Columns to Display:",
|
| 70 |
),
|
| 71 |
-
search_columns=[
|
| 72 |
-
hide_columns=[c.name for c in fields(
|
| 73 |
-
filter_columns=[
|
| 74 |
-
ColumnFilter(AutoEvalColumn.model_type.name, type="checkboxgroup", label="Model types"),
|
| 75 |
-
ColumnFilter(AutoEvalColumn.precision.name, type="checkboxgroup", label="Precision"),
|
| 76 |
-
ColumnFilter(
|
| 77 |
-
AutoEvalColumn.params.name,
|
| 78 |
-
type="slider",
|
| 79 |
-
min=0.01,
|
| 80 |
-
max=150,
|
| 81 |
-
label="Select the number of parameters (B)",
|
| 82 |
-
),
|
| 83 |
-
ColumnFilter(
|
| 84 |
-
AutoEvalColumn.still_on_hub.name, type="boolean", label="Deleted/incomplete", default=True
|
| 85 |
-
),
|
| 86 |
-
],
|
| 87 |
-
bool_checkboxgroup_label="Hide models",
|
| 88 |
interactive=False,
|
| 89 |
)
|
| 90 |
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
demo = gr.Blocks(css=custom_css)
|
| 93 |
with demo:
|
| 94 |
gr.HTML(TITLE)
|
| 95 |
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
| 96 |
|
| 97 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
| 98 |
-
with gr.TabItem("🏅
|
| 99 |
leaderboard = init_leaderboard(LEADERBOARD_DF)
|
| 100 |
|
| 101 |
-
with gr.TabItem("📝 About", elem_id="
|
| 102 |
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
| 103 |
|
| 104 |
-
with gr.TabItem("
|
| 105 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
-
gr.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
value=running_eval_queue_df,
|
| 128 |
-
headers=EVAL_COLS,
|
| 129 |
-
datatype=EVAL_TYPES,
|
| 130 |
-
row_count=5,
|
| 131 |
)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
pending_eval_table = gr.components.Dataframe(
|
| 139 |
-
value=pending_eval_queue_df,
|
| 140 |
-
headers=EVAL_COLS,
|
| 141 |
-
datatype=EVAL_TYPES,
|
| 142 |
-
row_count=5,
|
| 143 |
)
|
| 144 |
-
with gr.Row():
|
| 145 |
-
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
|
| 146 |
-
|
| 147 |
-
with gr.Row():
|
| 148 |
-
with gr.Column():
|
| 149 |
-
model_name_textbox = gr.Textbox(label="Model name")
|
| 150 |
-
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
| 151 |
-
model_type = gr.Dropdown(
|
| 152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
| 153 |
-
label="Model type",
|
| 154 |
-
multiselect=False,
|
| 155 |
-
value=None,
|
| 156 |
-
interactive=True,
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
with gr.Column():
|
| 160 |
-
precision = gr.Dropdown(
|
| 161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
| 162 |
-
label="Precision",
|
| 163 |
-
multiselect=False,
|
| 164 |
-
value="float16",
|
| 165 |
-
interactive=True,
|
| 166 |
-
)
|
| 167 |
-
weight_type = gr.Dropdown(
|
| 168 |
-
choices=[i.value.name for i in WeightType],
|
| 169 |
-
label="Weights type",
|
| 170 |
-
multiselect=False,
|
| 171 |
-
value="Original",
|
| 172 |
-
interactive=True,
|
| 173 |
-
)
|
| 174 |
-
base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
|
| 175 |
-
|
| 176 |
-
submit_button = gr.Button("Submit Eval")
|
| 177 |
-
submission_result = gr.Markdown()
|
| 178 |
-
submit_button.click(
|
| 179 |
-
add_new_eval,
|
| 180 |
-
[
|
| 181 |
-
model_name_textbox,
|
| 182 |
-
base_model_name_textbox,
|
| 183 |
-
revision_name_textbox,
|
| 184 |
-
precision,
|
| 185 |
-
weight_type,
|
| 186 |
-
model_type,
|
| 187 |
-
],
|
| 188 |
-
submission_result,
|
| 189 |
-
)
|
| 190 |
|
| 191 |
with gr.Row():
|
| 192 |
with gr.Accordion("📙 Citation", open=False):
|
|
@@ -201,4 +250,4 @@ with demo:
|
|
| 201 |
scheduler = BackgroundScheduler()
|
| 202 |
scheduler.add_job(restart_space, "interval", seconds=1800)
|
| 203 |
scheduler.start()
|
| 204 |
-
demo.queue(default_concurrency_limit=40).launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 4 |
+
from gradio_leaderboard import Leaderboard, SelectColumns
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
|
| 7 |
from src.about import (
|
| 8 |
CITATION_BUTTON_LABEL,
|
| 9 |
CITATION_BUTTON_TEXT,
|
|
|
|
| 10 |
INTRODUCTION_TEXT,
|
| 11 |
LLM_BENCHMARKS_TEXT,
|
| 12 |
TITLE,
|
| 13 |
)
|
| 14 |
from src.display.css_html_js import custom_css
|
| 15 |
+
from src.display.formatting import styled_error, styled_message
|
| 16 |
from src.display.utils import (
|
|
|
|
| 17 |
COLS,
|
| 18 |
+
SUBMISSION_COLS,
|
| 19 |
+
SUBMISSION_TYPES,
|
| 20 |
+
TeamColumn,
|
|
|
|
| 21 |
fields,
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
+
from src.envs import (
|
| 24 |
+
API,
|
| 25 |
+
REPO_ID,
|
| 26 |
+
SUBMISSIONS_PATH,
|
| 27 |
+
SUBMISSIONS_REPO,
|
| 28 |
+
TEAMS_PATH,
|
| 29 |
+
TEAMS_REPO,
|
| 30 |
+
TOKEN,
|
| 31 |
+
)
|
| 32 |
+
from src.evaluation.load_labels import load_true_labels
|
| 33 |
+
from src.populate import get_leaderboard_df, get_submission_queue_df
|
| 34 |
+
from src.submission.submit_csv import submit_csv
|
| 35 |
+
from src.teams.register import create_team
|
| 36 |
|
| 37 |
|
| 38 |
def restart_space():
|
| 39 |
API.restart_space(repo_id=REPO_ID)
|
| 40 |
|
| 41 |
+
|
| 42 |
try:
|
|
|
|
| 43 |
snapshot_download(
|
| 44 |
+
repo_id=TEAMS_REPO,
|
| 45 |
+
local_dir=TEAMS_PATH,
|
| 46 |
+
repo_type="dataset",
|
| 47 |
+
tqdm_class=None,
|
| 48 |
+
etag_timeout=30,
|
| 49 |
+
token=TOKEN,
|
| 50 |
)
|
| 51 |
except Exception:
|
| 52 |
+
print(f"Warning: Could not download teams dataset from {TEAMS_REPO}")
|
| 53 |
+
|
| 54 |
try:
|
|
|
|
| 55 |
snapshot_download(
|
| 56 |
+
repo_id=SUBMISSIONS_REPO,
|
| 57 |
+
local_dir=SUBMISSIONS_PATH,
|
| 58 |
+
repo_type="dataset",
|
| 59 |
+
tqdm_class=None,
|
| 60 |
+
etag_timeout=30,
|
| 61 |
+
token=TOKEN,
|
| 62 |
)
|
| 63 |
except Exception:
|
| 64 |
+
print(f"Warning: Could not download submissions dataset from {SUBMISSIONS_REPO}")
|
| 65 |
|
| 66 |
+
load_true_labels()
|
| 67 |
|
| 68 |
+
LEADERBOARD_DF = get_leaderboard_df(SUBMISSIONS_PATH, COLS)
|
| 69 |
|
| 70 |
(
|
| 71 |
+
accepted_submissions_df,
|
| 72 |
+
rejected_submissions_df,
|
| 73 |
+
all_submissions_df,
|
| 74 |
+
) = get_submission_queue_df(SUBMISSIONS_PATH, SUBMISSION_COLS)
|
| 75 |
+
|
| 76 |
|
| 77 |
def init_leaderboard(dataframe):
|
| 78 |
if dataframe is None or dataframe.empty:
|
| 79 |
+
return Leaderboard(
|
| 80 |
+
value=pd.DataFrame(columns=COLS),
|
| 81 |
+
datatype=[c.type for c in fields(TeamColumn)],
|
| 82 |
+
interactive=False,
|
| 83 |
+
)
|
| 84 |
return Leaderboard(
|
| 85 |
value=dataframe,
|
| 86 |
+
datatype=[c.type for c in fields(TeamColumn)],
|
| 87 |
select_columns=SelectColumns(
|
| 88 |
+
default_selection=[c.name for c in fields(TeamColumn) if c.displayed_by_default],
|
| 89 |
+
cant_deselect=[c.name for c in fields(TeamColumn) if c.never_hidden],
|
| 90 |
label="Select Columns to Display:",
|
| 91 |
),
|
| 92 |
+
search_columns=[TeamColumn.team_name.name],
|
| 93 |
+
hide_columns=[c.name for c in fields(TeamColumn) if c.hidden],
|
| 94 |
+
filter_columns=[],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
interactive=False,
|
| 96 |
)
|
| 97 |
|
| 98 |
|
| 99 |
+
def register_team_ui(team_name: str, num_teammates: int):
|
| 100 |
+
try:
|
| 101 |
+
num_teammates_int = int(num_teammates)
|
| 102 |
+
except (ValueError, TypeError):
|
| 103 |
+
return styled_error("Number of teammates must be a valid integer.")
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
token, team_data = create_team(team_name, num_teammates_int)
|
| 107 |
+
return styled_message(
|
| 108 |
+
f"Team '{team_name}' registered successfully!\n\n"
|
| 109 |
+
f"**IMPORTANT: Save your token now - you won't be able to see it again!**\n\n"
|
| 110 |
+
f"Your team token: `{token}`\n\n"
|
| 111 |
+
f"Use this token to submit your predictions."
|
| 112 |
+
)
|
| 113 |
+
except ValueError as e:
|
| 114 |
+
return styled_error(str(e))
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return styled_error(f"Registration failed: {str(e)}")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def submit_csv_ui(token: str, csv_file):
|
| 120 |
+
if not token or not token.strip():
|
| 121 |
+
return styled_error("Please provide your team token.")
|
| 122 |
+
|
| 123 |
+
if csv_file is None:
|
| 124 |
+
return styled_error("Please upload a CSV file.")
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
with open(csv_file.name, "r") as f:
|
| 128 |
+
csv_content = f.read()
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return styled_error(f"Could not read CSV file: {str(e)}")
|
| 131 |
+
|
| 132 |
+
success, message = submit_csv(token, csv_content)
|
| 133 |
+
|
| 134 |
+
if success:
|
| 135 |
+
return styled_message(message)
|
| 136 |
+
else:
|
| 137 |
+
return styled_error(message)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
demo = gr.Blocks(css=custom_css)
|
| 141 |
with demo:
|
| 142 |
gr.HTML(TITLE)
|
| 143 |
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
| 144 |
|
| 145 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
| 146 |
+
with gr.TabItem("🏅 Leaderboard", elem_id="leaderboard-tab", id=0):
|
| 147 |
leaderboard = init_leaderboard(LEADERBOARD_DF)
|
| 148 |
|
| 149 |
+
with gr.TabItem("📝 About", elem_id="about-tab", id=1):
|
| 150 |
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
| 151 |
|
| 152 |
+
with gr.TabItem("👥 Register Team", elem_id="register-tab", id=2):
|
| 153 |
with gr.Column():
|
| 154 |
+
gr.Markdown("## Create Your Team", elem_classes="markdown-text")
|
| 155 |
+
gr.Markdown(
|
| 156 |
+
"Register your team to participate in the hackathon. "
|
| 157 |
+
"You will receive a token that you'll need to submit predictions.",
|
| 158 |
+
elem_classes="markdown-text",
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
with gr.Row():
|
| 162 |
+
with gr.Column():
|
| 163 |
+
team_name_input = gr.Textbox(
|
| 164 |
+
label="Team Name",
|
| 165 |
+
placeholder="Enter your team name",
|
| 166 |
+
interactive=True,
|
| 167 |
+
)
|
| 168 |
+
num_teammates_input = gr.Number(
|
| 169 |
+
label="Number of Teammates",
|
| 170 |
+
value=1,
|
| 171 |
+
minimum=1,
|
| 172 |
+
maximum=100,
|
| 173 |
+
step=1,
|
| 174 |
+
interactive=True,
|
| 175 |
+
)
|
| 176 |
+
register_button = gr.Button("Register Team", variant="primary")
|
| 177 |
+
registration_result = gr.Markdown()
|
| 178 |
+
|
| 179 |
+
register_button.click(
|
| 180 |
+
register_team_ui,
|
| 181 |
+
[team_name_input, num_teammates_input],
|
| 182 |
+
registration_result,
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
with gr.TabItem("🚀 Submit Predictions", elem_id="submit-tab", id=3):
|
| 186 |
+
with gr.Column():
|
| 187 |
+
gr.Markdown("## Submit Your Predictions", elem_classes="markdown-text")
|
| 188 |
+
gr.Markdown(
|
| 189 |
+
"Upload a CSV file with your predictions. The CSV must have two columns: "
|
| 190 |
+
"`file_name` and `prediction`. Predictions should be binary (0/1 or 'real'/'fake').",
|
| 191 |
+
elem_classes="markdown-text",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column():
|
| 196 |
+
token_input = gr.Textbox(
|
| 197 |
+
label="Team Token",
|
| 198 |
+
placeholder="Enter your team token",
|
| 199 |
+
type="password",
|
| 200 |
+
interactive=True,
|
| 201 |
+
)
|
| 202 |
+
csv_file_input = gr.File(
|
| 203 |
+
label="CSV File",
|
| 204 |
+
file_types=[".csv"],
|
| 205 |
+
interactive=True,
|
| 206 |
+
)
|
| 207 |
+
submit_button = gr.Button("Submit CSV", variant="primary")
|
| 208 |
+
submission_result = gr.Markdown()
|
| 209 |
+
|
| 210 |
+
submit_button.click(
|
| 211 |
+
submit_csv_ui,
|
| 212 |
+
[token_input, csv_file_input],
|
| 213 |
+
submission_result,
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
with gr.Accordion("📊 Submission History", open=False):
|
| 217 |
+
with gr.Tabs():
|
| 218 |
+
with gr.TabItem("✅ Accepted Submissions"):
|
| 219 |
+
accepted_table = gr.components.Dataframe(
|
| 220 |
+
value=accepted_submissions_df,
|
| 221 |
+
headers=SUBMISSION_COLS,
|
| 222 |
+
datatype=SUBMISSION_TYPES,
|
| 223 |
+
row_count=10,
|
| 224 |
)
|
| 225 |
+
with gr.TabItem("❌ Rejected Submissions"):
|
| 226 |
+
rejected_table = gr.components.Dataframe(
|
| 227 |
+
value=rejected_submissions_df,
|
| 228 |
+
headers=SUBMISSION_COLS,
|
| 229 |
+
datatype=SUBMISSION_TYPES,
|
| 230 |
+
row_count=10,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
)
|
| 232 |
+
with gr.TabItem("📋 All Submissions"):
|
| 233 |
+
all_table = gr.components.Dataframe(
|
| 234 |
+
value=all_submissions_df,
|
| 235 |
+
headers=SUBMISSION_COLS,
|
| 236 |
+
datatype=SUBMISSION_TYPES,
|
| 237 |
+
row_count=10,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
with gr.Row():
|
| 241 |
with gr.Accordion("📙 Citation", open=False):
|
|
|
|
| 250 |
scheduler = BackgroundScheduler()
|
| 251 |
scheduler.add_job(restart_space, "interval", seconds=1800)
|
| 252 |
scheduler.start()
|
| 253 |
+
demo.queue(default_concurrency_limit=40).launch()
|
background.png
ADDED
|
Git LFS Details
|
logo.avif
ADDED
|
Git LFS Details
|
requirements.txt
CHANGED
|
@@ -10,6 +10,7 @@ matplotlib
|
|
| 10 |
numpy
|
| 11 |
pandas
|
| 12 |
python-dateutil
|
|
|
|
| 13 |
tqdm
|
| 14 |
transformers
|
| 15 |
tokenizers>=0.15.0
|
|
|
|
| 10 |
numpy
|
| 11 |
pandas
|
| 12 |
python-dateutil
|
| 13 |
+
scikit-learn
|
| 14 |
tqdm
|
| 15 |
transformers
|
| 16 |
tokenizers>=0.15.0
|
src/about.py
CHANGED
|
@@ -1,70 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
from enum import Enum
|
| 3 |
|
| 4 |
-
@dataclass
|
| 5 |
-
class Task:
|
| 6 |
-
benchmark: str
|
| 7 |
-
metric: str
|
| 8 |
-
col_name: str
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Select your tasks here
|
| 12 |
-
# ---------------------------------------------------
|
| 13 |
-
class Tasks(Enum):
|
| 14 |
-
# task_key in the json file, metric_key in the json file, name to display in the leaderboard
|
| 15 |
-
task0 = Task("anli_r1", "acc", "ANLI")
|
| 16 |
-
task1 = Task("logiqa", "acc_norm", "LogiQA")
|
| 17 |
-
|
| 18 |
-
NUM_FEWSHOT = 0 # Change with your few shot
|
| 19 |
-
# ---------------------------------------------------
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Your leaderboard name
|
| 24 |
-
TITLE = """<h1 align="center" id="space-title">Demo leaderboard</h1>"""
|
| 25 |
-
|
| 26 |
-
# What does your leaderboard evaluate?
|
| 27 |
INTRODUCTION_TEXT = """
|
| 28 |
-
|
| 29 |
"""
|
| 30 |
|
| 31 |
-
|
| 32 |
-
LLM_BENCHMARKS_TEXT = f"""
|
| 33 |
## How it works
|
| 34 |
|
| 35 |
-
##
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
EVALUATION_QUEUE_TEXT = """
|
| 41 |
-
##
|
| 42 |
-
|
| 43 |
-
###
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
This is a leaderboard for Open LLMs, and we'd love for as many people as possible to know they can use your model 🤗
|
| 60 |
-
|
| 61 |
-
### 4) Fill up your model card
|
| 62 |
-
When we add extra information about models to the leaderboard, it will be automatically taken from the model card
|
| 63 |
-
|
| 64 |
-
## In case of model failure
|
| 65 |
-
If your model is displayed in the `FAILED` category, its execution stopped.
|
| 66 |
-
Make sure you have followed the above steps first.
|
| 67 |
-
If everything is done, check you can launch the EleutherAIHarness on your model locally, using the above command without modifications (you can add `--limit` to limit the number of examples per task).
|
| 68 |
"""
|
| 69 |
|
| 70 |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
|
|
|
| 1 |
+
TITLE = """<h1 align="center" id="space-title">Truth vs. Machine Hackathon Leaderboard</h1>"""
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
INTRODUCTION_TEXT = """
|
| 4 |
+
Welcome to the Truth vs. Machine Hackathon Leaderboard! This leaderboard tracks teams competing in an audio deepfake detection challenge. Teams submit predictions on audio samples to determine whether they are real or fake, and the leaderboard displays the best performance metrics for each team.
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
LLM_BENCHMARKS_TEXT = """
|
|
|
|
| 8 |
## How it works
|
| 9 |
|
| 10 |
+
### 1. Register Your Team
|
| 11 |
+
- Go to the "Register Team" tab
|
| 12 |
+
- Enter your team name and number of teammates
|
| 13 |
+
- **Save your token immediately** - you'll need it to submit predictions
|
| 14 |
+
- You won't be able to see your token again after registration
|
| 15 |
+
|
| 16 |
+
### 2. Prepare Your Predictions
|
| 17 |
+
Create a CSV file with two columns:
|
| 18 |
+
- `file_name`: The name of the audio file (must match the test set)
|
| 19 |
+
- `prediction`: Your prediction (binary: 0/1, or "real"/"fake")
|
| 20 |
+
|
| 21 |
+
Example CSV format:
|
| 22 |
+
```csv
|
| 23 |
+
file_name,prediction
|
| 24 |
+
audio_001.wav,0
|
| 25 |
+
audio_002.wav,1
|
| 26 |
+
audio_003.wav,real
|
| 27 |
+
audio_004.wav,fake
|
| 28 |
+
```
|
| 29 |
|
| 30 |
+
### 3. Submit Your Predictions
|
| 31 |
+
- Go to the "Submit Predictions" tab
|
| 32 |
+
- Enter your team token
|
| 33 |
+
- Upload your CSV file
|
| 34 |
+
- Your submission will be automatically evaluated
|
| 35 |
+
|
| 36 |
+
### 4. Evaluation Metrics
|
| 37 |
+
Your predictions are evaluated on:
|
| 38 |
+
- **Accuracy**: Percentage of correct predictions
|
| 39 |
+
- **F1 Score**: Harmonic mean of precision and recall
|
| 40 |
+
- **Error Rate**: Percentage of incorrect predictions
|
| 41 |
+
|
| 42 |
+
### 5. Leaderboard Updates
|
| 43 |
+
- Only your **best** scores are displayed on the leaderboard
|
| 44 |
+
- A submission is accepted only if it improves at least one metric
|
| 45 |
+
- The leaderboard is sorted by best accuracy (primary metric)
|
| 46 |
+
- If accuracy is tied, F1 score is used as a tiebreaker
|
| 47 |
+
|
| 48 |
+
## Important Notes
|
| 49 |
+
- True labels are kept private and not accessible to participants
|
| 50 |
+
- You can submit multiple times - only your best scores count
|
| 51 |
+
- Make sure your CSV file format is correct before submitting
|
| 52 |
+
- File names in your CSV must exactly match the test set file names
|
| 53 |
"""
|
| 54 |
|
| 55 |
EVALUATION_QUEUE_TEXT = """
|
| 56 |
+
## Submission Guidelines
|
| 57 |
+
|
| 58 |
+
### CSV File Requirements
|
| 59 |
+
- Must contain exactly two columns: `file_name` and `prediction`
|
| 60 |
+
- `file_name` must match the test set file names exactly
|
| 61 |
+
- `prediction` must be binary: 0/1 or "real"/"fake"
|
| 62 |
+
- No missing values allowed
|
| 63 |
+
|
| 64 |
+
### Prediction Format
|
| 65 |
+
Accepted formats for predictions:
|
| 66 |
+
- Numeric: `0` (real) or `1` (fake)
|
| 67 |
+
- String: `"real"` or `"fake"` (case-insensitive)
|
| 68 |
+
|
| 69 |
+
### Scoring
|
| 70 |
+
- Submissions are evaluated immediately upon upload
|
| 71 |
+
- Scores are computed using accuracy, F1 score, and error rate
|
| 72 |
+
- Only submissions that improve your best scores are accepted
|
| 73 |
+
- Rejected submissions are logged but don't update the leaderboard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
"""
|
| 75 |
|
| 76 |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
src/display/utils.py
CHANGED
|
@@ -1,17 +1,10 @@
|
|
| 1 |
-
from dataclasses import dataclass
|
| 2 |
-
from enum import Enum
|
| 3 |
|
| 4 |
-
import pandas as pd
|
| 5 |
-
|
| 6 |
-
from src.about import Tasks
|
| 7 |
|
| 8 |
def fields(raw_class):
|
| 9 |
return [v for k, v in raw_class.__dict__.items() if k[:2] != "__" and k[-2:] != "__"]
|
| 10 |
|
| 11 |
|
| 12 |
-
# These classes are for user facing column names,
|
| 13 |
-
# to avoid having to change them all around the code
|
| 14 |
-
# when a modif is needed
|
| 15 |
@dataclass
|
| 16 |
class ColumnContent:
|
| 17 |
name: str
|
|
@@ -20,91 +13,27 @@ class ColumnContent:
|
|
| 20 |
hidden: bool = False
|
| 21 |
never_hidden: bool = False
|
| 22 |
|
| 23 |
-
## Leaderboard columns
|
| 24 |
-
auto_eval_column_dict = []
|
| 25 |
-
# Init
|
| 26 |
-
auto_eval_column_dict.append(["model_type_symbol", ColumnContent, ColumnContent("T", "str", True, never_hidden=True)])
|
| 27 |
-
auto_eval_column_dict.append(["model", ColumnContent, ColumnContent("Model", "markdown", True, never_hidden=True)])
|
| 28 |
-
#Scores
|
| 29 |
-
auto_eval_column_dict.append(["average", ColumnContent, ColumnContent("Average ⬆️", "number", True)])
|
| 30 |
-
for task in Tasks:
|
| 31 |
-
auto_eval_column_dict.append([task.name, ColumnContent, ColumnContent(task.value.col_name, "number", True)])
|
| 32 |
-
# Model information
|
| 33 |
-
auto_eval_column_dict.append(["model_type", ColumnContent, ColumnContent("Type", "str", False)])
|
| 34 |
-
auto_eval_column_dict.append(["architecture", ColumnContent, ColumnContent("Architecture", "str", False)])
|
| 35 |
-
auto_eval_column_dict.append(["weight_type", ColumnContent, ColumnContent("Weight type", "str", False, True)])
|
| 36 |
-
auto_eval_column_dict.append(["precision", ColumnContent, ColumnContent("Precision", "str", False)])
|
| 37 |
-
auto_eval_column_dict.append(["license", ColumnContent, ColumnContent("Hub License", "str", False)])
|
| 38 |
-
auto_eval_column_dict.append(["params", ColumnContent, ColumnContent("#Params (B)", "number", False)])
|
| 39 |
-
auto_eval_column_dict.append(["likes", ColumnContent, ColumnContent("Hub ❤️", "number", False)])
|
| 40 |
-
auto_eval_column_dict.append(["still_on_hub", ColumnContent, ColumnContent("Available on the hub", "bool", False)])
|
| 41 |
-
auto_eval_column_dict.append(["revision", ColumnContent, ColumnContent("Model sha", "str", False, False)])
|
| 42 |
-
|
| 43 |
-
# We use make dataclass to dynamically fill the scores from Tasks
|
| 44 |
-
AutoEvalColumn = make_dataclass("AutoEvalColumn", auto_eval_column_dict, frozen=True)
|
| 45 |
|
| 46 |
-
## For the queue columns in the submission tab
|
| 47 |
@dataclass(frozen=True)
|
| 48 |
-
class
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
status = ColumnContent("status", "str", True)
|
| 55 |
-
|
| 56 |
-
## All the model information that we might need
|
| 57 |
-
@dataclass
|
| 58 |
-
class ModelDetails:
|
| 59 |
-
name: str
|
| 60 |
-
display_name: str = ""
|
| 61 |
-
symbol: str = "" # emoji
|
| 62 |
-
|
| 63 |
|
| 64 |
-
class ModelType(Enum):
|
| 65 |
-
PT = ModelDetails(name="pretrained", symbol="🟢")
|
| 66 |
-
FT = ModelDetails(name="fine-tuned", symbol="🔶")
|
| 67 |
-
IFT = ModelDetails(name="instruction-tuned", symbol="⭕")
|
| 68 |
-
RL = ModelDetails(name="RL-tuned", symbol="🟦")
|
| 69 |
-
Unknown = ModelDetails(name="", symbol="?")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return ModelType.PT
|
| 80 |
-
if "RL-tuned" in type or "🟦" in type:
|
| 81 |
-
return ModelType.RL
|
| 82 |
-
if "instruction-tuned" in type or "⭕" in type:
|
| 83 |
-
return ModelType.IFT
|
| 84 |
-
return ModelType.Unknown
|
| 85 |
-
|
| 86 |
-
class WeightType(Enum):
|
| 87 |
-
Adapter = ModelDetails("Adapter")
|
| 88 |
-
Original = ModelDetails("Original")
|
| 89 |
-
Delta = ModelDetails("Delta")
|
| 90 |
-
|
| 91 |
-
class Precision(Enum):
|
| 92 |
-
float16 = ModelDetails("float16")
|
| 93 |
-
bfloat16 = ModelDetails("bfloat16")
|
| 94 |
-
Unknown = ModelDetails("?")
|
| 95 |
-
|
| 96 |
-
def from_str(precision):
|
| 97 |
-
if precision in ["torch.float16", "float16"]:
|
| 98 |
-
return Precision.float16
|
| 99 |
-
if precision in ["torch.bfloat16", "bfloat16"]:
|
| 100 |
-
return Precision.bfloat16
|
| 101 |
-
return Precision.Unknown
|
| 102 |
-
|
| 103 |
-
# Column selection
|
| 104 |
-
COLS = [c.name for c in fields(AutoEvalColumn) if not c.hidden]
|
| 105 |
|
| 106 |
-
EVAL_COLS = [c.name for c in fields(EvalQueueColumn)]
|
| 107 |
-
EVAL_TYPES = [c.type for c in fields(EvalQueueColumn)]
|
| 108 |
|
| 109 |
-
|
| 110 |
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def fields(raw_class):
|
| 5 |
return [v for k, v in raw_class.__dict__.items() if k[:2] != "__" and k[-2:] != "__"]
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
@dataclass
|
| 9 |
class ColumnContent:
|
| 10 |
name: str
|
|
|
|
| 13 |
hidden: bool = False
|
| 14 |
never_hidden: bool = False
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
@dataclass(frozen=True)
|
| 18 |
+
class TeamColumn:
|
| 19 |
+
team_name = ColumnContent("Team Name", "str", True, never_hidden=True)
|
| 20 |
+
best_accuracy = ColumnContent("Best Accuracy ⬆️", "number", True)
|
| 21 |
+
best_f1 = ColumnContent("Best F1 Score", "number", True)
|
| 22 |
+
best_error_rate = ColumnContent("Best Error Rate", "number", True)
|
| 23 |
+
last_submission_date = ColumnContent("Last Submission", "str", True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
@dataclass(frozen=True)
|
| 27 |
+
class SubmissionQueueColumn:
|
| 28 |
+
team_name = ColumnContent("Team Name", "str", True)
|
| 29 |
+
submission_date = ColumnContent("Submission Date", "str", True)
|
| 30 |
+
accuracy = ColumnContent("Accuracy", "number", True)
|
| 31 |
+
f1 = ColumnContent("F1 Score", "number", True)
|
| 32 |
+
error_rate = ColumnContent("Error Rate", "number", True)
|
| 33 |
+
status = ColumnContent("Status", "str", True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
COLS = [c.name for c in fields(TeamColumn) if not c.hidden]
|
| 37 |
|
| 38 |
+
SUBMISSION_COLS = [c.name for c in fields(SubmissionQueueColumn)]
|
| 39 |
+
SUBMISSION_TYPES = [c.type for c in fields(SubmissionQueueColumn)]
|
src/envs.py
CHANGED
|
@@ -4,22 +4,22 @@ from huggingface_hub import HfApi
|
|
| 4 |
|
| 5 |
# Info to change for your repository
|
| 6 |
# ----------------------------------
|
| 7 |
-
TOKEN = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
-
OWNER = "demo-leaderboard-backend"
|
| 10 |
# ----------------------------------
|
| 11 |
|
| 12 |
-
REPO_ID = f"{OWNER}/
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
# If you setup a cache later, just change HF_HOME
|
| 17 |
-
CACHE_PATH=os.getenv("HF_HOME", ".")
|
| 18 |
|
| 19 |
# Local caches
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
EVAL_RESULTS_PATH_BACKEND = os.path.join(CACHE_PATH, "eval-results-bk")
|
| 24 |
|
| 25 |
API = HfApi(token=TOKEN)
|
|
|
|
| 4 |
|
| 5 |
# Info to change for your repository
|
| 6 |
# ----------------------------------
|
| 7 |
+
TOKEN = os.environ.get("HF_TOKEN") # A read/write token for your org
|
| 8 |
|
| 9 |
+
OWNER = "demo-leaderboard-backend" # Change to your org - don't forget to create a results and request dataset, with the correct format!
|
| 10 |
# ----------------------------------
|
| 11 |
|
| 12 |
+
REPO_ID = f"{OWNER}/Hackathon_Truth_Vs_Machine"
|
| 13 |
+
TEAMS_REPO = f"{OWNER}/TVM_teams"
|
| 14 |
+
SUBMISSIONS_REPO = f"{OWNER}/TVM_submissions"
|
| 15 |
+
TRUE_LABELS_REPO = os.environ.get("TRUE_LABELS_REPO", f"{OWNER}/TVM_true-labels")
|
| 16 |
|
| 17 |
# If you setup a cache later, just change HF_HOME
|
| 18 |
+
CACHE_PATH = os.getenv("HF_HOME", ".")
|
| 19 |
|
| 20 |
# Local caches
|
| 21 |
+
TEAMS_PATH = os.path.join(CACHE_PATH, "teams")
|
| 22 |
+
SUBMISSIONS_PATH = os.path.join(CACHE_PATH, "submissions")
|
| 23 |
+
TRUE_LABELS_PATH = os.path.join(CACHE_PATH, "true-labels")
|
|
|
|
| 24 |
|
| 25 |
API = HfApi(token=TOKEN)
|
src/evaluation/__init__.py
ADDED
|
File without changes
|
src/evaluation/compute_metrics.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.metrics import accuracy_score, f1_score
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def compute_metrics(predictions_df: pd.DataFrame, true_labels: dict[str, int]) -> dict[str, float]:
|
| 6 |
+
y_true = []
|
| 7 |
+
y_pred = []
|
| 8 |
+
|
| 9 |
+
for _, row in predictions_df.iterrows():
|
| 10 |
+
file_name = str(row["file_name"]).strip()
|
| 11 |
+
if file_name not in true_labels:
|
| 12 |
+
continue
|
| 13 |
+
|
| 14 |
+
true_label = true_labels[file_name]
|
| 15 |
+
pred_label = int(row["prediction"])
|
| 16 |
+
|
| 17 |
+
y_true.append(true_label)
|
| 18 |
+
y_pred.append(pred_label)
|
| 19 |
+
|
| 20 |
+
if len(y_true) == 0:
|
| 21 |
+
return {
|
| 22 |
+
"accuracy": 0.0,
|
| 23 |
+
"f1": 0.0,
|
| 24 |
+
"error_rate": 1.0,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
accuracy = accuracy_score(y_true, y_pred)
|
| 28 |
+
f1 = f1_score(y_true, y_pred, zero_division=0.0)
|
| 29 |
+
error_rate = 1.0 - accuracy
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"accuracy": float(accuracy),
|
| 33 |
+
"f1": float(f1),
|
| 34 |
+
"error_rate": float(error_rate),
|
| 35 |
+
}
|
src/evaluation/load_labels.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
|
| 6 |
+
from src.envs import TOKEN, TRUE_LABELS_PATH, TRUE_LABELS_REPO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def load_true_labels() -> dict[str, int]:
|
| 10 |
+
os.makedirs(TRUE_LABELS_PATH, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
snapshot_download(
|
| 14 |
+
repo_id=TRUE_LABELS_REPO,
|
| 15 |
+
local_dir=TRUE_LABELS_PATH,
|
| 16 |
+
repo_type="dataset",
|
| 17 |
+
tqdm_class=None,
|
| 18 |
+
etag_timeout=30,
|
| 19 |
+
token=TOKEN,
|
| 20 |
+
)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Warning: Could not download true labels: {e}")
|
| 23 |
+
return {}
|
| 24 |
+
|
| 25 |
+
labels = {}
|
| 26 |
+
|
| 27 |
+
for root, _, files in os.walk(TRUE_LABELS_PATH):
|
| 28 |
+
for file in files:
|
| 29 |
+
if file.endswith(".json"):
|
| 30 |
+
filepath = os.path.join(root, file)
|
| 31 |
+
try:
|
| 32 |
+
with open(filepath, "r") as f:
|
| 33 |
+
data = json.load(f)
|
| 34 |
+
if isinstance(data, dict):
|
| 35 |
+
labels.update(data)
|
| 36 |
+
elif isinstance(data, list):
|
| 37 |
+
for item in data:
|
| 38 |
+
if isinstance(item, dict) and "file_name" in item and "label" in item:
|
| 39 |
+
labels[item["file_name"]] = item["label"]
|
| 40 |
+
except Exception:
|
| 41 |
+
continue
|
| 42 |
+
elif file.endswith(".csv"):
|
| 43 |
+
import pandas as pd
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
df = pd.read_csv(os.path.join(root, file))
|
| 47 |
+
if "file_name" in df.columns and "label" in df.columns:
|
| 48 |
+
for _, row in df.iterrows():
|
| 49 |
+
labels[str(row["file_name"])] = int(row["label"])
|
| 50 |
+
except Exception:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
return labels
|
src/leaderboard/read_team_results.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
|
| 5 |
+
from src.display.utils import TeamColumn
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@dataclass
|
| 9 |
+
class TeamResult:
|
| 10 |
+
team_name: str
|
| 11 |
+
best_accuracy: float
|
| 12 |
+
best_f1: float
|
| 13 |
+
best_error_rate: float
|
| 14 |
+
last_submission_date: str
|
| 15 |
+
|
| 16 |
+
def to_dict(self):
|
| 17 |
+
return {
|
| 18 |
+
TeamColumn.team_name.name: self.team_name,
|
| 19 |
+
TeamColumn.best_accuracy.name: self.best_accuracy,
|
| 20 |
+
TeamColumn.best_f1.name: self.best_f1,
|
| 21 |
+
TeamColumn.best_error_rate.name: self.best_error_rate,
|
| 22 |
+
TeamColumn.last_submission_date.name: self.last_submission_date,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_team_results(results_path: str) -> list[TeamResult]:
|
| 27 |
+
results = []
|
| 28 |
+
results_dir = os.path.join(results_path, "results")
|
| 29 |
+
|
| 30 |
+
if not os.path.exists(results_dir):
|
| 31 |
+
return results
|
| 32 |
+
|
| 33 |
+
for filename in os.listdir(results_dir):
|
| 34 |
+
if not filename.endswith(".json"):
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
filepath = os.path.join(results_dir, filename)
|
| 38 |
+
try:
|
| 39 |
+
with open(filepath, "r") as f:
|
| 40 |
+
data = json.load(f)
|
| 41 |
+
result = TeamResult(
|
| 42 |
+
team_name=data.get("team_name", ""),
|
| 43 |
+
best_accuracy=data.get("best_accuracy", 0.0),
|
| 44 |
+
best_f1=data.get("best_f1", 0.0),
|
| 45 |
+
best_error_rate=data.get("best_error_rate", 1.0),
|
| 46 |
+
last_submission_date=data.get("last_submission_date", ""),
|
| 47 |
+
)
|
| 48 |
+
results.append(result)
|
| 49 |
+
except Exception:
|
| 50 |
+
continue
|
| 51 |
+
|
| 52 |
+
return results
|
src/populate.py
CHANGED
|
@@ -3,56 +3,65 @@ import os
|
|
| 3 |
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
from src.display.
|
| 7 |
-
from src.
|
| 8 |
-
from src.leaderboard.read_evals import get_raw_eval_results
|
| 9 |
|
| 10 |
|
| 11 |
-
def get_leaderboard_df(results_path: str,
|
| 12 |
-
|
| 13 |
-
raw_data = get_raw_eval_results(results_path, requests_path)
|
| 14 |
all_data_json = [v.to_dict() for v in raw_data]
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
df = df[cols].round(decimals=2)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
df = df
|
|
|
|
| 22 |
return df
|
| 23 |
|
| 24 |
|
| 25 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from src.display.utils import SubmissionQueueColumn, TeamColumn
|
| 7 |
+
from src.leaderboard.read_team_results import get_team_results
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
+
def get_leaderboard_df(results_path: str, cols: list) -> pd.DataFrame:
|
| 11 |
+
raw_data = get_team_results(results_path)
|
|
|
|
| 12 |
all_data_json = [v.to_dict() for v in raw_data]
|
| 13 |
|
| 14 |
+
if not all_data_json:
|
| 15 |
+
return pd.DataFrame(columns=cols)
|
|
|
|
| 16 |
|
| 17 |
+
df = pd.DataFrame.from_records(all_data_json)
|
| 18 |
+
df = df.sort_values(by=[TeamColumn.best_accuracy.name], ascending=False)
|
| 19 |
+
df = df[cols].round(decimals=4)
|
| 20 |
return df
|
| 21 |
|
| 22 |
|
| 23 |
+
def get_submission_queue_df(save_path: str, cols: list) -> list[pd.DataFrame]:
|
| 24 |
+
all_submissions = []
|
| 25 |
+
|
| 26 |
+
if not os.path.exists(save_path):
|
| 27 |
+
empty_df = pd.DataFrame(columns=cols)
|
| 28 |
+
return empty_df, empty_df, empty_df
|
| 29 |
+
|
| 30 |
+
for filename in os.listdir(save_path):
|
| 31 |
+
if not filename.endswith(".json"):
|
| 32 |
+
continue
|
| 33 |
+
filepath = os.path.join(save_path, filename)
|
| 34 |
+
if not os.path.isfile(filepath):
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
filepath = os.path.join(save_path, filename)
|
| 38 |
+
try:
|
| 39 |
+
with open(filepath, "r") as f:
|
| 40 |
+
data = json.load(f)
|
| 41 |
+
|
| 42 |
+
submission_data = {
|
| 43 |
+
SubmissionQueueColumn.team_name.name: data.get("team_name", ""),
|
| 44 |
+
SubmissionQueueColumn.submission_date.name: data.get("timestamp", ""),
|
| 45 |
+
SubmissionQueueColumn.accuracy.name: data.get("scores", {}).get("accuracy", 0.0),
|
| 46 |
+
SubmissionQueueColumn.f1.name: data.get("scores", {}).get("f1", 0.0),
|
| 47 |
+
SubmissionQueueColumn.error_rate.name: data.get("scores", {}).get("error_rate", 1.0),
|
| 48 |
+
SubmissionQueueColumn.status.name: data.get("status", "UNKNOWN"),
|
| 49 |
+
}
|
| 50 |
+
all_submissions.append(submission_data)
|
| 51 |
+
except Exception:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
accepted_list = [s for s in all_submissions if s[SubmissionQueueColumn.status.name] == "ACCEPTED"]
|
| 55 |
+
rejected_list = [s for s in all_submissions if s[SubmissionQueueColumn.status.name] == "REJECTED"]
|
| 56 |
+
|
| 57 |
+
df_accepted = (
|
| 58 |
+
pd.DataFrame.from_records(accepted_list, columns=cols) if accepted_list else pd.DataFrame(columns=cols)
|
| 59 |
+
)
|
| 60 |
+
df_rejected = (
|
| 61 |
+
pd.DataFrame.from_records(rejected_list, columns=cols) if rejected_list else pd.DataFrame(columns=cols)
|
| 62 |
+
)
|
| 63 |
+
df_all = (
|
| 64 |
+
pd.DataFrame.from_records(all_submissions, columns=cols) if all_submissions else pd.DataFrame(columns=cols)
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
return df_accepted, df_rejected, df_all
|
src/submission/submit.py
DELETED
|
@@ -1,119 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
-
|
| 5 |
-
from src.display.formatting import styled_error, styled_message, styled_warning
|
| 6 |
-
from src.envs import API, EVAL_REQUESTS_PATH, TOKEN, QUEUE_REPO
|
| 7 |
-
from src.submission.check_validity import (
|
| 8 |
-
already_submitted_models,
|
| 9 |
-
check_model_card,
|
| 10 |
-
get_model_size,
|
| 11 |
-
is_model_on_hub,
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
REQUESTED_MODELS = None
|
| 15 |
-
USERS_TO_SUBMISSION_DATES = None
|
| 16 |
-
|
| 17 |
-
def add_new_eval(
|
| 18 |
-
model: str,
|
| 19 |
-
base_model: str,
|
| 20 |
-
revision: str,
|
| 21 |
-
precision: str,
|
| 22 |
-
weight_type: str,
|
| 23 |
-
model_type: str,
|
| 24 |
-
):
|
| 25 |
-
global REQUESTED_MODELS
|
| 26 |
-
global USERS_TO_SUBMISSION_DATES
|
| 27 |
-
if not REQUESTED_MODELS:
|
| 28 |
-
REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH)
|
| 29 |
-
|
| 30 |
-
user_name = ""
|
| 31 |
-
model_path = model
|
| 32 |
-
if "/" in model:
|
| 33 |
-
user_name = model.split("/")[0]
|
| 34 |
-
model_path = model.split("/")[1]
|
| 35 |
-
|
| 36 |
-
precision = precision.split(" ")[0]
|
| 37 |
-
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 38 |
-
|
| 39 |
-
if model_type is None or model_type == "":
|
| 40 |
-
return styled_error("Please select a model type.")
|
| 41 |
-
|
| 42 |
-
# Does the model actually exist?
|
| 43 |
-
if revision == "":
|
| 44 |
-
revision = "main"
|
| 45 |
-
|
| 46 |
-
# Is the model on the hub?
|
| 47 |
-
if weight_type in ["Delta", "Adapter"]:
|
| 48 |
-
base_model_on_hub, error, _ = is_model_on_hub(model_name=base_model, revision=revision, token=TOKEN, test_tokenizer=True)
|
| 49 |
-
if not base_model_on_hub:
|
| 50 |
-
return styled_error(f'Base model "{base_model}" {error}')
|
| 51 |
-
|
| 52 |
-
if not weight_type == "Adapter":
|
| 53 |
-
model_on_hub, error, _ = is_model_on_hub(model_name=model, revision=revision, token=TOKEN, test_tokenizer=True)
|
| 54 |
-
if not model_on_hub:
|
| 55 |
-
return styled_error(f'Model "{model}" {error}')
|
| 56 |
-
|
| 57 |
-
# Is the model info correctly filled?
|
| 58 |
-
try:
|
| 59 |
-
model_info = API.model_info(repo_id=model, revision=revision)
|
| 60 |
-
except Exception:
|
| 61 |
-
return styled_error("Could not get your model information. Please fill it up properly.")
|
| 62 |
-
|
| 63 |
-
model_size = get_model_size(model_info=model_info, precision=precision)
|
| 64 |
-
|
| 65 |
-
# Were the model card and license filled?
|
| 66 |
-
try:
|
| 67 |
-
license = model_info.cardData["license"]
|
| 68 |
-
except Exception:
|
| 69 |
-
return styled_error("Please select a license for your model")
|
| 70 |
-
|
| 71 |
-
modelcard_OK, error_msg = check_model_card(model)
|
| 72 |
-
if not modelcard_OK:
|
| 73 |
-
return styled_error(error_msg)
|
| 74 |
-
|
| 75 |
-
# Seems good, creating the eval
|
| 76 |
-
print("Adding new eval")
|
| 77 |
-
|
| 78 |
-
eval_entry = {
|
| 79 |
-
"model": model,
|
| 80 |
-
"base_model": base_model,
|
| 81 |
-
"revision": revision,
|
| 82 |
-
"precision": precision,
|
| 83 |
-
"weight_type": weight_type,
|
| 84 |
-
"status": "PENDING",
|
| 85 |
-
"submitted_time": current_time,
|
| 86 |
-
"model_type": model_type,
|
| 87 |
-
"likes": model_info.likes,
|
| 88 |
-
"params": model_size,
|
| 89 |
-
"license": license,
|
| 90 |
-
"private": False,
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
# Check for duplicate submission
|
| 94 |
-
if f"{model}_{revision}_{precision}" in REQUESTED_MODELS:
|
| 95 |
-
return styled_warning("This model has been already submitted.")
|
| 96 |
-
|
| 97 |
-
print("Creating eval file")
|
| 98 |
-
OUT_DIR = f"{EVAL_REQUESTS_PATH}/{user_name}"
|
| 99 |
-
os.makedirs(OUT_DIR, exist_ok=True)
|
| 100 |
-
out_path = f"{OUT_DIR}/{model_path}_eval_request_False_{precision}_{weight_type}.json"
|
| 101 |
-
|
| 102 |
-
with open(out_path, "w") as f:
|
| 103 |
-
f.write(json.dumps(eval_entry))
|
| 104 |
-
|
| 105 |
-
print("Uploading eval file")
|
| 106 |
-
API.upload_file(
|
| 107 |
-
path_or_fileobj=out_path,
|
| 108 |
-
path_in_repo=out_path.split("eval-queue/")[1],
|
| 109 |
-
repo_id=QUEUE_REPO,
|
| 110 |
-
repo_type="dataset",
|
| 111 |
-
commit_message=f"Add {model} to eval queue",
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# Remove the local file
|
| 115 |
-
os.remove(out_path)
|
| 116 |
-
|
| 117 |
-
return styled_message(
|
| 118 |
-
"Your request has been submitted to the evaluation queue!\nPlease wait for up to an hour for the model to show in the PENDING list."
|
| 119 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/submission/submit_csv.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
|
| 6 |
+
from src.envs import API, SUBMISSIONS_PATH, SUBMISSIONS_REPO
|
| 7 |
+
from src.evaluation.compute_metrics import compute_metrics
|
| 8 |
+
from src.evaluation.load_labels import load_true_labels
|
| 9 |
+
from src.submission.validate_csv import validate_csv
|
| 10 |
+
from src.teams.auth import validate_token
|
| 11 |
+
from src.teams.storage import hash_token
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def get_team_best_scores(team_name: str) -> dict | None:
|
| 15 |
+
results_file = os.path.join(SUBMISSIONS_PATH, "results", f"{team_name}.json")
|
| 16 |
+
if os.path.exists(results_file):
|
| 17 |
+
try:
|
| 18 |
+
with open(results_file, "r") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
except Exception:
|
| 21 |
+
pass
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def save_team_best_scores(team_name: str, scores: dict):
|
| 26 |
+
results_dir = os.path.join(SUBMISSIONS_PATH, "results")
|
| 27 |
+
os.makedirs(results_dir, exist_ok=True)
|
| 28 |
+
results_file = os.path.join(results_dir, f"{team_name}.json")
|
| 29 |
+
|
| 30 |
+
with open(results_file, "w") as f:
|
| 31 |
+
json.dump(scores, f)
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
API.upload_file(
|
| 35 |
+
path_or_fileobj=results_file,
|
| 36 |
+
path_in_repo=f"results/{team_name}.json",
|
| 37 |
+
repo_id=SUBMISSIONS_REPO,
|
| 38 |
+
repo_type="dataset",
|
| 39 |
+
commit_message=f"Update scores for team: {team_name}",
|
| 40 |
+
)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Warning: Could not upload results to hub: {e}")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def save_submission(team_name: str, token_hash: str, csv_content: str, scores: dict, status: str):
|
| 46 |
+
os.makedirs(SUBMISSIONS_PATH, exist_ok=True)
|
| 47 |
+
submission_id = str(uuid.uuid4())
|
| 48 |
+
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 49 |
+
|
| 50 |
+
submission_data = {
|
| 51 |
+
"submission_id": submission_id,
|
| 52 |
+
"team_name": team_name,
|
| 53 |
+
"token_hash": token_hash,
|
| 54 |
+
"timestamp": timestamp,
|
| 55 |
+
"scores": scores,
|
| 56 |
+
"status": status,
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
submission_file = os.path.join(SUBMISSIONS_PATH, f"{submission_id}.json")
|
| 60 |
+
with open(submission_file, "w") as f:
|
| 61 |
+
json.dump(submission_data, f)
|
| 62 |
+
|
| 63 |
+
csv_file = os.path.join(SUBMISSIONS_PATH, f"{submission_id}.csv")
|
| 64 |
+
with open(csv_file, "w") as f:
|
| 65 |
+
f.write(csv_content)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
API.upload_file(
|
| 69 |
+
path_or_fileobj=submission_file,
|
| 70 |
+
path_in_repo=f"{submission_id}.json",
|
| 71 |
+
repo_id=SUBMISSIONS_REPO,
|
| 72 |
+
repo_type="dataset",
|
| 73 |
+
commit_message=f"Submission from {team_name}",
|
| 74 |
+
)
|
| 75 |
+
API.upload_file(
|
| 76 |
+
path_or_fileobj=csv_file,
|
| 77 |
+
path_in_repo=f"{submission_id}.csv",
|
| 78 |
+
repo_id=SUBMISSIONS_REPO,
|
| 79 |
+
repo_type="dataset",
|
| 80 |
+
commit_message=f"CSV for submission {submission_id}",
|
| 81 |
+
)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Warning: Could not upload submission to hub: {e}")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def should_update_scores(new_scores: dict, best_scores: dict | None) -> bool:
|
| 87 |
+
if best_scores is None:
|
| 88 |
+
return True
|
| 89 |
+
|
| 90 |
+
new_accuracy = new_scores.get("accuracy", 0.0)
|
| 91 |
+
new_f1 = new_scores.get("f1", 0.0)
|
| 92 |
+
new_error = new_scores.get("error_rate", 1.0)
|
| 93 |
+
|
| 94 |
+
best_accuracy = best_scores.get("best_accuracy", 0.0)
|
| 95 |
+
best_f1 = best_scores.get("best_f1", 0.0)
|
| 96 |
+
best_error = best_scores.get("best_error_rate", 1.0)
|
| 97 |
+
|
| 98 |
+
if new_accuracy > best_accuracy:
|
| 99 |
+
return True
|
| 100 |
+
if new_accuracy == best_accuracy and new_f1 > best_f1:
|
| 101 |
+
return True
|
| 102 |
+
if new_accuracy == best_accuracy and new_f1 == best_f1 and new_error < best_error:
|
| 103 |
+
return True
|
| 104 |
+
|
| 105 |
+
return False
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def submit_csv(token: str, csv_content: str) -> tuple[bool, str]:
|
| 109 |
+
team = validate_token(token)
|
| 110 |
+
if not team:
|
| 111 |
+
return False, "Invalid token. Please check your team token."
|
| 112 |
+
|
| 113 |
+
team_name = team["team_name"]
|
| 114 |
+
token_hash = hash_token(token)
|
| 115 |
+
|
| 116 |
+
true_labels = load_true_labels()
|
| 117 |
+
if not true_labels:
|
| 118 |
+
return False, "Error: True labels not available. Please contact administrators."
|
| 119 |
+
|
| 120 |
+
is_valid, error_msg, predictions_df = validate_csv(csv_content, true_labels)
|
| 121 |
+
if not is_valid:
|
| 122 |
+
return False, f"CSV validation failed: {error_msg}"
|
| 123 |
+
|
| 124 |
+
scores = compute_metrics(predictions_df, true_labels)
|
| 125 |
+
|
| 126 |
+
best_scores = get_team_best_scores(team_name)
|
| 127 |
+
|
| 128 |
+
if should_update_scores(scores, best_scores):
|
| 129 |
+
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 130 |
+
updated_scores = {
|
| 131 |
+
"team_name": team_name,
|
| 132 |
+
"best_accuracy": scores["accuracy"],
|
| 133 |
+
"best_f1": scores["f1"],
|
| 134 |
+
"best_error_rate": scores["error_rate"],
|
| 135 |
+
"last_submission_date": timestamp,
|
| 136 |
+
}
|
| 137 |
+
save_team_best_scores(team_name, updated_scores)
|
| 138 |
+
status = "ACCEPTED"
|
| 139 |
+
message = f"Submission accepted! Your scores: Accuracy={scores['accuracy']:.4f}, F1={scores['f1']:.4f}, Error Rate={scores['error_rate']:.4f}"
|
| 140 |
+
else:
|
| 141 |
+
status = "REJECTED"
|
| 142 |
+
best_acc = best_scores.get("best_accuracy", 0.0) if best_scores else 0.0
|
| 143 |
+
best_f1 = best_scores.get("best_f1", 0.0) if best_scores else 0.0
|
| 144 |
+
message = f"Submission rejected. Your scores (Accuracy={scores['accuracy']:.4f}, F1={scores['f1']:.4f}) did not improve your best scores (Accuracy={best_acc:.4f}, F1={best_f1:.4f})."
|
| 145 |
+
|
| 146 |
+
save_submission(team_name, token_hash, csv_content, scores, status)
|
| 147 |
+
|
| 148 |
+
return True, message
|
src/submission/validate_csv.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import StringIO
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def normalize_prediction(pred: any) -> int | None:
|
| 7 |
+
if pd.isna(pred):
|
| 8 |
+
return None
|
| 9 |
+
|
| 10 |
+
if isinstance(pred, (int, float)):
|
| 11 |
+
if pred == 0 or pred == 1:
|
| 12 |
+
return int(pred)
|
| 13 |
+
if pred == 0.0 or pred == 1.0:
|
| 14 |
+
return int(pred)
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
if isinstance(pred, str):
|
| 18 |
+
pred_lower = pred.strip().lower()
|
| 19 |
+
if pred_lower in ["0", "1", "real", "fake"]:
|
| 20 |
+
if pred_lower in ["0", "real"]:
|
| 21 |
+
return 0
|
| 22 |
+
else:
|
| 23 |
+
return 1
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def validate_csv(csv_content: str, true_labels: dict[str, int]) -> tuple[bool, str, pd.DataFrame | None]:
|
| 30 |
+
if not csv_content or not csv_content.strip():
|
| 31 |
+
return False, "CSV content is empty", None
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
df = pd.read_csv(StringIO(csv_content))
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return False, f"Invalid CSV format: {str(e)}", None
|
| 37 |
+
|
| 38 |
+
if "file_name" not in df.columns:
|
| 39 |
+
return False, "CSV must contain 'file_name' column", None
|
| 40 |
+
|
| 41 |
+
if "prediction" not in df.columns:
|
| 42 |
+
return False, "CSV must contain 'prediction' column", None
|
| 43 |
+
|
| 44 |
+
if df.empty:
|
| 45 |
+
return False, "CSV is empty", None
|
| 46 |
+
|
| 47 |
+
if df["file_name"].isna().any():
|
| 48 |
+
return False, "file_name column contains missing values", None
|
| 49 |
+
|
| 50 |
+
if df["prediction"].isna().any():
|
| 51 |
+
return False, "prediction column contains missing values", None
|
| 52 |
+
|
| 53 |
+
normalized_predictions = []
|
| 54 |
+
invalid_predictions = []
|
| 55 |
+
|
| 56 |
+
for idx, row in df.iterrows():
|
| 57 |
+
file_name = str(row["file_name"]).strip()
|
| 58 |
+
pred = normalize_prediction(row["prediction"])
|
| 59 |
+
|
| 60 |
+
if pred is None:
|
| 61 |
+
invalid_predictions.append(f"Row {idx + 1}: invalid prediction value '{row['prediction']}'")
|
| 62 |
+
else:
|
| 63 |
+
normalized_predictions.append(pred)
|
| 64 |
+
|
| 65 |
+
if invalid_predictions:
|
| 66 |
+
return False, "Invalid predictions found:\n" + "\n".join(invalid_predictions[:5]), None
|
| 67 |
+
|
| 68 |
+
df["prediction"] = normalized_predictions
|
| 69 |
+
|
| 70 |
+
missing_files = []
|
| 71 |
+
for file_name in df["file_name"]:
|
| 72 |
+
if str(file_name) not in true_labels:
|
| 73 |
+
missing_files.append(str(file_name))
|
| 74 |
+
|
| 75 |
+
if missing_files:
|
| 76 |
+
return (
|
| 77 |
+
False,
|
| 78 |
+
f"Unknown file names found: {', '.join(missing_files[:5])}{'...' if len(missing_files) > 5 else ''}",
|
| 79 |
+
None,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return True, "CSV is valid", df
|
src/teams/__init__.py
ADDED
|
File without changes
|
src/teams/auth.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.teams.storage import get_team_by_token
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def validate_token(token: str) -> dict | None:
|
| 5 |
+
if not token or not token.strip():
|
| 6 |
+
return None
|
| 7 |
+
return get_team_by_token(token.strip())
|
src/teams/register.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import secrets
|
| 2 |
+
|
| 3 |
+
from src.teams.storage import register_team
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def create_team(team_name: str, num_teammates: int) -> tuple[str, dict]:
|
| 7 |
+
if not team_name or not team_name.strip():
|
| 8 |
+
raise ValueError("Team name cannot be empty")
|
| 9 |
+
|
| 10 |
+
if not isinstance(num_teammates, int) or num_teammates < 1:
|
| 11 |
+
raise ValueError("Number of teammates must be a positive integer")
|
| 12 |
+
|
| 13 |
+
token = secrets.token_urlsafe(32)
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
team_data = register_team(team_name.strip(), num_teammates, token)
|
| 17 |
+
return token, team_data
|
| 18 |
+
except ValueError as e:
|
| 19 |
+
raise ValueError(f"Team registration failed: {e}")
|
src/teams/storage.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
|
| 6 |
+
from src.envs import API, TEAMS_PATH, TEAMS_REPO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def hash_token(token: str) -> str:
|
| 10 |
+
return hashlib.sha256(token.encode()).hexdigest()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def ensure_teams_dir():
|
| 14 |
+
os.makedirs(TEAMS_PATH, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def register_team(team_name: str, num_teammates: int, token: str) -> dict:
|
| 18 |
+
ensure_teams_dir()
|
| 19 |
+
|
| 20 |
+
token_hash = hash_token(token)
|
| 21 |
+
team_data = {
|
| 22 |
+
"team_name": team_name,
|
| 23 |
+
"num_teammates": num_teammates,
|
| 24 |
+
"token_hash": token_hash,
|
| 25 |
+
"created_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
|
| 29 |
+
|
| 30 |
+
if os.path.exists(team_file):
|
| 31 |
+
with open(team_file, "r") as f:
|
| 32 |
+
existing = json.load(f)
|
| 33 |
+
if existing.get("token_hash") != token_hash:
|
| 34 |
+
raise ValueError("Team name already exists with different token")
|
| 35 |
+
return existing
|
| 36 |
+
|
| 37 |
+
with open(team_file, "w") as f:
|
| 38 |
+
json.dump(team_data, f)
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
API.upload_file(
|
| 42 |
+
path_or_fileobj=team_file,
|
| 43 |
+
path_in_repo=f"{team_name}.json",
|
| 44 |
+
repo_id=TEAMS_REPO,
|
| 45 |
+
repo_type="dataset",
|
| 46 |
+
commit_message=f"Register team: {team_name}",
|
| 47 |
+
)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Warning: Could not upload team registration to hub: {e}")
|
| 50 |
+
|
| 51 |
+
return team_data
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def get_team_by_token(token: str) -> dict | None:
|
| 55 |
+
ensure_teams_dir()
|
| 56 |
+
token_hash = hash_token(token)
|
| 57 |
+
|
| 58 |
+
for filename in os.listdir(TEAMS_PATH):
|
| 59 |
+
if not filename.endswith(".json"):
|
| 60 |
+
continue
|
| 61 |
+
filepath = os.path.join(TEAMS_PATH, filename)
|
| 62 |
+
try:
|
| 63 |
+
with open(filepath, "r") as f:
|
| 64 |
+
team_data = json.load(f)
|
| 65 |
+
if team_data.get("token_hash") == token_hash:
|
| 66 |
+
return team_data
|
| 67 |
+
except Exception:
|
| 68 |
+
continue
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def get_all_teams() -> list[dict]:
|
| 73 |
+
ensure_teams_dir()
|
| 74 |
+
teams = []
|
| 75 |
+
|
| 76 |
+
for filename in os.listdir(TEAMS_PATH):
|
| 77 |
+
if not filename.endswith(".json"):
|
| 78 |
+
continue
|
| 79 |
+
filepath = os.path.join(TEAMS_PATH, filename)
|
| 80 |
+
try:
|
| 81 |
+
with open(filepath, "r") as f:
|
| 82 |
+
teams.append(json.load(f))
|
| 83 |
+
except Exception:
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
return teams
|