Update app.py
Browse files
app.py
CHANGED
@@ -1,207 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
import pandas as pd
|
4 |
-
from urllib.request import urlopen
|
5 |
-
from urllib.error import URLError
|
6 |
-
import re
|
7 |
-
from datetime import datetime
|
8 |
-
|
9 |
-
# Constants
|
10 |
-
CITATION_BUTTON_TEXT = r"""@misc{2023opencompass,
|
11 |
-
title={OpenCompass: A Universal Evaluation Platform for Foundation Models},
|
12 |
-
author={OpenCompass Contributors},
|
13 |
-
howpublished = {\url{https://github.com/open-compass/opencompass}},
|
14 |
-
year={2023}
|
15 |
-
}"""
|
16 |
-
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
17 |
-
# 开发环境
|
18 |
-
# DATA_URL_BASE = "http://opencompass.oss-cn-shanghai.aliyuncs.com/dev-assets/research-rank/research-data.REALTIME."
|
19 |
-
# DATA_URL_BASE = "./s1test"
|
20 |
-
# 生产环境
|
21 |
-
DATA_URL_BASE = "http://opencompass.oss-cn-shanghai.aliyuncs.com/assets/research-rank/research-data.REALTIME."
|
22 |
-
|
23 |
-
|
24 |
-
def find_latest_data_url():
|
25 |
-
"""Find the latest available data URL by trying different dates."""
|
26 |
-
today = datetime.now()
|
27 |
-
for i in range(365):
|
28 |
-
date = today.replace(day=today.day - i)
|
29 |
-
date_str = date.strftime("%Y%m%d")
|
30 |
-
url = f"{DATA_URL_BASE}{date_str}.json"
|
31 |
-
try:
|
32 |
-
urlopen(url)
|
33 |
-
return url, date_str
|
34 |
-
except URLError:
|
35 |
-
continue
|
36 |
-
breakpoint()
|
37 |
-
return None, None
|
38 |
-
|
39 |
-
|
40 |
-
def get_latest_data():
|
41 |
-
"""Get latest data URL and update time"""
|
42 |
-
data_url, update_time = find_latest_data_url()
|
43 |
-
if not data_url:
|
44 |
-
raise Exception("Could not find valid data URL")
|
45 |
-
formatted_update_time = datetime.strptime(update_time, "%Y%m%d").strftime("%Y-%m-%d")
|
46 |
-
return data_url, formatted_update_time
|
47 |
-
|
48 |
-
|
49 |
-
def get_leaderboard_title(update_time):
|
50 |
-
return f"# Supported Datasets List (Last Updated: {update_time})"
|
51 |
-
|
52 |
-
|
53 |
-
MAIN_DESCRIPTION = """## The List of Datasets Supported by OpenCompass
|
54 |
-
Testing line.
|
55 |
-
- All configurations and datsets can be found in [**OpenCompass**: A Toolkit for Evaluation of LLMs](https://github.com/open-compass/opencompass)🏆.
|
56 |
-
"""
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
def load_data(data_url):
|
62 |
-
response = urlopen(data_url)
|
63 |
-
|
64 |
-
with open('s1.json','r',encoding='utf8') as f:
|
65 |
-
data = json.load(f)
|
66 |
-
|
67 |
-
return data
|
68 |
-
|
69 |
-
|
70 |
-
def build_main_table(data):
|
71 |
-
df = pd.DataFrame(data).transpose()
|
72 |
-
columns = {
|
73 |
-
'name': 'Name', 'category': 'Category', 'article': 'Article Address',
|
74 |
-
}
|
75 |
-
df = df[list(columns.keys())].rename(columns=columns)
|
76 |
-
return df
|
77 |
-
|
78 |
-
|
79 |
-
DATA_CATEGORY = ['med', 'law', 'code']
|
80 |
-
|
81 |
-
def filter_table1(df, data_category):
|
82 |
-
filtered_df = df.copy()
|
83 |
-
if data_category:
|
84 |
-
mask = pd.Series(False, index=filtered_df.index)
|
85 |
-
for category in data_category:
|
86 |
-
mask |= filtered_df['Category'] == category
|
87 |
-
filtered_df = filtered_df[mask]
|
88 |
-
|
89 |
-
return filtered_df
|
90 |
-
|
91 |
-
|
92 |
-
def calculate_column_widths(df):
|
93 |
-
column_widths = []
|
94 |
-
for column in df.columns:
|
95 |
-
header_length = len(str(column))
|
96 |
-
max_content_length = df[column].astype(str).map(len).max()
|
97 |
-
width = max(header_length * 10, max_content_length * 8) + 20
|
98 |
-
width = max(160, min(400, width))
|
99 |
-
column_widths.append(width)
|
100 |
-
return column_widths
|
101 |
-
|
102 |
-
|
103 |
-
class DataState:
|
104 |
-
def __init__(self):
|
105 |
-
self.current_df = None
|
106 |
-
|
107 |
-
|
108 |
-
data_state = DataState()
|
109 |
-
|
110 |
-
|
111 |
-
def create_interface():
|
112 |
-
empty_df = pd.DataFrame(columns=[
|
113 |
-
'Name', 'Category', 'Article Address'
|
114 |
-
])
|
115 |
-
|
116 |
-
def load_initial_data():
|
117 |
-
try:
|
118 |
-
data_url, update_time = get_latest_data()
|
119 |
-
data = load_data(data_url)
|
120 |
-
new_df = build_main_table(data)
|
121 |
-
data_state.current_df = new_df
|
122 |
-
filtered_df = filter_table1(new_df, DATA_CATEGORY)
|
123 |
-
return get_leaderboard_title(update_time), filtered_df.sort_values("Name", ascending=True)
|
124 |
-
except Exception as e:
|
125 |
-
print(f"Error loading initial data: {e}")
|
126 |
-
return "# Supported Datasets List (Error loading data)", empty_df
|
127 |
-
|
128 |
-
def refresh_data():
|
129 |
-
try:
|
130 |
-
data_url, update_time = get_latest_data()
|
131 |
-
data = load_data(data_url)
|
132 |
-
new_df = build_main_table(data)
|
133 |
-
data_state.current_df = new_df
|
134 |
-
filtered_df = filter_table1(new_df, DATA_CATEGORY)
|
135 |
-
return get_leaderboard_title(update_time), filtered_df.sort_values("Name", ascending=True)
|
136 |
-
except Exception as e:
|
137 |
-
print(f"Error refreshing data: {e}")
|
138 |
-
return None, None
|
139 |
-
|
140 |
-
def update_table(category):
|
141 |
-
if data_state.current_df is None:
|
142 |
-
return empty_df
|
143 |
-
filtered_df = filter_table1(data_state.current_df, category)
|
144 |
-
return filtered_df.sort_values("Name", ascending=True)
|
145 |
-
|
146 |
-
initial_title, initial_data = load_initial_data()
|
147 |
-
|
148 |
-
with gr.Blocks() as demo:
|
149 |
-
title_comp = gr.Markdown(initial_title)
|
150 |
-
|
151 |
-
with gr.Tabs() as tabs:
|
152 |
-
with gr.TabItem("Dataset List", elem_id='main'):
|
153 |
-
gr.Markdown(MAIN_DESCRIPTION)
|
154 |
-
|
155 |
-
with gr.Row():
|
156 |
-
with gr.Column():
|
157 |
-
category_filter = gr.CheckboxGroup(
|
158 |
-
choices=DATA_CATEGORY,
|
159 |
-
value=DATA_CATEGORY,
|
160 |
-
label='Category',
|
161 |
-
interactive=True,
|
162 |
-
)
|
163 |
-
|
164 |
-
with gr.Column():
|
165 |
-
table = gr.DataFrame(
|
166 |
-
value=initial_data,
|
167 |
-
interactive=False,
|
168 |
-
wrap=False,
|
169 |
-
column_widths=calculate_column_widths(initial_data),
|
170 |
-
)
|
171 |
-
|
172 |
-
refresh_button = gr.Button("Refresh Data")
|
173 |
-
|
174 |
-
def refresh_and_update():
|
175 |
-
title, data = refresh_data()
|
176 |
-
return title, data
|
177 |
-
|
178 |
-
refresh_button.click(
|
179 |
-
fn=refresh_and_update,
|
180 |
-
outputs=[title_comp, table],
|
181 |
-
)
|
182 |
-
|
183 |
-
category_filter.change(
|
184 |
-
fn=update_table,
|
185 |
-
inputs=[category_filter],
|
186 |
-
outputs=table,
|
187 |
-
)
|
188 |
-
|
189 |
-
|
190 |
-
with gr.Row():
|
191 |
-
with gr.Accordion("Citation", open=False):
|
192 |
-
citation_button = gr.Textbox(
|
193 |
-
value=CITATION_BUTTON_TEXT,
|
194 |
-
label=CITATION_BUTTON_LABEL,
|
195 |
-
elem_id='citation-button',
|
196 |
-
lines=6, # 增加行数
|
197 |
-
max_lines=8, # 设置最大行数
|
198 |
-
show_copy_button=True # 添加复制按钮使其更方便使用
|
199 |
-
)
|
200 |
-
|
201 |
-
return demo
|
202 |
-
|
203 |
-
|
204 |
-
if __name__ == '__main__':
|
205 |
-
demo = create_interface()
|
206 |
-
demo.queue()
|
207 |
-
demo.launch(server_name='0.0.0.0')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|