File size: 13,755 Bytes
50ed5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ea00ca
 
50ed5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1188a5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import gradio as gr
import pandas as pd
import requests
import os
import shutil
import json
import pandas as pd
import subprocess
import plotly.express as px
def on_confirm(dataset_radio, num_parts_dropdown, token_counts_radio, line_counts_radio, cyclomatic_complexity_radio, problem_type_radio):
    # 根据用户选择的参数构建文件路径
    num_parts = num_parts_dropdown
    token_counts_split = token_counts_radio
    line_counts_split = line_counts_radio
    cyclomatic_complexity_split = cyclomatic_complexity_radio


    # 读取数据
    dataframes = []
    if token_counts_split=="Equal Frequency Partitioning":
        token_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/token_counts_QS.csv")
        dataframes.append(token_counts_df)

    if line_counts_split=="Equal Frequency Partitioning":
        line_counts_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/line_counts_QS.csv")
        dataframes.append(line_counts_df)

    if cyclomatic_complexity_split=="Equal Frequency Partitioning":
        cyclomatic_complexity_df = pd.read_csv(f"E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num_parts}/QS/CC_QS.csv")
        dataframes.append(cyclomatic_complexity_df)
    #以下改为直接从一个划分文件中读取即可
    # if problem_type_radio:
    #     problem_type_df = pd.read_csv(f"{num_parts}/problem_type_{problem_type_split}.csv")
    #     dataframes.append(problem_type_df)

    # 如果所有三个radio都有value,将三个文件中的所有行拼接
    if len(dataframes) > 0:
        combined_df = dataframes[0]
        for df in dataframes[1:]:
            combined_df = pd.merge(combined_df, df, left_index=True, right_index=True, suffixes=('', '_y'))
            combined_df = combined_df.loc[:, ~combined_df.columns.str.endswith('_y')]  # 去除重复的列
        return combined_df
    else:
        return pd.DataFrame()



#用于更新数据文件的部分
def execute_specified_python_files(directory_list, file_list):
    for directory in directory_list:
        for py_file in file_list:
            file_path = os.path.join(directory, py_file)
            if os.path.isfile(file_path) and py_file.endswith('.py'):
                print(f"Executing {file_path}...")
                try:
                    # 使用subprocess执行Python文件
                    subprocess.run(['python', file_path], check=True)
                    print(f"{file_path} executed successfully.")
                except subprocess.CalledProcessError as e:
                    print(f"Error executing {file_path}: {e}")
            else:
                print(f"File {file_path} does not exist or is not a Python file.")




def generate_file(file_obj, user_string, user_number,dataset_choice):
    tmpdir = 'tmpdir'

    print('临时文件夹地址:{}'.format(tmpdir))
    FilePath = file_obj.name
    print('上传文件的地址:{}'.format(file_obj.name))  # 输出上传后的文件在gradio中保存的绝对地址

    # 将文件复制到临时目录中
    shutil.copy(file_obj.name, tmpdir)

    # 获取上传Gradio的文件名称
    FileName = os.path.basename(file_obj.name)

    print(FilePath)
    # 获取拷贝在临时目录的新的文件地址

    # 打开复制到新路径后的文件
    with open(FilePath, 'r', encoding="utf-8") as file_obj:
        # 在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
        outputPath = os.path.join('F:/Desktop/test', FileName)
        data = json.load(file_obj)
        print("data:", data)

        # 将数据写入新的 JSON 文件
        with open(outputPath, 'w', encoding="utf-8") as w:
            json.dump(data, w, ensure_ascii=False, indent=4)

        # 读取文件内容并上传到服务器
        file_content = json.dumps(data)  # 将数据转换为 JSON 字符串
        url = "http://localhost:6222/submit"  # 替换为你的后端服务器地址
        files = {'file': (FileName, file_content, 'application/json')}
        payload = {
            'user_string': user_string,
            'user_number': user_number,
            'dataset_choice':dataset_choice
        }

        response = requests.post(url, files=files, data=payload)
        print(response)
        #返回服务器处理后的文件
        if response.status_code == 200:
            # 获取服务器返回的 JSON 数据
            output_data = response.json()

            # 保存 JSON 数据到本地
            output_file_path = os.path.join('E:/python-testn/pythonProject3/hh_1/evaluate_result', 'new-model.json')
            with open(output_file_path, 'w', encoding="utf-8") as f:
                json.dump(output_data, f, ensure_ascii=False, indent=4)

            print(f"File saved at: {output_file_path}")

            # 调用更新数据文件的函数
            directory_list = ['/path/to/directory1', '/path/to/directory2']  # 替换为你的目录路径列表
            file_list = ['file1.py', 'file2.py', 'file3.py']  # 替换为你想要执行的Python文件列表

            execute_specified_python_files(directory_list, file_list)

            return {"status": "success", "message": "File received and saved"}
        else:
            return {"status": "error", "message": response.text}

        # 返回服务器响应
    return {"status": "success", "message": response.text}

def update_radio_options(token_counts, line_counts, cyclomatic_complexity, problem_type):
    options = []
    if token_counts:
        options.append("Token Counts in Prompt")
    if line_counts:
        options.append("Line Counts in Prompt")
    if cyclomatic_complexity:
        options.append("Cyclomatic Complexity")
    if problem_type:
        options.append("Problem Type")

    return gr.update(choices=options)

def plot_csv(radio,num):
    # 读取本地的CSV文件
    #token_counts_df = pd.read_csv(f"{num_parts}/QS/token_counts_QS.csv")
    if radio=="Line Counts in Prompt":
        radio_choice="line_counts"
        file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
    elif radio=="Token Counts in Prompt":
        radio_choice="token_counts"
        file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
    elif radio=="Cyclomatic Complexity":
        radio_choice="CC"
        file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/{num}/QS/{radio_choice}_QS.csv'
    elif radio=="Problem Type":
        radio_choice="problem_type"
        file_path = f'E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/cata_result.csv'
    print("test!")

    # file_path="E:/python-testn/pythonProject3/hh_1/dividing_into_different_subsets/3/QS/CC_QS.csv"
    df = pd.read_csv(file_path)
    # 将第一列作为索引
    df.set_index('Model', inplace=True)

    # 转置数据框,使得模型作为列,横轴作为行
    df_transposed = df.T

    # 使用plotly绘制折线图
    fig = px.line(df_transposed, x=df_transposed.index, y=df_transposed.columns,
                  title='Model Evaluation Results',
                  labels={'value': 'Evaluation Score', 'index': 'Evaluation Metric'},
                  color_discrete_sequence=px.colors.qualitative.Plotly)

    # 设置悬停效果
    fig.update_traces(hovertemplate='%{y}')

    return fig



    # 创建 Gradio 界面
with gr.Blocks() as iface:
    gr.HTML("""
               <style>
                   .title {
                       text-align: center;
                       font-size: 3em;
                       font-weight: bold;
                       margin-bottom: 0.5em;
                   }
                   .subtitle {
                       text-align: center;
                       font-size: 2em;
                       margin-bottom: 1em;
                   }
               </style>
               <div class="title">📊 Demo-Leaderboard 📊</div>
           """)

    with gr.Tabs() as tabs:
        with gr.TabItem("evaluation_result"):
            with gr.Row():
                with gr.Column(scale=2):
                    with gr.Row():
                        with gr.Column():
                            dataset_radio = gr.Radio(["HumanEval", "MBPP"], label="Select Dataset ")

                    with gr.Row():
                        custom_css = """  
                            <style>  
                                .markdown-class {  
                                    font-family: 'Helvetica', sans-serif;
                                    font-size: 17px; 
                                    font-weight: bold;  
                                    color: #333; 
                                }  
                            </style>  
                            """

                        with gr.Column():
                            gr.Markdown(
                                f"{custom_css}<div class='markdown-class'> Choose Classification Perspective </div>")

                            token_counts_checkbox = gr.Checkbox(label="Token Counts in Prompt ")
                            line_counts_checkbox = gr.Checkbox(label="Line Counts in Prompt ")
                            cyclomatic_complexity_checkbox = gr.Checkbox(label="Cyclomatic Complexity ")
                            problem_type_checkbox = gr.Checkbox(label="Problem Type ")

                        with gr.Column():
                            gr.Markdown("<div class='markdown-class'>Choose Subsets </div>")
                            num_parts_dropdown = gr.Dropdown(choices=[3, 4, 5, 6, 7, 8], label="Number of Subsets")

            with gr.Row():
                with gr.Column():
                    token_counts_radio = gr.Radio(
                        ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
                        visible=False)
                with gr.Column():
                    line_counts_radio = gr.Radio(
                        ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
                        visible=False)
                with gr.Column():
                    cyclomatic_complexity_radio = gr.Radio(
                        ["Equal Frequency Partitioning", "Equal Interval Partitioning"], label="Select Dataset",
                        visible=False)

            token_counts_checkbox.change(fn=lambda x: toggle_radio(x, token_counts_radio),
                                         inputs=token_counts_checkbox, outputs=token_counts_radio)
            line_counts_checkbox.change(fn=lambda x: toggle_radio(x, line_counts_radio),
                                        inputs=line_counts_checkbox, outputs=line_counts_radio)
            cyclomatic_complexity_checkbox.change(fn=lambda x: toggle_radio(x, cyclomatic_complexity_radio),
                                                  inputs=cyclomatic_complexity_checkbox,
                                                  outputs=cyclomatic_complexity_radio)

            with gr.Tabs() as inner_tabs:
                with gr.TabItem("Leaderboard"):
                    dataframe_output = gr.Dataframe(elem_id="dataframe")
                    css_output = gr.HTML()
                    confirm_button = gr.Button("Confirm ")
                    confirm_button.click(fn=on_confirm, inputs=[dataset_radio, num_parts_dropdown, token_counts_radio,
                                                                line_counts_radio, cyclomatic_complexity_radio],
                                         outputs=dataframe_output)

                with gr.TabItem("Line chart"):
                    select_radio = gr.Radio(choices=[])
                    checkboxes = [token_counts_checkbox, line_counts_checkbox, cyclomatic_complexity_checkbox,
                                  problem_type_checkbox]
                    for checkbox in checkboxes:
                        checkbox.change(fn=update_radio_options, inputs=checkboxes, outputs=select_radio)
                    select_radio.change(fn=plot_csv, inputs=[select_radio, num_parts_dropdown],
                                        outputs=gr.Plot(label="Line Plot "))

        with gr.TabItem("upload"):
            gr.Markdown("Upload a JSON file")
            with gr.Row():
                with gr.Column():
                    string_input = gr.Textbox(label="Enter the Model Name")
                    number_input = gr.Number(label="Select the Number of Samples")
                    dataset_choice = gr.Dropdown(label="Select Dataset", choices=["humaneval", "mbpp"])
                with gr.Column():
                    file_input = gr.File(label="Upload Generation Result in JSON file")
                    upload_button = gr.Button("Confirm and Upload")

            json_output = gr.JSON(label="")

            upload_button.click(fn=generate_file, inputs=[file_input, string_input, number_input, dataset_choice],
                                outputs=json_output)


    # 定义事件处理函数
    def toggle_radio(checkbox, radio):
        return gr.update(visible=checkbox)



    css = """  
        #scale1 {  
    border: 1px solid rgba(0, 0, 0, 0.2); /* 使用浅色边框,并带有透明度 */  
    padding: 10px; /* 添加内边距 */  
    border-radius: 8px; /* 更圆滑的圆角 */  
    background-color: #f9f9f9; /* 背景颜色 */  
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */  
}  
        }  
        """
    gr.HTML(f"<style>{css}</style>")




# 启动界面
iface.launch()