File size: 6,771 Bytes
9be4956
 
 
 
 
943733f
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a3b852
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
5b8f2ac
e40c1b0
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40c1b0
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f7423
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
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "./leaderboard/evaluation")))
sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "./leaderboard")))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.environ['CURL_CA_BUNDLE'] = ''
import json
import datetime
from email.utils import parseaddr

import gradio as gr
import pandas as pd
import numpy as np

from datasets import load_dataset
from apscheduler.schedulers.background import BackgroundScheduler
from huggingface_hub import HfApi

# InfoStrings
# from scorer import question_scorer
from content import format_error, format_warning, format_log, TITLE, INTRODUCTION_TEXT, CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, model_hyperlink
from eval import eval_score

TOKEN = os.environ.get("TOKEN", None)
OWNER="osunlp"
DATA_DATASET = f"{OWNER}/TravelBench"
EVAL_DATASET = f"{OWNER}/TravelBenchEval"

api = HfApi()

YEAR_VERSION = "2024"

os.makedirs("scored", exist_ok=True)

# # Display the results
query_data_list  = load_dataset('osunlp/TravelBenchEval','validation',token=TOKEN)['validation']
eval_results = load_dataset(EVAL_DATASET, 'scores', token=TOKEN)
def get_dataframe_from_results(eval_results, split):
    local_df = eval_results[split]
    local_df = local_df.remove_columns(["Mail"])
    df = pd.DataFrame(local_df)
    df = df.sort_values(by=["Final Pass Rate"], ascending=False)
    numeric_cols = [c for c in local_df.column_names if "Rate" in c]
    df[numeric_cols] = df[numeric_cols].multiply(100).round(decimals=2)
    return df


eval_dataframe_val = get_dataframe_from_results(eval_results=eval_results, split="validation")
eval_dataframe_test = get_dataframe_from_results(eval_results=eval_results, split="test")



# def restart_space():
#     api.restart_space(repo_id=LEADERBOARD_PATH, token=TOKEN)


def load_line_json_data(filename):
    data = []
    with open(filename, 'r', encoding='utf-8') as f:
        for line in f.read().strip().split('\n'):
            unit = json.loads(line)
            data.append(unit)
    return data


def add_new_eval(
    val_or_test: str,
    eval_mode: str,
    model: str,
    planning_strategy: str,
    organization: str,
    mail: str,
    path_to_file: str,
):
    # Very basic email parsing
    _, parsed_mail = parseaddr(mail)
    if not "@" in parsed_mail:
        return format_warning("Please provide a valid email adress.")

    print("Adding new eval")
    
    if path_to_file is None:
        return format_warning("Please attach a file.")

    # Save submitted file
    api.upload_file(
        repo_id=EVAL_DATASET, 
        path_or_fileobj=path_to_file.name, 
        path_in_repo=f"{organization}/{val_or_test}_{eval_mode}_{planning_strategy}_raw_{datetime.datetime.today()}.jsonl",
        repo_type="dataset", 
        token=TOKEN
    )

    # Compute score
    file_path = path_to_file.name     
    result = eval_score(val_or_test,file_path=file_path,TOKEN=TOKEN)   
    with open(f"scored/{organization}_{val_or_test}_{eval_mode}_{planning_strategy}.jsonl", "w") as scored_file:
        scored_file.write(json.dumps(result) + "\n")
    
    # Save scored file
    api.upload_file(
        repo_id=EVAL_DATASET, 
        path_or_fileobj=f"scored/{organization}_{val_or_test}_{eval_mode}_{planning_strategy}.jsonl",
        path_in_repo=f"{organization}/{model}/{val_or_test}_{eval_mode}_{planning_strategy}_scored_{datetime.datetime.today()}.jsonl", 
        repo_type="dataset", 
        token=TOKEN
    )

    # Actual submission
    eval_entry = {
        "Model": model,
        "Planning Strategy": planning_strategy,
        "Organization": organization,
        "Mail": mail,
        "Delivery Rate": result['Delivery Rate'],
        "Commonsense Constraint Micro Pass Rate":result['Commonsense Constraint Micro Pass Rate'],
        "Commonsense Constraint Macro Pass Rate":result['Commonsense Constraint Macro Pass Rate'],
        "Hard Constraint Micro Pass Rate":result['Hard Constraint Micro Pass Rate'],
        "Hard Constraint Macro Pass Rate":result['Hard Constraint Macro Pass Rate'],
        "Final Pass Rate":result['Final Pass Rate']
    }

    eval_results[val_or_test] = eval_results[val_or_test].add_item(eval_entry)

    print(eval_results)

    eval_results.push_to_hub(EVAL_DATASET, config_name = 'scores', token=TOKEN)

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


def refresh():
    eval_results = load_dataset(EVAL_DATASET, 'scores', token=TOKEN)
    eval_dataframe_val = get_dataframe_from_results(eval_results=eval_results, split="validation")
    eval_dataframe_test = get_dataframe_from_results(eval_results=eval_results, split="test")
    return eval_dataframe_val, eval_dataframe_test

# def upload_file(files):
#     file_paths = [file.name for file in files]
#     return file_paths


demo = gr.Blocks()
with demo:
    gr.HTML(TITLE)
    gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")

    with gr.Tab("Results: Validation"):
        leaderboard_table_val = gr.components.Dataframe(
            value=eval_dataframe_val, interactive=False,
        )
    with gr.Tab("Results: Test"):
        leaderboard_table_test = gr.components.Dataframe(
            value=eval_dataframe_test, interactive=False,
        )

    refresh_button = gr.Button("Refresh")
    refresh_button.click(
        refresh,
        inputs=[],
        outputs=[
            leaderboard_table_val,
            leaderboard_table_test,
        ],
    )
    with gr.Accordion("Submit a new file for evaluation"):
        with gr.Row():
            with gr.Column():
                level_of_test = gr.Radio(["validation", "test"], value="validation", label="Split")
                eval_mode = gr.Radio(["two-stage", "sole-planning"], value="two-stage", label="Eval Mode")
                model = gr.Textbox(label="Foundation Model")
                planning_strategy = gr.Textbox(label="Planning Strategy")
            with gr.Column():
                organization = gr.Textbox(label="Organization")
                mail = gr.Textbox(label="Contact email")
                file_output = gr.File()


        submit_button = gr.Button("Submit Eval")
        submission_result = gr.Markdown()
        submit_button.click(
            add_new_eval,
            [
                level_of_test,
                eval_mode,
                model,
                planning_strategy,
                organization,
                mail,
                file_output,
            ],
            submission_result,
        )

# scheduler = BackgroundScheduler()
# scheduler.add_job(restart_space, "interval", seconds=3600)
# scheduler.start()
demo.launch(debug=True)