Mengyuan Liu commited on
Commit
50ed5c4
·
verified ·
1 Parent(s): dfe37be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +396 -397
app.py CHANGED
@@ -1,398 +1,397 @@
1
- import gradio as gr
2
- import pandas as pd
3
- from slider import create_subset_ratios_tab
4
- from change_output import change_file
5
- import requests
6
- import os
7
- import shutil
8
- import json
9
- import pandas as pd
10
- import subprocess
11
- import plotly.express as px
12
- def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_counts_radio, cyclomatic_complexity_radio, problem_type_radio):
13
- # 根据用户选择的参数构建文件路径
14
- num_parts = num_parts_dropdown
15
- token_counts_split = token_counts_radio
16
- line_counts_split = line_counts_radio
17
- cyclomatic_complexity_split = cyclomatic_complexity_radio
18
-
19
-
20
- # 读取数据
21
- dataframes = []
22
- if token_counts_split=="Equal Frequency Partitioning":
23
- token_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/token_counts_QS.csv")
24
- dataframes.append(token_counts_df)
25
-
26
- if line_counts_split=="Equal Frequency Partitioning":
27
- line_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/line_counts_QS.csv")
28
- dataframes.append(line_counts_df)
29
-
30
- if cyclomatic_complexity_split=="Equal Frequency Partitioning":
31
- cyclomatic_complexity_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/CC_QS.csv")
32
- dataframes.append(cyclomatic_complexity_df)
33
- #以下改为直接从一个划分文件中读取即可
34
- # if problem_type_radio:
35
- # problem_type_df = pd.read_csv(f"{num_parts}/problem_type_{problem_type_split}.csv")
36
- # dataframes.append(problem_type_df)
37
-
38
- # 如果所有三个radio都有value,将三个文件中的所有行拼接
39
- if len(dataframes) > 0:
40
- combined_df = dataframes[0]
41
- for df in dataframes[1:]:
42
- combined_df = pd.merge(combined_df, df, left_index=True, right_index=True, suffixes=('', '_y'))
43
- combined_df = combined_df.loc[:, ~combined_df.columns.str.endswith('_y')] # 去除重复的列
44
- return combined_df
45
- else:
46
- return pd.DataFrame()
47
-
48
-
49
-
50
- # 定义一个函数来返回数据
51
- # def show_data(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low):
52
- # columns = ["Model"]
53
- #
54
- # if token_counts:
55
- # if show_high:
56
- # columns.append("Token Counts.I")
57
- #
58
- # if show_medium:
59
- # columns.append("Token Counts.II")
60
- # if show_low:
61
- # columns.append("Token Counts.III")
62
- # if line_counts:
63
- # if show_high:
64
- # columns.append("Line Counts.I")
65
- # if show_medium:
66
- # columns.append("Line Counts.II")
67
- # if show_low:
68
- # columns.append("Line Counts.III")
69
- # if cyclomatic_complexity:
70
- # if show_high:
71
- # columns.append("Cyclomatic Complexity.I")
72
- # if show_medium:
73
- # columns.append("Cyclomatic Complexity.II")
74
- # if show_low:
75
- # columns.append("Cyclomatic Complexity.III")
76
- # if problem_type:
77
- # columns.extend(["Problem Type_String", "Problem Type_Math", "Problem Type_Array"])
78
- # return data[columns]
79
- #用于更新数据文件的部分
80
- def execute_specified_python_files(directory_list, file_list):
81
- for directory in directory_list:
82
- for py_file in file_list:
83
- file_path = os.path.join(directory, py_file)
84
- if os.path.isfile(file_path) and py_file.endswith('.py'):
85
- print(f"Executing {file_path}...")
86
- try:
87
- # 使用subprocess执行Python文件
88
- subprocess.run(['python', file_path], check=True)
89
- print(f"{file_path} executed successfully.")
90
- except subprocess.CalledProcessError as e:
91
- print(f"Error executing {file_path}: {e}")
92
- else:
93
- print(f"File {file_path} does not exist or is not a Python file.")
94
- # 定义一个函数来生成 CSS 样式
95
- def generate_css(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low):
96
- css = """
97
- #dataframe th {
98
- background-color: #f2f2f2
99
-
100
- }
101
- """
102
- colors = ["#e6f7ff", "#ffeecc", "#e6ffe6", "#ffe6e6"]
103
- categories = [line_counts, token_counts, cyclomatic_complexity]
104
- category_index = 0
105
- column_index = 1
106
-
107
- for category in categories:
108
- if category:
109
- if show_high:
110
- css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
111
- column_index += 1
112
- if show_medium:
113
- css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
114
- column_index += 1
115
- if show_low:
116
- css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
117
- column_index += 1
118
- category_index += 1
119
-
120
- # 为 Problem Type 相关的三个子列设置固定颜色
121
- if problem_type:
122
- problem_type_color = "#d4f0fc" # 你可以选择任何你喜欢的颜色
123
- css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {problem_type_color}; }}\n"
124
- css += f"#dataframe td:nth-child({column_index + 2}) {{ background-color: {problem_type_color}; }}\n"
125
- css += f"#dataframe td:nth-child({column_index + 3}) {{ background-color: {problem_type_color}; }}\n"
126
-
127
- # 隐藏 "data" 标识
128
- css += """
129
- .gradio-container .dataframe-container::before {
130
- content: none !important;
131
- }
132
- """
133
-
134
- return css
135
- # def update_dataframe(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium,
136
- # show_low):
137
- # df = show_data(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low)
138
- # css = generate_css(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium,
139
- # show_low)
140
- # return gr.update(value=df), gr.update(value=f"<style>{css}</style>")
141
-
142
-
143
- def generate_file(file_obj, user_string, user_number,dataset_choice):
144
- tmpdir = 'tmpdir'
145
-
146
- print('临时文件夹地址:{}'.format(tmpdir))
147
- FilePath = file_obj.name
148
- print('上传文件的地址:{}'.format(file_obj.name)) # 输出上传后的文件在gradio中保存的绝对地址
149
-
150
- # 将文件复制到临时目录中
151
- shutil.copy(file_obj.name, tmpdir)
152
-
153
- # 获取上传Gradio的文件名称
154
- FileName = os.path.basename(file_obj.name)
155
-
156
- print(FilePath)
157
- # 获取拷贝在临时目录的新的文件地址
158
-
159
- # 打开复制到新路径后的文件
160
- with open(FilePath, 'r', encoding="utf-8") as file_obj:
161
- # 在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
162
- outputPath = os.path.join('F:/Desktop/test', FileName)
163
- data = json.load(file_obj)
164
- print("data:", data)
165
-
166
- # 将数据写入新的 JSON 文件
167
- with open(outputPath, 'w', encoding="utf-8") as w:
168
- json.dump(data, w, ensure_ascii=False, indent=4)
169
-
170
- # 读取文件内容并上传到服务器
171
- file_content = json.dumps(data) # 将数据转换为 JSON 字符串
172
- url = "http://localhost:6222/submit" # 替换为你的后端服务器地址
173
- files = {'file': (FileName, file_content, 'application/json')}
174
- payload = {
175
- 'user_string': user_string,
176
- 'user_number': user_number,
177
- 'dataset_choice':dataset_choice
178
- }
179
-
180
- response = requests.post(url, files=files, data=payload)
181
- print(response)
182
- #返回服务器处理后的文件
183
- if response.status_code == 200:
184
- # 获取服务器返回的 JSON 数据
185
- output_data = response.json()
186
-
187
- # 保存 JSON 数据到本地
188
- output_file_path = os.path.join('E:/python-testn/pythonProject3/hh_1/evaluate_result', 'new-model.json')
189
- with open(output_file_path, 'w', encoding="utf-8") as f:
190
- json.dump(output_data, f, ensure_ascii=False, indent=4)
191
-
192
- print(f"File saved at: {output_file_path}")
193
-
194
- # 调用更新数据文件的函数
195
- directory_list = ['/path/to/directory1', '/path/to/directory2'] # 替换为你的目录路径列表
196
- file_list = ['file1.py', 'file2.py', 'file3.py'] # 替换为你想要执行的Python文件列表
197
-
198
- execute_specified_python_files(directory_list, file_list)
199
-
200
- return {"status": "success", "message": "File received and saved"}
201
- else:
202
- return {"status": "error", "message": response.text}
203
-
204
- # 返回服务器响应
205
- return {"status": "success", "message": response.text}
206
-
207
- def update_radio_options(token_counts, line_counts, cyclomatic_complexity, problem_type):
208
- options = []
209
- if token_counts:
210
- options.append("Token Counts in Prompt")
211
- if line_counts:
212
- options.append("Line Counts in Prompt")
213
- if cyclomatic_complexity:
214
- options.append("Cyclomatic Complexity")
215
- if problem_type:
216
- options.append("Problem Type")
217
-
218
- return gr.update(choices=options)
219
-
220
- def plot_csv(radio,num):
221
- # 读取本地的CSV文件
222
- #token_counts_df = pd.read_csv(f"{num_parts}/QS/token_counts_QS.csv")
223
- if radio=="Line Counts in Prompt":
224
- radio_choice="line_counts"
225
- file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
226
- elif radio=="Token Counts in Prompt":
227
- radio_choice="token_counts"
228
- file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
229
- elif radio=="Cyclomatic Complexity":
230
- radio_choice="CC"
231
- file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
232
- elif radio=="Problem Type":
233
- radio_choice="problem_type"
234
- file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/cata_result.csv'
235
- print("test!")
236
-
237
- # file_path="E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/3/QS/CC_QS.csv"
238
- df = pd.read_csv(file_path)
239
- # 将第一列作为索引
240
- df.set_index('Model', inplace=True)
241
-
242
- # 转置数据框,使得模型作为列,横轴作为行
243
- df_transposed = df.T
244
-
245
- # 使用plotly绘制折线图
246
- fig = px.line(df_transposed, x=df_transposed.index, y=df_transposed.columns,
247
- title='Model Evaluation Results',
248
- labels={'value': 'Evaluation Score', 'index': 'Evaluation Metric'},
249
- color_discrete_sequence=px.colors.qualitative.Plotly)
250
-
251
- # 设置悬停效果
252
- fig.update_traces(hovertemplate='%{y}')
253
-
254
- return fig
255
-
256
-
257
-
258
- # 创建 Gradio 界面
259
- with gr.Blocks() as iface:
260
- gr.HTML("""
261
- <style>
262
- .title {
263
- text-align: center;
264
- font-size: 3em;
265
- font-weight: bold;
266
- margin-bottom: 0.5em;
267
- }
268
- .subtitle {
269
- text-align: center;
270
- font-size: 2em;
271
- margin-bottom: 1em;
272
- }
273
- </style>
274
- <div class="title">📊 Demo-Leaderboard 📊</div>
275
- """)
276
-
277
- with gr.Tabs() as tabs:
278
- with gr.TabItem("evaluation_result"):
279
- with gr.Row():
280
- with gr.Column(scale=2):
281
- with gr.Row():
282
- with gr.Column():
283
- dataset_radio = gr.Radio(["HumanEval", "MBPP"], label="Select Dataset ")
284
-
285
- with gr.Row():
286
- custom_css = """
287
- <style>
288
- .markdown-class {
289
- font-family: 'Helvetica', sans-serif;
290
- font-size: 17px;
291
- font-weight: bold;
292
- color: #333;
293
- }
294
- </style>
295
- """
296
-
297
- with gr.Column():
298
- gr.Markdown(
299
- f"{custom_css}<div class='markdown-class'> Choose Classification Perspective </div>")
300
-
301
- token_counts_checkbox = gr.Checkbox(label="Token Counts in Prompt ")
302
- line_counts_checkbox = gr.Checkbox(label="Line Counts in Prompt ")
303
- cyclomatic_complexity_checkbox = gr.Checkbox(label="Cyclomatic Complexity ")
304
- problem_type_checkbox = gr.Checkbox(label="Problem Type ")
305
-
306
- with gr.Column():
307
- gr.Markdown("<div class='markdown-class'>Choose Subsets </div>")
308
- num_parts_dropdown = gr.Dropdown(choices=[3, 4, 5, 6, 7, 8], label="Number of Subsets")
309
-
310
- with gr.Row():
311
- with gr.Column():
312
- token_counts_radio = gr.Radio(
313
- ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
314
- visible=False)
315
- with gr.Column():
316
- line_counts_radio = gr.Radio(
317
- ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
318
- visible=False)
319
- with gr.Column():
320
- cyclomatic_complexity_radio = gr.Radio(
321
- ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
322
- visible=False)
323
-
324
- token_counts_checkbox.change(fn=lambda x: toggle_radio(x, token_counts_radio),
325
- inputs=token_counts_checkbox, outputs=token_counts_radio)
326
- line_counts_checkbox.change(fn=lambda x: toggle_radio(x, line_counts_radio),
327
- inputs=line_counts_checkbox, outputs=line_counts_radio)
328
- cyclomatic_complexity_checkbox.change(fn=lambda x: toggle_radio(x, cyclomatic_complexity_radio),
329
- inputs=cyclomatic_complexity_checkbox,
330
- outputs=cyclomatic_complexity_radio)
331
-
332
- with gr.Tabs() as inner_tabs:
333
- with gr.TabItem("Leaderboard"):
334
- dataframe_output = gr.Dataframe(elem_id="dataframe")
335
- css_output = gr.HTML()
336
- confirm_button = gr.Button("Confirm ")
337
- confirm_button.click(fn=on_confirm, inputs=[dataset_radio, num_parts_dropdown, token_counts_radio,
338
- line_counts_radio, cyclomatic_complexity_radio],
339
- outputs=dataframe_output)
340
-
341
- with gr.TabItem("Line chart"):
342
- select_radio = gr.Radio(choices=[])
343
- checkboxes = [token_counts_checkbox, line_counts_checkbox, cyclomatic_complexity_checkbox,
344
- problem_type_checkbox]
345
- for checkbox in checkboxes:
346
- checkbox.change(fn=update_radio_options, inputs=checkboxes, outputs=select_radio)
347
- select_radio.change(fn=plot_csv, inputs=[select_radio, num_parts_dropdown],
348
- outputs=gr.Plot(label="Line Plot "))
349
-
350
- with gr.TabItem("upload"):
351
- gr.Markdown("Upload a JSON file")
352
- with gr.Row():
353
- with gr.Column():
354
- string_input = gr.Textbox(label="Enter the Model Name")
355
- number_input = gr.Number(label="Select the Number of Samples")
356
- dataset_choice = gr.Dropdown(label="Select Dataset", choices=["humaneval", "mbpp"])
357
- with gr.Column():
358
- file_input = gr.File(label="Upload Generation Result in JSON file")
359
- upload_button = gr.Button("Confirm and Upload")
360
-
361
- json_output = gr.JSON(label="")
362
-
363
- upload_button.click(fn=generate_file, inputs=[file_input, string_input, number_input, dataset_choice],
364
- outputs=json_output)
365
-
366
-
367
- # 定义事件处理函数
368
- def toggle_radio(checkbox, radio):
369
- return gr.update(visible=checkbox)
370
-
371
-
372
-
373
- css = """
374
- #scale1 {
375
- border: 1px solid rgba(0, 0, 0, 0.2); /* 使用浅色边框,并带有透明度 */
376
- padding: 10px; /* 添加内边距 */
377
- border-radius: 8px; /* 更圆滑的圆角 */
378
- background-color: #f9f9f9; /* 背景颜色 */
379
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
380
- }
381
- }
382
- """
383
- gr.HTML(f"<style>{css}</style>")
384
-
385
-
386
-
387
-
388
- # 初始化数据表格
389
- # initial_df = show_data(False, False, False, False, False, False, False)
390
- # initial_css = generate_css(False, False, False, False, True, False, False)
391
- # dataframe_output.value = initial_df
392
- # css_output.value = f"<style>{initial_css}</style>"
393
-
394
-
395
-
396
-
397
- # 启动界面
398
  iface.launch()
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from change_output import change_file
4
+ import requests
5
+ import os
6
+ import shutil
7
+ import json
8
+ import pandas as pd
9
+ import subprocess
10
+ import plotly.express as px
11
+ def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_counts_radio, cyclomatic_complexity_radio, problem_type_radio):
12
+ # 根据用户选择的参数构建文件路径
13
+ num_parts = num_parts_dropdown
14
+ token_counts_split = token_counts_radio
15
+ line_counts_split = line_counts_radio
16
+ cyclomatic_complexity_split = cyclomatic_complexity_radio
17
+
18
+
19
+ # 读取数据
20
+ dataframes = []
21
+ if token_counts_split=="Equal Frequency Partitioning":
22
+ token_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/token_counts_QS.csv")
23
+ dataframes.append(token_counts_df)
24
+
25
+ if line_counts_split=="Equal Frequency Partitioning":
26
+ line_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/line_counts_QS.csv")
27
+ dataframes.append(line_counts_df)
28
+
29
+ if cyclomatic_complexity_split=="Equal Frequency Partitioning":
30
+ cyclomatic_complexity_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/CC_QS.csv")
31
+ dataframes.append(cyclomatic_complexity_df)
32
+ #以下改为直接从一个划分文件中读取即可
33
+ # if problem_type_radio:
34
+ # problem_type_df = pd.read_csv(f"{num_parts}/problem_type_{problem_type_split}.csv")
35
+ # dataframes.append(problem_type_df)
36
+
37
+ # 如果所有三个radio都有value,将三个文件中的所有行拼接
38
+ if len(dataframes) > 0:
39
+ combined_df = dataframes[0]
40
+ for df in dataframes[1:]:
41
+ combined_df = pd.merge(combined_df, df, left_index=True, right_index=True, suffixes=('', '_y'))
42
+ combined_df = combined_df.loc[:, ~combined_df.columns.str.endswith('_y')] # 去除重复的列
43
+ return combined_df
44
+ else:
45
+ return pd.DataFrame()
46
+
47
+
48
+
49
+ # 定义一个函数来返回数据
50
+ # def show_data(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low):
51
+ # columns = ["Model"]
52
+ #
53
+ # if token_counts:
54
+ # if show_high:
55
+ # columns.append("Token Counts.I")
56
+ #
57
+ # if show_medium:
58
+ # columns.append("Token Counts.II")
59
+ # if show_low:
60
+ # columns.append("Token Counts.III")
61
+ # if line_counts:
62
+ # if show_high:
63
+ # columns.append("Line Counts.I")
64
+ # if show_medium:
65
+ # columns.append("Line Counts.II")
66
+ # if show_low:
67
+ # columns.append("Line Counts.III")
68
+ # if cyclomatic_complexity:
69
+ # if show_high:
70
+ # columns.append("Cyclomatic Complexity.I")
71
+ # if show_medium:
72
+ # columns.append("Cyclomatic Complexity.II")
73
+ # if show_low:
74
+ # columns.append("Cyclomatic Complexity.III")
75
+ # if problem_type:
76
+ # columns.extend(["Problem Type_String", "Problem Type_Math", "Problem Type_Array"])
77
+ # return data[columns]
78
+ #用于更新数据文件的部分
79
+ def execute_specified_python_files(directory_list, file_list):
80
+ for directory in directory_list:
81
+ for py_file in file_list:
82
+ file_path = os.path.join(directory, py_file)
83
+ if os.path.isfile(file_path) and py_file.endswith('.py'):
84
+ print(f"Executing {file_path}...")
85
+ try:
86
+ # 使用subprocess执行Python文件
87
+ subprocess.run(['python', file_path], check=True)
88
+ print(f"{file_path} executed successfully.")
89
+ except subprocess.CalledProcessError as e:
90
+ print(f"Error executing {file_path}: {e}")
91
+ else:
92
+ print(f"File {file_path} does not exist or is not a Python file.")
93
+ # 定义一个函数来生成 CSS 样式
94
+ def generate_css(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low):
95
+ css = """
96
+ #dataframe th {
97
+ background-color: #f2f2f2
98
+
99
+ }
100
+ """
101
+ colors = ["#e6f7ff", "#ffeecc", "#e6ffe6", "#ffe6e6"]
102
+ categories = [line_counts, token_counts, cyclomatic_complexity]
103
+ category_index = 0
104
+ column_index = 1
105
+
106
+ for category in categories:
107
+ if category:
108
+ if show_high:
109
+ css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
110
+ column_index += 1
111
+ if show_medium:
112
+ css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
113
+ column_index += 1
114
+ if show_low:
115
+ css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
116
+ column_index += 1
117
+ category_index += 1
118
+
119
+ # 为 Problem Type 相关的三个子列设置固定颜色
120
+ if problem_type:
121
+ problem_type_color = "#d4f0fc" # 你可以选择任何你喜欢的颜色
122
+ css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {problem_type_color}; }}\n"
123
+ css += f"#dataframe td:nth-child({column_index + 2}) {{ background-color: {problem_type_color}; }}\n"
124
+ css += f"#dataframe td:nth-child({column_index + 3}) {{ background-color: {problem_type_color}; }}\n"
125
+
126
+ # 隐藏 "data" 标识
127
+ css += """
128
+ .gradio-container .dataframe-container::before {
129
+ content: none !important;
130
+ }
131
+ """
132
+
133
+ return css
134
+ # def update_dataframe(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium,
135
+ # show_low):
136
+ # df = show_data(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low)
137
+ # css = generate_css(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium,
138
+ # show_low)
139
+ # return gr.update(value=df), gr.update(value=f"<style>{css}</style>")
140
+
141
+
142
+ def generate_file(file_obj, user_string, user_number,dataset_choice):
143
+ tmpdir = 'tmpdir'
144
+
145
+ print('临时文件夹地址:{}'.format(tmpdir))
146
+ FilePath = file_obj.name
147
+ print('上传文件的地址:{}'.format(file_obj.name)) # 输出上传后的文件在gradio中保存的绝对地址
148
+
149
+ # 将文件复制到临时目录中
150
+ shutil.copy(file_obj.name, tmpdir)
151
+
152
+ # 获取上传Gradio的文件名称
153
+ FileName = os.path.basename(file_obj.name)
154
+
155
+ print(FilePath)
156
+ # 获取拷贝在临时目录的新的文件地址
157
+
158
+ # 打开复制到新路径后的文件
159
+ with open(FilePath, 'r', encoding="utf-8") as file_obj:
160
+ # 在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
161
+ outputPath = os.path.join('F:/Desktop/test', FileName)
162
+ data = json.load(file_obj)
163
+ print("data:", data)
164
+
165
+ # 将数据写入新的 JSON 文件
166
+ with open(outputPath, 'w', encoding="utf-8") as w:
167
+ json.dump(data, w, ensure_ascii=False, indent=4)
168
+
169
+ # 读取文件内容并上传到服务器
170
+ file_content = json.dumps(data) # 将数据转换为 JSON 字符串
171
+ url = "http://localhost:6222/submit" # 替换为你的后端服务器地址
172
+ files = {'file': (FileName, file_content, 'application/json')}
173
+ payload = {
174
+ 'user_string': user_string,
175
+ 'user_number': user_number,
176
+ 'dataset_choice':dataset_choice
177
+ }
178
+
179
+ response = requests.post(url, files=files, data=payload)
180
+ print(response)
181
+ #返回服务器处理后的文件
182
+ if response.status_code == 200:
183
+ # 获取服务器返回的 JSON 数据
184
+ output_data = response.json()
185
+
186
+ # 保存 JSON 数据到本地
187
+ output_file_path = os.path.join('E:/python-testn/pythonProject3/hh_1/evaluate_result', 'new-model.json')
188
+ with open(output_file_path, 'w', encoding="utf-8") as f:
189
+ json.dump(output_data, f, ensure_ascii=False, indent=4)
190
+
191
+ print(f"File saved at: {output_file_path}")
192
+
193
+ # 调用更新数据文件的函数
194
+ directory_list = ['/path/to/directory1', '/path/to/directory2'] # 替换为你的目录路径列表
195
+ file_list = ['file1.py', 'file2.py', 'file3.py'] # 替换为你想要执行的Python文件列表
196
+
197
+ execute_specified_python_files(directory_list, file_list)
198
+
199
+ return {"status": "success", "message": "File received and saved"}
200
+ else:
201
+ return {"status": "error", "message": response.text}
202
+
203
+ # 返回服务器响应
204
+ return {"status": "success", "message": response.text}
205
+
206
+ def update_radio_options(token_counts, line_counts, cyclomatic_complexity, problem_type):
207
+ options = []
208
+ if token_counts:
209
+ options.append("Token Counts in Prompt")
210
+ if line_counts:
211
+ options.append("Line Counts in Prompt")
212
+ if cyclomatic_complexity:
213
+ options.append("Cyclomatic Complexity")
214
+ if problem_type:
215
+ options.append("Problem Type")
216
+
217
+ return gr.update(choices=options)
218
+
219
+ def plot_csv(radio,num):
220
+ # 读取本地的CSV文件
221
+ #token_counts_df = pd.read_csv(f"{num_parts}/QS/token_counts_QS.csv")
222
+ if radio=="Line Counts in Prompt":
223
+ radio_choice="line_counts"
224
+ file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
225
+ elif radio=="Token Counts in Prompt":
226
+ radio_choice="token_counts"
227
+ file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
228
+ elif radio=="Cyclomatic Complexity":
229
+ radio_choice="CC"
230
+ file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
231
+ elif radio=="Problem Type":
232
+ radio_choice="problem_type"
233
+ file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/cata_result.csv'
234
+ print("test!")
235
+
236
+ # file_path="E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/3/QS/CC_QS.csv"
237
+ df = pd.read_csv(file_path)
238
+ # 将第一列作为索引
239
+ df.set_index('Model', inplace=True)
240
+
241
+ # 转置数据框,使得模型作为列,横轴作为行
242
+ df_transposed = df.T
243
+
244
+ # 使用plotly绘制折线图
245
+ fig = px.line(df_transposed, x=df_transposed.index, y=df_transposed.columns,
246
+ title='Model Evaluation Results',
247
+ labels={'value': 'Evaluation Score', 'index': 'Evaluation Metric'},
248
+ color_discrete_sequence=px.colors.qualitative.Plotly)
249
+
250
+ # 设置悬停效果
251
+ fig.update_traces(hovertemplate='%{y}')
252
+
253
+ return fig
254
+
255
+
256
+
257
+ # 创建 Gradio 界面
258
+ with gr.Blocks() as iface:
259
+ gr.HTML("""
260
+ <style>
261
+ .title {
262
+ text-align: center;
263
+ font-size: 3em;
264
+ font-weight: bold;
265
+ margin-bottom: 0.5em;
266
+ }
267
+ .subtitle {
268
+ text-align: center;
269
+ font-size: 2em;
270
+ margin-bottom: 1em;
271
+ }
272
+ </style>
273
+ <div class="title">📊 Demo-Leaderboard 📊</div>
274
+ """)
275
+
276
+ with gr.Tabs() as tabs:
277
+ with gr.TabItem("evaluation_result"):
278
+ with gr.Row():
279
+ with gr.Column(scale=2):
280
+ with gr.Row():
281
+ with gr.Column():
282
+ dataset_radio = gr.Radio(["HumanEval", "MBPP"], label="Select Dataset ")
283
+
284
+ with gr.Row():
285
+ custom_css = """
286
+ <style>
287
+ .markdown-class {
288
+ font-family: 'Helvetica', sans-serif;
289
+ font-size: 17px;
290
+ font-weight: bold;
291
+ color: #333;
292
+ }
293
+ </style>
294
+ """
295
+
296
+ with gr.Column():
297
+ gr.Markdown(
298
+ f"{custom_css}<div class='markdown-class'> Choose Classification Perspective </div>")
299
+
300
+ token_counts_checkbox = gr.Checkbox(label="Token Counts in Prompt ")
301
+ line_counts_checkbox = gr.Checkbox(label="Line Counts in Prompt ")
302
+ cyclomatic_complexity_checkbox = gr.Checkbox(label="Cyclomatic Complexity ")
303
+ problem_type_checkbox = gr.Checkbox(label="Problem Type ")
304
+
305
+ with gr.Column():
306
+ gr.Markdown("<div class='markdown-class'>Choose Subsets </div>")
307
+ num_parts_dropdown = gr.Dropdown(choices=[3, 4, 5, 6, 7, 8], label="Number of Subsets")
308
+
309
+ with gr.Row():
310
+ with gr.Column():
311
+ token_counts_radio = gr.Radio(
312
+ ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
313
+ visible=False)
314
+ with gr.Column():
315
+ line_counts_radio = gr.Radio(
316
+ ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
317
+ visible=False)
318
+ with gr.Column():
319
+ cyclomatic_complexity_radio = gr.Radio(
320
+ ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
321
+ visible=False)
322
+
323
+ token_counts_checkbox.change(fn=lambda x: toggle_radio(x, token_counts_radio),
324
+ inputs=token_counts_checkbox, outputs=token_counts_radio)
325
+ line_counts_checkbox.change(fn=lambda x: toggle_radio(x, line_counts_radio),
326
+ inputs=line_counts_checkbox, outputs=line_counts_radio)
327
+ cyclomatic_complexity_checkbox.change(fn=lambda x: toggle_radio(x, cyclomatic_complexity_radio),
328
+ inputs=cyclomatic_complexity_checkbox,
329
+ outputs=cyclomatic_complexity_radio)
330
+
331
+ with gr.Tabs() as inner_tabs:
332
+ with gr.TabItem("Leaderboard"):
333
+ dataframe_output = gr.Dataframe(elem_id="dataframe")
334
+ css_output = gr.HTML()
335
+ confirm_button = gr.Button("Confirm ")
336
+ confirm_button.click(fn=on_confirm, inputs=[dataset_radio, num_parts_dropdown, token_counts_radio,
337
+ line_counts_radio, cyclomatic_complexity_radio],
338
+ outputs=dataframe_output)
339
+
340
+ with gr.TabItem("Line chart"):
341
+ select_radio = gr.Radio(choices=[])
342
+ checkboxes = [token_counts_checkbox, line_counts_checkbox, cyclomatic_complexity_checkbox,
343
+ problem_type_checkbox]
344
+ for checkbox in checkboxes:
345
+ checkbox.change(fn=update_radio_options, inputs=checkboxes, outputs=select_radio)
346
+ select_radio.change(fn=plot_csv, inputs=[select_radio, num_parts_dropdown],
347
+ outputs=gr.Plot(label="Line Plot "))
348
+
349
+ with gr.TabItem("upload"):
350
+ gr.Markdown("Upload a JSON file")
351
+ with gr.Row():
352
+ with gr.Column():
353
+ string_input = gr.Textbox(label="Enter the Model Name")
354
+ number_input = gr.Number(label="Select the Number of Samples")
355
+ dataset_choice = gr.Dropdown(label="Select Dataset", choices=["humaneval", "mbpp"])
356
+ with gr.Column():
357
+ file_input = gr.File(label="Upload Generation Result in JSON file")
358
+ upload_button = gr.Button("Confirm and Upload")
359
+
360
+ json_output = gr.JSON(label="")
361
+
362
+ upload_button.click(fn=generate_file, inputs=[file_input, string_input, number_input, dataset_choice],
363
+ outputs=json_output)
364
+
365
+
366
+ # 定义事件处理函数
367
+ def toggle_radio(checkbox, radio):
368
+ return gr.update(visible=checkbox)
369
+
370
+
371
+
372
+ css = """
373
+ #scale1 {
374
+ border: 1px solid rgba(0, 0, 0, 0.2); /* 使用浅色边框,并带有透明度 */
375
+ padding: 10px; /* 添加内边距 */
376
+ border-radius: 8px; /* 更圆滑的圆角 */
377
+ background-color: #f9f9f9; /* 背景颜色 */
378
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
379
+ }
380
+ }
381
+ """
382
+ gr.HTML(f"<style>{css}</style>")
383
+
384
+
385
+
386
+
387
+ # 初始化数据表格
388
+ # initial_df = show_data(False, False, False, False, False, False, False)
389
+ # initial_css = generate_css(False, False, False, False, True, False, False)
390
+ # dataframe_output.value = initial_df
391
+ # css_output.value = f"<style>{initial_css}</style>"
392
+
393
+
394
+
395
+
396
+ # 启动界面
 
397
  iface.launch()