File size: 11,058 Bytes
53a304d
9b8659c
1094cbb
9b8659c
177eef9
936d8d9
eb49ce1
9b8659c
 
f5d4c87
42c2745
f5d4c87
a2fa160
936d8d9
9b8659c
f5d4c87
42c2745
9b8659c
 
bef585f
1094cbb
936d8d9
09d288e
9b8659c
 
 
 
b745d94
 
 
 
bef585f
5840f5c
42c2745
 
 
9b8659c
177eef9
936d8d9
f5d4c87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf3d263
 
 
 
 
936d8d9
 
 
 
6aa12a9
 
 
 
 
1094cbb
 
 
 
 
 
 
 
 
9b8659c
bef585f
 
 
9b8659c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
936d8d9
 
 
 
5840f5c
177eef9
936d8d9
9b8659c
 
5f7f284
eb49ce1
5f7f284
eb49ce1
 
9b8659c
5ccf24c
 
 
 
 
 
 
 
 
 
 
 
 
bef585f
 
d3f0444
 
177eef9
 
 
 
 
bef585f
 
 
9b8659c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177eef9
 
 
 
 
 
 
09d288e
 
 
 
 
 
 
 
9b8659c
 
b745d94
 
 
9b8659c
 
 
 
 
42c2745
0ec6e70
9b8659c
53a304d
 
 
 
9b8659c
a2fa160
0ec6e70
 
9b8659c
 
 
 
936d8d9
 
d3f0444
 
 
0bfa106
936d8d9
 
 
 
 
a2fa160
2a67404
936d8d9
42c2745
0ec6e70
42c2745
 
 
3b19076
42c2745
3b19076
bf3d263
42c2745
 
6aa12a9
0ec6e70
3b19076
6aa12a9
3b19076
 
09d288e
 
 
bf3d263
 
09d288e
 
 
 
3b19076
bf3d263
09d288e
 
9b8659c
936d8d9
 
 
 
81cb2cd
a2fa160
936d8d9
81cb2cd
3b19076
936d8d9
3b19076
 
 
d3f0444
 
 
81cb2cd
 
 
0bfa106
a04edc8
 
 
 
 
 
 
936d8d9
 
 
 
 
a2fa160
2a67404
936d8d9
42c2745
 
 
 
a04edc8
42c2745
 
 
 
936d8d9
6aa12a9
 
 
 
d3f0444
 
 
0bfa106
6aa12a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf3d263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import datetime
import os
import threading

import requests
from fastapi import FastAPI, File, Form, Request, UploadFile
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import disable_progress_bars
from huggingface_hub.utils._errors import EntryNotFoundError
from loguru import logger
from pydantic import BaseModel

from competitions import utils
from competitions.errors import AuthenticationError
from competitions.info import CompetitionInfo
from competitions.leaderboard import Leaderboard
from competitions.oauth import attach_oauth
from competitions.runner import JobRunner
from competitions.submissions import Submissions
from competitions.text import SUBMISSION_SELECTION_TEXT, SUBMISSION_TEXT


HF_TOKEN = os.environ.get("HF_TOKEN", None)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
COMPETITION_ID = os.environ.get("COMPETITION_ID")
OUTPUT_PATH = os.environ.get("OUTPUT_PATH", "/tmp/model")
START_DATE = os.environ.get("START_DATE", "2000-12-31")
DISABLE_PUBLIC_LB = int(os.environ.get("DISABLE_PUBLIC_LB", 0))
USE_OAUTH = int(os.environ.get("USE_OAUTH", 0))
VERSION_COMMIT_ID = os.environ.get("VERSION_COMMIT_ID", "0687567")

disable_progress_bars()

COMP_INFO = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=HF_TOKEN)
RULES_AVAILABLE = COMP_INFO.rules is not None

try:
    REQUIREMENTS_FNAME = hf_hub_download(
        repo_id=COMPETITION_ID,
        filename="requirements.txt",
        token=HF_TOKEN,
        repo_type="dataset",
    )
except EntryNotFoundError:
    REQUIREMENTS_FNAME = None

if REQUIREMENTS_FNAME:
    logger.info("Uninstalling and installing requirements")
    utils.uninstall_requirements(REQUIREMENTS_FNAME)
    utils.install_requirements(REQUIREMENTS_FNAME)


class UserTeamNameUpdate(BaseModel):
    user_token: str
    new_team_name: str


class User(BaseModel):
    user_token: str


class UserSubmissionUpdate(BaseModel):
    user_token: str
    submission_ids: str


def run_job_runner():
    job_runner = JobRunner(token=HF_TOKEN, competition_info=COMP_INFO, output_path=OUTPUT_PATH)
    job_runner.run()


thread = threading.Thread(target=run_job_runner)
thread.start()


app = FastAPI()
if USE_OAUTH == 1:
    attach_oauth(app)

static_path = os.path.join(BASE_DIR, "static")
app.mount("/static", StaticFiles(directory=static_path), name="static")
templates_path = os.path.join(BASE_DIR, "templates")
templates = Jinja2Templates(directory=templates_path)


@app.get("/", response_class=HTMLResponse)
async def read_form(request: Request):
    """
    This function is used to render the HTML file
    :param request:
    :return:
    """
    if HF_TOKEN is None:
        return templates.TemplateResponse("error.html", {"request": request})
    context = {
        "request": request,
        "logo": COMP_INFO.logo_url,
        "competition_type": COMP_INFO.competition_type,
        "version_commit_id": VERSION_COMMIT_ID[:7],
        "rules_available": RULES_AVAILABLE,
    }
    return templates.TemplateResponse("index.html", context)


@app.get("/oauth_login", response_class=HTMLResponse)
async def oauth_login(request: Request):
    return RedirectResponse("/login/huggingface")


@app.get("/logout", response_class=HTMLResponse)
async def oauth_logout(request: Request):
    """Endpoint that logs out the user (e.g. delete cookie session)."""
    request.session.pop("oauth_info", None)
    context = {
        "request": request,
        "logo": COMP_INFO.logo_url,
        "competition_type": COMP_INFO.competition_type,
    }

    return templates.TemplateResponse("index.html", context)


@app.get("/use_oauth", response_class=JSONResponse)
async def use_oauth(request: Request):
    if USE_OAUTH == 1:
        if request.session.get("oauth_info") is not None:
            try:
                utils.user_authentication(request.session.get("oauth_info")["access_token"])
                return {"response": 2}
            except requests.exceptions.JSONDecodeError:
                return {"response": USE_OAUTH}
    return {"response": USE_OAUTH}


@app.get("/competition_info", response_class=JSONResponse)
async def get_comp_info(request: Request):
    info = COMP_INFO.competition_desc
    # info = markdown.markdown(info)
    resp = {"response": info}
    return resp


@app.get("/dataset_info", response_class=JSONResponse)
async def get_dataset_info(request: Request):
    info = COMP_INFO.dataset_desc
    # info = markdown.markdown(info)
    resp = {"response": info}
    return resp


@app.get("/rules", response_class=JSONResponse)
async def get_rules(request: Request):
    if COMP_INFO.rules is not None:
        return {"response": COMP_INFO.rules}
    return {"response": "No rules available."}


@app.get("/submission_info", response_class=JSONResponse)
async def get_submission_info(request: Request):
    info = COMP_INFO.submission_desc
    # info = markdown.markdown(info)
    resp = {"response": info}
    return resp


@app.get("/leaderboard/{lb}", response_class=JSONResponse)
async def get_leaderboard(request: Request, lb: str):
    if DISABLE_PUBLIC_LB == 1 and lb == "public":
        return {"response": "Public leaderboard is disabled by the competition host."}

    leaderboard = Leaderboard(
        end_date=COMP_INFO.end_date,
        eval_higher_is_better=COMP_INFO.eval_higher_is_better,
        max_selected_submissions=COMP_INFO.selection_limit,
        competition_id=COMPETITION_ID,
        token=HF_TOKEN,
        scoring_metric=COMP_INFO.scoring_metric,
    )
    if lb == "private":
        current_utc_time = datetime.datetime.utcnow()
        if current_utc_time < COMP_INFO.end_date:
            return {"response": "Private leaderboard will be available after the competition ends."}
    df = leaderboard.fetch(private=lb == "private")
    logger.info(df)
    if len(df) == 0:
        return {"response": "No teams yet. Why not make a submission?"}
    resp = {"response": df.to_markdown(index=False)}
    return resp


@app.post("/my_submissions", response_class=JSONResponse)
async def my_submissions(request: Request, user: User):
    if USE_OAUTH == 1:
        if request.session.get("oauth_info") is not None:
            user.user_token = request.session.get("oauth_info")["access_token"]

    sub = Submissions(
        end_date=COMP_INFO.end_date,
        submission_limit=COMP_INFO.submission_limit,
        competition_id=COMPETITION_ID,
        token=HF_TOKEN,
        competition_type=COMP_INFO.competition_type,
        hardware=COMP_INFO.hardware,
    )
    try:
        subs = sub.my_submissions(user.user_token)
    except AuthenticationError:
        return {
            "response": {
                "submissions": "",
                "submission_text": SUBMISSION_TEXT.format(COMP_INFO.submission_limit),
                "error": "**Invalid token**",
                "team_name": "",
            }
        }
    subs = subs.to_dict(orient="records")
    logger.info(subs)
    error = ""
    if len(subs) == 0:
        error = "**You have not made any submissions yet.**"
        subs = ""
    submission_text = SUBMISSION_TEXT.format(COMP_INFO.submission_limit)
    submission_selection_text = SUBMISSION_SELECTION_TEXT.format(COMP_INFO.selection_limit)

    team_name = utils.get_team_name(user.user_token, COMPETITION_ID, HF_TOKEN)

    resp = {
        "response": {
            "submissions": subs,
            "submission_text": submission_text + submission_selection_text,
            "error": error,
            "team_name": team_name,
        }
    }
    return resp


@app.post("/new_submission", response_class=JSONResponse)
async def new_submission(
    request: Request,
    submission_file: UploadFile = File(None),
    hub_model: str = Form(...),
    token: str = Form(None),
    submission_comment: str = Form(None),
):
    if submission_comment is None:
        submission_comment = ""

    if USE_OAUTH == 1:
        if request.session.get("oauth_info") is not None:
            token = request.session.get("oauth_info")["access_token"]

    if token is None:
        return {"response": "Invalid token"}

    todays_date = datetime.datetime.now()
    start_date = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
    if todays_date < start_date:
        comp_org = COMPETITION_ID.split("/")[0]
        if not utils.can_user_submit_before_start(token, comp_org):
            return {"response": "Competition has not started yet!"}

    sub = Submissions(
        end_date=COMP_INFO.end_date,
        submission_limit=COMP_INFO.submission_limit,
        competition_id=COMPETITION_ID,
        token=HF_TOKEN,
        competition_type=COMP_INFO.competition_type,
        hardware=COMP_INFO.hardware,
    )
    try:
        if COMP_INFO.competition_type == "generic":
            resp = sub.new_submission(token, submission_file, submission_comment)
            return {"response": f"Success! You have {resp} submissions remaining today."}
        if COMP_INFO.competition_type == "script":
            resp = sub.new_submission(token, hub_model, submission_comment)
            return {"response": f"Success! You have {resp} submissions remaining today."}
    except AuthenticationError:
        return {"response": "Invalid token"}
    return {"response": "Invalid competition type"}


@app.post("/update_selected_submissions", response_class=JSONResponse)
def update_selected_submissions(request: Request, user_sub: UserSubmissionUpdate):
    if USE_OAUTH == 1:
        if request.session.get("oauth_info") is not None:
            user_sub.user_token = request.session.get("oauth_info")["access_token"]

    sub = Submissions(
        end_date=COMP_INFO.end_date,
        submission_limit=COMP_INFO.submission_limit,
        competition_id=COMPETITION_ID,
        token=HF_TOKEN,
        competition_type=COMP_INFO.competition_type,
        hardware=COMP_INFO.hardware,
    )
    submission_ids = user_sub.submission_ids.split(",")
    submission_ids = [s.strip() for s in submission_ids]
    if len(submission_ids) > COMP_INFO.selection_limit:
        return {
            "success": False,
            "error": f"Please select at most {COMP_INFO.selection_limit} submissions.",
        }
    sub.update_selected_submissions(user_token=user_sub.user_token, selected_submission_ids=submission_ids)
    return {"success": True, "error": ""}


@app.post("/update_team_name", response_class=JSONResponse)
def update_team_name(request: Request, user_team: UserTeamNameUpdate):
    if USE_OAUTH == 1:
        if request.session.get("oauth_info") is not None:
            user_token = request.session.get("oauth_info")["access_token"]

    user_token = user_team.user_token

    try:
        utils.update_team_name(user_token, user_team.new_team_name, COMPETITION_ID, HF_TOKEN)
        return {"success": True, "error": ""}
    except Exception as e:
        return {"success": False, "error": str(e)}