Spaces:
Running
Running
Peiran
commited on
Commit
·
591d755
1
Parent(s):
ed54e20
Persist evaluations: write CSV locally and optionally push to Space repo when HF_TOKEN/SPACE_ID available
Browse files
app.py
CHANGED
|
@@ -2,9 +2,14 @@ import csv
|
|
| 2 |
import itertools
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
|
|
|
| 5 |
from typing import Dict, List, Tuple
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
BASE_DIR = os.path.dirname(__file__)
|
|
@@ -141,6 +146,34 @@ def _append_evaluation(task_name: str, pair: Dict[str, str], scores: Dict[str, i
|
|
| 141 |
row.update(scores)
|
| 142 |
writer.writerow(row)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
def on_task_change(task_name: str, _state_pairs: List[Dict[str, str]]):
|
| 146 |
pairs = load_task(task_name)
|
|
|
|
| 2 |
import itertools
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
from typing import Dict, List, Tuple
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
+
try:
|
| 10 |
+
from huggingface_hub import HfApi
|
| 11 |
+
except Exception: # optional dependency at runtime
|
| 12 |
+
HfApi = None # type: ignore
|
| 13 |
|
| 14 |
|
| 15 |
BASE_DIR = os.path.dirname(__file__)
|
|
|
|
| 146 |
row.update(scores)
|
| 147 |
writer.writerow(row)
|
| 148 |
|
| 149 |
+
# Optionally push updated CSV to the Space repo if credentials are available
|
| 150 |
+
_try_push_to_hub(csv_path)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def _try_push_to_hub(csv_path: str) -> None:
|
| 154 |
+
"""Attempt to commit the CSV to the current Space repo if HF_TOKEN and SPACE_ID exist.
|
| 155 |
+
Safe no-op if huggingface_hub isn't available or env vars are missing.
|
| 156 |
+
"""
|
| 157 |
+
if HfApi is None:
|
| 158 |
+
return
|
| 159 |
+
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 160 |
+
space_id = os.environ.get("SPACE_ID")
|
| 161 |
+
if not token or not space_id:
|
| 162 |
+
return
|
| 163 |
+
try:
|
| 164 |
+
api = HfApi(token=token)
|
| 165 |
+
rel_path = os.path.relpath(csv_path, BASE_DIR).replace(os.sep, "/")
|
| 166 |
+
api.upload_file(
|
| 167 |
+
path_or_fileobj=csv_path,
|
| 168 |
+
path_in_repo=rel_path,
|
| 169 |
+
repo_id=space_id,
|
| 170 |
+
repo_type="space",
|
| 171 |
+
commit_message=f"Update eval CSV: {rel_path} at {datetime.utcnow().isoformat()}",
|
| 172 |
+
)
|
| 173 |
+
except Exception:
|
| 174 |
+
# Silently ignore push errors to avoid breaking the UI flow
|
| 175 |
+
pass
|
| 176 |
+
|
| 177 |
|
| 178 |
def on_task_change(task_name: str, _state_pairs: List[Dict[str, str]]):
|
| 179 |
pairs = load_task(task_name)
|