File size: 6,059 Bytes
c50a91b
ae02377
815b0dc
f3fe9b4
8ec4d2d
c50a91b
f3fe9b4
5839973
815b0dc
ae02377
815b0dc
 
 
 
8e26e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae02377
3ea1b9b
 
470460b
 
 
 
 
 
 
 
 
 
3ea1b9b
177eef9
 
 
 
 
 
 
 
 
 
 
0ec6e70
 
 
 
 
 
3ea1b9b
385467a
3ea1b9b
 
ae02377
 
385467a
ae02377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea1b9b
ae02377
 
 
 
 
c50a91b
 
 
 
470460b
 
 
 
d5ca063
 
3ea1b9b
936d8d9
 
 
 
 
 
 
 
1094cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0558a9f
 
 
 
abe3598
 
 
90724cf
f63d039
 
 
 
de15d44
 
 
 
0ec6e70
 
 
 
 
 
2d208af
 
0ec6e70
 
177eef9
 
 
 
c50a91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import json
from dataclasses import dataclass
from datetime import datetime

from huggingface_hub import HfApi, hf_hub_download


@dataclass
class CompetitionInfo:
    competition_id: str
    autotrain_token: str

    def __post_init__(self):
        config_fname = hf_hub_download(
            repo_id=self.competition_id,
            filename="conf.json",
            use_auth_token=self.autotrain_token,
            repo_type="dataset",
        )
        competition_desc = hf_hub_download(
            repo_id=self.competition_id,
            filename="COMPETITION_DESC.md",
            use_auth_token=self.autotrain_token,
            repo_type="dataset",
        )
        dataset_desc = hf_hub_download(
            repo_id=self.competition_id,
            filename="DATASET_DESC.md",
            use_auth_token=self.autotrain_token,
            repo_type="dataset",
        )
        self.config = self.load_config(config_fname)
        self.competition_desc = self.load_md(competition_desc)
        self.dataset_desc = self.load_md(dataset_desc)
        try:
            submission_desc = hf_hub_download(
                repo_id=self.competition_id,
                filename="SUBMISSION_DESC.md",
                use_auth_token=self.autotrain_token,
                repo_type="dataset",
            )
            self.submission_desc = self.load_md(submission_desc)
        except Exception:
            self.submission_desc = None

        try:
            rules_md = hf_hub_download(
                repo_id=self.competition_id,
                filename="RULES.md",
                use_auth_token=self.autotrain_token,
                repo_type="dataset",
            )
            self.rules_md = self.load_md(rules_md)
        except Exception:
            self.rules_md = None

        if self.config["EVAL_METRIC"] == "custom":
            if "SCORING_METRIC" not in self.config:
                raise ValueError(
                    "For custom metrics, please provide a single SCORING_METRIC name in the competition config file: conf.json"
                )

    def load_md(self, md_path):
        with open(md_path, "r", encoding="utf-8") as f:
            md = f.read()
        return md

    def load_config(self, config_path):
        with open(config_path, "r", encoding="utf-8") as f:
            config = json.load(f)
        return config

    @property
    def submission_limit(self):
        return self.config["SUBMISSION_LIMIT"]

    @property
    def selection_limit(self):
        return self.config["SELECTION_LIMIT"]

    @property
    def end_date(self):
        e_d = self.config["END_DATE"]
        return datetime.strptime(e_d, "%Y-%m-%d")

    @property
    def eval_higher_is_better(self):
        hb = self.config["EVAL_HIGHER_IS_BETTER"]
        return True if int(hb) == 1 else False

    @property
    def competition_description(self):
        return self.competition_desc

    @property
    def submission_columns(self):
        return self.config["SUBMISSION_COLUMNS"].split(",")

    @property
    def submission_columns_raw(self):
        return self.config["SUBMISSION_COLUMNS"]

    @property
    def submission_description(self):
        return self.submission_desc

    @property
    def dataset_description(self):
        return self.dataset_desc

    @property
    def logo_url(self):
        return self.config["LOGO"]

    @property
    def competition_type(self):
        return self.config["COMPETITION_TYPE"].lower().strip()

    @property
    def metric(self):
        return self.config["EVAL_METRIC"]

    @property
    def submission_id_col(self):
        return self.config["SUBMISSION_ID_COLUMN"]

    @property
    def submission_cols(self):
        cols = self.config["SUBMISSION_COLUMNS"].split(",")
        cols = [c.strip() for c in cols]
        return cols

    @property
    def submission_rows(self):
        return self.config["SUBMISSION_ROWS"]

    @property
    def time_limit(self):
        return self.config["TIME_LIMIT"]

    @property
    def hardware(self):
        return self.config.get("HARDWARE", "cpu-basic")

    @property
    def dataset(self):
        return self.config.get("DATASET", "")

    @property
    def submission_filenames(self):
        return self.config.get("SUBMISSION_FILENAMES", ["submission.csv"])

    @property
    def scoring_metric(self):
        if self.config["EVAL_METRIC"] == "custom":
            if "SCORING_METRIC" not in self.config:
                raise Exception("Please provide a single SCORING_METRIC in the competition config file: conf.json")
            if self.config["SCORING_METRIC"] is None:
                raise Exception("Please provide a single SCORING_METRIC in the competition config file: conf.json")
            return self.config["SCORING_METRIC"]
        return self.config["EVAL_METRIC"]

    @property
    def rules(self):
        return self.rules_md

    def _save_md(self, md, filename, api):
        md = io.BytesIO(md.encode())
        api.upload_file(
            path_or_fileobj=md,
            path_in_repo=filename,
            repo_id=self.competition_id,
            repo_type="dataset",
        )

    def update_competition_info(self, config, markdowns, token):
        api = HfApi(token=token)
        conf_json = json.dumps(config, indent=4)
        conf_json_bytes = conf_json.encode("utf-8")
        conf_json_buffer = io.BytesIO(conf_json_bytes)
        api.upload_file(
            path_or_fileobj=conf_json_buffer,
            path_in_repo="conf.json",
            repo_id=self.competition_id,
            repo_type="dataset",
        )

        competition_desc = markdowns["competition_desc"]
        dataset_desc = markdowns["dataset_desc"]
        submission_desc = markdowns["submission_desc"]
        rules_md = markdowns["rules"]

        self._save_md(competition_desc, "COMPETITION_DESC.md", api)
        self._save_md(dataset_desc, "DATASET_DESC.md", api)
        self._save_md(submission_desc, "SUBMISSION_DESC.md", api)
        self._save_md(rules_md, "RULES.md", api)