IAMJB commited on
Commit
ab663e5
·
1 Parent(s): e2605ea
Files changed (1) hide show
  1. pr_paper_central_tab.py +17 -28
pr_paper_central_tab.py CHANGED
@@ -9,32 +9,31 @@ import requests
9
 
10
  # PR function remains the same
11
  def create_pr_in_hf_dataset(new_entry, oauth_token: gr.OAuthToken):
12
- # Dataset and filename
13
  REPO_ID = 'IAMJB/paper-central-pr'
14
- FILENAME = 'data.json'
15
 
16
  # Initialize HfApi
17
  api = HfApi()
18
  token = oauth_token.token
19
 
20
- # Ensure the repository exists and has an initial empty data.json if not present
21
  try:
22
  # Create the repository if it doesn't exist
23
  api.create_repo(repo_id=REPO_ID, token=token, repo_type='dataset', exist_ok=True)
24
 
25
- # Check if data.json exists; if not, create it with an empty list
26
  files = api.list_repo_files(REPO_ID, repo_type='dataset', token=token)
27
- if FILENAME not in files:
28
- # Initialize with empty list
29
- empty_data = []
30
- temp_filename = 'temp_data.json'
31
  with open(temp_filename, 'w') as f:
32
- json.dump(empty_data, f)
33
- commit = CommitOperationAdd(path_in_repo=FILENAME, path_or_fileobj=temp_filename)
34
  api.create_commit(
35
  repo_id=REPO_ID,
36
  operations=[commit],
37
- commit_message="Initialize data.json",
38
  repo_type="dataset",
39
  token=token,
40
  )
@@ -42,26 +41,16 @@ def create_pr_in_hf_dataset(new_entry, oauth_token: gr.OAuthToken):
42
  except Exception as e:
43
  return f"Error creating or accessing repository: {e}"
44
 
45
- # Download existing data from the dataset
46
- try:
47
- # Download the existing data.json file
48
- local_filepath = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, repo_type='dataset', token=token)
49
- with open(local_filepath, 'r') as f:
50
- data = json.load(f)
51
- except Exception as e:
52
- print(f"Error downloading existing data: {e}")
53
- data = []
54
-
55
- # Add the new entry
56
- data.append(new_entry)
57
-
58
- # Save to temporary file
59
- temp_filename = 'temp_data.json'
60
  with open(temp_filename, 'w') as f:
61
- json.dump(data, f, indent=2)
 
 
 
62
 
63
  # Create commit operation
64
- commit = CommitOperationAdd(path_in_repo=FILENAME, path_or_fileobj=temp_filename)
65
 
66
  # Create PR
67
  try:
 
9
 
10
  # PR function remains the same
11
  def create_pr_in_hf_dataset(new_entry, oauth_token: gr.OAuthToken):
12
+ # Dataset and directory
13
  REPO_ID = 'IAMJB/paper-central-pr'
14
+ DATA_DIR = 'data'
15
 
16
  # Initialize HfApi
17
  api = HfApi()
18
  token = oauth_token.token
19
 
20
+ # Ensure the repository exists and the data directory is initialized
21
  try:
22
  # Create the repository if it doesn't exist
23
  api.create_repo(repo_id=REPO_ID, token=token, repo_type='dataset', exist_ok=True)
24
 
25
+ # Check if the data directory exists; if not, create it
26
  files = api.list_repo_files(REPO_ID, repo_type='dataset', token=token)
27
+ if DATA_DIR not in files:
28
+ # Create an empty directory (you can add a .gitkeep file)
29
+ temp_filename = 'temp_gitkeep'
 
30
  with open(temp_filename, 'w') as f:
31
+ pass # Empty file
32
+ commit = CommitOperationAdd(path_in_repo=f"{DATA_DIR}/.gitkeep", path_or_fileobj=temp_filename)
33
  api.create_commit(
34
  repo_id=REPO_ID,
35
  operations=[commit],
36
+ commit_message="Initialize data directory",
37
  repo_type="dataset",
38
  token=token,
39
  )
 
41
  except Exception as e:
42
  return f"Error creating or accessing repository: {e}"
43
 
44
+ # Save the new entry to a temporary file
45
+ temp_filename = f"{new_entry['arxiv_id']}.json"
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  with open(temp_filename, 'w') as f:
47
+ json.dump(new_entry, f, indent=2)
48
+
49
+ # Define the path in the repository
50
+ path_in_repo = f"{DATA_DIR}/{temp_filename}"
51
 
52
  # Create commit operation
53
+ commit = CommitOperationAdd(path_in_repo=path_in_repo, path_or_fileobj=temp_filename)
54
 
55
  # Create PR
56
  try: