File size: 5,291 Bytes
5b27273
d70e16b
 
 
 
 
5b27273
 
d70e16b
 
5b27273
d70e16b
5b27273
 
d70e16b
 
 
5b27273
d70e16b
 
 
 
 
 
 
 
5b27273
 
36713b7
 
 
 
 
 
5b27273
d70e16b
 
 
 
 
 
 
 
 
 
 
f60b1f2
 
 
 
 
 
 
d70e16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71e3f1c
 
d70e16b
 
 
 
 
 
 
 
 
 
 
 
7d93964
d70e16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d93964
d70e16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459aaf8
d70e16b
 
 
 
 
a7a733e
d70e16b
 
 
 
 
459aaf8
d70e16b
 
5b27273
d70e16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2dff95
 
5b27273
d70e16b
 
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
import gradio as gr

__all__ = ["block", "make_clickable_model", "make_clickable_user", "get_submissions"]


import numpy as np
import pandas as pd

from constants import *
from src.auto_leaderboard.model_metadata_type import ModelType

global data_component, filter_component, ref_dic


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


def read_xlsx_leaderboard():
    df_dict = pd.read_excel(XLSX_DIR, sheet_name=None)  # get all sheet
    return df_dict


def get_specific_df(sheet_name):
    df = read_xlsx_leaderboard()[sheet_name].sort_values(by="Overall", ascending=False)
    return df

def get_link_df(sheet_name):
    df = read_xlsx_leaderboard()[sheet_name]
    return df


ref_df = get_link_df("main")

ref_dic = {}
for id, row in ref_df.iterrows():
    
    ref_dic[
        str(row["Model"])
    ] = f'<a href="{row["Link"]}" target="_blank"  style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{row["Model"]}</a>'

def wrap_model(func):
    def wrapper(*args, **kwargs):
        df = func(*args, **kwargs)
        df["Model"] = df["Model"].apply(lambda x: ref_dic[x])
        # cols_to_round = df.select_dtypes(include=[np.number]).columns.tolist()
        # cols_to_round = [col for col in cols_to_round if col != "Model"]
        # df[cols_to_round] = df[cols_to_round].apply(lambda x: np.round(x, 2))

        all_cols = df.columns.tolist()
        non_numeric_cols = df.select_dtypes(exclude=[np.number]).columns.tolist()
        cols_to_round = [col for col in all_cols if col not in non_numeric_cols and col != "Model"]
        df[cols_to_round] = df[cols_to_round].apply(lambda x: np.round(x, 2))
        return df

    return wrapper


@wrap_model
def get_base_zh_df():
    return get_specific_df("base-zh")


@wrap_model
def get_base_en_df():
    return get_specific_df("base-en")


@wrap_model
def get_attack_zh_df():
    return get_specific_df("attack-zh")


@wrap_model
def get_attack_en_df():
    return get_specific_df("attack-en")


def build_leaderboard(
    TABLE_INTRODUCTION, TAX_COLUMNS, get_chinese_df, get_english_df
):

    gr.Markdown(TABLE_INTRODUCTION, elem_classes="markdown-text")
    data_spilt_radio = gr.Radio(
        choices=["Chinese", "English"],
        value="Chinese",
        label=SELECT_SET_INTRO,
    )

    # 创建数据帧组件
    data_component = gr.components.Dataframe(
        value=get_chinese_df,
        headers=OVERALL_INFO + TAX_COLUMNS,
        type="pandas",
        datatype=["markdown"] + ["number"] + ["number"] * len(TAX_COLUMNS),
        interactive=False,
        visible=True,
        wrap=True,
        column_widths=[250] + [100] + [150] * len(TAX_COLUMNS),
    )

    def on_data_split_radio(seleted_split):
        if "Chinese" in seleted_split:
            updated_data = get_chinese_df()
        if "English" in seleted_split:
            updated_data = get_english_df()
        current_columns = data_component.headers  # 获取的当前的column
        current_datatype = data_component.datatype  # 获取当前的datatype
        filter_component = gr.components.Dataframe(
            value=updated_data,
            headers=current_columns,
            type="pandas",
            datatype=current_datatype,
            interactive=False,
            visible=True,
            wrap=True,
            column_widths=[250] + [100] + [150] * (len(current_columns) - 2),
        )
        return filter_component

    # 关联处理函数
    data_spilt_radio.change(
        fn=on_data_split_radio, inputs=data_spilt_radio, outputs=data_component
    )


def build_demo():
    block = gr.Blocks()

    with block:
        gr.Markdown(LEADERBOARD_INTRODUCTION)

        with gr.Tabs(elem_classes="tab-buttons") as tabs:
            # first
            with gr.TabItem(
                "Base Risk Prompt Set Results",
                elem_id="evalcrafter-benchmark-tab-table",
                id=0,
            ):
                build_leaderboard(
                    TABLE_INTRODUCTION_1,
                    risk_topic_1_columns,
                    get_base_zh_df,
                    get_base_en_df
                )
            # second
            with gr.TabItem(
                "Attack Prompt Set Results",
                elem_id="evalcrafter-benchmark-tab-table",
                id=1,
            ):
                build_leaderboard(
                    TABLE_INTRODUCTION_2,
                    attack_columns,
                    get_attack_zh_df,
                    get_attack_en_df
                )
            # last table about
            with gr.TabItem("📝 About", elem_id="evalcrafter-benchmark-tab-table", id=3):
                gr.Markdown(LEADERBORAD_INFO, elem_classes="markdown-text")

        with gr.Row():
            with gr.Accordion("📙 Citation", open=True):
                citation_button = gr.Textbox(
                    value=CITATION_BUTTON_TEXT,
                    label=CITATION_BUTTON_LABEL,
                    lines=10,
                    elem_id="citation-button",
                    show_label=True,
                    show_copy_button=True,
                )

    # block.launch(share=True)
    block.launch()

if __name__ == "__main__":
    build_demo()