Spaces:
Sleeping
Sleeping
Peiran
commited on
Commit
·
b25a877
1
Parent(s):
43656b3
Improve Hub upload robustness: return detailed error messages to UI and print exceptions to logs
Browse files
app.py
CHANGED
|
@@ -159,16 +159,19 @@ def _append_local_persist_csv(task_name: str, row: Dict[str, object]) -> bool:
|
|
| 159 |
return False
|
| 160 |
|
| 161 |
|
| 162 |
-
def _upload_eval_record_to_dataset(task_name: str, row: Dict[str, object]) -> bool:
|
| 163 |
"""Upload a single-eval JSONL record to a dataset repo.
|
| 164 |
Repo is taken from EVAL_REPO_ID env or defaults to 'peiranli0930/VisEval'.
|
|
|
|
| 165 |
"""
|
| 166 |
if HfApi is None:
|
| 167 |
-
return False
|
| 168 |
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 169 |
repo_id = os.environ.get("EVAL_REPO_ID", "peiranli0930/VisEval")
|
| 170 |
-
if not token
|
| 171 |
-
return False
|
|
|
|
|
|
|
| 172 |
try:
|
| 173 |
from huggingface_hub import CommitOperationAdd
|
| 174 |
|
|
@@ -185,9 +188,14 @@ def _upload_eval_record_to_dataset(task_name: str, row: Dict[str, object]) -> bo
|
|
| 185 |
operations=operations,
|
| 186 |
commit_message=f"Add eval {folder} {row.get('test_id')} {uid}",
|
| 187 |
)
|
| 188 |
-
return True
|
| 189 |
-
except Exception:
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
def on_task_change(task_name: str, _state_pairs: List[Dict[str, str]]):
|
|
@@ -260,12 +268,12 @@ def on_submit(
|
|
| 260 |
}
|
| 261 |
row = _build_eval_row(pair, score_map)
|
| 262 |
ok_local = _append_local_persist_csv(task_name, row)
|
| 263 |
-
ok_hub = _upload_eval_record_to_dataset(task_name, row)
|
| 264 |
|
| 265 |
next_index = min(index + 1, len(pairs) - 1)
|
| 266 |
info = f"已保存 Test ID {pair['test_id']} 的评价结果。"
|
| 267 |
info += " 本地持久化" + ("成功" if ok_local else "失败") + "。"
|
| 268 |
-
info += " 上传Hub" + ("成功" if ok_hub else "失败") + "。"
|
| 269 |
|
| 270 |
if next_index != index:
|
| 271 |
pair = pairs[next_index]
|
|
|
|
| 159 |
return False
|
| 160 |
|
| 161 |
|
| 162 |
+
def _upload_eval_record_to_dataset(task_name: str, row: Dict[str, object]) -> tuple[bool, str]:
|
| 163 |
"""Upload a single-eval JSONL record to a dataset repo.
|
| 164 |
Repo is taken from EVAL_REPO_ID env or defaults to 'peiranli0930/VisEval'.
|
| 165 |
+
Returns (ok, message) for UI feedback and debugging.
|
| 166 |
"""
|
| 167 |
if HfApi is None:
|
| 168 |
+
return False, "huggingface_hub 未安装"
|
| 169 |
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 170 |
repo_id = os.environ.get("EVAL_REPO_ID", "peiranli0930/VisEval")
|
| 171 |
+
if not token:
|
| 172 |
+
return False, "未找到写入 Token (HF_TOKEN/HUGGINGFACEHUB_API_TOKEN)"
|
| 173 |
+
if not repo_id:
|
| 174 |
+
return False, "未设置 EVAL_REPO_ID"
|
| 175 |
try:
|
| 176 |
from huggingface_hub import CommitOperationAdd
|
| 177 |
|
|
|
|
| 188 |
operations=operations,
|
| 189 |
commit_message=f"Add eval {folder} {row.get('test_id')} {uid}",
|
| 190 |
)
|
| 191 |
+
return True, f"上传成功: {repo_id}/{path_in_repo}"
|
| 192 |
+
except Exception as e:
|
| 193 |
+
# Print to logs for debugging in Space
|
| 194 |
+
try:
|
| 195 |
+
print("[VisArena] Upload to dataset failed:", repr(e))
|
| 196 |
+
except Exception:
|
| 197 |
+
pass
|
| 198 |
+
return False, f"异常: {type(e).__name__}: {e}"
|
| 199 |
|
| 200 |
|
| 201 |
def on_task_change(task_name: str, _state_pairs: List[Dict[str, str]]):
|
|
|
|
| 268 |
}
|
| 269 |
row = _build_eval_row(pair, score_map)
|
| 270 |
ok_local = _append_local_persist_csv(task_name, row)
|
| 271 |
+
ok_hub, hub_msg = _upload_eval_record_to_dataset(task_name, row)
|
| 272 |
|
| 273 |
next_index = min(index + 1, len(pairs) - 1)
|
| 274 |
info = f"已保存 Test ID {pair['test_id']} 的评价结果。"
|
| 275 |
info += " 本地持久化" + ("成功" if ok_local else "失败") + "。"
|
| 276 |
+
info += " 上传Hub" + ("成功" if ok_hub else "失败") + (f"({hub_msg})" if hub_msg else "") + "。"
|
| 277 |
|
| 278 |
if next_index != index:
|
| 279 |
pair = pairs[next_index]
|