File size: 4,564 Bytes
281711d
1c33a6b
281711d
 
 
2982a51
281711d
 
3e3ca09
b57f917
b8be656
281711d
 
51aaefc
281711d
63bdadc
 
50e75cf
04ba2d3
956b725
1c33a6b
c6e6b77
1c33a6b
 
 
 
 
8e2e988
1c33a6b
 
 
0003044
 
3d67011
0003044
63bdadc
2a5d0b4
 
be51a4e
 
b57f917
2982a51
 
 
b8be656
2982a51
82c5741
50e75cf
82c5741
281711d
 
20a6e65
 
 
 
0003044
2cdfab6
efa6cf8
9818d92
be51a4e
9818d92
be51a4e
2cdfab6
0003044
 
e1db249
3e3ca09
 
 
 
 
 
 
d6d49d1
b57f917
 
 
20a6e65
b8be656
 
20a6e65
b8be656
 
 
e8b7916
1c33a6b
 
e8b7916
1e4ed96
e8b7916
 
1e4ed96
b8be656
82c5741
b8be656
 
 
 
52940ae
b8be656
 
e8b7916
1c33a6b
82c5741
 
1c33a6b
 
3e3ca09
1c33a6b
 
 
 
d6d49d1
281711d
 
 
 
 
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
import pathlib
from pathlib import Path
import tempfile
from typing import BinaryIO, Literal
import json
import pandas as pd

import gradio as gr
from datasets import load_dataset
from huggingface_hub import upload_file, hf_hub_download, list_files_info
from gradio_leaderboard import ColumnFilter, Leaderboard, SelectColumns
from evaluation import evaluate_problem
from datetime import datetime
import os

from submit import submit_boundary
from about import PROBLEM_TYPES, TOKEN, CACHE_PATH, API, submissions_repo, results_repo
from utils import read_boundary, write_results, get_user
from visualize import make_visual

def evaluate_boundary(filename):
    print(filename)
    local_path = read_boundary(filename)
    with Path(local_path).open("r") as f:
        raw = f.read()
        data_dict = json.loads(raw)
    result = evaluate_problem(data_dict['problem_type'], local_path)
    
    write_results(data_dict, result)
    return

def make_clickable(name):
    link =f'https://huggingface.co/{name}'
    return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{name}</a>'

def get_leaderboard():
    ds = load_dataset(results_repo, split='train')
    df = pd.DataFrame(ds)

    df.rename(columns={'submission_time': 'submission time', 'problem_type': 'problem type'}, inplace=True)
    # df['user'] = df['user'].apply(lambda x: make_clickable(x)).astype(str)
    score_field = "score" if "score" in df.columns else "objective"  # fallback

    df = df.sort_values(by=score_field, ascending=True)
    return df

def show_output_box(message):
    return gr.update(value=message, visible=True)

def gradio_interface() -> gr.Blocks:
    with gr.Blocks() as demo:
        with gr.Tabs(elem_classes="tab-buttons"):
            with gr.TabItem("Leaderboard", elem_id="boundary-benchmark-tab-table"):                
                gr.Markdown("# Boundary Design Leaderboard")

                Leaderboard(
                    value=get_leaderboard(),
                    datatype=['str', 'date', 'str', 'str', 'bool', 'markdown', 'number', 'bool', 'number', 'number', 'str'],
                    select_columns=["submission time", "feasibility", "score", "problem type", "user"],
                    search_columns=["submission time", "score", "user"],
                    hide_columns=["result_filename", "submission_filename", "objective", "minimize_objective", "boundary_json", "evaluated"],
                    filter_columns=["problem type"],
                    every=60,
                    render=True
                )

            with gr.TabItem("About", elem_id="boundary-benchmark-tab-table"):
                gr.Markdown(
                    """
                # Welcome to the Fusion Challenge!
                Upload your plasma boundary JSON and select the problem type to get your score.
                """
                )

                # dropdown = gr.Dropdown(choices=filenames, label="Choose a file")
                # plot_output = gr.Plot()

            with gr.TabItem("Submit", elem_id="boundary-benchmark-tab-table"):
                gr.Markdown(
                    """
                # Plasma Boundary Evaluation Submission
                Upload your plasma boundary JSON and select the problem type to get your score.
                """
                )
                user_state = gr.State(value=None) 
                filename = gr.State(value=None) 
                eval_state = gr.State(value=None) 

                gr.LoginButton()

                demo.load(get_user, inputs=None, outputs=user_state)

                with gr.Row():
                    problem_type = gr.Dropdown(PROBLEM_TYPES, label="Problem Type")
                    boundary_file = gr.File(label="Boundary JSON File (.json)")

                boundary_file
                submit_btn = gr.Button("Evaluate")
                message = gr.Textbox(label="Status", lines=1, visible=False)
                submit_btn.click(
                    submit_boundary,
                    inputs=[problem_type, boundary_file, user_state],
                    outputs=[message, filename],
                ).then(
                    fn=show_output_box,
                    inputs=[message],
                    outputs=[message],
                ).then(
                    fn=evaluate_boundary,
                    inputs=[filename],
                    outputs=[eval_state]
                )
 
    return demo


if __name__ == "__main__":
    gradio_interface().launch()