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 = [] # script_path = os.path.abspath(__file__) # # 获取当前脚本所在的目录 # script_dir = os.path.dirname(script_path) # print("当前脚本文件的绝对路径:", script_path) # print("当前脚本文件所在的目录:", script_dir) if token_counts_split=="Equal Frequency Partitioning": token_counts_df = pd.read_csv(f"/home/user/app/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 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.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' FilePath = file_obj.name print('上传文件的地址:{}'.format(file_obj.name)) shutil.copy(file_obj.name, tmpdir) 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) with open(outputPath, 'w', encoding="utf-8") as w: json.dump(data, w, ensure_ascii=False, indent=4) file_content = json.dumps(data) 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: output_data = response.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'] 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): 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' df = pd.read_csv(file_path) df.set_index('Model', inplace=True) df_transposed = df.T 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 with gr.Blocks() as iface: gr.HTML("""