File size: 2,255 Bytes
1538407
 
 
 
 
 
a283e53
1538407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from email.utils import parseaddr
from huggingface_hub import HfApi
import io
import os
import base64
import pandas as pd
from utils import DEFAULT_METRICS, LEADERBOARD_PATH


api = HfApi()
TOKEN = os.environ.get("TOKEN", None)
YEAR_VERSION = "2024"


def format_error(msg):
    return f"<p style='color: red; font-size: 20px; text-align: center;'>{msg}</p>"


def format_warning(msg):
    return f"<p style='color: orange; font-size: 20px; text-align: center;'>{msg}</p>"


def format_log(msg):
    return f"<p style='color: green; font-size: 20px; text-align: center;'>{msg}</p>"


def add_new_eval(
    corpus: str,
    organization: str,
    mail: str,
    fpath: str,
):
    for input in [corpus, organization, mail, fpath]:
        if not input:
            return format_warning("Please fill all the fields.")
    
    if organization == 'Baseline':
        return format_warning("Your organization name cannot be Baseline.")

    _, parsed_mail = parseaddr(mail)
    if '@' not in parsed_mail:
        return format_warning("Please provide a valid email adress.")

    # load the file 
    io_path = f"submission/{corpus}.csv"
    df = pd.read_csv(io_path)
    df_new = pd.read_csv(fpath)

    for col in ['Method'] + DEFAULT_METRICS:
        if col not in df_new.columns:
            return format_warning(f"Missing column in the submitted file: {col}")

    df_new['Submitted By'] = organization
    df_new['Id'] = base64.b64encode(os.urandom(6)).decode('ascii')
    df_new = df_new[['Method', 'Submitted By'] + DEFAULT_METRICS + ['Id']]

    df = pd.concat([df, df_new]).reset_index(drop=True)
    buffer = io.BytesIO()
    df.to_csv(buffer, index=False)  # Write the DataFrame to a buffer in CSV format
    buffer.seek(0)  # Rewind the buffer to the beginning

    api.delete_file(
        repo_id = LEADERBOARD_PATH,
        path_in_repo = io_path,
        token = TOKEN,
        repo_type='space'
    )

    api.upload_file(
        repo_id = LEADERBOARD_PATH,
        path_in_repo = io_path,
        path_or_fileobj = buffer,
        token = TOKEN,
        repo_type = 'space',
    )

    return format_log(f"Submitted to {corpus} by {organization} successfully.\nPlease refresh the leaderboard, and wait a bit to see the score displayed")