File size: 8,143 Bytes
d7041cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
889a80b
d7041cd
 
 
 
 
 
 
 
 
 
 
 
 
b5caa7a
 
 
 
 
 
 
 
 
 
 
 
 
d7041cd
ba75603
d7041cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1bc2bf
d7041cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1bc2bf
d7041cd
 
 
 
 
 
 
 
c5184ef
d7041cd
 
 
 
 
 
 
 
 
 
 
b5caa7a
 
c66aadd
b5caa7a
 
 
 
 
 
 
d7041cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab03678
d7041cd
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
from typing import List

import gradio as gr
import numpy as np
import pandas as pd
from assets.text import INTRODUCTION_TEXT, METRICS_TEXT, EVALUTION_TEXT, ACKNOWLEDGEMENTS_TEXT, REFERENCE_TEXT


ORIGINAL_DF = pd.read_csv("./data/chinese_benchmark_gen.csv", sep='\t') # space separated values
ORIGINAL_DF_PER = pd.read_csv("./data/chinese_benchmark_per.csv", sep='\t') #

ORIGINAL_DF_SUB_GEN = pd.read_csv("./data/subclass_gen.csv", sep=',') #
ORIGINAL_DF_SUB_PER = pd.read_csv("./data/subclass_per.csv", sep=',')

METRICS = ["Accuracy", "Precision_Unsafe", "Recall_Unsafe", "Precision_Safe", "Recall_Safe", "None"]


SUBCLASS = ["Discrimination", "Variant", "Psychology", "Politics", "Eroticism", "Vulgarity", "Property", "Injury", "Criminality", "Ethics"]

#SPLITS = ["Overall", "Subclass"]
SPLITS = ["Overall", "Discrimination", "Variant", "Psychology", "Politics", "Eroticism", "Vulgarity", "Property", "Injury", "Criminality", "Ethics"]

CLASSIFICATION = {
    "model_size": [
        ">65B",
        "~30B",
        "10B~20B",
        "5B~10B",
        "API",
    ]
    
}


# _BIBTEX = """ Waiting for paper ... """

_BIBTEX = """
@misc{zhang2024chinesesafechinesebenchmarkevaluating,
      title={ChineseSafe: A Chinese Benchmark for Evaluating Safety in Large Language Models}, 
      author={Hengxiang Zhang and Hongfu Gao and Qiang Hu and Guanhua Chen and Lili Yang and Bingyi Jing and Hongxin Wei and Bing Wang and Haifeng Bai and Lei Yang},
      year={2024},
      eprint={2410.18491},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2410.18491}, 
}
"""

_LAST_UPDATED = "November 24, 2024"

banner_url = "./assets/logo.png"
_BANNER = f'<div style="display: flex; justify-content: space-around;"><img src="{banner_url}" alt="Banner" style="width: 40vw; min-width: 300px; max-width: 600px;"> </div>'  # noqa




def retrieve_array_from_text(text):
    return np.fromstring(text.replace("[", "").replace("]", ""), dtype=float, sep=",")

def format_csv_numbers(text):
    return text.split('/')[0]

def format_csv_numbers_second(text):
    return text.split()
    
    
def format_number(x):
    return float(f"{x:.3}")


def get_dataset_csv(
    model_size: List[str],
):
    df = ORIGINAL_DF[ORIGINAL_DF['Size'].isin(model_size)]
    df = df.drop(columns="Size")
    
    leaderboard_table = gr.components.Dataframe(
        value=df,
        interactive=False,
        visible=True,
    )
    return leaderboard_table

def get_dataset_csv_per(
    model_size: List[str],
):
    df = ORIGINAL_DF_PER[ORIGINAL_DF_PER['Size'].isin(model_size)]
    df = df.drop(columns="Size")

    leaderboard_table = gr.components.Dataframe(
        value=df,
        interactive=False,
        visible=True,
    )
    return leaderboard_table

# this is a sub function for csv table
def get_dataset_csv_sub_gen(
    model_size: List[str],
    subclass_choice: List[str],
):
    df = ORIGINAL_DF_SUB_GEN[ORIGINAL_DF_SUB_GEN['Size'].isin(model_size)]
    df = df.drop(columns="Size")
    
    # get subclass
    subclass_choice_label = ["Model", subclass_choice+"_Accuracy", subclass_choice+"_Precision", subclass_choice+"_Recall"]
    df = df[subclass_choice_label]
    
    leaderboard_table = gr.components.Dataframe(
        value=df,
        interactive=False,
        visible=True,
    )
    return leaderboard_table

# this is a sub function for csv table
def get_dataset_csv_sub_per(
    model_size: List[str],
    subclass_choice: List[str],
):
    df = ORIGINAL_DF_SUB_PER[ORIGINAL_DF_SUB_PER['Size'].isin(model_size)]
    df = df.drop(columns="Size")
    
    # get subclass
    subclass_choice_label = ["Model", subclass_choice+"_Accuracy", subclass_choice+"_Precision", subclass_choice+"_Recall"]
    df = df[subclass_choice_label]
    
    leaderboard_table = gr.components.Dataframe(
        value=df,
        interactive=False,
        visible=True,
    )
    return leaderboard_table


def get_dataset_classfier_gen(
    model_size: List[str],
    main_choice: List[str],
):
    if main_choice == "Overall":
        leaderboard_table = get_dataset_csv(model_size)
    elif main_choice != "Subclass":
        subclass_choice = main_choice
        leaderboard_table = get_dataset_csv_sub_gen(model_size, subclass_choice)
    return leaderboard_table
        
def get_dataset_classfier_per(
    model_size: List[str],
    main_choice: List[str],
):
    if main_choice == "Overall":
        leaderboard_table = get_dataset_csv_per(model_size)
    elif main_choice != "Overall":
        subclass_choice = main_choice
        leaderboard_table = get_dataset_csv_sub_per(model_size, subclass_choice)
    return leaderboard_table

with gr.Blocks() as demo:
    gr.Markdown("<center><h1>ChineseSafe Leaderboard</h1></center>", elem_classes="markdown-text")
    with gr.Row():
        #gr.Image(banner_url, height=160, scale=1) # πŸ‘‰ this part is for image
        gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
        # gr.Textbox(_INTRODUCTION_TEXT, scale=5)

    with gr.Row():
        gr.Markdown(METRICS_TEXT, elem_classes="markdown-text")
    
    with gr.Row():
        gr.Markdown(EVALUTION_TEXT, elem_classes="markdown-text")
        
    with gr.Row():
        with gr.Column(scale=0.8):
            main_choice = gr.Dropdown(
                choices=SPLITS,
                value="Overall",
                label="Type",
                info="Please choose the type to display.",
            )
            
        with gr.Column(scale=10): 
            model_choice = gr.CheckboxGroup(
                choices=CLASSIFICATION["model_size"],
                value=CLASSIFICATION["model_size"],  # all be choosed
                label="Model Size",
                info="Please choose the model size to display.",
            )

    #πŸ‘‰ this part is for csv table generatived
    with gr.Tabs(elem_classes="tab-buttons") as tabs:
        # ----------------- modify text -----------------
        
        with gr.TabItem("πŸ… Generation", elem_id="od-benchmark-tab-table", id=6):
            dataframe_all_gen = gr.components.Dataframe(
                elem_id="leaderboard-table",
            )
            
        with gr.TabItem("πŸ… Perplexity", elem_id="od-benchmark-tab-table", id=5):
            dataframe_all_per = gr.components.Dataframe(
                elem_id="leaderboard-table",
            )

    # ----------------- modify text -----------------
    with gr.Row():
        gr.Markdown(ACKNOWLEDGEMENTS_TEXT, elem_classes="markdown-text")
    
    with gr.Row():
        gr.Markdown(REFERENCE_TEXT, elem_classes="markdown-text")
    
    # πŸ‘‰ this part is for citation
    with gr.Row():
        with gr.Accordion("πŸ“™ Citation", open=True):
            gr.Textbox(
                value=_BIBTEX,
                lines=7,
                label="Copy the BibTeX snippet to cite this source",
                elem_id="citation-button",
                show_copy_button=True
            )
    
    gr.Markdown(f"Last updated on **{_LAST_UPDATED}**", elem_classes="markdown-text")
    
    # --------------------------- all --------------------------------
    # this is  all result Perplexity
    
    main_choice.change(
        get_dataset_classfier_per,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_per,
    )
        
    model_choice.change(
        get_dataset_classfier_per,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_per,
    )

    demo.load(
        fn=get_dataset_classfier_per,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_per,
    )
    
    # this is all result generatived
    main_choice.change(
        get_dataset_classfier_gen,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_gen,
    )
        
    model_choice.change(
        get_dataset_classfier_gen,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_gen,
    )
    
    demo.load(
        fn=get_dataset_classfier_gen,
        inputs=[model_choice, main_choice],
        outputs=dataframe_all_gen,
    )
    
    
demo.launch(share=True)