Spaces:
Sleeping
Sleeping
Update miniapp_leaderboard.py
Browse files- miniapp_leaderboard.py +21 -63
miniapp_leaderboard.py
CHANGED
|
@@ -74,65 +74,42 @@ def refresh():
|
|
| 74 |
return _load_df(APPROVED_PREFIX)
|
| 75 |
|
| 76 |
def _today_utc():
|
| 77 |
-
return datetime.datetime.utcnow().date().isoformat()
|
| 78 |
|
| 79 |
def _send_email_resend(subject: str, text: str):
|
| 80 |
-
# 可替换为 SendGrid/Mailgun
|
| 81 |
if not (RESEND_API_KEY and NOTIFY_EMAIL_TO and NOTIFY_EMAIL_FROM):
|
| 82 |
-
return
|
| 83 |
-
|
| 84 |
requests.post(
|
| 85 |
"https://api.resend.com/emails",
|
| 86 |
-
headers={
|
| 87 |
-
|
| 88 |
-
"Content-Type": "application/json",
|
| 89 |
-
},
|
| 90 |
-
json={
|
| 91 |
-
"from": NOTIFY_EMAIL_FROM,
|
| 92 |
-
"to": [NOTIFY_EMAIL_TO],
|
| 93 |
-
"subject": subject,
|
| 94 |
-
"text": text,
|
| 95 |
-
},
|
| 96 |
timeout=20,
|
| 97 |
)
|
| 98 |
|
| 99 |
def _already_submitted_today(api: HfApi, day: str, username: str) -> bool:
|
| 100 |
-
# 用“标记文件”判断:pending/YYYY-MM-DD/<username>/_submitted.json
|
| 101 |
marker = f"{PENDING_PREFIX}{day}/{username}/_submitted.json"
|
| 102 |
try:
|
| 103 |
files = api.list_repo_files(repo_id=LEADERBOARD_DATASET, repo_type="dataset")
|
| 104 |
return marker in files
|
| 105 |
except Exception:
|
| 106 |
-
#
|
| 107 |
-
return True
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# 不同版本字段可能略有差异;这里做多种兜底
|
| 115 |
-
username = getattr(request, "username", None) \
|
| 116 |
-
or (request.headers.get("x-forwarded-user") if request else None)
|
| 117 |
-
except Exception:
|
| 118 |
-
username = None
|
| 119 |
|
| 120 |
-
if not username:
|
| 121 |
-
return "Please sign in first (HF login) before submitting.", refresh()
|
| 122 |
-
|
| 123 |
-
# 2) 基础校验
|
| 124 |
if not model_name or not model_family or zip_file is None:
|
| 125 |
return "All fields are required.", refresh()
|
| 126 |
if not zip_file.name.endswith(".zip"):
|
| 127 |
return "Please upload a .zip file.", refresh()
|
| 128 |
-
|
| 129 |
if not HF_TOKEN or not LEADERBOARD_DATASET:
|
| 130 |
return "Server is not configured (HF_TOKEN / LEADERBOARD_DATASET).", refresh()
|
| 131 |
|
| 132 |
api = _api()
|
| 133 |
day = _today_utc()
|
| 134 |
|
| 135 |
-
# 3) 每天一次限制
|
| 136 |
if _already_submitted_today(api, day, username):
|
| 137 |
return f"Limit: you can only submit once per day. (user={username}, day={day})", refresh()
|
| 138 |
|
|
@@ -140,7 +117,6 @@ def submit(model_name, model_family, zip_file, request: gr.Request):
|
|
| 140 |
safe_model = _slug(model_name)
|
| 141 |
nonce = uuid.uuid4().hex[:6]
|
| 142 |
|
| 143 |
-
# 4) 按日期/用户名组织路径
|
| 144 |
base_dir = f"{PENDING_PREFIX}{day}/{username}/"
|
| 145 |
json_path = f"{base_dir}{now}-{safe_model}-{nonce}.json"
|
| 146 |
zip_path = f"{base_dir}{now}-{safe_model}-{nonce}.zip"
|
|
@@ -149,52 +125,36 @@ def submit(model_name, model_family, zip_file, request: gr.Request):
|
|
| 149 |
payload = {
|
| 150 |
"model name": model_name,
|
| 151 |
"model family": model_family,
|
| 152 |
-
"avg": 0,
|
| 153 |
-
"easy": 0,
|
| 154 |
-
"mid": 0,
|
| 155 |
-
"hard": 0,
|
| 156 |
"submitted_at": now,
|
| 157 |
"username": username,
|
| 158 |
"day": day,
|
| 159 |
}
|
| 160 |
|
| 161 |
-
# 5) 上传 JSON / ZIP / marker(marker 用于每天一次)
|
| 162 |
api.upload_file(
|
| 163 |
-
repo_id=LEADERBOARD_DATASET,
|
| 164 |
-
repo_type="dataset",
|
| 165 |
path_or_fileobj=io.BytesIO(json.dumps(payload, indent=2).encode("utf-8")),
|
| 166 |
path_in_repo=json_path,
|
| 167 |
commit_message=f"submit {model_name} by {username}",
|
| 168 |
)
|
| 169 |
|
| 170 |
api.upload_file(
|
| 171 |
-
repo_id=LEADERBOARD_DATASET,
|
| 172 |
-
repo_type="dataset",
|
| 173 |
path_or_fileobj=zip_file,
|
| 174 |
path_in_repo=zip_path,
|
| 175 |
commit_message=f"upload zip {model_name} by {username}",
|
| 176 |
)
|
| 177 |
|
| 178 |
api.upload_file(
|
| 179 |
-
repo_id=LEADERBOARD_DATASET,
|
| 180 |
-
repo_type="dataset",
|
| 181 |
path_or_fileobj=io.BytesIO(json.dumps({"submitted_at": now, "username": username, "day": day}, indent=2).encode("utf-8")),
|
| 182 |
path_in_repo=marker_path,
|
| 183 |
commit_message=f"marker {day} {username}",
|
| 184 |
)
|
| 185 |
|
| 186 |
-
# 6) 邮件提醒
|
| 187 |
_send_email_resend(
|
| 188 |
subject=f"[{APP_NAME}] New submission from {username} ({day})",
|
| 189 |
-
text=
|
| 190 |
-
f"New submission received.\n\n"
|
| 191 |
-
f"user: {username}\n"
|
| 192 |
-
f"day: {day}\n"
|
| 193 |
-
f"model: {model_name}\n"
|
| 194 |
-
f"family: {model_family}\n"
|
| 195 |
-
f"json: {json_path}\n"
|
| 196 |
-
f"zip: {zip_path}\n"
|
| 197 |
-
),
|
| 198 |
)
|
| 199 |
|
| 200 |
return "Submitted. Waiting for review.", refresh()
|
|
@@ -203,26 +163,24 @@ def submit(model_name, model_family, zip_file, request: gr.Request):
|
|
| 203 |
with gr.Blocks(title=f"{APP_NAME} leaderboard") as demo:
|
| 204 |
gr.Markdown(f"# {APP_NAME} Leaderboard")
|
| 205 |
|
| 206 |
-
leaderboard = gr.Dataframe(
|
| 207 |
-
value=_load_df(APPROVED_PREFIX),
|
| 208 |
-
interactive=False,
|
| 209 |
-
wrap=True,
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
refresh_btn = gr.Button("Refresh")
|
| 213 |
|
| 214 |
gr.Markdown("## Submit (login required)")
|
| 215 |
-
|
| 216 |
model_name = gr.Textbox(label="Model name")
|
| 217 |
model_family = gr.Textbox(label="Model family")
|
| 218 |
zip_file = gr.File(label="Upload zip", file_types=[".zip"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 220 |
status = gr.Markdown()
|
| 221 |
|
| 222 |
refresh_btn.click(refresh, outputs=[leaderboard])
|
| 223 |
submit_btn.click(
|
| 224 |
submit,
|
| 225 |
-
inputs=[model_name, model_family, zip_file],
|
| 226 |
outputs=[status, leaderboard],
|
| 227 |
)
|
| 228 |
|
|
|
|
| 74 |
return _load_df(APPROVED_PREFIX)
|
| 75 |
|
| 76 |
def _today_utc():
|
| 77 |
+
return datetime.datetime.utcnow().date().isoformat()
|
| 78 |
|
| 79 |
def _send_email_resend(subject: str, text: str):
|
|
|
|
| 80 |
if not (RESEND_API_KEY and NOTIFY_EMAIL_TO and NOTIFY_EMAIL_FROM):
|
| 81 |
+
return
|
|
|
|
| 82 |
requests.post(
|
| 83 |
"https://api.resend.com/emails",
|
| 84 |
+
headers={"Authorization": f"Bearer {RESEND_API_KEY}", "Content-Type": "application/json"},
|
| 85 |
+
json={"from": NOTIFY_EMAIL_FROM, "to": [NOTIFY_EMAIL_TO], "subject": subject, "text": text},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
timeout=20,
|
| 87 |
)
|
| 88 |
|
| 89 |
def _already_submitted_today(api: HfApi, day: str, username: str) -> bool:
|
|
|
|
| 90 |
marker = f"{PENDING_PREFIX}{day}/{username}/_submitted.json"
|
| 91 |
try:
|
| 92 |
files = api.list_repo_files(repo_id=LEADERBOARD_DATASET, repo_type="dataset")
|
| 93 |
return marker in files
|
| 94 |
except Exception:
|
| 95 |
+
return True # 更安全:查不到就当提交过
|
|
|
|
| 96 |
|
| 97 |
+
# ✅ 改这里:用 profile 取 username
|
| 98 |
+
def submit(model_name, model_family, zip_file, profile: gr.OAuthProfile):
|
| 99 |
+
if profile is None or not getattr(profile, "username", None):
|
| 100 |
+
return "Please sign in with Hugging Face first.", refresh()
|
| 101 |
+
username = profile.username
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if not model_name or not model_family or zip_file is None:
|
| 104 |
return "All fields are required.", refresh()
|
| 105 |
if not zip_file.name.endswith(".zip"):
|
| 106 |
return "Please upload a .zip file.", refresh()
|
|
|
|
| 107 |
if not HF_TOKEN or not LEADERBOARD_DATASET:
|
| 108 |
return "Server is not configured (HF_TOKEN / LEADERBOARD_DATASET).", refresh()
|
| 109 |
|
| 110 |
api = _api()
|
| 111 |
day = _today_utc()
|
| 112 |
|
|
|
|
| 113 |
if _already_submitted_today(api, day, username):
|
| 114 |
return f"Limit: you can only submit once per day. (user={username}, day={day})", refresh()
|
| 115 |
|
|
|
|
| 117 |
safe_model = _slug(model_name)
|
| 118 |
nonce = uuid.uuid4().hex[:6]
|
| 119 |
|
|
|
|
| 120 |
base_dir = f"{PENDING_PREFIX}{day}/{username}/"
|
| 121 |
json_path = f"{base_dir}{now}-{safe_model}-{nonce}.json"
|
| 122 |
zip_path = f"{base_dir}{now}-{safe_model}-{nonce}.zip"
|
|
|
|
| 125 |
payload = {
|
| 126 |
"model name": model_name,
|
| 127 |
"model family": model_family,
|
| 128 |
+
"avg": 0, "easy": 0, "mid": 0, "hard": 0,
|
|
|
|
|
|
|
|
|
|
| 129 |
"submitted_at": now,
|
| 130 |
"username": username,
|
| 131 |
"day": day,
|
| 132 |
}
|
| 133 |
|
|
|
|
| 134 |
api.upload_file(
|
| 135 |
+
repo_id=LEADERBOARD_DATASET, repo_type="dataset",
|
|
|
|
| 136 |
path_or_fileobj=io.BytesIO(json.dumps(payload, indent=2).encode("utf-8")),
|
| 137 |
path_in_repo=json_path,
|
| 138 |
commit_message=f"submit {model_name} by {username}",
|
| 139 |
)
|
| 140 |
|
| 141 |
api.upload_file(
|
| 142 |
+
repo_id=LEADERBOARD_DATASET, repo_type="dataset",
|
|
|
|
| 143 |
path_or_fileobj=zip_file,
|
| 144 |
path_in_repo=zip_path,
|
| 145 |
commit_message=f"upload zip {model_name} by {username}",
|
| 146 |
)
|
| 147 |
|
| 148 |
api.upload_file(
|
| 149 |
+
repo_id=LEADERBOARD_DATASET, repo_type="dataset",
|
|
|
|
| 150 |
path_or_fileobj=io.BytesIO(json.dumps({"submitted_at": now, "username": username, "day": day}, indent=2).encode("utf-8")),
|
| 151 |
path_in_repo=marker_path,
|
| 152 |
commit_message=f"marker {day} {username}",
|
| 153 |
)
|
| 154 |
|
|
|
|
| 155 |
_send_email_resend(
|
| 156 |
subject=f"[{APP_NAME}] New submission from {username} ({day})",
|
| 157 |
+
text=f"user: {username}\nday: {day}\nmodel: {model_name}\nfamily: {model_family}\njson: {json_path}\nzip: {zip_path}\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
return "Submitted. Waiting for review.", refresh()
|
|
|
|
| 163 |
with gr.Blocks(title=f"{APP_NAME} leaderboard") as demo:
|
| 164 |
gr.Markdown(f"# {APP_NAME} Leaderboard")
|
| 165 |
|
| 166 |
+
leaderboard = gr.Dataframe(value=_load_df(APPROVED_PREFIX), interactive=False, wrap=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
refresh_btn = gr.Button("Refresh")
|
| 168 |
|
| 169 |
gr.Markdown("## Submit (login required)")
|
|
|
|
| 170 |
model_name = gr.Textbox(label="Model name")
|
| 171 |
model_family = gr.Textbox(label="Model family")
|
| 172 |
zip_file = gr.File(label="Upload zip", file_types=[".zip"])
|
| 173 |
+
|
| 174 |
+
# ✅ 增加登录按钮
|
| 175 |
+
login_btn = gr.LoginButton()
|
| 176 |
+
|
| 177 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 178 |
status = gr.Markdown()
|
| 179 |
|
| 180 |
refresh_btn.click(refresh, outputs=[leaderboard])
|
| 181 |
submit_btn.click(
|
| 182 |
submit,
|
| 183 |
+
inputs=[model_name, model_family, zip_file], # profile 不需要作为 inputs 传,会自动注入
|
| 184 |
outputs=[status, leaderboard],
|
| 185 |
)
|
| 186 |
|