Abhishek Thakur commited on
Commit
177eef9
·
1 Parent(s): 6389ea3

add rules, check oauth

Browse files
competitions/app.py CHANGED
@@ -2,6 +2,7 @@ import datetime
2
  import os
3
  import threading
4
 
 
5
  from fastapi import FastAPI, File, Form, Request, UploadFile
6
  from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
7
  from fastapi.staticfiles import StaticFiles
@@ -34,7 +35,7 @@ VERSION_COMMIT_ID = os.environ.get("VERSION_COMMIT_ID", "0687567")
34
  disable_progress_bars()
35
 
36
  COMP_INFO = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
37
-
38
 
39
  try:
40
  REQUIREMENTS_FNAME = hf_hub_download(
@@ -99,6 +100,7 @@ async def read_form(request: Request):
99
  "logo": COMP_INFO.logo_url,
100
  "competition_type": COMP_INFO.competition_type,
101
  "version_commit_id": VERSION_COMMIT_ID[:7],
 
102
  }
103
  return templates.TemplateResponse("index.html", context)
104
 
@@ -125,7 +127,11 @@ async def oauth_logout(request: Request):
125
  async def use_oauth(request: Request):
126
  if USE_OAUTH == 1:
127
  if request.session.get("oauth_info") is not None:
128
- return {"response": 2}
 
 
 
 
129
  return {"response": USE_OAUTH}
130
 
131
 
@@ -145,6 +151,13 @@ async def get_dataset_info(request: Request):
145
  return resp
146
 
147
 
 
 
 
 
 
 
 
148
  @app.get("/submission_info", response_class=JSONResponse)
149
  async def get_submission_info(request: Request):
150
  info = COMP_INFO.submission_desc
 
2
  import os
3
  import threading
4
 
5
+ import requests
6
  from fastapi import FastAPI, File, Form, Request, UploadFile
7
  from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
8
  from fastapi.staticfiles import StaticFiles
 
35
  disable_progress_bars()
36
 
37
  COMP_INFO = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
38
+ RULES_AVAILABLE = COMP_INFO.rules is not None
39
 
40
  try:
41
  REQUIREMENTS_FNAME = hf_hub_download(
 
100
  "logo": COMP_INFO.logo_url,
101
  "competition_type": COMP_INFO.competition_type,
102
  "version_commit_id": VERSION_COMMIT_ID[:7],
103
+ "rules_available": RULES_AVAILABLE,
104
  }
105
  return templates.TemplateResponse("index.html", context)
106
 
 
127
  async def use_oauth(request: Request):
128
  if USE_OAUTH == 1:
129
  if request.session.get("oauth_info") is not None:
130
+ try:
131
+ utils.user_authentication(request.session.get("oauth_info")["access_token"])
132
+ return {"response": 2}
133
+ except requests.exceptions.JSONDecodeError:
134
+ return {"response": USE_OAUTH}
135
  return {"response": USE_OAUTH}
136
 
137
 
 
151
  return resp
152
 
153
 
154
+ @app.get("/rules", response_class=JSONResponse)
155
+ async def get_rules(request: Request):
156
+ if COMP_INFO.rules is not None:
157
+ return {"response": COMP_INFO.rules}
158
+ return {"response": "No rules available."}
159
+
160
+
161
  @app.get("/submission_info", response_class=JSONResponse)
162
  async def get_submission_info(request: Request):
163
  info = COMP_INFO.submission_desc
competitions/info.py CHANGED
@@ -43,6 +43,17 @@ class CompetitionInfo:
43
  except Exception:
44
  self.submission_desc = None
45
 
 
 
 
 
 
 
 
 
 
 
 
46
  if self.config["EVAL_METRIC"] == "custom":
47
  if "SCORING_METRIC" not in self.config:
48
  raise ValueError(
@@ -144,3 +155,7 @@ class CompetitionInfo:
144
  raise Exception("Please provide a single SCORING_METRIC in the competition config file: conf.json")
145
  return self.config["SCORING_METRIC"]
146
  return self.config["EVAL_METRIC"]
 
 
 
 
 
43
  except Exception:
44
  self.submission_desc = None
45
 
46
+ try:
47
+ rules_md = hf_hub_download(
48
+ repo_id=self.competition_id,
49
+ filename="RULES.md",
50
+ use_auth_token=self.autotrain_token,
51
+ repo_type="dataset",
52
+ )
53
+ self.rules_md = self.load_md(rules_md)
54
+ except Exception:
55
+ self.rules_md = None
56
+
57
  if self.config["EVAL_METRIC"] == "custom":
58
  if "SCORING_METRIC" not in self.config:
59
  raise ValueError(
 
155
  raise Exception("Please provide a single SCORING_METRIC in the competition config file: conf.json")
156
  return self.config["SCORING_METRIC"]
157
  return self.config["EVAL_METRIC"]
158
+
159
+ @property
160
+ def rules(self):
161
+ return self.rules_md
competitions/templates/index.html CHANGED
@@ -240,7 +240,28 @@
240
  });
241
  }
242
 
243
- // Find the 'Home' link in the sidebar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  const homeLink = document.getElementById('home');
245
  const datasetLink = document.getElementById('dataset');
246
  const publicLBLink = document.getElementById('public_lb');
@@ -248,6 +269,7 @@
248
  const newSubmission = document.getElementById('new_submission');
249
  const mySubmissions = document.getElementById('my_submissions');
250
  const submissionInfo = document.getElementById('submission_info');
 
251
 
252
  // Add a click event listener to the 'Home' link
253
  homeLink.addEventListener('click', function (event) {
@@ -282,6 +304,10 @@
282
  event.preventDefault(); // Prevent the default link behavior
283
  fetchAndDisplaySubmissionInfo(); // Fetch and display info on click
284
  });
 
 
 
 
285
 
286
  // Fetch and display info when the page loads
287
  fetchAndDisplayCompetitionInfo();
@@ -358,6 +384,20 @@
358
  <span class="flex-1 ms-3 whitespace-nowrap">Dataset</span>
359
  </a>
360
  </li>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  <li>
362
  <button type="button"
363
  class="flex items-center w-full p-2 text-base text-gray-900 transition duration-75 rounded-lg group hover:bg-gray-100"
 
240
  });
241
  }
242
 
243
+ function fetchAndDisplayRules() {
244
+ const articleLoadingSpinner = document.getElementById('articleLoadingSpinner');
245
+ articleLoadingSpinner.classList.remove('hidden');
246
+ fetch('/rules')
247
+ .then(response => {
248
+ if (!response.ok) {
249
+ throw new Error('Network response was not ok');
250
+ }
251
+ return response.json(); // Parse the JSON response
252
+ })
253
+ .then(data => {
254
+ // Populate the 'content' div with the HTML from the response
255
+ const contentDiv = document.getElementById('content');
256
+ contentDiv.innerHTML = marked.parse(data.response);
257
+ articleLoadingSpinner.classList.add('hidden');
258
+ })
259
+ .catch(error => {
260
+ console.error('There has been a problem with your fetch operation:', error);
261
+ articleLoadingSpinner.classList.add('hidden');
262
+ });
263
+ }
264
+
265
  const homeLink = document.getElementById('home');
266
  const datasetLink = document.getElementById('dataset');
267
  const publicLBLink = document.getElementById('public_lb');
 
269
  const newSubmission = document.getElementById('new_submission');
270
  const mySubmissions = document.getElementById('my_submissions');
271
  const submissionInfo = document.getElementById('submission_info');
272
+ const rulesLink = document.getElementById('rules');
273
 
274
  // Add a click event listener to the 'Home' link
275
  homeLink.addEventListener('click', function (event) {
 
304
  event.preventDefault(); // Prevent the default link behavior
305
  fetchAndDisplaySubmissionInfo(); // Fetch and display info on click
306
  });
307
+ rulesLink.addEventListener('click', function (event) {
308
+ event.preventDefault(); // Prevent the default link behavior
309
+ fetchAndDisplayRules(); // Fetch and display info on click
310
+ });
311
 
312
  // Fetch and display info when the page loads
313
  fetchAndDisplayCompetitionInfo();
 
384
  <span class="flex-1 ms-3 whitespace-nowrap">Dataset</span>
385
  </a>
386
  </li>
387
+ {% if rules_available %}
388
+ <li>
389
+ <a href="#" id="rules"
390
+ class="flex items-center p-2 text-gray-900 rounded-lg hover:bg-gray-100 group">
391
+ <svg class="flex-shrink-0 w-5 h-5 text-gray-500 transition duration-75 group-hover:text-gray-900"
392
+ aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor"
393
+ viewBox="0 0 18 18">
394
+ <path
395
+ d="M6.143 0H1.857A1.857 1.857 0 0 0 0 1.857v4.286C0 7.169.831 8 1.857 8h4.286A1.857 1.857 0 0 0 8 6.143V1.857A1.857 1.857 0 0 0 6.143 0Zm10 0h-4.286A1.857 1.857 0 0 0 10 1.857v4.286C10 7.169 10.831 8 11.857 8h4.286A1.857 1.857 0 0 0 18 6.143V1.857A1.857 1.857 0 0 0 16.143 0Zm-10 10H1.857A1.857 1.857 0 0 0 0 11.857v4.286C0 17.169.831 18 1.857 18h4.286A1.857 1.857 0 0 0 8 16.143v-4.286A1.857 1.857 0 0 0 6.143 10Zm10 0h-4.286A1.857 1.857 0 0 0 10 11.857v4.286c0 1.026.831 1.857 1.857 1.857h4.286A1.857 1.857 0 0 0 18 16.143v-4.286A1.857 1.857 0 0 0 16.143 10Z" />
396
+ </svg>
397
+ <span class="flex-1 ms-3 whitespace-nowrap">Rules</span>
398
+ </a>
399
+ </li>
400
+ {% endif %}
401
  <li>
402
  <button type="button"
403
  class="flex items-center w-full p-2 text-base text-gray-900 transition duration-75 rounded-lg group hover:bg-gray-100"