Spaces:
Sleeping
Sleeping
SAGE OSS Evaluator
commited on
Commit
·
7f5c29a
1
Parent(s):
b1a0fd2
update
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import datetime
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
|
@@ -43,6 +45,10 @@ except ImportError as e:
|
|
| 43 |
TOKEN = os.environ.get("HF_TOKEN", None)
|
| 44 |
OWNER = "opencompass"
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def format_error(msg):
|
| 47 |
return f"<p style='color: red; font-size: 20px; text-align: center;'>{msg}</p>"
|
| 48 |
|
|
@@ -57,6 +63,107 @@ def model_hyperlink(link, model_name):
|
|
| 57 |
return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'
|
| 58 |
return model_name
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def get_leaderboard_dataframe():
|
| 61 |
"""Generate leaderboard dataframe from SAGE results"""
|
| 62 |
print("🔄 Loading SAGE leaderboard data...")
|
|
@@ -177,13 +284,22 @@ with demo:
|
|
| 177 |
with gr.Accordion("📊 Submit Your SAGE Results", open=False):
|
| 178 |
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
| 179 |
|
| 180 |
-
#
|
| 181 |
gr.Markdown("""
|
| 182 |
### 📋 提交要求
|
|
|
|
|
|
|
|
|
|
| 183 |
- **文件格式**: 上传符合SAGE格式的JSON文件
|
| 184 |
- **组织信息**: 填写准确的组织名称(将显示在排行榜)
|
| 185 |
- **联系邮箱**: 提供有效邮箱用于结果通知
|
| 186 |
- **自动评测**: 提交后将自动进行LLM评测并更新排行榜
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
""", elem_classes="markdown-text")
|
| 188 |
|
| 189 |
with gr.Row():
|
|
@@ -203,16 +319,16 @@ with demo:
|
|
| 203 |
type="filepath"
|
| 204 |
)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
with gr.Row():
|
|
|
|
| 208 |
submit_button = gr.Button("Submit Results", variant="primary", size="lg")
|
| 209 |
|
| 210 |
submission_result = gr.HTML()
|
| 211 |
|
| 212 |
-
|
| 213 |
-
def handle_submission(file_upload, org_name, email):
|
| 214 |
try:
|
| 215 |
-
# 基本验证
|
| 216 |
if not file_upload:
|
| 217 |
return format_error("请选择要上传的文件")
|
| 218 |
if not org_name or not org_name.strip():
|
|
@@ -220,18 +336,39 @@ with demo:
|
|
| 220 |
if not email or not email.strip():
|
| 221 |
return format_error("请输入邮箱地址")
|
| 222 |
|
| 223 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
from src.submission.simple_submit import process_sage_submission_simple
|
| 225 |
result = process_sage_submission_simple(file_upload, org_name, email)
|
| 226 |
|
| 227 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
success_info = f"""
|
| 229 |
<div style="background-color: #d4edda; border: 1px solid #c3e6cb; border-radius: 5px; padding: 15px; margin: 10px 0;">
|
| 230 |
<h4 style="color: #155724; margin-top: 0;">🎉 提交成功!</h4>
|
|
|
|
| 231 |
<p style="color: #155724; margin: 5px 0;"><strong>组织:</strong> {org_name}</p>
|
| 232 |
<p style="color: #155724; margin: 5px 0;"><strong>邮箱:</strong> {email}</p>
|
| 233 |
<p style="color: #155724; margin: 5px 0;"><strong>提交时间:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 234 |
-
<p style="color: #155724; margin
|
|
|
|
| 235 |
</div>
|
| 236 |
"""
|
| 237 |
|
|
@@ -240,7 +377,9 @@ with demo:
|
|
| 240 |
except ImportError as e:
|
| 241 |
return format_error(f"提交系统模块不可用: {e}")
|
| 242 |
except Exception as e:
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
|
| 245 |
submit_button.click(
|
| 246 |
handle_submission,
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import datetime
|
| 4 |
+
import requests
|
| 5 |
+
from email.utils import parseaddr
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
import numpy as np
|
|
|
|
| 45 |
TOKEN = os.environ.get("HF_TOKEN", None)
|
| 46 |
OWNER = "opencompass"
|
| 47 |
|
| 48 |
+
# OSS submission tracking paths
|
| 49 |
+
SUBMISSION_TRACKING_PATH = "atlas_eval/submissions/user_tracking/"
|
| 50 |
+
SUBMISSION_HISTORY_FILE = "submission_history.json"
|
| 51 |
+
|
| 52 |
def format_error(msg):
|
| 53 |
return f"<p style='color: red; font-size: 20px; text-align: center;'>{msg}</p>"
|
| 54 |
|
|
|
|
| 63 |
return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'
|
| 64 |
return model_name
|
| 65 |
|
| 66 |
+
def load_submission_history():
|
| 67 |
+
"""从OSS加载用户提交历史"""
|
| 68 |
+
try:
|
| 69 |
+
from src.oss.oss_file_manager import OSSFileManager
|
| 70 |
+
oss_manager = OSSFileManager()
|
| 71 |
+
|
| 72 |
+
# 尝试下载提交历史文件
|
| 73 |
+
history_content = oss_manager.download_file_content(
|
| 74 |
+
SUBMISSION_TRACKING_PATH + SUBMISSION_HISTORY_FILE
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if history_content:
|
| 78 |
+
return json.loads(history_content)
|
| 79 |
+
else:
|
| 80 |
+
print("📝 创建新的提交历史记录")
|
| 81 |
+
return {}
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"⚠️ 加载提交历史失败: {e}")
|
| 85 |
+
return {}
|
| 86 |
+
|
| 87 |
+
def save_submission_history(history):
|
| 88 |
+
"""保存用户提交历史到OSS"""
|
| 89 |
+
try:
|
| 90 |
+
from src.oss.oss_file_manager import OSSFileManager
|
| 91 |
+
oss_manager = OSSFileManager()
|
| 92 |
+
|
| 93 |
+
# 上传提交历史
|
| 94 |
+
history_json = json.dumps(history, indent=2, ensure_ascii=False)
|
| 95 |
+
success = oss_manager.upload_file_content(
|
| 96 |
+
content=history_json,
|
| 97 |
+
object_key=SUBMISSION_TRACKING_PATH + SUBMISSION_HISTORY_FILE
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
return success
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"❌ 保存提交历史失败: {e}")
|
| 104 |
+
return False
|
| 105 |
+
|
| 106 |
+
def check_user_submission_eligibility(profile: gr.OAuthProfile, org_name: str):
|
| 107 |
+
"""检查用户提交资格"""
|
| 108 |
+
try:
|
| 109 |
+
# 1. 检查账号年龄限制 (60天)
|
| 110 |
+
user_data = requests.get(f"https://huggingface.co/api/users/{profile.username}/overview")
|
| 111 |
+
if user_data.status_code == 200:
|
| 112 |
+
creation_date = json.loads(user_data.content)["createdAt"]
|
| 113 |
+
account_age = datetime.datetime.now() - datetime.datetime.strptime(creation_date, '%Y-%m-%dT%H:%M:%S.%fZ')
|
| 114 |
+
|
| 115 |
+
if account_age < datetime.timedelta(days=60):
|
| 116 |
+
return False, "此账号不满足提交要求。账号创建时间必须超过60天。"
|
| 117 |
+
else:
|
| 118 |
+
return False, "无法验证账号信息,请稍后再试。"
|
| 119 |
+
|
| 120 |
+
# 2. 检查每日提交限制
|
| 121 |
+
submission_history = load_submission_history()
|
| 122 |
+
user_submissions = submission_history.get(profile.username, [])
|
| 123 |
+
|
| 124 |
+
today = datetime.datetime.today().strftime('%Y-%m-%d')
|
| 125 |
+
today_submissions = [s for s in user_submissions if s.get("date", "") == today]
|
| 126 |
+
|
| 127 |
+
if len(today_submissions) > 0:
|
| 128 |
+
return False, "您今天已经提交过一次,请明天再试。"
|
| 129 |
+
|
| 130 |
+
# 3. 检查重复提交 (组织名+用户名组合)
|
| 131 |
+
for submission in user_submissions:
|
| 132 |
+
if submission.get("organization", "").lower() == org_name.lower():
|
| 133 |
+
return False, f"您已经为组织 '{org_name}' 提交过结果。如需更新,请联系管理员。"
|
| 134 |
+
|
| 135 |
+
return True, "检查通过"
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
print(f"❌ 用户资格检查失败: {e}")
|
| 139 |
+
return False, f"系统检查出现错误,请稍后再试: {str(e)}"
|
| 140 |
+
|
| 141 |
+
def record_user_submission(profile: gr.OAuthProfile, org_name: str, email: str):
|
| 142 |
+
"""记录用户提交"""
|
| 143 |
+
try:
|
| 144 |
+
submission_history = load_submission_history()
|
| 145 |
+
|
| 146 |
+
if profile.username not in submission_history:
|
| 147 |
+
submission_history[profile.username] = []
|
| 148 |
+
|
| 149 |
+
# 记录本次提交
|
| 150 |
+
submission_record = {
|
| 151 |
+
"date": datetime.datetime.today().strftime('%Y-%m-%d'),
|
| 152 |
+
"time": datetime.datetime.now().strftime('%H:%M:%S'),
|
| 153 |
+
"organization": org_name,
|
| 154 |
+
"email": email,
|
| 155 |
+
"username": profile.username
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
submission_history[profile.username].append(submission_record)
|
| 159 |
+
|
| 160 |
+
# 保存历史记录
|
| 161 |
+
return save_submission_history(submission_history)
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"❌ 记录提交历史失败: {e}")
|
| 165 |
+
return False
|
| 166 |
+
|
| 167 |
def get_leaderboard_dataframe():
|
| 168 |
"""Generate leaderboard dataframe from SAGE results"""
|
| 169 |
print("🔄 Loading SAGE leaderboard data...")
|
|
|
|
| 284 |
with gr.Accordion("📊 Submit Your SAGE Results", open=False):
|
| 285 |
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
| 286 |
|
| 287 |
+
# 添加提交说明和限制
|
| 288 |
gr.Markdown("""
|
| 289 |
### 📋 提交要求
|
| 290 |
+
- **登录要求**: 必须使用 HuggingFace 账号登录
|
| 291 |
+
- **账号限制**: 账号创建时间必须超过 60 天
|
| 292 |
+
- **提交频率**: 每个用户每天只能提交 1 次
|
| 293 |
- **文件格式**: 上传符合SAGE格式的JSON文件
|
| 294 |
- **组织信息**: 填写准确的组织名称(将显示在排行榜)
|
| 295 |
- **联系邮箱**: 提供有效邮箱用于结果通知
|
| 296 |
- **自动评测**: 提交后将自动进行LLM评测并更新排行榜
|
| 297 |
+
|
| 298 |
+
### 🔐 安全政策
|
| 299 |
+
为防止垃圾提交和确保评测质量,我们采用了严格的提交限制:
|
| 300 |
+
- 新账号需要等待60天才能提交(防止恶意注册)
|
| 301 |
+
- 每日提交限制确保排行榜质量和服务器稳定性
|
| 302 |
+
- 重复提交检查避免同一组织多次提交相同结果
|
| 303 |
""", elem_classes="markdown-text")
|
| 304 |
|
| 305 |
with gr.Row():
|
|
|
|
| 319 |
type="filepath"
|
| 320 |
)
|
| 321 |
|
| 322 |
+
# 登录和提交按钮
|
| 323 |
with gr.Row():
|
| 324 |
+
login_button = gr.LoginButton("🔐 Login with HuggingFace", size="lg")
|
| 325 |
submit_button = gr.Button("Submit Results", variant="primary", size="lg")
|
| 326 |
|
| 327 |
submission_result = gr.HTML()
|
| 328 |
|
| 329 |
+
def handle_submission(file_upload, org_name, email, profile: gr.OAuthProfile = None):
|
|
|
|
| 330 |
try:
|
| 331 |
+
# 1. 基本验证
|
| 332 |
if not file_upload:
|
| 333 |
return format_error("请选择要上传的文件")
|
| 334 |
if not org_name or not org_name.strip():
|
|
|
|
| 336 |
if not email or not email.strip():
|
| 337 |
return format_error("请输入邮箱地址")
|
| 338 |
|
| 339 |
+
# 2. 验证邮箱格式
|
| 340 |
+
_, parsed_email = parseaddr(email)
|
| 341 |
+
if "@" not in parsed_email:
|
| 342 |
+
return format_warning("请提供有效的邮箱地址")
|
| 343 |
+
|
| 344 |
+
# 3. 检查登录状态
|
| 345 |
+
if not profile:
|
| 346 |
+
return format_error("请先登录 HuggingFace 账号后再提交")
|
| 347 |
+
|
| 348 |
+
# 4. 检查用户提交资格 (账号年龄、每日限制、重复提交)
|
| 349 |
+
eligible, message = check_user_submission_eligibility(profile, org_name)
|
| 350 |
+
if not eligible:
|
| 351 |
+
return format_error(message)
|
| 352 |
+
|
| 353 |
+
# 5. 处理文件提交 (通过OSS)
|
| 354 |
from src.submission.simple_submit import process_sage_submission_simple
|
| 355 |
result = process_sage_submission_simple(file_upload, org_name, email)
|
| 356 |
|
| 357 |
+
# 6. 记录用户提交历史
|
| 358 |
+
record_success = record_user_submission(profile, org_name, email)
|
| 359 |
+
if not record_success:
|
| 360 |
+
print(f"⚠️ 提交历史记录失败,但提交已成功处理")
|
| 361 |
+
|
| 362 |
+
# 7. 添加成功提交的详细信息
|
| 363 |
success_info = f"""
|
| 364 |
<div style="background-color: #d4edda; border: 1px solid #c3e6cb; border-radius: 5px; padding: 15px; margin: 10px 0;">
|
| 365 |
<h4 style="color: #155724; margin-top: 0;">🎉 提交成功!</h4>
|
| 366 |
+
<p style="color: #155724; margin: 5px 0;"><strong>用户:</strong> {profile.username}</p>
|
| 367 |
<p style="color: #155724; margin: 5px 0;"><strong>组织:</strong> {org_name}</p>
|
| 368 |
<p style="color: #155724; margin: 5px 0;"><strong>邮箱:</strong> {email}</p>
|
| 369 |
<p style="color: #155724; margin: 5px 0;"><strong>提交时间:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 370 |
+
<p style="color: #155724; margin: 5px 0;"><strong>下次可提交:</strong> 明天</p>
|
| 371 |
+
<p style="color: #155724; margin-bottom: 0;">您的结果已通过OSS提交,将在5-10分钟内完成LLM评测并更新排行榜。</p>
|
| 372 |
</div>
|
| 373 |
"""
|
| 374 |
|
|
|
|
| 377 |
except ImportError as e:
|
| 378 |
return format_error(f"提交系统模块不可用: {e}")
|
| 379 |
except Exception as e:
|
| 380 |
+
import traceback
|
| 381 |
+
traceback.print_exc()
|
| 382 |
+
return format_error(f"提交过程中出现错误: {str(e)}")
|
| 383 |
|
| 384 |
submit_button.click(
|
| 385 |
handle_submission,
|