File size: 14,152 Bytes
e2473e2
cdf268e
e2473e2
04f40cd
04b315a
cdf268e
a70fbd7
e2473e2
cdf268e
 
 
 
e2473e2
cdf268e
 
6c92442
04f40cd
e2473e2
 
 
 
 
 
 
 
 
 
 
 
2d26479
e2473e2
 
04b315a
e2473e2
 
a70fbd7
 
e2473e2
 
a70fbd7
e2473e2
a70fbd7
e2473e2
 
 
6c92442
e2473e2
cdf268e
e2473e2
04b315a
 
 
 
 
 
2d26479
04b315a
 
 
 
 
 
 
 
a70fbd7
04b315a
 
 
 
2d26479
04b315a
 
 
 
 
 
 
 
 
 
 
 
a70fbd7
 
 
 
 
04b315a
 
 
 
 
 
 
 
cdf268e
 
 
 
 
e2473e2
a70fbd7
e2473e2
 
1323fe2
e2473e2
 
 
 
cdf268e
e2473e2
6c92442
a70fbd7
cdf268e
6c92442
cdf268e
 
 
 
 
 
6c92442
cdf268e
 
 
 
 
 
6c92442
cdf268e
 
 
6c92442
cdf268e
 
6c92442
e2473e2
cdf268e
 
 
 
 
 
 
 
 
2d26479
04b315a
cdf268e
 
 
 
a70fbd7
cdf268e
 
 
 
a70fbd7
 
 
 
2d26479
a70fbd7
 
 
 
cdf268e
 
6c92442
cdf268e
 
 
e2473e2
cdf268e
 
e2473e2
cdf268e
e2473e2
cdf268e
 
9203553
cdf268e
 
 
2d26479
04b315a
cdf268e
 
04b315a
 
cdf268e
 
a70fbd7
6c92442
 
cdf268e
6c92442
 
cdf268e
 
 
6c92442
cdf268e
04f40cd
 
 
 
 
 
6c92442
cdf268e
04b315a
e2473e2
 
 
 
 
 
 
2d26479
04b315a
e2473e2
 
04b315a
 
e2473e2
 
2d1a3df
 
cdf268e
9203553
cdf268e
 
 
2d26479
cdf268e
 
 
04b315a
 
cdf268e
 
2d1a3df
 
9203553
 
2d1a3df
 
9203553
2d1a3df
a70fbd7
04b315a
a70fbd7
04b315a
2d1a3df
 
 
a70fbd7
 
 
 
2d1a3df
cdf268e
 
2d1a3df
 
 
 
e2473e2
cdf268e
 
6c92442
cdf268e
 
 
 
 
 
 
2d26479
cdf268e
 
 
 
 
 
a70fbd7
d47b526
a70fbd7
6c92442
d47b526
 
 
 
 
04b315a
a70fbd7
04b315a
 
 
 
 
 
2d26479
04b315a
 
 
 
 
 
 
 
a70fbd7
04b315a
 
 
 
 
 
 
d47b526
e2473e2
cdf268e
afb227e
 
 
 
 
d47b526
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
325
326
327
328
329
330
import json
import logging
import os
import re
import time
from tempfile import TemporaryDirectory
from typing import List, Optional

import jsonlines
from huggingface_hub import CommitOperationAdd  # type: ignore[import]
from huggingface_hub import Discussion, HfApi, HfFileSystem
from tqdm import tqdm

from .evaluation import METRICS
from .formatting import styled_error, styled_message, styled_warning
from .tasks_content import TASKS_PRETTY_REVERSE
from .utils import MD_LINK_PATTERN


class AlreadyExists(Exception):
    pass


class SubmissionUploader:
    """Class for adding new files to a dataset on a Hub and opening a PR.

    Heavily influenced by these amazing spaces:
    * https://huggingface.co/spaces/safetensors/convert
    * https://huggingface.co/spaces/gaia-benchmark/leaderboard
    * https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard
    """

    def __init__(self, dataset_id: str, private_dataset_id: str):
        self._api = HfApi(token=os.environ["HF_TOKEN"])
        self._fs = HfFileSystem(token=os.environ["HF_TOKEN"])
        self._results_dataset_id = dataset_id
        self._requests_dataset_id = private_dataset_id

    def _get_previous_pr(self, pr_title: str) -> Optional[Discussion]:
        """Searches among discussions of the results dataset for a PR with the given title."""
        try:
            discussions = self._api.get_repo_discussions(repo_id=self._results_dataset_id, repo_type="dataset")
        except Exception:
            return None
        for discussion in discussions:
            if discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:
                return discussion
        return None

    def _upload_request(
        self,
        task_id: str,
        model_folder: str,
        model_name_pretty: str,
        model_availability: str,
        model_url: Optional[str],
        urls: Optional[str],
        context_size: str,
        submitted_by: str,
        contact_information: str,
        comment: Optional[str],
        pr_url: str,
        temp_directory: str,
    ) -> List[CommitOperationAdd]:
        """Adds a file with metadata about the current request to the requests dataset."""
        request_metadata = {
            "model_folder": model_folder,
            "model_name_pretty": model_name_pretty,
            "model_availability": model_availability,
            "model_url": model_url,
            "urls": urls,
            "context_size": context_size,
            "submitted_by": submitted_by,
            "contact_information": contact_information,
            "comment": comment,
            "timestamp": time.time(),
            "pr_url": pr_url,
        }

        with open(os.path.join(temp_directory, "request_metadata.json"), "w") as f:
            json.dump(request_metadata, f)

        num_requests_already_present = (
            len(self._fs.ls(f"datasets/{self._requests_dataset_id}/{task_id}/"))
            if self._fs.isdir(f"datasets/{self._requests_dataset_id}/{task_id}/")
            else 0
        )
        commit_operations = [
            CommitOperationAdd(
                path_in_repo=f"{task_id}/{num_requests_already_present}_{model_folder}.json",
                path_or_fileobj=os.path.join(temp_directory, "request_metadata.json"),
            )
        ]
        return commit_operations

    def _upload_predictions(
        self,
        task_id: str,
        model_folder: str,
        filenames: List[str],
    ) -> List[CommitOperationAdd]:
        """Adds all files with current model's predictions to the results dataset."""
        commit_operations = [
            CommitOperationAdd(
                path_in_repo=f"{task_id}/predictions/{model_folder}/{os.path.basename(filename)}",
                path_or_fileobj=filename,
            )
            for filename in filenames
        ]
        return commit_operations

    def _compute_metrics_for_predictions(self, task_id: str, filenames: List[str], temp_directory: str) -> None:
        """Computes metrics for each submitted file with the current model's predictions."""
        metrics_module = METRICS[task_id]
        assert metrics_module is not None, f"Computing metrics for {task_id} is not supported."
        metrics_module.reset()
        open(os.path.join(temp_directory, "metrics.jsonl"), "w").close()

        # compute the metrics for each submitted file
        for filename in filenames:
            with jsonlines.open(filename, "r") as reader:
                for example in tqdm(reader, desc=f"Computing metrics for {os.path.basename(filename)}"):
                    metrics_module.add_batch(
                        predictions=[example["prediction"]],
                        references=[example["reference"]],
                    )
            computed_metrics = metrics_module.compute()
            metrics_module.reset()
            with jsonlines.open(os.path.join(temp_directory, "metrics.jsonl"), "a") as writer:
                writer.write(computed_metrics)

        # aggregate the metrics over submitted files
        with jsonlines.open(os.path.join(temp_directory, "metrics.jsonl"), "r") as reader:
            metrics_results = [line for line in reader]
        final_metrics_results = {
            key: sum(entry[key] for entry in metrics_results) / len(metrics_results) for key in metrics_results[0]
        }
        with open(os.path.join(temp_directory, "final_metrics.json"), "w") as f:
            json.dump(final_metrics_results, f)

    def _upload_results(
        self,
        task_id: str,
        model_folder: str,
        model_name_pretty: str,
        model_availability: str,
        model_url: Optional[str],
        urls: Optional[str],
        context_size: str,
        submitted_by: str,
        temp_directory: str,
    ) -> List[CommitOperationAdd]:
        """Adds files with the current model's metrics values to the results dataset."""
        final_results = {}
        with open(os.path.join(temp_directory, "final_metrics.json"), "r") as f:
            metrics = json.load(f)
        final_results.update(metrics)
        final_results.update(
            {
                "model_name": model_name_pretty,
                "model_availability": model_availability,
                "model_url": model_url,
                "urls": urls,
                "context_size": context_size,
                "submitted_by": submitted_by,
            }
        )

        with jsonlines.open(os.path.join(temp_directory, "final_results.jsonl"), "w") as writer:
            writer.write(final_results)

        return [
            CommitOperationAdd(
                path_in_repo=f"{task_id}/results/{model_folder}.jsonl",
                path_or_fileobj=os.path.join(temp_directory, "final_results.jsonl"),
            )
        ]

    def _verify_arguments(
        self,
        task_pretty: str,
        model_folder: str,
        model_name_pretty: str,
        model_availability: str,
        model_url: Optional[str],
        urls: Optional[str],
        context_size: str,
        submitted_by: str,
        contact_information: str,
        comment: Optional[str],
        filenames: Optional[List[str]],
    ):
        """Verifies that all necessary arguments are not None (and also runs other sanity checks)."""
        assert task_pretty and task_pretty in TASKS_PRETTY_REVERSE, "Please, select one of the supported tasks."
        assert model_folder, "Please, specify non-empty name for a directory with a model's results."
        assert model_name_pretty, "Please, specify non-empty name for a model."
        assert model_availability, "Please, specify non-empty information about a model's availability."
        assert context_size, "Please, specify non-empty information about a model's context size."
        try:
            _ = int(context_size)
        except:
            raise ValueError("Please, specify a model's context size as an integer (e.g., 16000).")

        if urls is not None and "," in urls:
            urls_list = urls.split(",")
            assert all(
                re.match(rf"^{MD_LINK_PATTERN}$", url.strip()) for url in urls_list
            ), 'Please, use the following format for URLs: "[text1](link1), [text2](link2)"'

        assert submitted_by, "Please, specify non-empty information about a submission's author(s)."
        assert filenames, "Please, attach at least one file with predictions."
        assert contact_information, "Please, fill in the field with contact information."

    def upload_files(
        self,
        task_pretty: str,
        model_folder: str,
        model_name_pretty: str,
        model_availability: str,
        model_url: Optional[str],
        urls: Optional[str],
        context_size: str,
        submitted_by: str,
        contact_information: str,
        comment: Optional[str],
        filenames: Optional[List[str]],
        force: bool = False,
    ) -> str:
        try:
            self._verify_arguments(
                task_pretty=task_pretty,
                model_folder=model_folder,
                model_name_pretty=model_name_pretty,
                model_availability=model_availability,
                model_url=model_url,
                urls=urls,
                context_size=context_size,
                submitted_by=submitted_by,
                contact_information=contact_information,
                comment=comment,
                filenames=filenames,
            )
            pr_title = f"πŸš€ New submission to {task_pretty} task: {model_name_pretty} with {context_size} context size from {submitted_by}"

            logging.info(f"Start processing {pr_title}")

            task_id = TASKS_PRETTY_REVERSE[task_pretty]

            logging.info("Checking if this request has already been submitted...")
            if not force:
                if self._fs.isdir(f"datasets/{self._results_dataset_id}/{task_id}/predictions/{model_folder}"):
                    return styled_warning(
                        f"{model_folder} is already present in {self._results_dataset_id}, please, select another folder name."
                    )

                prev_pr = self._get_previous_pr(pr_title)
                if prev_pr is not None:
                    url = f"https://huggingface.co/datasets/{self._results_dataset_id}/discussions/{prev_pr.num}"
                    return styled_warning(
                        f"{self._results_dataset_id} already has an open PR for this submission: {url}."
                    )

            logging.info("Processing predictions...")
            predictions_commit_operations = self._upload_predictions(
                task_id=task_id,
                model_folder=model_folder,
                filenames=filenames,
            )

            with TemporaryDirectory() as d:
                logging.info("Computing metrics...")
                self._compute_metrics_for_predictions(task_id=task_id, filenames=filenames, temp_directory=str(d))

                logging.info("Processing results...")
                results_commit_operations = self._upload_results(
                    task_id=task_id,
                    model_folder=model_folder,
                    model_name_pretty=model_name_pretty,
                    model_availability=model_availability,
                    model_url=model_url,
                    urls=urls,
                    context_size=context_size,
                    submitted_by=submitted_by,
                    temp_directory=str(d),
                )

                logging.info("Creating commit to the results dataset...")
                new_pr = self._api.create_commit(
                    repo_id=self._results_dataset_id,
                    operations=predictions_commit_operations + results_commit_operations,
                    commit_message=pr_title,
                    commit_description=f"""New submission to {task_pretty} task in 🏟️ Long Code Arena benchmark!\n* Model name: {model_name_pretty}\n* Model availability: {model_availability}\n* Context Size: {context_size}\n* Relevant URLs: {urls}\n* Submitted By: {submitted_by}""",
                    create_pr=True,
                    repo_type="dataset",
                )

                logging.info("Creating commit to the requests dataset...")
                request_commit_operations = self._upload_request(
                    task_id=task_id,
                    model_folder=model_folder,
                    temp_directory=str(d),
                    model_name_pretty=model_name_pretty,
                    model_availability=model_availability,
                    model_url=model_url,
                    urls=urls,
                    context_size=context_size,
                    submitted_by=submitted_by,
                    contact_information=contact_information,
                    comment=comment,
                    pr_url=new_pr.pr_url,
                )
                self._api.create_commit(
                    repo_id=self._requests_dataset_id,
                    operations=request_commit_operations,
                    commit_message=pr_title,
                    commit_description=f"""New submission to {task_pretty} task in 🏟️ Long Code Arena benchmark!\n* Model name: {model_name_pretty}\n* Model availability: {model_availability}\n* Context Size: {context_size}\n* Relevant URLs: {urls}\n* Submitted By: {submitted_by}\n* PR: {new_pr.pr_url}\n* Contact information: {contact_information}\n* Comment: {comment}""",
                    create_pr=True,
                    repo_type="dataset",
                )

                return styled_message(f"πŸŽ‰ PR created at {new_pr.pr_url}.")

        except Exception as e:
            exception_msg = str(e)
            if exception_msg and os.environ["PRIVATE_DATASET_ID"] in exception_msg:
                exception_msg = exception_msg.replace(os.environ["PRIVATE_DATASET_ID"], "{private_dataset}")
            if exception_msg:
                return styled_error(exception_msg)
            return styled_error("An exception occurred. Please, try again.")