hailey2024 commited on
Commit
2d76c4b
·
1 Parent(s): ba9cbd5

push to remote hf dataset

Browse files
Files changed (1) hide show
  1. 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
- # # ========== Auto Commit (For HF Spaces) ==========
134
- # def commit_and_push_db(self) -> None:
135
- # """Commit and push the database to the repository."""
136
- # space_id = os.getenv('SPACE_ID', None)
137
- # if not space_id:
138
- # return
139
-
140
- # try:
141
- # repo_root = subprocess.run(
142
- # ['git', 'rev-parse', '--show-toplevel'],
143
- # check=True,
144
- # capture_output=True,
145
- # text=True,
146
- # ).stdout.strip()
147
-
148
- # ts = _get_time_now()
149
- # db_rel = os.path.relpath(self.db_path, repo_root)
150
- # subprocess.run(['git', 'add', db_rel], check=True, cwd=repo_root)
151
- # api = HfApi(token=os.getenv('HF_TOKEN', None))
152
- # api.create_commit(
153
- # repo_id='JaneDing2025/IconEval',
154
- # repo_type='space',
155
- # operations=[CommitOperationAdd(path_in_repo=db_rel, path_or_fileobj=self.db_path)],
156
- # commit_message=f'update db {ts}',
157
- # )
158
- # except Exception as e:
159
- # raise RuntimeError(f'Failed to commit and push the database: {e}')
 
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}')