Spaces:
Running
Running
File size: 32,877 Bytes
1e0ed83 b4929ca b30e7f7 1e0ed83 b4929ca 9346f1c d66a6a3 60906bd 0b237ab b4929ca 4596a70 60906bd 8b1f7a0 01ea22b b98f07f 04c5cbd b98f07f b4929ca ae66b87 04c5cbd 3b3db42 12947f9 3b3db42 c85dcc4 3b3db42 3165936 3d8dbe8 3b3db42 fe8ec74 3b3db42 3f84332 852f90d 3f84332 b98f07f b30e7f7 c85dcc4 fabb601 d66a6a3 60906bd 3165936 1b8a36b c85dcc4 2a73469 ffefe11 fe8ec74 614ee1f 3165936 7b47b0b cd1b5e8 7b47b0b cd1b5e8 7b47b0b cd1b5e8 7b47b0b cd1b5e8 7b47b0b cd1b5e8 c768dfc c85dcc4 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc 12947f9 c768dfc c85dcc4 c768dfc 12947f9 c768dfc 12947f9 c768dfc c85dcc4 12947f9 c768dfc 7b47b0b 12947f9 c768dfc c85dcc4 c768dfc c85dcc4 c768dfc 944602c 12947f9 c768dfc 12947f9 c768dfc 7b47b0b c768dfc 12947f9 c768dfc 7b47b0b 12947f9 c768dfc 12947f9 c768dfc d66a6a3 12947f9 c768dfc 4103566 12947f9 c768dfc beaaa9e f6475aa 3f84332 9719e89 3f84332 1b55e08 81adfdd 3f84332 81adfdd 8125e59 81adfdd 8125e59 81adfdd 3f84332 81adfdd 04c5cbd 81adfdd 5a3aec3 81adfdd 3f84332 81adfdd aff7b1f 81adfdd 3f84332 81adfdd b30e7f7 aff7b1f 81adfdd ba366ad 81adfdd ba366ad 81adfdd 04c5cbd 81adfdd 04c5cbd 81adfdd 04c5cbd 81adfdd 04c5cbd 81adfdd 04c5cbd 3f84332 04c5cbd 3f84332 04c5cbd 3f84332 04c5cbd 3f84332 81adfdd 04c5cbd 1e0ed83 8125e59 04c5cbd 58bbf33 04c5cbd 58bbf33 1e0ed83 04c5cbd b30e7f7 04c5cbd aff7b1f 04c5cbd 5a3aec3 ba366ad 04c5cbd 5a3aec3 04c5cbd 6ad8c8e 04c5cbd 8125e59 b30e7f7 04c5cbd 6ad8c8e ace97cb 6ad8c8e f10ff89 f163070 61d4a7d f163070 61d4a7d f163070 6ad8c8e 04c5cbd f163070 1e0ed83 04c5cbd 1e0ed83 8125e59 3f84332 04c5cbd 8125e59 04c5cbd 6ad8c8e 04c5cbd 1b55e08 81adfdd 1b55e08 81adfdd 04c5cbd b30e7f7 04c5cbd b30e7f7 ecc1bf2 8125e59 b30e7f7 ecc1bf2 04c5cbd 3f84332 ae66b87 3f84332 c2c3c10 3f84332 ae66b87 3f84332 c85dcc4 3f84332 c85dcc4 3f84332 13edd99 3f84332 0b237ab 3f84332 0b237ab 3f84332 13edd99 3f84332 c85dcc4 3f84332 ae66b87 12947f9 ae66b87 e7226cc 8daa060 ae66b87 3f84332 ae66b87 3f84332 ae66b87 f7d1b51 b4929ca ae66b87 b4929ca bd30e41 c85dcc4 b4929ca 0b237ab b4929ca 3f84332 b4929ca |
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 |
import json
import re
import sys
import threading
import typing
from textwrap import dedent
import gradio as gr
import gradio.components as grc
import pandas as pd
import requests
import uvicorn
from apscheduler.schedulers.background import BackgroundScheduler
from rich import print
from src.about import (
CITATION_BUTTON_LABEL,
CITATION_BUTTON_TEXT,
EVALUATION_QUEUE_TEXT,
INTRODUCTION_TEXT,
LLM_BENCHMARKS_TEXT,
SUBMISSION_INSTRUCTIONS_TEXT,
TITLE,
)
from src.backend.app import create_app
from src.display.css_html_js import (
backend_status_indicator_css,
backend_status_indicator_html,
backend_status_js,
custom_css,
)
from src.display.formatting import styled_error
from src.display.utils import (
BASE_COLS,
BENCHMARK_COLS,
EVAL_COLS,
EVAL_TYPES,
NOT_SUPPORTED_COLS,
AutoEvalColumn,
ModelType,
Precision,
WeightType,
)
from src.envs import API, settings
from src.populate import get_evaluation_queue_df, get_leaderboard_df
from src.prepare import get_benchmarks, load_display_toml, prepare_space
from src.submission.submit import add_new_submit
from src.utils.hf import restart_space
if typing.TYPE_CHECKING:
from src.schemas.meta_toml import MetaToml_Benchmark
prepare_space()
print("///// --- Settings --- /////", settings.model_dump())
# LEADERBOARD_DF = get_leaderboard_df(
# settings.EVAL_RESULTS_PATH,
# settings.EVAL_REQUESTS_PATH,
# COLS,
# BENCHMARK_COLS,
# )
(
finished_eval_queue_df,
running_eval_queue_df,
pending_eval_queue_df,
) = get_evaluation_queue_df(settings.EVAL_REQUESTS_PATH, EVAL_COLS)
def format_dataframe_with_styler(df: pd.DataFrame):
"""
使用 pandas Styler 格式化 DataFrame 中的数字列,保留1位小数
返回 Styler 对象,可以直接传递给 Gradio Dataframe
"""
# 先截断数据(如果需要截断而不是四舍五入)
df = df.copy()
numeric_cols = {}
for col in df.columns:
if col in ['Model', 'T']: # 跳过非数字列
continue
# 检查是否为数值类型
if pd.api.types.is_numeric_dtype(df[col]):
# 记录需要格式化的列: 保留1位小数
numeric_cols[col] = "{:.1f}"
# 使用 Styler 格式化显示
if numeric_cols:
return df.style.format(numeric_cols)
return df
def filter_dataframe_by_columns(selected_cols: list[str], original_df: pd.DataFrame) -> pd.DataFrame:
"""
根据选择的列过滤 DataFrame
"""
# # 始终包含基础列 'T' 和 'Model'
# base_cols = ['T', 'Model']
base_cols = ['Model']
all_selected_cols = [col for col in base_cols if col in original_df.columns]
# 添加用户选择的列(排除已存在的基础列)
for col in selected_cols:
if col in original_df.columns and col not in all_selected_cols:
all_selected_cols.append(col)
# 确保列的顺序:基础列在前,然后是按原始顺序的选中列
ordered_cols = []
for col in original_df.columns:
if col in all_selected_cols:
ordered_cols.append(col)
# 确保总是返回 DataFrame,即使是单列也使用 [[]] 来保持 DataFrame 类型
if ordered_cols:
filtered_df = original_df.loc[:, ordered_cols]
else:
filtered_df = original_df
return filtered_df
def filter_dataframe_by_precision(selected_precisions: list[str], df: pd.DataFrame) -> pd.DataFrame:
"""
根据选择的 precision 筛选 DataFrame
如果没有选择 precision,返回空的 DataFrame
"""
if not selected_precisions:
return df.iloc[0:0].copy() # 返回相同结构但为空的 DataFrame
precision_col = AutoEvalColumn.precision.name
if precision_col not in df.columns:
return df
# 筛选包含任一选定 precision 的行
mask = df[precision_col].isin(selected_precisions)
filtered_df = df.loc[mask, :]
return filtered_df
def search_models_in_dataframe(search_text: str, df: pd.DataFrame) -> pd.DataFrame:
"""
在 DataFrame 中搜索包含关键词的 Model 名称
支持逗号分隔的多个关键词,匹配包含任一关键词的行
"""
if not search_text or not search_text.strip():
return df
# 分割逗号,去除空白并转换为小写用于匹配
keywords = [keyword.strip().lower() for keyword in search_text.split(',') if keyword.strip()]
if not keywords:
return df
if 'Model' not in df.columns:
return df
# 匹配函数:从 HTML 中提取纯文本并检查是否包含关键词
def matches_search(model_cell):
if pd.isna(model_cell):
return False
# 从 HTML 链接中提取纯文本(model_name)
# 格式: <a ...>model_name</a> 或直接是文本
text = str(model_cell)
# 提取 HTML 标签内的文本
# 匹配 <a>...</a> 标签内的内容,或直接使用文本
match = re.search(r'<a[^>]*>([^<]+)</a>', text, re.IGNORECASE)
if match:
model_name = match.group(1).lower()
else:
model_name = text.lower()
# 检查是否包含任一关键词
return any(keyword in model_name for keyword in keywords)
# 应用搜索过滤
mask = df['Model'].apply(matches_search)
filtered_df = df.loc[mask, :]
return filtered_df
def init_leaderboard_tabs(
dataframe: pd.DataFrame,
cols: list[str],
not_supported_cols: list[str],
):
# 存储原始 DataFrame 以便后续过滤使用(使用闭包保存)
original_df = dataframe.copy()
available_precisions = sorted(original_df["Precision"].dropna().unique().tolist())
default_precision = (
['bfloat16']
if 'bfloat16' in available_precisions
else (available_precisions[:1] if available_precisions else [])
)
# 初始化显示的列(包含基础列和默认选中的列)
default_selected = [col for col in dataframe.columns if col in cols and col not in not_supported_cols] + [
'Average ⬆️'
]
# 先按 precision 筛选 original_df
precision_filtered_df = filter_dataframe_by_precision(default_precision, original_df)
# 根据默认选择再筛选一次 DataFrame
initial_filtered_df = filter_dataframe_by_columns(default_selected, precision_filtered_df)
# 使用 Styler 格式化数字列,保留1位小数
initial_styler = format_dataframe_with_styler(initial_filtered_df)
with gr.Row():
with gr.Column(scale=1):
search = gr.Textbox(label="Search", placeholder="Separate multiple queries with commas")
column_choices = [
col
for col in dataframe.columns
if col not in ['T', 'Model'] and (not not_supported_cols or col not in not_supported_cols)
]
show_columns = gr.CheckboxGroup(
choices=column_choices,
label="Select Columns to Display",
value=default_selected,
interactive=True,
)
with gr.Column(scale=1, visible=False):
_model_type = gr.CheckboxGroup(
[],
label="Model Type",
value=[],
)
precision = gr.CheckboxGroup(
choices=available_precisions,
label="Precision",
value=default_precision,
interactive=True,
)
_hide_models = gr.CheckboxGroup(
['Deleted/incomplete'],
label="Hide Models",
value=['Deleted/incomplete'],
interactive=True,
)
with gr.Row():
with gr.Column(scale=3):
leaderboard = gr.Dataframe(
value=initial_styler, # 使用 Styler 对象格式化显示
interactive=False,
wrap=False,
datatype='markdown',
elem_id="auto-width-dataframe",
)
# 统一的更新函数:同时处理 precision、列筛选和搜索
def update_dataframe(search_text: str, selected_cols: list[str], selected_precisions: list[str]):
# 先按 precision 筛选 original_df
precision_filtered_df = filter_dataframe_by_precision(selected_precisions, original_df)
# 再按列筛选
column_filtered_df = filter_dataframe_by_columns(selected_cols, precision_filtered_df)
# 最后按搜索关键词筛选
final_df = search_models_in_dataframe(search_text, column_filtered_df)
# 使用 Styler 格式化数字列,保留1位小数
final_styler = format_dataframe_with_styler(final_df)
return final_styler
# 绑定搜索、列选择和 precision 的变化事件,动态更新 DataFrame
search.change(
fn=update_dataframe,
inputs=[search, show_columns, precision],
outputs=leaderboard,
)
show_columns.change(
fn=update_dataframe,
inputs=[search, show_columns, precision],
outputs=leaderboard,
)
precision.change(
fn=update_dataframe,
inputs=[search, show_columns, precision],
outputs=leaderboard,
)
return leaderboard
def create_overview_tab(results_version: str):
"""Create the Overview tab with all benchmarks."""
with gr.TabItem("📝 Overview", elem_id="benchmark-overview-tab", id=0):
benchmark_cols = BENCHMARK_COLS.copy()
print("benchmark_cols:", benchmark_cols)
cols = BASE_COLS + benchmark_cols
benchmark_df = get_leaderboard_df(
settings.EVAL_RESULTS_VERSIONS_DIR,
settings.EVAL_REQUESTS_PATH,
results_version=results_version,
cols=cols,
benchmark_cols=benchmark_cols,
)
_leaderboard = init_leaderboard_tabs(benchmark_df, benchmark_cols, NOT_SUPPORTED_COLS)
def create_benchmark_tab(benchmark, results_version: str, tab_id: int):
"""Create a single benchmark tab."""
with gr.TabItem(f"🏅 {benchmark.title}", elem_id="llm-benchmark-tab-table", id=tab_id):
print(f"benchmark.title: {benchmark.title!r}")
benchmark_cols = [col for col in BENCHMARK_COLS if col.startswith(benchmark.title)]
cols = BASE_COLS + benchmark_cols
benchmark_df = get_leaderboard_df(
settings.EVAL_RESULTS_VERSIONS_DIR,
settings.EVAL_REQUESTS_PATH,
results_version=results_version,
cols=cols,
benchmark_cols=benchmark_cols,
)
_leaderboard = init_leaderboard_tabs(benchmark_df, benchmark_cols, NOT_SUPPORTED_COLS)
def create_about_tab(tab_id: int):
"""Create the About tab."""
with gr.TabItem("📝 About", elem_id="about-tab", id=tab_id):
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
def create_submit_tab(tab_id: int, demo: gr.Blocks):
"""Create the Submit tab with evaluation queue and submission form."""
with gr.TabItem("🚀 Submit here! ", elem_id="submit-tab", id=tab_id):
with gr.Column():
with gr.Row():
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
with gr.Column(visible=False, render=False):
with gr.Accordion(
f"✅ Finished Evaluations ({len(finished_eval_queue_df)})",
open=False,
):
with gr.Row():
_finished_eval_table = grc.Dataframe(
value=finished_eval_queue_df,
headers=EVAL_COLS,
datatype=EVAL_TYPES,
row_count=5,
)
with gr.Accordion(
f"🔄 Running Evaluation Queue ({len(running_eval_queue_df)})",
open=False,
):
with gr.Row():
_running_eval_table = grc.Dataframe(
value=running_eval_queue_df,
headers=EVAL_COLS,
datatype=EVAL_TYPES,
row_count=5,
)
with gr.Accordion(
f"⏳ Pending Evaluation Queue ({len(pending_eval_queue_df)})",
open=False,
):
with gr.Row():
_pending_eval_table = grc.Dataframe(
value=pending_eval_queue_df,
headers=EVAL_COLS,
datatype=EVAL_TYPES,
row_count=5,
)
with gr.Row():
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
with gr.Row():
gr.Markdown(SUBMISSION_INSTRUCTIONS_TEXT, elem_classes="markdown-text")
# ========== Login Section (First Step) ==========
login_button = gr.LoginButton()
# Function to get current HuggingFace user ID from the visiting user
def get_current_user_id(oauth_profile: gr.OAuthProfile | None = None) -> str:
"""Get current HuggingFace user ID from the visiting user (not the server token)"""
if oauth_profile is None:
# User is not logged in
return "Submitter ID: Not logged in"
# User is logged in, get user ID from OAuth profile
# OAuth profile contains 'sub' (user ID) and 'preferred_username' fields
user_id = oauth_profile.get("preferred_username") or oauth_profile.get("sub", "Unknown")
return f"Submitter ID: **{user_id}**"
# Container for all form sections - will be hidden if user is not logged in
form_container = gr.Column(visible=False)
with form_container:
# ========== Section 1: Steps 1-7 (Model Information) ==========
with gr.Accordion("📋 Model Information (Steps 1-7)", open=True):
with gr.Row():
search_name = gr.Textbox(label="1. Search model name", placeholder="org/model_name")
with gr.Row():
table = gr.Dataframe(
headers=["Model Name", "Pipeline", "Downloads", "Likes"],
datatype=["str", "str", "number", "number"],
interactive=False,
wrap=True,
label="2. Click model name to select",
)
with gr.Row():
with gr.Column():
model_name_textbox = gr.Textbox(
label="2. Model name (auto-filled)", placeholder="org/model_name", interactive=False
)
revision_name_textbox = gr.Textbox(label="3. Revision commit (Optional)", placeholder="main")
model_type = gr.Dropdown(
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
label="4. Model type",
multiselect=False,
value=None,
interactive=True,
)
precision = gr.Dropdown(
choices=[i.value.name for i in Precision if i != Precision.Unknown],
label="5. Precision",
multiselect=False,
value="bfloat16", # default to bfloat16 (torch.bfloat16)
interactive=True,
)
weight_type = gr.Dropdown(
choices=[i.value.name for i in WeightType],
label="6. Weights type",
multiselect=False,
value="Original",
interactive=True,
)
base_model_name_textbox = gr.Textbox(
label="7. Base model (Optional, for delta or adapter weights)",
placeholder="org/model_name",
)
def search_models(query):
if not query.strip():
return []
models = API.list_models(search=query, limit=10)
results = []
for m in models:
results.append([m.id, m.pipeline_tag or "N/A", m.downloads or 0, m.likes or 0])
return results
def on_select(evt: gr.SelectData, data):
row_idx = evt.index[0] # 获取点击行号
if row_idx < len(data):
return data.iloc[row_idx, 0] # 返回模型名
return ""
search_name.change(fn=search_models, inputs=search_name, outputs=table)
table.select(fn=on_select, inputs=table, outputs=model_name_textbox)
# ========== Section 2: Steps 8-10 (Benchmark Evaluation Results) ==========
benchmarks_list = get_benchmarks()
with gr.Accordion("📊 Benchmark Evaluation Results (Steps 8-10)", open=True):
gr.Markdown(
"**8-9. Select benchmarks and fill in evaluation result values**\n\n"
"Fill in the form below to submit your benchmark evaluation results."
)
# Simple form for benchmark results
benchmark_results_form: list[tuple[MetaToml_Benchmark, gr.Checkbox, gr.Number]] = []
for benchmark in benchmarks_list:
with gr.Row():
benchmark_checkbox = gr.Checkbox(
label=f"{benchmark.title} ({benchmark.default_metric_label})",
value=False,
)
result_input = gr.Number(
label="Result Value (in %, e.g. 42.0 for 42.0%)",
value=None,
interactive=True,
visible=False,
)
def make_visibility_fn(_result_comp):
def toggle_visibility(checked):
return gr.update(visible=checked)
return toggle_visibility
benchmark_checkbox.change(
fn=make_visibility_fn(result_input),
inputs=benchmark_checkbox,
outputs=result_input,
)
benchmark_results_form.append((benchmark, benchmark_checkbox, result_input))
# JSON upload as alternative
def file_to_json_str(file) -> str:
"""
读取上传的 JSON 文件并返回字符串
如果解析失败,抛出异常以阻止上传成功
"""
if file is None:
return ""
try:
# file 是文件路径字符串(当 type="filepath" 时)
file_path = file if isinstance(file, str) else file.name
with open(file_path, encoding='utf-8') as f:
json_data = json.load(f)
# 将 JSON 对象转换为格式化的字符串
json_str = json.dumps(json_data, indent=2, ensure_ascii=False)
return json_str
except Exception as e:
raise gr.Error(f"Error reading JSON file: {str(e)}") from e
json_upload = gr.File(
label="Or upload JSON file (alternative to form above)",
file_types=[".json"],
type="filepath",
visible=False,
)
json_str = gr.Textbox(
label="JSON Content",
placeholder='{"config": {...}, "results": {...}}',
lines=15,
interactive=True,
visible=False,
)
json_upload.upload(
fn=file_to_json_str,
inputs=json_upload,
outputs=json_str,
)
# ========== Submit Section ==========
with gr.Row():
commit_textbox = gr.Textbox(label="Commit Message (Optional)", placeholder="Optional commit message")
def build_json_from_form(
model_name: str,
revision: str,
precision: str,
benchmark_checkbox_values: list[bool],
benchmark_result_values: list[float | None],
) -> str:
"""Build JSON from form inputs"""
if not model_name or not model_name.strip():
raise ValueError("Model name is required")
# Extract model_key from model_name (simple conversion)
model_key = model_name.lower().replace("-", "_").replace("/", "-")
# Build config
config = {
"model_name": model_name,
"model_key": model_key,
"model_dtype": precision or None,
"model_sha": revision or None, # None means "main"
"model_args": None,
}
# Build results: {benchmark_key: {metric: value}}
results = {}
assert (
len(benchmarks_list) > 0 and len(benchmark_checkbox_values) > 0 and len(benchmark_result_values) > 0
), "Empty benchmarks list, checkbox values, or result values."
for benchmark, checkbox_checked, result_value in zip(
benchmarks_list,
benchmark_checkbox_values,
benchmark_result_values,
strict=True,
):
if checkbox_checked:
metric_key = benchmark.default_metric
results[benchmark.key] = {metric_key: float(result_value) if result_value is not None else None}
if not results:
raise ValueError("At least one benchmark result is required.")
for benchmark_key, result in results.items():
if result is None:
benchmark = next((b for b in benchmarks_list if b.key == benchmark_key), None)
assert benchmark is not None, f"Benchmark not found: {benchmark_key}."
raise ValueError(f"Benchmark result is not filled in for {benchmark_key}.")
return json.dumps({"config": config, "results": results}, indent=2, ensure_ascii=False)
def submit_with_form_or_json(
model: str,
base_model: str,
revision: str,
precision: str,
weight_type: str,
model_type: str,
json_str: str,
commit_message: str,
current_user_display: str,
*benchmark_values: list[bool | float],
):
"""Submit with either form data or JSON"""
# Check if user is logged in
if not current_user_display:
return styled_error("Please log in before submitting.")
# Extract user_id from "Submitter ID: **{user_id}**" format
reg = re.compile(r"\*\*(.+?)\*\*")
user_id_match = reg.search(current_user_display)
if not user_id_match:
msg = f'Unable to get user ID ("{current_user_display!s}"). Please try logging in again.'
return styled_error(msg)
user_id = user_id_match.group(1)
if not user_id:
msg = f'Unable to get user ID ("{current_user_display!s}"). Please try logging in again.'
return styled_error(msg)
# If JSON is provided, use it; otherwise build from form
if json_str and json_str.strip():
try:
# Validate JSON
json.loads(json_str)
final_json = json_str
except json.JSONDecodeError:
return styled_error("Invalid JSON format. Please check your JSON content.")
else:
# Build JSON from form
# benchmark_values contains pairs of (checkbox_value, result_value) for each benchmark
benchmarks_list = get_benchmarks()
if len(benchmark_values) != len(benchmarks_list) * 2:
print(
dedent(f"""
Invalid benchmark form data. Please check your inputs.
* benchmarks_list: {benchmarks_list!r}
* benchmark_values: {benchmark_values!r}
"""),
file=sys.stderr,
)
return styled_error("Invalid benchmark form data. Please check your inputs.")
# Split into checkbox values and result values
benchmark_checkbox_values: list[bool] = []
benchmark_result_values: list[float | None] = []
for i, value in enumerate(benchmark_values):
if i % 2 == 0:
benchmark_checkbox_values.append(bool(value))
else:
benchmark_result_values.append(float(value) if value is not None else None) # pyright: ignore[reportArgumentType]
try:
final_json = build_json_from_form(
model,
revision,
precision,
benchmark_checkbox_values,
benchmark_result_values,
)
except ValueError as e:
return styled_error(str(e))
except Exception as e:
return styled_error(f"Error building JSON: {str(e)}")
# Call the submit function with all fields including user_id
return add_new_submit(
model=model,
base_model=base_model,
revision=revision,
precision=precision,
weight_type=weight_type,
model_type=model_type,
json_str=final_json,
commit_message=commit_message,
user_id=user_id,
)
current_user_display = gr.Markdown(value="Submitter ID: Loading...")
# Create submit button outside form_container so it can be referenced
# Initially hidden and disabled (will be shown and enabled when user logs in)
submit_button = gr.Button("Submit Eval", variant="primary", interactive=False, visible=False)
# Update user ID and form visibility when page loads or when user logs in
def update_user_display(oauth_profile: gr.OAuthProfile | None = None) -> tuple[str, dict, dict]:
"""Update user display, form visibility, and submit button state based on login status"""
user_display = get_current_user_id(oauth_profile)
# Show form and button only if user is logged in
is_logged_in = oauth_profile is not None
return (
user_display,
gr.update(visible=is_logged_in),
gr.update(visible=is_logged_in, interactive=is_logged_in),
)
demo.load(
fn=update_user_display,
inputs=None,
outputs=[current_user_display, form_container, submit_button],
)
# Also update when login button is clicked
login_button.click(
fn=update_user_display,
inputs=None,
outputs=[current_user_display, form_container, submit_button],
)
submission_result = gr.Markdown()
# Add benchmark form inputs (these will be captured by *benchmark_values)
benchmark_values: list[gr.Checkbox | gr.Number] = []
for _, checkbox, result_input in benchmark_results_form:
benchmark_values.extend((checkbox, result_input))
submit_button.click(
fn=submit_with_form_or_json,
inputs=[
model_name_textbox,
base_model_name_textbox,
revision_name_textbox,
precision,
weight_type,
model_type,
json_str,
commit_textbox,
current_user_display,
*benchmark_values,
],
outputs=submission_result,
)
# Backend status indicator
backend_status = gr.HTML(
value=get_backend_status_undefined_html(),
elem_id="backend-status-container",
)
# trigger button to bind the click event
status_trigger = gr.Button(elem_id="backend-status-trigger-btn", visible=False)
status_trigger.click(
fn=lambda: check_backend_health()[1],
inputs=None,
outputs=backend_status,
)
# load external JavaScript file
js_content = backend_status_js()
status_trigger_js_html = f'<script>{js_content}</script>'
gr.HTML(status_trigger_js_html, visible=False)
demo.load(
fn=lambda: check_backend_health()[1],
inputs=None,
outputs=backend_status,
)
def main():
"""Main function to create and configure the Gradio demo."""
results_version = load_display_toml().version
# Bulma CSS will be loaded dynamically and scoped to submit form only
demo = gr.Blocks(css_paths=[custom_css, backend_status_indicator_css])
with demo:
gr.HTML(TITLE)
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
with gr.Tabs(elem_classes="tab-buttons") as _tabs:
# Overview tab
create_overview_tab(results_version)
# Benchmark tabs
i_bench = 1
benchmarks = get_benchmarks()
if settings.ENABLE_BENCHMARK_TABS:
for i_bench, benchmark in enumerate(benchmarks, start=1):
create_benchmark_tab(benchmark, results_version, i_bench)
# About tab
create_about_tab(i_bench + 1)
# Submit tab
if settings.ENABLE_SUBMISSION:
create_submit_tab(i_bench + 2, demo)
# Citation section
with gr.Row():
with gr.Accordion("📙 Citation", open=False):
_citation_button = gr.Textbox(
value=CITATION_BUTTON_TEXT,
label=CITATION_BUTTON_LABEL,
lines=20,
elem_id="citation-button",
show_copy_button=True,
)
return demo
def get_backend_status_undefined_html() -> str:
"""
返回未定义状态(首次检查前)的 HTML
"""
return backend_status_indicator_html("undefined")
def check_backend_health() -> tuple[bool, str]:
"""
查询后端健康状态
返回: (is_healthy, status_html)
"""
health_url = f"http://localhost:{settings.BACKEND_PORT}/api/v1/health/"
try:
response = requests.get(health_url, timeout=2)
if response.status_code == 200:
data = response.json()
if data.get("code") == 0:
return (
True,
backend_status_indicator_html("healthy"),
)
return (
False,
backend_status_indicator_html("unhealthy"),
)
except Exception:
return (
False,
backend_status_indicator_html("unhealthy"),
)
if __name__ == "__main__":
demo = main()
# Backend server - 在单独的线程中运行
app = create_app()
def run_fastapi():
host = settings.BACKEND_HOST
port = settings.BACKEND_PORT
print("Starting FastAPI server:")
uvicorn.run(
app,
host=host,
port=port,
log_level="debug",
access_log=True,
)
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
fastapi_thread.start()
# Gradio server - 在主线程中运行(阻塞)
scheduler = BackgroundScheduler()
scheduler.add_job(lambda: restart_space(settings.REPO_ID), "interval", seconds=1800)
scheduler.start()
demo.queue(default_concurrency_limit=40).launch()
|