Mengyuan Liu commited on
Commit
1188a5e
1 Parent(s): 538a2cd

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +398 -0
app.py ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()