import gradio as gr import os import numpy as np import pandas as pd from scipy.signal import find_peaks from werkzeug.utils import secure_filename def process_text(file): row = [] # 行号 x = [] # x值 y = [] # y值 filename = secure_filename(file.name) upload_path = "./uploads/" if not os.path.exists(upload_path): os.mkdir(upload_path) file_path = upload_path + filename file.save(file_path) name = file.name.split(".")[0] target_path = upload_path + f"{name}.csv" # 目标路径 # 读取文件内容 with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() data = [] for line in lines: temp = line.split() temp1 = [int(float(x)) for x in temp] data.append(temp1) data = np.array(data) peaks, _ = find_peaks(data[:, 1], distance=2500) # 根据第2列找到所有峰值, 前后比较阈值为distance=2500 temp_row = [] temp_x = [] temp_y = [] for peak in peaks: temp_row.append(peak) temp_x.append(data[peak, 0]) temp_y.append(data[peak, 1]) temp_y = sorted(enumerate(temp_y), key=lambda z: z[1], reverse=True) for index, value in temp_y: row.append(temp_row[index]) x.append(temp_x[index]) y.append(value) datas = { 'row_num': row[:10], 'x': x[:10], 'y': y[:10], } df = pd.DataFrame(datas) df.to_csv(target_path, index=False) return df iface = gr.Interface( fn=process_text, inputs="file", outputs=["file", "text"], title="Upload and Process Text File", description="Upload a txt file and it will be processed by this app.", ) iface.launch()