Huhujingjing commited on
Commit
7ca6ce4
·
1 Parent(s): 3ca5af2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -17
app.py CHANGED
@@ -1,23 +1,64 @@
1
- from zipfile import ZipFile
2
-
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
 
 
 
5
 
6
- def zip_to_json(file_obj):
7
- files = []
8
- with ZipFile(file_obj.name) as zfile:
9
- for zinfo in zfile.infolist():
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
- demo = gr.Interface(zip_to_json, "file", "txt")
 
 
 
 
 
 
21
 
22
- if __name__ == "__main__":
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()