File size: 1,847 Bytes
683946e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12de094
683946e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from argparse import ArgumentParser

import requests

from . import BaseCompetitionsCommand


def submit_commands_factory(args):
    return SubmitCompetitionAppCommand(args)


class SubmitCompetitionAppCommand(BaseCompetitionsCommand):
    def __init__(self, args):
        self.args = args

    @staticmethod
    def register_subcommand(parser: ArgumentParser):
        submit_competition_parser = parser.add_parser("submit", description="Submit to a competition")
        submit_competition_parser.add_argument(
            "--competition_id", type=str, help="ID of the competition, e.g. huggingface/cool-competition"
        )
        submit_competition_parser.add_argument(
            "--submission", type=str, help="Path to submission file or HuggingFace hub repo"
        )
        submit_competition_parser.add_argument("--comment", type=str, help="Submission comment", default="")
        submit_competition_parser.add_argument("--token", type=str, help="User token, read-only", default="")
        submit_competition_parser.set_defaults(func=submit_commands_factory)

    def run(self):
        if os.path.isfile(self.args.submission):
            files = {"submission_file": open(self.args.submission, "rb")}
            data = {
                "hub_model": "None",
                "submission_comment": self.args.comment,
            }
        else:
            files = {"submission_file": None}
            data = {
                "hub_model": self.args.submission,
                "submission_comment": self.args.comment,
            }

        headers = {"Authorization": f"Bearer {self.args.token}"}

        api_url = "https://" + self.args.competition_id.replace("/", "-") + ".hf.space/new_submission"

        response = requests.post(api_url, data=data, files=files, headers=headers)
        print(response.json())