Spaces:
Runtime error
Runtime error
File size: 1,860 Bytes
3ca5af2 7ca6ce4 a33afb6 7ca6ce4 a33afb6 7ca6ce4 a33afb6 7ca6ce4 8e63bc3 7ca6ce4 3f7a930 7ca6ce4 3ca5af2 7ca6ce4 3ca5af2 7ca6ce4 ca7b90c 7ca6ce4 3ca5af2 7ca6ce4 |
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 |
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() |