JarvisKi commited on
Commit
e7fd246
1 Parent(s): 7d88617

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +246 -0
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from huggingface_hub import CommitScheduler
4
+ from pathlib import Path
5
+ import requests
6
+ from huggingface_hub import HfApi, HfFolder
7
+ import os
8
+
9
+ # Use the token from an environment variable
10
+ token = os.getenv('stb_leaderboard_json')
11
+ HfFolder.save_token(token)
12
+
13
+ def load_data():
14
+ url = "https://huggingface.co/datasets/stabletoolbench/StableToolBench_data/resolve/main/leaderboard_data.json"
15
+ response = requests.get(url)
16
+ data = response.json()
17
+ return data
18
+
19
+ # load data from the dataset
20
+ existing_data = load_data()
21
+
22
+ # Create local folders for temporary storage of modified data files
23
+ dataset_dir = Path("my_dataset")
24
+ dataset_dir.mkdir(parents=True, exist_ok=True)
25
+
26
+ # initialize CommitScheduler
27
+ scheduler = CommitScheduler(
28
+ repo_id="stabletoolbench/StableToolBench_data", # 替换为你的用户名和数据集仓库名
29
+ repo_type="dataset",
30
+ folder_path=dataset_dir,
31
+ path_in_repo="",
32
+ )
33
+
34
+ # Generate table data based on Average Score ranking by default
35
+ def generate_table(data):
36
+ if not data:
37
+ return [], [] # No data, return empty headers and rows
38
+
39
+ # Make sure all entries have a 'Scores' dictionary
40
+ valid_data = [entry for entry in data if 'Scores' in entry and isinstance(entry['Scores'], dict)]
41
+
42
+ # Sort the data based on the 'Average' score in descending order
43
+ sorted_data = sorted(valid_data, key=lambda x: x['Scores'].get('Average', 0), reverse=True)
44
+
45
+ # Now, build the table rows with the sorted data
46
+ headers = ["Method"] + list(sorted_data[0]['Scores'].keys()) if sorted_data else ["Method"]
47
+
48
+ rows = []
49
+ for entry in sorted_data:
50
+ row = [entry['Method']] + [entry['Scores'].get(key, "N/A") for key in headers[1:]]
51
+ rows.append(row)
52
+
53
+ return headers, rows
54
+
55
+ # Raw data for the paper
56
+ protected_methods = ["GPT-4-Turbo-Preview (DFS)", "GPT-3.5-Turbo-1106 (DFS)", "GPT-4-0613 (DFS)", "GPT-3.5-Turbo-0613 (DFS)", "GPT-4-Turbo-Preview (CoT)", "ToolLLaMA v2 (DFS)", "GPT-4-0613 (CoT)", "GPT-3.5-Turbo-1106 (CoT)", "GPT-3.5-Turbo-0613 (CoT)", "ToolLLaMA v2 (CoT)"]
57
+
58
+ # Merge uploaded data into existing data
59
+ def merge_data(uploaded_data_json):
60
+ # No need to call json.loads here because uploaded_data is already a Python dict
61
+ new_data = uploaded_data_json
62
+
63
+ with scheduler.lock:
64
+
65
+ # Define a helper function to merge scores for an entry
66
+ def merge_scores(existing_scores, new_scores):
67
+ for key, value in new_scores.items():
68
+ existing_scores[key] = value
69
+
70
+ # Merge 'SolvablePassRateScores'
71
+ for new_entry in new_data["SolvablePassRateScores"]:
72
+ if new_entry["Method"] not in protected_methods:
73
+ existing_entry = next(
74
+ (item for item in existing_data["SolvablePassRateScores"] if item["Method"] == new_entry["Method"]),
75
+ None)
76
+ if existing_entry:
77
+ # It's a non-protected method, update it
78
+ merge_scores(existing_entry["Scores"], new_entry["Scores"])
79
+ else:
80
+ # It's a new method to add to the list
81
+ existing_data["SolvablePassRateScores"].append(new_entry)
82
+
83
+ # Merge 'SolvableWinRateScores'
84
+ for new_entry in new_data["SolvableWinRateScores"]:
85
+ if new_entry["Method"] not in protected_methods:
86
+ existing_entry = next(
87
+ (item for item in existing_data["SolvableWinRateScores"] if item["Method"] == new_entry["Method"]),
88
+ None)
89
+ if existing_entry:
90
+ merge_scores(existing_entry["Scores"], new_entry["Scores"])
91
+ else:
92
+ existing_data["SolvableWinRateScores"].append(new_entry)
93
+
94
+ data_file_path = dataset_dir / "leaderboard_data.json"
95
+ with open(data_file_path, 'w') as file:
96
+ json.dump(existing_data, file, indent=4)
97
+
98
+ return existing_data
99
+
100
+ def process_file(file_info):
101
+ if file_info is not None:
102
+ with open(file_info, "r") as uploaded_file:
103
+ data_content = uploaded_file.read()
104
+ uploaded_data_json = json.loads(data_content)
105
+ # Merge the uploaded data
106
+ merge_data(uploaded_data_json)
107
+
108
+ pass_rate_table, win_rate_table = refresh_table_data()
109
+ return pass_rate_table, win_rate_table
110
+
111
+ def refresh_table_data():
112
+ # 重新加载数据
113
+ new_data = load_data()
114
+ # 重新生成表格数据
115
+ new_pass_rate_data = generate_table(new_data["SolvablePassRateScores"])[1]
116
+ new_win_rate_data = generate_table(new_data["SolvableWinRateScores"])[1]
117
+ # 返回新的表格数据
118
+ return new_pass_rate_data, new_win_rate_data
119
+
120
+ with gr.Blocks() as app:
121
+
122
+ # The large title
123
+ gr.Markdown("# StableToolBench Leaderboard")
124
+
125
+ # The introductory content
126
+ gr.Markdown("""
127
+ **Large Language Models (LLMs)** have witnessed remarkable advancements in recent years, prompting the exploration of tool learning, which integrates LLMs with external tools to address diverse real-world challenges. Assessing the capability of LLMs to utilise tools necessitates large-scale and stable benchmarks. However, previous works relied on either hand-crafted online tools with limited scale, or large-scale real online APIs suffering from instability of API status. To address this problem, we introduce StableToolBench, a benchmark evolving from ToolBench, proposing a virtual API server and stable evaluation system. The virtual API server contains a caching system and API simulators which are complementary to alleviate the change in API status. Meanwhile, the stable evaluation system designs solvable pass and win rates using GPT-4 as the automatic evaluator to eliminate the randomness during evaluation. Experimental results demonstrate the stability of StableToolBench, and further discuss the effectiveness of API simulators, the caching system, and the evaluation system.
128
+ """)
129
+ gr.Markdown(""" ### For further information, please refer to: """)
130
+ buttons_html = """
131
+ <style>
132
+ .custom-link-button {
133
+ font-size: 18px !important; /* Adjust the font size as needed */
134
+ padding: 10px 15px !important; /* Add some padding */
135
+ margin: 5px !important;
136
+ color: white !important; /* Text color */
137
+ background-color: #106BA3 !important; /* Background color */
138
+ text-decoration: none !important; /* Remove underline from links */
139
+ display: inline-block !important;
140
+ border-radius: 5px !important; /* Rounded corners */
141
+ border: none !important; /* Remove borders */
142
+ cursor: pointer !important; /* Mouse pointer on hover */
143
+ text-align: center !important;
144
+ }
145
+ .custom-link-button:hover {
146
+ background-color: #0D5B8F !important;
147
+ }
148
+ </style>
149
+ <a href="https://arxiv.org/pdf/2403.07714.pdf" target="_blank" class="custom-link-button">Paper</a>
150
+ <a href="https://arxiv.org/abs/2403.07714" target="_blank" class="custom-link-button">arXiv</a>
151
+ <a href="https://github.com/zhichengg/StableToolBench" target="_blank" class="custom-link-button">Code</a>
152
+ <a href="https://drive.google.com/file/d/1XUiCMA5NV359UGR-eknF0TcXORuR7RXj/view?pli=1" target="_blank" class="custom-link-button">Cache Data</a>
153
+ """
154
+
155
+ gr.HTML(buttons_html)
156
+
157
+ gr.Markdown("## Solvable Pass Rate Scores")
158
+ headers1, rows1 = generate_table(existing_data["SolvablePassRateScores"])
159
+ table1 = gr.Dataframe(headers=headers1, value=rows1, interactive=False)
160
+
161
+ gr.Markdown("## Solvable Win Rate Scores")
162
+ headers2, rows2 = generate_table(existing_data["SolvableWinRateScores"])
163
+ table2 = gr.Dataframe(headers=headers2, value=rows2, interactive=False)
164
+
165
+ refresh_button = gr.Button("Refresh Leaderboards")
166
+ refresh_button.click(
167
+ fn=refresh_table_data,
168
+ outputs=[table1, table2]
169
+ )
170
+
171
+ gr.Markdown("## Upload Your Own Results")
172
+ gr.Markdown("""
173
+ If you would like to contribute to the leaderboard, please follow the JSON structure below for your method's scores.
174
+
175
+ **Solvable Pass Rate Scores Template:**
176
+ ```json
177
+ {
178
+ "SolvablePassRateScores": [
179
+ {
180
+ "Method": "Your Method Name",
181
+ "Scores": {
182
+ "I1 Instruction": 85.5,
183
+ "I1 Instruction SE": 1.2,
184
+ "I1 Category": 80.0,
185
+ "I1 Category SE": 1.0,
186
+ "I1 Tool": 88.5,
187
+ "I1 Tool SE": 0.8,
188
+ "I2 Category": 82.5,
189
+ "I2 Category SE": 1.3,
190
+ "I2 Instruction": 86.0,
191
+ "I2 Instruction SE": 0.5,
192
+ "I3 Instruction": 90.0,
193
+ "I3 Instruction SE": 0.7,
194
+ "Average": 87.5,
195
+ "Average SE": 1.1
196
+ }
197
+ }
198
+ // Add more methods here...
199
+ ],
200
+ "SolvableWinRateScores": [
201
+ {
202
+ "Method": "Your Method Name",
203
+ "Scores": {
204
+ "I1 Instruction": 65.0,
205
+ "I1 Category": 68.5,
206
+ "I1 Tool": 66.8,
207
+ "I2 Category": 70.0,
208
+ "I2 Instruction": 69.2,
209
+ "I3 Instruction": 71.5,
210
+ "Average": 68.5
211
+ }
212
+ }
213
+ // Add more methods here...
214
+ ]
215
+ }
216
+ ```
217
+
218
+ Make sure your uploaded JSON file follows this structure.
219
+ """)
220
+
221
+ upload_component = gr.File(label="Upload JSON File")
222
+ submit_button = gr.Button("Submit")
223
+ submit_button.click(
224
+ fn=process_file,
225
+ inputs= upload_component,
226
+ outputs=[table1, table2]
227
+ )
228
+
229
+ gr.Markdown(" ## If you like our project, please consider cite our work as follows: ")
230
+ citation_text = """
231
+ ```
232
+ @misc{guo2024stabletoolbench,
233
+ title={StableToolBench: Towards Stable Large-Scale Benchmarking on Tool Learning of Large Language Models},
234
+ author={Zhicheng Guo and Sijie Cheng and Hao Wang and Shihao Liang and Yujia Qin and Peng Li and Zhiyuan Liu and Maosong Sun and Yang Liu},
235
+ year={2024},
236
+ eprint={2403.07714},
237
+ archivePrefix={arXiv},
238
+ primaryClass={cs.CL}
239
+ }
240
+ ```
241
+ """
242
+ gr.Markdown(citation_text)
243
+
244
+ if __name__ == "__main__":
245
+ app.launch()
246
+ scheduler.commit() # Ensure all changes are committed on exit