Abhishek Thakur
commited on
Commit
·
fc4883c
1
Parent(s):
5bafe72
rebuild no need
Browse files- competitions/app.py +48 -41
competitions/app.py
CHANGED
@@ -34,9 +34,6 @@ VERSION_COMMIT_ID = os.environ.get("VERSION_COMMIT_ID", "0687567")
|
|
34 |
|
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(
|
42 |
repo_id=COMPETITION_ID,
|
@@ -73,7 +70,8 @@ class UserSubmissionUpdate(BaseModel):
|
|
73 |
|
74 |
|
75 |
def run_job_runner():
|
76 |
-
|
|
|
77 |
job_runner.run()
|
78 |
|
79 |
|
@@ -100,12 +98,13 @@ async def read_form(request: Request):
|
|
100 |
"""
|
101 |
if HF_TOKEN is None:
|
102 |
return templates.TemplateResponse("error.html", {"request": request})
|
|
|
103 |
context = {
|
104 |
"request": request,
|
105 |
-
"logo":
|
106 |
-
"competition_type":
|
107 |
"version_commit_id": VERSION_COMMIT_ID[:7],
|
108 |
-
"rules_available":
|
109 |
}
|
110 |
return templates.TemplateResponse("index.html", context)
|
111 |
|
@@ -119,10 +118,11 @@ async def oauth_login(request: Request):
|
|
119 |
async def oauth_logout(request: Request):
|
120 |
"""Endpoint that logs out the user (e.g. delete cookie session)."""
|
121 |
request.session.pop("oauth_info", None)
|
|
|
122 |
context = {
|
123 |
"request": request,
|
124 |
-
"logo":
|
125 |
-
"competition_type":
|
126 |
}
|
127 |
|
128 |
return templates.TemplateResponse("index.html", context)
|
@@ -143,7 +143,8 @@ async def use_oauth(request: Request):
|
|
143 |
|
144 |
@app.get("/competition_info", response_class=JSONResponse)
|
145 |
async def get_comp_info(request: Request):
|
146 |
-
|
|
|
147 |
# info = markdown.markdown(info)
|
148 |
resp = {"response": info}
|
149 |
return resp
|
@@ -151,7 +152,8 @@ async def get_comp_info(request: Request):
|
|
151 |
|
152 |
@app.get("/dataset_info", response_class=JSONResponse)
|
153 |
async def get_dataset_info(request: Request):
|
154 |
-
|
|
|
155 |
# info = markdown.markdown(info)
|
156 |
resp = {"response": info}
|
157 |
return resp
|
@@ -159,14 +161,16 @@ async def get_dataset_info(request: Request):
|
|
159 |
|
160 |
@app.get("/rules", response_class=JSONResponse)
|
161 |
async def get_rules(request: Request):
|
162 |
-
|
163 |
-
|
|
|
164 |
return {"response": "No rules available."}
|
165 |
|
166 |
|
167 |
@app.get("/submission_info", response_class=JSONResponse)
|
168 |
async def get_submission_info(request: Request):
|
169 |
-
|
|
|
170 |
# info = markdown.markdown(info)
|
171 |
resp = {"response": info}
|
172 |
return resp
|
@@ -178,23 +182,24 @@ async def fetch_leaderboard(request: Request, user_lb: UserLB):
|
|
178 |
if request.session.get("oauth_info") is not None:
|
179 |
user_lb.user_token = request.session.get("oauth_info")["access_token"]
|
180 |
|
181 |
-
|
182 |
-
|
183 |
|
184 |
if DISABLE_PUBLIC_LB == 1 and user_lb.lb == "public" and not is_user_admin:
|
185 |
return {"response": "Public leaderboard is disabled by the competition host."}
|
186 |
|
|
|
187 |
leaderboard = Leaderboard(
|
188 |
-
end_date=
|
189 |
-
eval_higher_is_better=
|
190 |
-
max_selected_submissions=
|
191 |
competition_id=COMPETITION_ID,
|
192 |
token=HF_TOKEN,
|
193 |
-
scoring_metric=
|
194 |
)
|
195 |
if user_lb.lb == "private":
|
196 |
current_utc_time = datetime.datetime.now()
|
197 |
-
if current_utc_time <
|
198 |
return {"response": "Private leaderboard will be available after the competition ends."}
|
199 |
df = leaderboard.fetch(private=user_lb.lb == "private")
|
200 |
|
@@ -209,14 +214,14 @@ async def my_submissions(request: Request, user: User):
|
|
209 |
if USE_OAUTH == 1:
|
210 |
if request.session.get("oauth_info") is not None:
|
211 |
user.user_token = request.session.get("oauth_info")["access_token"]
|
212 |
-
|
213 |
sub = Submissions(
|
214 |
-
end_date=
|
215 |
-
submission_limit=
|
216 |
competition_id=COMPETITION_ID,
|
217 |
token=HF_TOKEN,
|
218 |
-
competition_type=
|
219 |
-
hardware=
|
220 |
)
|
221 |
try:
|
222 |
subs = sub.my_submissions(user.user_token)
|
@@ -224,7 +229,7 @@ async def my_submissions(request: Request, user: User):
|
|
224 |
return {
|
225 |
"response": {
|
226 |
"submissions": "",
|
227 |
-
"submission_text": SUBMISSION_TEXT.format(
|
228 |
"error": "**Invalid token**",
|
229 |
"team_name": "",
|
230 |
}
|
@@ -235,8 +240,8 @@ async def my_submissions(request: Request, user: User):
|
|
235 |
if len(subs) == 0:
|
236 |
error = "**You have not made any submissions yet.**"
|
237 |
subs = ""
|
238 |
-
submission_text = SUBMISSION_TEXT.format(
|
239 |
-
submission_selection_text = SUBMISSION_SELECTION_TEXT.format(
|
240 |
|
241 |
team_name = utils.get_team_name(user.user_token, COMPETITION_ID, HF_TOKEN)
|
242 |
|
@@ -276,19 +281,20 @@ async def new_submission(
|
|
276 |
if not utils.is_user_admin(token, comp_org):
|
277 |
return {"response": "Competition has not started yet!"}
|
278 |
|
|
|
279 |
sub = Submissions(
|
280 |
-
end_date=
|
281 |
-
submission_limit=
|
282 |
competition_id=COMPETITION_ID,
|
283 |
token=HF_TOKEN,
|
284 |
-
competition_type=
|
285 |
-
hardware=
|
286 |
)
|
287 |
try:
|
288 |
-
if
|
289 |
resp = sub.new_submission(token, submission_file, submission_comment)
|
290 |
return {"response": f"Success! You have {resp} submissions remaining today."}
|
291 |
-
if
|
292 |
resp = sub.new_submission(token, hub_model, submission_comment)
|
293 |
return {"response": f"Success! You have {resp} submissions remaining today."}
|
294 |
except AuthenticationError:
|
@@ -302,20 +308,21 @@ def update_selected_submissions(request: Request, user_sub: UserSubmissionUpdate
|
|
302 |
if request.session.get("oauth_info") is not None:
|
303 |
user_sub.user_token = request.session.get("oauth_info")["access_token"]
|
304 |
|
|
|
305 |
sub = Submissions(
|
306 |
-
end_date=
|
307 |
-
submission_limit=
|
308 |
competition_id=COMPETITION_ID,
|
309 |
token=HF_TOKEN,
|
310 |
-
competition_type=
|
311 |
-
hardware=
|
312 |
)
|
313 |
submission_ids = user_sub.submission_ids.split(",")
|
314 |
submission_ids = [s.strip() for s in submission_ids]
|
315 |
-
if len(submission_ids) >
|
316 |
return {
|
317 |
"success": False,
|
318 |
-
"error": f"Please select at most {
|
319 |
}
|
320 |
sub.update_selected_submissions(user_token=user_sub.user_token, selected_submission_ids=submission_ids)
|
321 |
return {"success": True, "error": ""}
|
|
|
34 |
|
35 |
disable_progress_bars()
|
36 |
|
|
|
|
|
|
|
37 |
try:
|
38 |
REQUIREMENTS_FNAME = hf_hub_download(
|
39 |
repo_id=COMPETITION_ID,
|
|
|
70 |
|
71 |
|
72 |
def run_job_runner():
|
73 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
74 |
+
job_runner = JobRunner(token=HF_TOKEN, competition_info=competition_info, output_path=OUTPUT_PATH)
|
75 |
job_runner.run()
|
76 |
|
77 |
|
|
|
98 |
"""
|
99 |
if HF_TOKEN is None:
|
100 |
return templates.TemplateResponse("error.html", {"request": request})
|
101 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
102 |
context = {
|
103 |
"request": request,
|
104 |
+
"logo": competition_info.logo_url,
|
105 |
+
"competition_type": competition_info.competition_type,
|
106 |
"version_commit_id": VERSION_COMMIT_ID[:7],
|
107 |
+
"rules_available": competition_info.rules is not None,
|
108 |
}
|
109 |
return templates.TemplateResponse("index.html", context)
|
110 |
|
|
|
118 |
async def oauth_logout(request: Request):
|
119 |
"""Endpoint that logs out the user (e.g. delete cookie session)."""
|
120 |
request.session.pop("oauth_info", None)
|
121 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
122 |
context = {
|
123 |
"request": request,
|
124 |
+
"logo": competition_info.logo_url,
|
125 |
+
"competition_type": competition_info.competition_type,
|
126 |
}
|
127 |
|
128 |
return templates.TemplateResponse("index.html", context)
|
|
|
143 |
|
144 |
@app.get("/competition_info", response_class=JSONResponse)
|
145 |
async def get_comp_info(request: Request):
|
146 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
147 |
+
info = competition_info.competition_desc
|
148 |
# info = markdown.markdown(info)
|
149 |
resp = {"response": info}
|
150 |
return resp
|
|
|
152 |
|
153 |
@app.get("/dataset_info", response_class=JSONResponse)
|
154 |
async def get_dataset_info(request: Request):
|
155 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
156 |
+
info = competition_info.dataset_desc
|
157 |
# info = markdown.markdown(info)
|
158 |
resp = {"response": info}
|
159 |
return resp
|
|
|
161 |
|
162 |
@app.get("/rules", response_class=JSONResponse)
|
163 |
async def get_rules(request: Request):
|
164 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
165 |
+
if competition_info.rules is not None:
|
166 |
+
return {"response": competition_info.rules}
|
167 |
return {"response": "No rules available."}
|
168 |
|
169 |
|
170 |
@app.get("/submission_info", response_class=JSONResponse)
|
171 |
async def get_submission_info(request: Request):
|
172 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
173 |
+
info = competition_info.submission_desc
|
174 |
# info = markdown.markdown(info)
|
175 |
resp = {"response": info}
|
176 |
return resp
|
|
|
182 |
if request.session.get("oauth_info") is not None:
|
183 |
user_lb.user_token = request.session.get("oauth_info")["access_token"]
|
184 |
|
185 |
+
comp_org = COMPETITION_ID.split("/")[0]
|
186 |
+
is_user_admin = utils.is_user_admin(user_lb.user_token, comp_org)
|
187 |
|
188 |
if DISABLE_PUBLIC_LB == 1 and user_lb.lb == "public" and not is_user_admin:
|
189 |
return {"response": "Public leaderboard is disabled by the competition host."}
|
190 |
|
191 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
192 |
leaderboard = Leaderboard(
|
193 |
+
end_date=competition_info.end_date,
|
194 |
+
eval_higher_is_better=competition_info.eval_higher_is_better,
|
195 |
+
max_selected_submissions=competition_info.selection_limit,
|
196 |
competition_id=COMPETITION_ID,
|
197 |
token=HF_TOKEN,
|
198 |
+
scoring_metric=competition_info.scoring_metric,
|
199 |
)
|
200 |
if user_lb.lb == "private":
|
201 |
current_utc_time = datetime.datetime.now()
|
202 |
+
if current_utc_time < competition_info.end_date and not is_user_admin:
|
203 |
return {"response": "Private leaderboard will be available after the competition ends."}
|
204 |
df = leaderboard.fetch(private=user_lb.lb == "private")
|
205 |
|
|
|
214 |
if USE_OAUTH == 1:
|
215 |
if request.session.get("oauth_info") is not None:
|
216 |
user.user_token = request.session.get("oauth_info")["access_token"]
|
217 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
218 |
sub = Submissions(
|
219 |
+
end_date=competition_info.end_date,
|
220 |
+
submission_limit=competition_info.submission_limit,
|
221 |
competition_id=COMPETITION_ID,
|
222 |
token=HF_TOKEN,
|
223 |
+
competition_type=competition_info.competition_type,
|
224 |
+
hardware=competition_info.hardware,
|
225 |
)
|
226 |
try:
|
227 |
subs = sub.my_submissions(user.user_token)
|
|
|
229 |
return {
|
230 |
"response": {
|
231 |
"submissions": "",
|
232 |
+
"submission_text": SUBMISSION_TEXT.format(competition_info.submission_limit),
|
233 |
"error": "**Invalid token**",
|
234 |
"team_name": "",
|
235 |
}
|
|
|
240 |
if len(subs) == 0:
|
241 |
error = "**You have not made any submissions yet.**"
|
242 |
subs = ""
|
243 |
+
submission_text = SUBMISSION_TEXT.format(competition_info.submission_limit)
|
244 |
+
submission_selection_text = SUBMISSION_SELECTION_TEXT.format(competition_info.selection_limit)
|
245 |
|
246 |
team_name = utils.get_team_name(user.user_token, COMPETITION_ID, HF_TOKEN)
|
247 |
|
|
|
281 |
if not utils.is_user_admin(token, comp_org):
|
282 |
return {"response": "Competition has not started yet!"}
|
283 |
|
284 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
285 |
sub = Submissions(
|
286 |
+
end_date=competition_info.end_date,
|
287 |
+
submission_limit=competition_info.submission_limit,
|
288 |
competition_id=COMPETITION_ID,
|
289 |
token=HF_TOKEN,
|
290 |
+
competition_type=competition_info.competition_type,
|
291 |
+
hardware=competition_info.hardware,
|
292 |
)
|
293 |
try:
|
294 |
+
if competition_info.competition_type == "generic":
|
295 |
resp = sub.new_submission(token, submission_file, submission_comment)
|
296 |
return {"response": f"Success! You have {resp} submissions remaining today."}
|
297 |
+
if competition_info.competition_type == "script":
|
298 |
resp = sub.new_submission(token, hub_model, submission_comment)
|
299 |
return {"response": f"Success! You have {resp} submissions remaining today."}
|
300 |
except AuthenticationError:
|
|
|
308 |
if request.session.get("oauth_info") is not None:
|
309 |
user_sub.user_token = request.session.get("oauth_info")["access_token"]
|
310 |
|
311 |
+
competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
|
312 |
sub = Submissions(
|
313 |
+
end_date=competition_info.end_date,
|
314 |
+
submission_limit=competition_info.submission_limit,
|
315 |
competition_id=COMPETITION_ID,
|
316 |
token=HF_TOKEN,
|
317 |
+
competition_type=competition_info.competition_type,
|
318 |
+
hardware=competition_info.hardware,
|
319 |
)
|
320 |
submission_ids = user_sub.submission_ids.split(",")
|
321 |
submission_ids = [s.strip() for s in submission_ids]
|
322 |
+
if len(submission_ids) > competition_info.selection_limit:
|
323 |
return {
|
324 |
"success": False,
|
325 |
+
"error": f"Please select at most {competition_info.selection_limit} submissions.",
|
326 |
}
|
327 |
sub.update_selected_submissions(user_token=user_sub.user_token, selected_submission_ids=submission_ids)
|
328 |
return {"success": True, "error": ""}
|