hailey2024 commited on
Commit
4180588
·
1 Parent(s): 02d193f

use hf api

Browse files
Files changed (1) hide show
  1. src/database.py +11 -5
src/database.py CHANGED
@@ -9,6 +9,8 @@ from contextlib import contextmanager
9
  from datetime import datetime, timezone
10
  from typing import Optional
11
 
 
 
12
  REQUIRED_TABLES = ('tasks', 'assignments', 'task_config', 'answers', 'allocation_history')
13
 
14
 
@@ -134,7 +136,8 @@ class DB:
134
  # ========== Auto Commit (For HF Spaces) ==========
135
  def commit_and_push_db(self) -> None:
136
  """Commit and push the database to the repository."""
137
- if not os.getenv('SPACE_ID'):
 
138
  return
139
 
140
  try:
@@ -148,9 +151,12 @@ class DB:
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
- diff = subprocess.run(['git', 'diff', '--cached', '--quiet'], check=False, cwd=repo_root)
152
- if diff.returncode != 0:
153
- subprocess.run(['git', 'commit', '-m', f'update db {ts}'], check=True, cwd=repo_root)
154
- subprocess.run(['git', 'push', 'origin', 'main'], check=True, cwd=repo_root)
 
 
 
155
  except Exception as e:
156
  raise RuntimeError(f'Failed to commit and push the database: {e}')
 
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
 
 
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:
 
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=space_id,
157
+ repo_type='space',
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}')