Spaces:
Sleeping
Sleeping
Commit ·
2d76c4b
1
Parent(s): ba9cbd5
push to remote hf dataset
Browse files- src/database.py +30 -27
src/database.py
CHANGED
|
@@ -3,11 +3,14 @@
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import sqlite3
|
|
|
|
| 6 |
import threading
|
| 7 |
from contextlib import contextmanager
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from typing import Optional
|
| 10 |
|
|
|
|
|
|
|
| 11 |
REQUIRED_TABLES = ('tasks', 'assignments', 'task_config', 'answers', 'allocation_history')
|
| 12 |
|
| 13 |
|
|
@@ -130,30 +133,30 @@ class DB:
|
|
| 130 |
)
|
| 131 |
return cur.rowcount or 0
|
| 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 |
-
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import sqlite3
|
| 6 |
+
import subprocess
|
| 7 |
import threading
|
| 8 |
from contextlib import contextmanager
|
| 9 |
from datetime import datetime, timezone
|
| 10 |
from typing import Optional
|
| 11 |
|
| 12 |
+
from huggingface_hub import CommitOperationAdd, HfApi
|
| 13 |
+
|
| 14 |
REQUIRED_TABLES = ('tasks', 'assignments', 'task_config', 'answers', 'allocation_history')
|
| 15 |
|
| 16 |
|
|
|
|
| 133 |
)
|
| 134 |
return cur.rowcount or 0
|
| 135 |
|
| 136 |
+
# ========== Auto Commit (For HF Spaces) ==========
|
| 137 |
+
def commit_and_push_db(self) -> None:
|
| 138 |
+
"""Commit and push the database to the repository."""
|
| 139 |
+
space_id = os.getenv('SPACE_ID', None)
|
| 140 |
+
if not space_id:
|
| 141 |
+
return
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
repo_root = subprocess.run(
|
| 145 |
+
['git', 'rev-parse', '--show-toplevel'],
|
| 146 |
+
check=True,
|
| 147 |
+
capture_output=True,
|
| 148 |
+
text=True,
|
| 149 |
+
).stdout.strip()
|
| 150 |
+
|
| 151 |
+
ts = _get_time_now()
|
| 152 |
+
db_rel = os.path.relpath(self.db_path, repo_root)
|
| 153 |
+
subprocess.run(['git', 'add', db_rel], check=True, cwd=repo_root)
|
| 154 |
+
api = HfApi(token=os.getenv('HF_TOKEN', None))
|
| 155 |
+
api.create_commit(
|
| 156 |
+
repo_id='JaneDing2025/IconEval_Pilot',
|
| 157 |
+
repo_type='dataset',
|
| 158 |
+
operations=[CommitOperationAdd(path_in_repo=db_rel, path_or_fileobj=self.db_path)],
|
| 159 |
+
commit_message=f'update db {ts}',
|
| 160 |
+
)
|
| 161 |
+
except Exception as e:
|
| 162 |
+
raise RuntimeError(f'Failed to commit and push the database: {e}')
|