Spaces:
Runtime error
Runtime error
Commit
·
7ca6ce4
1
Parent(s):
3ca5af2
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,64 @@
|
|
1 |
-
from zipfile import ZipFile
|
2 |
-
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
files.append(
|
11 |
-
{
|
12 |
-
"name": zinfo.filename,
|
13 |
-
"file_size": zinfo.file_size,
|
14 |
-
"compressed_size": zinfo.compress_size,
|
15 |
-
}
|
16 |
-
)
|
17 |
-
return files
|
18 |
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import codecs
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
from scipy.signal import find_peaks
|
7 |
+
|
8 |
+
|
9 |
+
def process_text(file):
|
10 |
+
|
11 |
+
row = [] # 行号
|
12 |
+
x = [] # x值
|
13 |
+
y = [] # y值
|
14 |
+
|
15 |
+
filename = file.split(".")[0]
|
16 |
+
target_path = f"{filename}.csv" # 目标路径
|
17 |
+
|
18 |
+
# 读取文件内容
|
19 |
+
with codecs.open(file, 'r', 'utf-8') as f:
|
20 |
+
lines = f.readlines()
|
21 |
+
data = []
|
22 |
+
for line in lines:
|
23 |
+
temp = line.split()
|
24 |
+
temp1 = [int(float(x)) for x in temp]
|
25 |
+
data.append(temp1)
|
26 |
+
data = np.array(data)
|
27 |
+
peaks, _ = find_peaks(data[:, 1], distance=2500) # 根据第2列找到所有峰值, 前后比较阈值为distance=2500
|
28 |
+
|
29 |
+
temp_row = []
|
30 |
+
temp_x = []
|
31 |
+
temp_y = []
|
32 |
+
for peak in peaks:
|
33 |
+
temp_row.append(peak)
|
34 |
+
temp_x.append(data[peak, 0])
|
35 |
+
temp_y.append(data[peak, 1])
|
36 |
+
|
37 |
+
temp_y = sorted(enumerate(temp_y), key=lambda z: z[1], reverse=True)
|
38 |
+
|
39 |
+
for index, value in temp_y:
|
40 |
+
row.append(temp_row[index])
|
41 |
+
x.append(temp_x[index])
|
42 |
+
y.append(value)
|
43 |
|
44 |
+
datas = {
|
45 |
+
'row_num': row,
|
46 |
+
'x': x,
|
47 |
+
'y': y,
|
48 |
+
}
|
49 |
|
50 |
+
df = pd.DataFrame(datas)
|
51 |
+
df.to_csv(target_path, index=False)
|
52 |
+
|
53 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
|
56 |
+
iface = gr.Interface(
|
57 |
+
fn=process_text,
|
58 |
+
inputs="file",
|
59 |
+
outputs="text",
|
60 |
+
title="Upload and Process Text File",
|
61 |
+
description="Upload a txt file and it will be processed by this app.",
|
62 |
+
)
|
63 |
|
64 |
+
iface.launch()
|
|