jbdel commited on
Commit
c554f7e
1 Parent(s): 0fec03e

pr_paper_page

Browse files
Files changed (1) hide show
  1. pr_paper_central_tab.py +110 -19
pr_paper_central_tab.py CHANGED
@@ -4,12 +4,13 @@ import pandas as pd
4
  from huggingface_hub import HfApi, hf_hub_download, CommitOperationAdd
5
  import json
6
  import os
 
7
 
8
 
9
- # PR function as before
10
  def create_pr_in_hf_dataset(new_entry, oauth_token: gr.OAuthToken):
11
  # Dataset and filename
12
- REPO_ID = 'IAMJB/paper-central-pr'
13
  FILENAME = 'data.json'
14
 
15
  # Initialize HfApi
@@ -21,7 +22,7 @@ def create_pr_in_hf_dataset(new_entry, oauth_token: gr.OAuthToken):
21
  # Create the repository if it doesn't exist
22
  api.create_repo(repo_id=REPO_ID, token=token, repo_type='dataset', exist_ok=True)
23
 
24
- # Check if data.json exists; if not, create it with empty list
25
  files = api.list_repo_files(REPO_ID, repo_type='dataset', token=token)
26
  if FILENAME not in files:
27
  # Initialize with empty list
@@ -95,9 +96,12 @@ def pr_paper_central_tab(paper_central_df):
95
  # Message to display errors or information
96
  message = gr.Markdown("", visible=False)
97
 
98
- # Define the fields dynamically
 
 
 
 
99
  fields = [
100
- {'name': 'paper_page', 'label': 'Paper Page'},
101
  {'name': 'github', 'label': 'GitHub URL'},
102
  {'name': 'conference_name', 'label': 'Conference Name'},
103
  {'name': 'type_', 'label': 'Type'}, # Renamed from 'type' to 'type_'
@@ -110,45 +114,81 @@ def pr_paper_central_tab(paper_central_df):
110
  input_fields[field['name']] = gr.Textbox(label=field['label'], visible=False)
111
 
112
  # Button to create PR
113
- create_pr_button = gr.Button("Create PR", visible=False)
 
114
 
115
  # Output message
116
  pr_message = gr.Markdown("", visible=False)
117
 
 
 
 
118
  # Function to handle arxiv_id submission and check login
119
  def check_login_and_handle_arxiv_id(arxiv_id, oauth_token: Optional[gr.OAuthToken]):
120
  if oauth_token is None:
 
121
  return [gr.update(value="Please log in to proceed.", visible=True)] + \
122
  [gr.update(visible=False) for _ in fields] + \
123
- [gr.update(visible=False)] # create_pr_button
 
124
  else:
 
 
 
 
 
 
125
  if arxiv_id not in paper_central_df['arxiv_id'].values:
126
- return [gr.update(value="arXiv ID not found. Please try again.", visible=True)] + \
127
- [gr.update(visible=False) for _ in fields] + [gr.update(visible=False)] # create_pr_button
128
- else:
129
- row = paper_central_df[paper_central_df['arxiv_id'] == arxiv_id].iloc[0]
130
- updates = [gr.update(value="", visible=False)] # message
131
  for field in fields:
132
- value = row.get(field['name'], "")
133
- updates.append(gr.update(value=value, visible=True))
134
  updates.append(gr.update(visible=True)) # create_pr_button
135
- return updates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  arxiv_id_button.click(
138
  fn=check_login_and_handle_arxiv_id,
139
  inputs=[arxiv_id_input],
140
- outputs=[message] + [input_fields[field['name']] for field in fields] + [create_pr_button]
 
 
141
  )
142
 
143
  # Function to create PR
144
- def create_pr(message, arxiv_id, paper_page, github, conference_name, type_, proceedings,
145
  oauth_token: Optional[gr.OAuthToken] = None):
146
  if oauth_token is None:
147
  return gr.update(value="Please log in first.", visible=True)
148
  else:
149
  new_entry = {
150
  'arxiv_id': arxiv_id,
151
- 'paper_page': paper_page,
152
  'github': github,
153
  'conference_name': conference_name,
154
  'type': type_,
@@ -159,7 +199,58 @@ def pr_paper_central_tab(paper_central_df):
159
  return gr.update(value=f"PR created: {pr_url}", visible=True)
160
 
161
  create_pr_button.click(
 
 
 
 
162
  fn=create_pr,
163
- inputs=[pr_message, arxiv_id_input] + [input_fields[field['name']] for field in fields],
164
  outputs=[pr_message]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  )
 
4
  from huggingface_hub import HfApi, hf_hub_download, CommitOperationAdd
5
  import json
6
  import os
7
+ import requests
8
 
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-test'
14
  FILENAME = 'data.json'
15
 
16
  # Initialize HfApi
 
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
 
96
  # Message to display errors or information
97
  message = gr.Markdown("", visible=False)
98
 
99
+ # Button to create paper page
100
+ create_paper_page_button = gr.Button("Create Paper Page", visible=False,
101
+ icon="https://huggingface.co/front/assets/huggingface_logo-noborder.svg")
102
+
103
+ # Define the fields dynamically (removed 'paper_page')
104
  fields = [
 
105
  {'name': 'github', 'label': 'GitHub URL'},
106
  {'name': 'conference_name', 'label': 'Conference Name'},
107
  {'name': 'type_', 'label': 'Type'}, # Renamed from 'type' to 'type_'
 
114
  input_fields[field['name']] = gr.Textbox(label=field['label'], visible=False)
115
 
116
  # Button to create PR
117
+ create_pr_button = gr.Button("Create PR", visible=False,
118
+ icon="https://huggingface.co/front/assets/huggingface_logo-noborder.svg")
119
 
120
  # Output message
121
  pr_message = gr.Markdown("", visible=False)
122
 
123
+ # Loading message
124
+ loading_message = gr.Markdown("Creating PR, please wait...", visible=False)
125
+
126
  # Function to handle arxiv_id submission and check login
127
  def check_login_and_handle_arxiv_id(arxiv_id, oauth_token: Optional[gr.OAuthToken]):
128
  if oauth_token is None:
129
+ # Not logged in
130
  return [gr.update(value="Please log in to proceed.", visible=True)] + \
131
  [gr.update(visible=False) for _ in fields] + \
132
+ [gr.update(visible=False)] + [gr.update(visible=False)] + [
133
+ gr.update(visible=False)] # create_pr_button, create_paper_page_button, pr_message
134
  else:
135
+ ACCESS_TOKEN = os.getenv('paper_space_pr_token')
136
+ access_token_exists = ACCESS_TOKEN is not None
137
+
138
+ # Prepare the updates list
139
+ updates = []
140
+
141
  if arxiv_id not in paper_central_df['arxiv_id'].values:
142
+ # arXiv ID not found
143
+ updates.append(gr.update(value="arXiv ID not found. You can create a paper page.", visible=True))
144
+ # Input fields are empty
 
 
145
  for field in fields:
146
+ updates.append(gr.update(value="", visible=True))
 
147
  updates.append(gr.update(visible=True)) # create_pr_button
148
+ # Show 'Create Paper Page' button if access token exists
149
+ updates.append(gr.update(visible=access_token_exists)) # create_paper_page_button
150
+ updates.append(gr.update(visible=False)) # pr_message
151
+ else:
152
+ # arXiv ID found
153
+ row = paper_central_df[paper_central_df['arxiv_id'] == arxiv_id].iloc[0]
154
+ paper_page = row.get('paper_page', "")
155
+ if not paper_page:
156
+ # paper_page missing or empty
157
+ updates.append(gr.update(value="Paper page not found. You can create one.", visible=True))
158
+ for field in fields:
159
+ value = row.get(field['name'], "")
160
+ updates.append(gr.update(value=value, visible=True))
161
+ updates.append(gr.update(visible=True)) # create_pr_button
162
+ updates.append(gr.update(visible=access_token_exists)) # create_paper_page_button
163
+ updates.append(gr.update(visible=False)) # pr_message
164
+ else:
165
+ # paper_page exists
166
+ updates.append(gr.update(value="", visible=False)) # message
167
+ for field in fields:
168
+ value = row.get(field['name'], "")
169
+ updates.append(gr.update(value=value, visible=True))
170
+ updates.append(gr.update(visible=True)) # create_pr_button
171
+ updates.append(gr.update(visible=False)) # create_paper_page_button
172
+ updates.append(gr.update(visible=False)) # pr_message
173
+
174
+ return updates
175
 
176
  arxiv_id_button.click(
177
  fn=check_login_and_handle_arxiv_id,
178
  inputs=[arxiv_id_input],
179
+ outputs=[message] + [input_fields[field['name']] for field in fields] + [create_pr_button,
180
+ create_paper_page_button,
181
+ pr_message]
182
  )
183
 
184
  # Function to create PR
185
+ def create_pr(arxiv_id, github, conference_name, type_, proceedings,
186
  oauth_token: Optional[gr.OAuthToken] = None):
187
  if oauth_token is None:
188
  return gr.update(value="Please log in first.", visible=True)
189
  else:
190
  new_entry = {
191
  'arxiv_id': arxiv_id,
 
192
  'github': github,
193
  'conference_name': conference_name,
194
  'type': type_,
 
199
  return gr.update(value=f"PR created: {pr_url}", visible=True)
200
 
201
  create_pr_button.click(
202
+ fn=lambda: gr.update(visible=True), # Show loading message
203
+ inputs=[],
204
+ outputs=[loading_message]
205
+ ).then(
206
  fn=create_pr,
207
+ inputs=[arxiv_id_input] + [input_fields[field['name']] for field in fields],
208
  outputs=[pr_message]
209
+ ).then(
210
+ fn=lambda: gr.update(visible=False), # Hide loading message
211
+ inputs=[],
212
+ outputs=[loading_message]
213
+ )
214
+
215
+ # Function to create paper page
216
+ def create_paper_page(arxiv_id):
217
+ # Implement the API calls to create the paper page
218
+ INDEX_URL = "https://huggingface.co/api/papers/index"
219
+ SUBMIT_URL = "https://huggingface.co/api/papers/submit"
220
+ ACCESS_TOKEN = os.getenv('paper_space_pr_token')
221
+
222
+ if not ACCESS_TOKEN:
223
+ return gr.update(value="Server error: Access token not found.", visible=True)
224
+
225
+ # Index the paper
226
+ payload_index = {"arxivId": arxiv_id}
227
+ headers = {
228
+ "Authorization": f"Bearer {ACCESS_TOKEN}",
229
+ "Content-Type": "application/json"
230
+ }
231
+
232
+ response_index = requests.post(INDEX_URL, json=payload_index, headers=headers)
233
+ if response_index.status_code == 200:
234
+ # Successfully indexed, now submit the paper
235
+ paper_id = arxiv_id # Assuming paperId is the same as arxivId
236
+ payload_submit = {
237
+ "paperId": paper_id,
238
+ "comment": "",
239
+ "mediaUrls": []
240
+ }
241
+ response_submit = requests.post(SUBMIT_URL, json=payload_submit, headers=headers)
242
+ if response_submit.status_code == 200:
243
+ return gr.update(value="Paper page created successfully.", visible=True)
244
+ else:
245
+ return gr.update(
246
+ value=f"Failed to submit paper: {response_submit.status_code}, {response_submit.text}",
247
+ visible=True)
248
+ else:
249
+ return gr.update(value=f"Failed to index paper: {response_index.status_code}, {response_index.text}",
250
+ visible=True)
251
+
252
+ create_paper_page_button.click(
253
+ fn=create_paper_page,
254
+ inputs=[arxiv_id_input],
255
+ outputs=[message]
256
  )