File size: 29,679 Bytes
aeb6d58
 
 
 
 
 
cc8a66b
f276a79
6d540bf
 
 
ccde434
aac9ef0
1158e1e
9210847
fb1f20c
aeb6d58
a4ab56b
aeb6d58
 
 
 
c89c654
 
 
 
 
d7f014d
aeb6d58
 
 
c89c654
 
 
aeb6d58
 
 
c89c654
 
 
aeb6d58
 
 
c89c654
 
 
 
 
d7f014d
aeb6d58
 
 
c89c654
 
 
aeb6d58
 
 
c89c654
 
 
 
 
d7f014d
aeb6d58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c89c654
aeb6d58
 
 
 
c89c654
aeb6d58
 
 
 
 
 
 
 
 
 
 
 
 
 
86e1422
 
 
 
 
 
 
 
 
 
 
c89c654
 
 
 
 
 
 
 
fb1f20c
c89c654
5cd2be1
 
 
c89c654
 
 
 
 
fb1f20c
c89c654
 
 
 
234a449
fb1f20c
c89c654
 
fb1f20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234a449
fb1f20c
 
 
 
 
234a449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb1f20c
 
 
 
 
 
 
 
 
 
234a449
 
 
 
 
 
 
 
fb1f20c
 
 
 
 
 
 
 
 
234a449
 
 
 
 
 
 
 
fb1f20c
 
234a449
 
 
 
 
 
 
 
 
fb1f20c
234a449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb1f20c
 
 
 
 
aeb6d58
c89c654
89688fa
c89c654
89688fa
c89c654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89688fa
 
 
c89c654
 
 
 
 
 
89688fa
 
 
 
 
 
 
 
aeb6d58
c89c654
aac9ef0
 
aeb6d58
aac9ef0
ccde434
 
 
aac9ef0
 
ccde434
aac9ef0
ccde434
aeb6d58
 
 
c89c654
aeb6d58
 
 
 
 
 
c89c654
aeb6d58
 
 
 
 
 
 
 
 
 
 
 
fb1f20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f276a79
c89c654
f276a79
 
 
 
fb1f20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54a0a2e
c89c654
fb1f20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
848ffbd
 
f276a79
 
 
6e57415
 
 
 
 
 
 
 
 
 
 
 
 
c89c654
6e57415
 
 
 
c589db3
 
 
 
 
 
 
 
 
 
 
 
 
 
c89c654
c589db3
8171dbf
8aa4f17
 
 
 
aeb6d58
 
fb1f20c
c89c654
9210847
fb1f20c
aeb6d58
c89c654
cc8a66b
 
fb1f20c
cc8a66b
 
 
 
 
 
 
 
fb1f20c
 
 
 
 
c89c654
cc8a66b
6e57415
 
 
cc8a66b
6e57415
 
aeb6d58
 
 
 
8aa4f17
9210847
 
fb1f20c
aeb6d58
234a449
acb5890
 
c89c654
fb1f20c
 
 
 
 
 
 
c89c654
 
234a449
 
 
c89c654
 
234a449
 
 
c89c654
 
234a449
 
 
c89c654
 
234a449
 
 
c89c654
 
234a449
 
 
c89c654
 
234a449
 
 
 
 
 
 
 
c89c654
aeb6d58
 
 
89688fa
fb1f20c
f276a79
8aa4f17
f276a79
 
 
 
 
 
 
 
aeb6d58
 
fb1f20c
aeb6d58
 
 
c89c654
aeb6d58
c89c654
aeb6d58
 
2690375
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import pandas as pd
import gradio as gr
import os
import re
import requests
from dotenv import load_dotenv
from matplotlib.colors import LinearSegmentedColormap
import plotly.express as px
import plotly.graph_objects as go
from sklearn.linear_model import LinearRegression
import numpy as np
from huggingface_hub import HfApi
from huggingface_hub.hf_api import HTTPError
from huggingface_hub.utils import GatedRepoError
from gradio_rangeslider import RangeSlider
import datetime


load_dotenv()
webhook_url = os.environ.get("WEBHOOK_URL")

file_name_list = [
    "14b",
    "9b",
    "7b",
    "3b",
    "1b5",
    "other",
]

sheet_name_list = [
    "cr",
    "bpc",
    "bpb",
]

metric_list = [
    "Compression Rate (%)",
    "Bits Per Character (BPC)",
    "Bits Per Byte (BPB)",
]

model_size_list = [
    "~14B",
    "~9B",
    "~7B",
    "~3B",
    "~1.5B",
    "Other",
]

metric_to_sheet = {
    "Compression Rate (%)": "cr",
    "Bits Per Character (BPC)": "bpc",
    "Bits Per Byte (BPB)": "bpb",
}

model_size_to_file_name = {
    "~14B": "14b",
    "~9B": "9b",
    "~7B": "7b",
    "~3B": "3b",
    "~1.5B": "1b5",
    "Other": "other",
}

about_md = """

# Uncheatable Eval



GitHub page: [https://github.com/Jellyfish042/uncheatable_eval](https://github.com/Jellyfish042/uncheatable_eval)



## Introduction

Traditional LLM benchmarks are easily compromised by unintentional or intentional data leakage, making many benchmarks unreliable and unable to truly reflect the capabilities of LLMs.



Uncheatable Eval addresses this issue by testing LLMs on real-time, newly generated data from the internet, 

ensuring that the evaluation is immune to data leaks and cannot be gamed.



## How?

Uncheatable Eval assesses the language modeling capabilities of LLMs on new data from various sources such as recent papers on arXiv, new projects on GitHub, news articles, and more. Since this data is brand new (e.g., from the past 1-2 weeks), it is impossible for these data to be included in the training sets of publicly released models, thus avoiding the impact of unintentional or intentional data leaks.



Specifically, we calculate the sum of negative log probabilities of the models on these texts. In other words, models that are more likely to generate these texts are considered better.



*Note* : Uncheatable Eval only tests base models.



## Q&A

### Why Calculate the Sum of Negative Log Probabilities?

First, the goal of language models, at least today's language models, is to generate text that is as realistic as possible, maximizing the probability of real text. They are trained and designed to do exactly this. Calculating the sum of negative log probabilities on real text is the most direct way to test this capability.



Second, from the perspective of "compression is intelligence," a good way to test a language model would be to use the model with an entropy coding algorithm for compression and test the model's compression rate [[1]](https://arxiv.org/abs/2309.10668)[[2]](https://arxiv.org/abs/2402.00861). A model with a lower compression rate is considered better. Using a language model + arithmetic coding as an example, it is easy to prove that a model's ability to compress a piece of text is proportional to the sum of its negative log probabilities on that text (see [proof](#proof-of-the-equivalence-between-compression-capability-and-negative-log-probability-sum)).

Therefore, the compression rate of a model can be directly calculated through the sum of negative log probabilities, and the method for this has been provided in `show_results_v2.ipynb`.

### Can Models Using Different Tokenizers Be Directly Compared?

Yes. When calculating the sum of negative log probabilities, we essentially treat the model + tokenizer as a single entity or system. As long as this system has a high probability of generating real text, we consider it better. From the perspective of compression, you can choose any tokenizer. From the compression rate perspective, we don't care; we only care about whether your system can compress the text more effectively.



### Is It Really Uncheatable? Can't I train my model on a large number of arXiv papers to improve its test performance on arXiv papers?

Uncheatable Eval's data sources currently include new arXiv papers, new GitHub projects, BBC news, AO3 fanfictions, and new Wikipedia entries, with more sources to be added in the future. If you genuinely achieve excellent results across these data by training extensively on these sources, I would consider you to have developed a genuinely good language model rather than cheating.



From my test results, accurately modeling these data is very challenging. I believe Uncheatable Eval more accurately reflects the value of every bit of data and computing you invest compared to other benchmarks. Models trained with more data and computing are almost always better, and there are no shortcuts. This is a key strength of Uncheatable Eval.



### Is This Too "Random"? Why Consider Random Texts from the Internet as Ground Truth?

This is why we choose rigorous and verified texts such as arXiv papers and news reports, which typically have better quality. Additionally, a round of Uncheatable Eval evaluates a model over millions of tokens, increasing the reliability of the results.



In fact, the model rankings obtained through Uncheatable Eval are very stable. For instance, the model ranked first in January's data is highly likely to remain first in February, March, April, May, and June, indicating that the data obtained through this method is sufficiently representative.

"""


def rename_columns(df):
    df.columns = [col.rsplit("_", maxsplit=1)[0] for col in df.columns]
    return df


def get_folders_matching_format(directory):
    pattern = re.compile(r"^\d{4}-\d{2}$")
    folders = []

    if not os.path.exists(directory):
        return folders

    for item in os.listdir(directory):
        full_path = os.path.join(directory, item)
        if os.path.isdir(full_path) and pattern.match(item):
            folders.append(full_path)

    return folders


def get_unique_column_names(all_data):
    # column_names = {}
    #
    # for folder_name, files in all_data.items():
    #     for file_name, sheets in files.items():
    #         for sheet_name, dataframe in sheets.items():
    #             for column in dataframe.columns:
    #                 if column not in ['Name', 'Average (The lower the better)', 'Parameters Count (B)']:
    #                     column_names[column] = None
    #
    # return list(column_names.keys())

    return [
        "ao3_\u200benglish",
        "bbc_\u200bnews",
        "wikipedia_\u200benglish",
        "arxiv_\u200bcomputer_\u200bscience",
        "arxiv_\u200bphysics",
        "github_\u200bcpp",
        "github_\u200bpython",
        # "ao3_\u200bchinese",
    ]


def color_cell(value):
    return "background-color: #fffdd0" if pd.notna(value) else "default"


def update_table(

    period: str,

    models_size: list,

    metric: str,

    visible_columns: list,

    color_columns: list,

    size_range: list,

    midpoint: float = 0.5,

    sort_by: str = "Average (lower=better)",

    ascending: bool = True,

):
    print(
        f"Updating - time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, period: {period}, models: {models_size}, metric: {metric}, visible_columns: {visible_columns}, color_columns: {color_columns}, size_range: {size_range}, sort_by: {sort_by}, ascending: {ascending}\n"
    )

    if not models_size:
        return "No data available for the selected models and period."
        # return pd.DataFrame()

    target_period_data = all_data[period]
    target_file_name = [model_size_to_file_name[model] for model in models_size]
    sheet_name = metric_to_sheet[metric]

    # combined_data = pd.concat([target_period_data[file_name][sheet_name] for file_name in target_file_name], axis=0)
    combined_data = pd.concat(
        [df.dropna(axis=1, how="all") for df in [target_period_data[file_name][sheet_name] for file_name in target_file_name]], axis=0
    )
    if len(combined_data) == 0:
        return "No data available for the selected models and period."
        # return pd.DataFrame()

    # Filter models based on the size range
    combined_data = combined_data[combined_data["Parameters Count (B)"].between(size_range[0], size_range[1])]
    combined_data.reset_index(drop=True, inplace=True)
    if len(combined_data) == 0:
        return "No data available for the selected models and period."
        # return pd.DataFrame()

    combined_data["Name"] = combined_data["Name"].apply(lambda x: x.replace(".pth", ""))

    relevant_columns = [col for col in visible_columns if col not in ["Name", "Parameters Count (B)", "Average (The lower the better)"]]
    if len(combined_data) > 0:
        combined_data["Average (The lower the better)"] = round(combined_data[relevant_columns].mean(axis=1), 3)
    combined_data = combined_data.rename(columns={"Parameters Count (B)": "Params (B)"})
    combined_data = combined_data.rename(columns={"Average (The lower the better)": "Average (lower=better)"})
    sorted_data = combined_data.sort_values(by=sort_by, ascending=ascending)
    visible_columns = ["Name", "Params (B)", "Average (lower=better)"] + visible_columns
    filtered_data = sorted_data[visible_columns]
    filtered_data.columns = [col.replace("_", " ") for col in filtered_data.columns]

    formatter = {col: "{:.3f}" for col in filtered_data.columns if filtered_data[col].dtype in ["float64", "float32"]}

    # color gradient
    colors = ["#63be7b", "#ffffff", "#f8696b"]
    vmin = {}
    vmax = {}
    vmid = {}
    for column in filtered_data.columns:
        if column in ["Name", "Params (B)"]:
            continue
        col_values = filtered_data[column]
        if len(col_values) > 1:
            sorted_values = np.sort(col_values)
            vmin[column] = sorted_values.min()
            vmax[column] = sorted_values.max()
            idx = int(len(sorted_values) * midpoint)
            vmid[column] = sorted_values[idx]

    def custom_background_gradient(series, cmap, vmin, vmax, vmid):
        if len(series) == 0:
            return series

        def normalize(x):
            if x <= vmid:
                return 0.5 * (x - vmin) / (vmid - vmin)
            else:
                return 0.5 + 0.5 * (x - vmid) / (vmax - vmid)

        normed = series.apply(normalize)
        colors = [cmap(x) for x in normed]
        return ["background-color: rgba({}, {}, {}, {})".format(*[int(255 * x) for x in c[:3]], c[3]) for c in colors]

    target_color_columns = []
    if "Average" in color_columns:
        target_color_columns.append("Average (lower=better)")
    if "Individual Tests" in color_columns:
        target_color_columns.extend([col for col in filtered_data.columns if col not in ["Name", "Params (B)", "Average (lower=better)"]])

    styler = filtered_data.style.format(formatter).map(color_cell, subset=["Params (B)"])

    for column in target_color_columns:
        styler = styler.apply(
            custom_background_gradient,
            cmap=LinearSegmentedColormap.from_list("custom_cmap", colors),
            vmin=vmin[column],
            vmax=vmax[column],
            vmid=vmid[column],
            subset=[column],
        )

    # return styler
    styler = styler.hide(axis="index")

    widths = [300, 150, 150, 100, 100, 100, 100, 100, 100, 100, 100]
    table_styles = []

    for i, w in enumerate(widths):
        table_styles.append(
            {
                "selector": "th",
                "props": [
                    ("background-color", "#f5f5f5"),
                    ("padding", "8px"),
                    ("font-weight", "bold"),
                ],
            }
        )
        table_styles.append(
            {
                "selector": f"th.col{i}",
                "props": [
                    ("min-width", f"{w}px"),
                    ("max-width", f"{w}px"),
                    ("text-align", "center"),
                    ("border", "1px solid #dddddd"),
                ],
            }
        )
        table_styles.append(
            {
                "selector": f"td.col{i}",
                "props": [
                    ("min-width", f"{w}px"),
                    ("max-width", f"{w}px"),
                    ("text-align", "center"),
                    ("border", "1px solid #dddddd"),
                ],
            }
        )

    table_styles.append(
        {
            "selector": "table",
            "props": [
                ("border-collapse", "collapse"),
                ("border", "1px solid #dddddd"),
            ],
        }
    )

    styler = styler.set_table_styles(table_styles)

    html_output = styler.to_html()
    return html_output


def create_world_languages_gdp_chart():
    languages = ["English", "Chinese", "Spanish", "Japanese", "German", "French", "Arabic", "Italian", "Portuguese", "Korean", "Other"]
    shares = [27, 18, 8, 6, 5, 4, 3, 2, 2, 2, 23]
    colors = ["#FF7F7F", "#FFA07A", "#FFDB58", "#90EE90", "#98FB98", "#87CEFA", "#B0C4DE", "#DDA0DD", "#D8BFD8", "#F0E68C", "#E0FFFF"]

    fig = go.Figure(
        data=[
            go.Pie(
                labels=languages,
                values=shares,
                hole=0.3,
                marker=dict(colors=colors, line=dict(color="#FFFFFF", width=2)),
                textinfo="label+percent",
                textposition="outside",
                insidetextorientation="radial",
                textfont=dict(size=12),
            )
        ]
    )

    fig.update_layout(
        title={
            "text": "World Languages by Share of Global GDP",
            "y": 0.95,
            "x": 0.5,
            "xanchor": "center",
            "yanchor": "top",
            "font": dict(size=20, color="black"),
        },
        showlegend=False,
        width=700,
        height=500,
        margin=dict(t=80, b=20, l=20, r=20),
    )

    return fig


def check_model_exists(model_id):
    api = HfApi()
    try:
        model_info = api.model_info(model_id)
        return "Exists and is accessible"
    except GatedRepoError:
        return "Exists but is restricted"
    except HTTPError as e:
        if e.response.status_code == 404:
            return "Does not exist"
        else:
            return "Error: " + str(e)


def submit_model(name):
    if "Exists" not in check_model_exists(name):
        return f"# ERROR: Model {name} does not exist on HF中国镜像站!"

    try:
        response = requests.post(webhook_url, json={"content": name})
        if response.status_code == 200:
            response_data = response.json()
            if response_data.get("status") == "success":
                return "# SUCCESS: We will check the model as soon as possible. Thank you for your submission!"
            else:
                return f"# ERROR: {response_data.get('message', 'Unknown error')}"
        else:
            return f"# ERROR: Failed to submit model {name}. Server returned status code {response.status_code}."
    except requests.exceptions.HTTPError:
        return "# ERROR: Network error while contacting queue. Please try again in a few minutes."
    except Exception as e:
        print(e)
        return "ERROR: Unexpected error. Please try again later."


# def create_scaling_plot(all_data, period):
#     selected_columns = ["Name", "Parameters Count (B)", "Average (The lower the better)"]
#     target_data = all_data[period]
#     new_df = pd.DataFrame()

#     for size in target_data.keys():
#         new_df = pd.concat([new_df, target_data[size]["cr"].loc[:, selected_columns].dropna(axis=1, how="all")], axis=0)

#     new_df.rename(columns={"Parameters Count (B)": "Params(B)", "Average (The lower the better)": "Compression Rate (%)"}, inplace=True)

#     new_df["Log Params(B)"] = np.log(new_df["Params(B)"])
#     new_df["Log Compression Rate (%)"] = np.log(new_df["Compression Rate (%)"])

#     fig = px.scatter(
#         new_df,
#         x="Log Params(B)",
#         y="Log Compression Rate (%)",
#         title="Compression Rate Scaling Law",
#         hover_name="Name",
#         custom_data=["Params(B)", "Compression Rate (%)"],
#     )

#     fig.update_traces(
#         hovertemplate="<b>%{hovertext}</b><br>Params(B): %{customdata[0]:.2f} B<br>Compression Rate (%): %{customdata[1]:.2f}<extra></extra>"
#     )
#     fig.update_layout(
#         width=800,  # 设置图像宽度
#         height=600,  # 设置图像高度
#         title={"text": "Compression Rate Scaling Law", "x": 0.5, "xanchor": "center", "yanchor": "top"},
#         showlegend=True,
#         xaxis={"showgrid": True, "zeroline": False, "type": "linear", "title": "Params(B)"},  # 确保坐标轴类型正确
#         yaxis={"showgrid": True, "zeroline": False, "type": "linear", "title": "Compression Rate (%)", "autorange": "reversed"},
#     )

#     names_to_connect_dict = {
#         "2024-05": ["Meta-Llama-3-8B", "stablelm-3b-4e1t", "Qwen2-1.5B", "TinyLlama-1.1B-intermediate-step-1431k-3T", "Mistral-Nemo-Base-2407"],
#         "2024-06": ["Meta-Llama-3-8B", "stablelm-3b-4e1t", "Qwen2-1.5B", "TinyLlama-1.1B-intermediate-step-1431k-3T", "Mistral-Nemo-Base-2407"],
#         "2024-07": ["Meta-Llama-3.1-8B", "stablelm-3b-4e1t", "Qwen2-1.5B", "TinyLlama-1.1B-intermediate-step-1431k-3T", "Mistral-Nemo-Base-2407"],
#         "2024-08": [
#             "Meta-Llama-3.1-8B",
#             "Rene-v0.1-1.3b-pytorch",
#             "stablelm-3b-4e1t",
#             "Qwen2-1.5B",
#             "TinyLlama-1.1B-intermediate-step-1431k-3T",
#             "Mistral-Nemo-Base-2407",
#         ],
#         "2025-01": ["Qwen2.5-1.5B"],
#     }

#     names_to_connect = names_to_connect_dict.get(period, names_to_connect_dict["2024-08"])

#     connection_points = new_df[new_df["Name"].isin(names_to_connect)]
#     print(connection_points)

#     new_df["Color"] = new_df["Name"].apply(lambda name: "#39C5BB" if name in names_to_connect else "#636efa")

#     fig.update_traces(marker=dict(color=new_df["Color"]))

#     X = connection_points["Log Params(B)"].values.reshape(-1, 1)
#     y = connection_points["Log Compression Rate (%)"].values
#     model = LinearRegression().fit(X, y)

#     x_min = connection_points["Log Params(B)"].min()
#     x_max = connection_points["Log Params(B)"].max()
#     extended_x = np.linspace(x_min, x_max * 1.5, 100)
#     extended_x_original = np.exp(extended_x)
#     trend_line_y = model.predict(extended_x.reshape(-1, 1))
#     trend_line_y_original = np.exp(trend_line_y)

#     trend_line = go.Scatter(
#         x=extended_x,
#         y=trend_line_y,
#         mode="lines",
#         line=dict(color="skyblue", dash="dash"),
#         name="Trend Line",
#         hovertemplate="<b>Params(B):</b> %{customdata[0]:.2f}<br>" + "<b>Compression Rate (%):</b> %{customdata[1]:.2f}<extra></extra>",
#         customdata=np.stack((extended_x_original, trend_line_y_original), axis=-1),
#     )

#     fig.add_trace(trend_line)

#     x_min = new_df["Params(B)"].min()
#     x_max = new_df["Params(B)"].max()
#     x_tick_vals = np.geomspace(x_min, x_max, num=5)
#     x_tick_text = [f"{val:.1f}" for val in x_tick_vals]

#     y_min = new_df["Compression Rate (%)"].min()
#     y_max = new_df["Compression Rate (%)"].max()
#     y_tick_vals = np.geomspace(y_min, y_max, num=5)
#     y_tick_text = [f"{val:.1f}" for val in y_tick_vals]

#     fig.update_xaxes(tickvals=np.log(x_tick_vals), ticktext=x_tick_text, title="Params(B)")
#     fig.update_yaxes(tickvals=np.log(y_tick_vals), ticktext=y_tick_text, title="Compression Rate (%)", autorange="reversed")

#     fig.update_layout(xaxis=dict(showgrid=True, zeroline=False), yaxis=dict(showgrid=True, zeroline=False))

#     fig.update_traces(marker=dict(size=12))

#     print(fig.layout)

#     return fig


def create_scaling_plot(all_data, period):
    selected_columns = ["Name", "Parameters Count (B)", "Average (The lower the better)"]
    target_data = all_data[period]
    new_df = pd.DataFrame()

    for size in target_data.keys():
        new_df = pd.concat([new_df, target_data[size]["cr"].loc[:, selected_columns].dropna(axis=1, how="all")], axis=0)

    x_values = new_df["Parameters Count (B)"].astype(float).tolist()
    y_values = new_df["Average (The lower the better)"].astype(float).tolist()
    names = new_df["Name"].tolist()

    # 计算对数空间的范围
    x_min, x_max = np.log10(min(x_values)), np.log10(max(x_values))
    y_min, y_max = np.log10(min(y_values)), np.log10(max(y_values))

    # 计算合适的刻度间隔
    x_dtick = (x_max - x_min) / 4  # 分成5个刻度
    y_dtick = (y_max - y_min) / 4

    fig = go.Figure()

    fig.add_trace(
        go.Scatter(
            x=x_values,
            y=y_values,
            mode="markers",
            name="Models",
            marker=dict(size=12, color="#39C5BB", opacity=0.8),
            text=names,
            customdata=list(zip(x_values, y_values)),
            hovertemplate=(
                "<b>%{text}</b><br>" + "Params: %{customdata[0]:.2f}B<br>" + "Compression Rate: %{customdata[1]:.2f}%<br>" + "<extra></extra>"
            ),
        )
    )

    fig.update_layout(
        title={"text": "Compression Rate Scaling Law", "x": 0.5, "xanchor": "center", "yanchor": "top"},
        width=800,
        height=600,
        showlegend=True,
        xaxis=dict(
            title="Parameters (B)",
            showgrid=True,
            zeroline=False,
            type="log",
            dtick=x_dtick,
            tickformat=".2f",  # 保留两位小数
            range=[x_min - 0.1, x_max + 0.1],
        ),
        yaxis=dict(
            title="Compression Rate (%)",
            showgrid=True,
            zeroline=False,
            type="log",
            dtick=y_dtick,
            tickformat=".2f",  # 保留两位小数
            range=[y_min - 0.1, y_max + 0.1],
            autorange="reversed",
        ),
    )

    return fig


def read_all_data(folder_name):
    all_data = {}
    time_list = []
    for folder in get_folders_matching_format(folder_name):
        folder_name = os.path.basename(folder)
        time_list.append(folder_name)
        if all_data.get(folder) is None:
            all_data[folder_name] = {}
        for file_name in file_name_list:
            if all_data.get(file_name) is None:
                all_data[folder_name][file_name] = {}
            for sheet_name in sheet_name_list:
                final_file_name = os.path.join(folder, file_name)
                all_data[folder_name][file_name][sheet_name] = rename_columns(pd.read_excel(final_file_name + ".xlsx", sheet_name=sheet_name))

    return all_data, time_list


# def read_mutilange_data(folder_path='mutilang_data'):
#     mutilange_data = {}
#     excel_files = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith('.xlsx')]
#     time_list = [file.split('.')[0] for file in excel_files]
#     time_list = [x.split('\\')[-1] for x in time_list]
#     for file_name in excel_files:
#         if mutilange_data.get(file_name) is None:
#             mutilange_data[file_name] = {}
#         for sheet_name in sheet_name_list:
#             mutilange_data[file_name][sheet_name] = rename_columns(
#                 pd.read_excel(file_name, sheet_name=sheet_name))
#     return mutilange_data, time_list


all_data, time_list = read_all_data("data")
# muti_lang_data, muti_lang_time_list = read_mutilange_data()

time_list.sort()
last_period = time_list[-1]

initial_fig = create_scaling_plot(all_data, last_period)
initial_metric = metric_list[0]
initial_columns = get_unique_column_names(all_data)
# initial_columns = initial_columns[:-1]
initial_colors = ["Average", "Individual Tests"]
initial_size_range = [0, 15]
initial_data = update_table(last_period, model_size_list, initial_metric, initial_columns, initial_colors, initial_size_range)

css = """

.gradio-container {

    max-width: 95% !important;

    margin: 0 auto;

}

.tab-buttons button {

    font-size: 1.3em;

}

.gr-dataframe th {

    white-space: normal;

    word-break: break-word;

}

table {

    margin-left: auto !important;

    margin-right: auto !important;

    width: 100% !important;

}

"""

TITLE_HTML = '<h1 style="text-align:center"><span style="font-size:1.3em">🏆 LLM Compression Leaderboard</span></h1>'
SUBTITLE_HTML = "<h1 style='text-align:center'><span style='font-size:0.8em'>Welcome to Uncheatable Eval LLM Compression Leaderboard, where fancy fine-tuning and cheating won’t work 🚫; only compute 💻, data 📊, and real innovation 🔥 can prevail!</span></h1>"

with gr.Blocks(css=css) as demo:
    gr.HTML(TITLE_HTML)
    gr.HTML(SUBTITLE_HTML)
    with gr.Tabs() as tabs:
        with gr.Tab("🏆 Leaderboard"):
            with gr.Row():
                with gr.Column():
                    period_selector = gr.Dropdown(label="Period", choices=time_list, value=last_period)
                    model_selector = gr.CheckboxGroup(label="Model Size", choices=model_size_list, value=model_size_list)
                    size_range_slider = RangeSlider(minimum=0, maximum=15, value=[0, 15], step=0.1, label="Model Size Range")
                    metric_selector = gr.Dropdown(label="Metric", choices=metric_list, value=initial_metric)
                with gr.Column():
                    midpoint_slider = gr.Slider(minimum=0.1, maximum=0.9, value=0.5, step=0.01, label="Color Gradient Midpoint")
                    color_selector = gr.CheckboxGroup(label="Colored Columns", choices=["Average", "Individual Tests"], value=initial_colors)
                    colfilter = gr.CheckboxGroup(label="Data Source", choices=get_unique_column_names(all_data), value=initial_columns)

            # table = gr.Dataframe(
            #     initial_data,
            #     column_widths=[130, 50, 50, 35, 35, 35, 35, 35, 35, 35, 35],
            #     wrap=True,
            #     max_height=800,
            # )
            table = gr.HTML(initial_data)

            period_selector.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            model_selector.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            metric_selector.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            colfilter.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            color_selector.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            size_range_slider.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )
            midpoint_slider.change(
                update_table,
                inputs=[period_selector, model_selector, metric_selector, colfilter, color_selector, size_range_slider, midpoint_slider],
                outputs=table,
            )

        with gr.Tab("🌍 MultiLang"):
            gr.Markdown("## Coming soon...")
            world_languages_plot = gr.Plot(create_world_languages_gdp_chart())

        with gr.Tab("📈 Scaling Law"):
            period_selector_2 = gr.Dropdown(label="Period", choices=time_list, value=last_period)

            def update_plot(period):
                new_fig = create_scaling_plot(all_data, period)
                return new_fig

            plot = gr.Plot(initial_fig)
            period_selector_2.change(update_plot, inputs=period_selector_2, outputs=plot)

        with gr.Tab("ℹ️ About"):
            gr.Markdown(about_md)

        with gr.Tab("🚀 Submit"):
            with gr.Group():
                with gr.Row():
                    model_name = gr.Textbox(max_lines=1, placeholder="Enter model name...", show_label=False, scale=4)
                    submit = gr.Button("Submit", variant="primary", scale=0)
            output = gr.Markdown("# Enter a public HF repo id, then hit Submit to add it to the evaluation queue.")
            submit.click(fn=submit_model, inputs=model_name, outputs=output)

demo.launch(share=False)