Mengyuan Liu commited on
Commit
d0c890e
1 Parent(s): de18c1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -49
app.py CHANGED
@@ -8,17 +8,15 @@ import pandas as pd
8
  import subprocess
9
  import plotly.express as px
10
  def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_counts_radio, cyclomatic_complexity_radio, problem_type_radio):
11
- # 根据用户选择的参数构建文件路径
12
  num_parts = num_parts_dropdown
13
  token_counts_split = token_counts_radio
14
  line_counts_split = line_counts_radio
15
  cyclomatic_complexity_split = cyclomatic_complexity_radio
16
 
17
-
18
- # 读取数据
19
  dataframes = []
20
  if token_counts_split=="Equal Frequency Partitioning":
21
- token_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/token_counts_QS.csv")
22
  dataframes.append(token_counts_df)
23
 
24
  if line_counts_split=="Equal Frequency Partitioning":
@@ -28,12 +26,7 @@ def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_count
28
  if cyclomatic_complexity_split=="Equal Frequency Partitioning":
29
  cyclomatic_complexity_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/CC_QS.csv")
30
  dataframes.append(cyclomatic_complexity_df)
31
- #以下改为直接从一个划分文件中读取即可
32
- # if problem_type_radio:
33
- # problem_type_df = pd.read_csv(f"{num_parts}/problem_type_{problem_type_split}.csv")
34
- # dataframes.append(problem_type_df)
35
-
36
- # 如果所有三个radio都有value,将三个文件中的所有行拼接
37
  if len(dataframes) > 0:
38
  combined_df = dataframes[0]
39
  for df in dataframes[1:]:
@@ -45,7 +38,7 @@ def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_count
45
 
46
 
47
 
48
- #用于更新数据文件的部分
49
  def execute_specified_python_files(directory_list, file_list):
50
  for directory in directory_list:
51
  for py_file in file_list:
@@ -67,33 +60,31 @@ def execute_specified_python_files(directory_list, file_list):
67
  def generate_file(file_obj, user_string, user_number,dataset_choice):
68
  tmpdir = 'tmpdir'
69
 
70
- print('临时文件夹地址:{}'.format(tmpdir))
71
  FilePath = file_obj.name
72
- print('上传文件的地址:{}'.format(file_obj.name)) # 输出上传后的文件在gradio中保存的绝对地址
73
 
74
- # 将文件复制到临时目录中
75
  shutil.copy(file_obj.name, tmpdir)
76
 
77
- # 获取上传Gradio的文件名称
78
  FileName = os.path.basename(file_obj.name)
79
 
80
  print(FilePath)
81
- # 获取拷贝在临时目录的新的文件地址
82
-
83
- # 打开复制到新路径后的文件
84
  with open(FilePath, 'r', encoding="utf-8") as file_obj:
85
- # 在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
86
  outputPath = os.path.join('F:/Desktop/test', FileName)
87
  data = json.load(file_obj)
88
  print("data:", data)
89
 
90
- # 将数据写入新的 JSON 文件
91
  with open(outputPath, 'w', encoding="utf-8") as w:
92
  json.dump(data, w, ensure_ascii=False, indent=4)
93
 
94
- # 读取文件内容并上传到服务器
95
- file_content = json.dumps(data) # 将数据转换为 JSON 字符串
96
- url = "http://localhost:6222/submit" # 替换为你的后端服务器地址
97
  files = {'file': (FileName, file_content, 'application/json')}
98
  payload = {
99
  'user_string': user_string,
@@ -103,12 +94,12 @@ def generate_file(file_obj, user_string, user_number,dataset_choice):
103
 
104
  response = requests.post(url, files=files, data=payload)
105
  print(response)
106
- #返回服务器处理后的文件
107
  if response.status_code == 200:
108
- # 获取服务器返回的 JSON 数据
109
  output_data = response.json()
110
 
111
- # 保存 JSON 数据到本地
112
  output_file_path = os.path.join('E:/python-testn/pythonProject3/hh_1/evaluate_result', 'new-model.json')
113
  with open(output_file_path, 'w', encoding="utf-8") as f:
114
  json.dump(output_data, f, ensure_ascii=False, indent=4)
@@ -116,8 +107,8 @@ def generate_file(file_obj, user_string, user_number,dataset_choice):
116
  print(f"File saved at: {output_file_path}")
117
 
118
  # 调用更新数据文件的函数
119
- directory_list = ['/path/to/directory1', '/path/to/directory2'] # 替换为你的目录路径列表
120
- file_list = ['file1.py', 'file2.py', 'file3.py'] # 替换为你想要执行的Python文件列表
121
 
122
  execute_specified_python_files(directory_list, file_list)
123
 
@@ -142,8 +133,6 @@ def update_radio_options(token_counts, line_counts, cyclomatic_complexity, probl
142
  return gr.update(choices=options)
143
 
144
  def plot_csv(radio,num):
145
- # 读取本地的CSV文件
146
- #token_counts_df = pd.read_csv(f"{num_parts}/QS/token_counts_QS.csv")
147
  if radio=="Line Counts in Prompt":
148
  radio_choice="line_counts"
149
  file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
@@ -156,30 +145,22 @@ def plot_csv(radio,num):
156
  elif radio=="Problem Type":
157
  radio_choice="problem_type"
158
  file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/cata_result.csv'
159
- print("test!")
160
-
161
- # file_path="E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/3/QS/CC_QS.csv"
162
  df = pd.read_csv(file_path)
163
- # 将第一列作为索引
164
  df.set_index('Model', inplace=True)
165
 
166
- # 转置数据框,使得模型作为列,横轴作为行
167
  df_transposed = df.T
168
 
169
- # 使用plotly绘制折线图
170
  fig = px.line(df_transposed, x=df_transposed.index, y=df_transposed.columns,
171
  title='Model Evaluation Results',
172
  labels={'value': 'Evaluation Score', 'index': 'Evaluation Metric'},
173
  color_discrete_sequence=px.colors.qualitative.Plotly)
174
 
175
- # 设置悬停效果
176
  fig.update_traces(hovertemplate='%{y}')
177
 
178
  return fig
179
 
180
-
181
-
182
- # 创建 Gradio 界面
183
  with gr.Blocks() as iface:
184
  gr.HTML("""
185
  <style>
@@ -199,7 +180,7 @@ with gr.Blocks() as iface:
199
  """)
200
 
201
  with gr.Tabs() as tabs:
202
- with gr.TabItem("evaluation_result"):
203
  with gr.Row():
204
  with gr.Column(scale=2):
205
  with gr.Row():
@@ -271,7 +252,7 @@ with gr.Blocks() as iface:
271
  select_radio.change(fn=plot_csv, inputs=[select_radio, num_parts_dropdown],
272
  outputs=gr.Plot(label="Line Plot "))
273
 
274
- with gr.TabItem("upload"):
275
  gr.Markdown("Upload a JSON file")
276
  with gr.Row():
277
  with gr.Column():
@@ -288,7 +269,7 @@ with gr.Blocks() as iface:
288
  outputs=json_output)
289
 
290
 
291
- # 定义事件处理函数
292
  def toggle_radio(checkbox, radio):
293
  return gr.update(visible=checkbox)
294
 
@@ -296,11 +277,11 @@ with gr.Blocks() as iface:
296
 
297
  css = """
298
  #scale1 {
299
- border: 1px solid rgba(0, 0, 0, 0.2); /* 使用浅色边框,并带有透明度 */
300
- padding: 10px; /* 添加内边距 */
301
- border-radius: 8px; /* 更圆滑的圆角 */
302
- background-color: #f9f9f9; /* 背景颜色 */
303
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
304
  }
305
  }
306
  """
@@ -309,5 +290,5 @@ with gr.Blocks() as iface:
309
 
310
 
311
 
312
- # 启动界面
313
  iface.launch()
 
8
  import subprocess
9
  import plotly.express as px
10
  def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_counts_radio, cyclomatic_complexity_radio, problem_type_radio):
11
+
12
  num_parts = num_parts_dropdown
13
  token_counts_split = token_counts_radio
14
  line_counts_split = line_counts_radio
15
  cyclomatic_complexity_split = cyclomatic_complexity_radio
16
 
 
 
17
  dataframes = []
18
  if token_counts_split=="Equal Frequency Partitioning":
19
+ token_counts_df = pd.read_csv(f"home/user/app/dividing_into_different_subsets/{num_parts}/QS/token_counts_QS.csv")
20
  dataframes.append(token_counts_df)
21
 
22
  if line_counts_split=="Equal Frequency Partitioning":
 
26
  if cyclomatic_complexity_split=="Equal Frequency Partitioning":
27
  cyclomatic_complexity_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/CC_QS.csv")
28
  dataframes.append(cyclomatic_complexity_df)
29
+
 
 
 
 
 
30
  if len(dataframes) > 0:
31
  combined_df = dataframes[0]
32
  for df in dataframes[1:]:
 
38
 
39
 
40
 
41
+
42
  def execute_specified_python_files(directory_list, file_list):
43
  for directory in directory_list:
44
  for py_file in file_list:
 
60
  def generate_file(file_obj, user_string, user_number,dataset_choice):
61
  tmpdir = 'tmpdir'
62
 
63
+
64
  FilePath = file_obj.name
65
+ print('上传文件的地址:{}'.format(file_obj.name))
66
 
67
+
68
  shutil.copy(file_obj.name, tmpdir)
69
 
70
+
71
  FileName = os.path.basename(file_obj.name)
72
 
73
  print(FilePath)
74
+
 
 
75
  with open(FilePath, 'r', encoding="utf-8") as file_obj:
76
+
77
  outputPath = os.path.join('F:/Desktop/test', FileName)
78
  data = json.load(file_obj)
79
  print("data:", data)
80
 
81
+
82
  with open(outputPath, 'w', encoding="utf-8") as w:
83
  json.dump(data, w, ensure_ascii=False, indent=4)
84
 
85
+
86
+ file_content = json.dumps(data)
87
+ url = "http://localhost:6222/submit"
88
  files = {'file': (FileName, file_content, 'application/json')}
89
  payload = {
90
  'user_string': user_string,
 
94
 
95
  response = requests.post(url, files=files, data=payload)
96
  print(response)
97
+
98
  if response.status_code == 200:
99
+
100
  output_data = response.json()
101
 
102
+
103
  output_file_path = os.path.join('E:/python-testn/pythonProject3/hh_1/evaluate_result', 'new-model.json')
104
  with open(output_file_path, 'w', encoding="utf-8") as f:
105
  json.dump(output_data, f, ensure_ascii=False, indent=4)
 
107
  print(f"File saved at: {output_file_path}")
108
 
109
  # 调用更新数据文件的函数
110
+ directory_list = ['/path/to/directory1', '/path/to/directory2']
111
+ file_list = ['file1.py', 'file2.py', 'file3.py']
112
 
113
  execute_specified_python_files(directory_list, file_list)
114
 
 
133
  return gr.update(choices=options)
134
 
135
  def plot_csv(radio,num):
 
 
136
  if radio=="Line Counts in Prompt":
137
  radio_choice="line_counts"
138
  file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
 
145
  elif radio=="Problem Type":
146
  radio_choice="problem_type"
147
  file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/cata_result.csv'
148
+
 
 
149
  df = pd.read_csv(file_path)
150
+
151
  df.set_index('Model', inplace=True)
152
 
 
153
  df_transposed = df.T
154
 
 
155
  fig = px.line(df_transposed, x=df_transposed.index, y=df_transposed.columns,
156
  title='Model Evaluation Results',
157
  labels={'value': 'Evaluation Score', 'index': 'Evaluation Metric'},
158
  color_discrete_sequence=px.colors.qualitative.Plotly)
159
 
 
160
  fig.update_traces(hovertemplate='%{y}')
161
 
162
  return fig
163
 
 
 
 
164
  with gr.Blocks() as iface:
165
  gr.HTML("""
166
  <style>
 
180
  """)
181
 
182
  with gr.Tabs() as tabs:
183
+ with gr.TabItem("Evaluation Result"):
184
  with gr.Row():
185
  with gr.Column(scale=2):
186
  with gr.Row():
 
252
  select_radio.change(fn=plot_csv, inputs=[select_radio, num_parts_dropdown],
253
  outputs=gr.Plot(label="Line Plot "))
254
 
255
+ with gr.TabItem("Upload"):
256
  gr.Markdown("Upload a JSON file")
257
  with gr.Row():
258
  with gr.Column():
 
269
  outputs=json_output)
270
 
271
 
272
+
273
  def toggle_radio(checkbox, radio):
274
  return gr.update(visible=checkbox)
275
 
 
277
 
278
  css = """
279
  #scale1 {
280
+ border: 1px solid rgba(0, 0, 0, 0.2);
281
+ padding: 10px;
282
+ border-radius: 8px;
283
+ background-color: #f9f9f9;
284
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
285
  }
286
  }
287
  """
 
290
 
291
 
292
 
293
+
294
  iface.launch()