Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
|
5 |
+
# 第一個檢查
|
6 |
+
def check_spacing_around_dollar(df):
|
7 |
+
errors = []
|
8 |
+
for i, row in df.iterrows():
|
9 |
+
for col in df.columns:
|
10 |
+
text = str(row[col])
|
11 |
+
for match in re.finditer(r'\$\S+|\S+\$', text):
|
12 |
+
if text[match.start() - 1] != ' ' or text[match.end()] != ' ':
|
13 |
+
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
14 |
+
return errors
|
15 |
+
|
16 |
+
# 第二個檢查
|
17 |
+
def check_spacing_between_dollars(df):
|
18 |
+
errors = []
|
19 |
+
for i, row in df.iterrows():
|
20 |
+
for col in df.columns:
|
21 |
+
text = str(row[col])
|
22 |
+
for match in re.finditer(r'\$\S+', text):
|
23 |
+
if text[match.end()] != ' ' and text[match.start() - 1] != ' ':
|
24 |
+
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
25 |
+
return errors
|
26 |
+
|
27 |
+
# 第三個檢查
|
28 |
+
def check_numbers_surrounded_by_dollar(df):
|
29 |
+
errors = []
|
30 |
+
for i, row in df.iterrows():
|
31 |
+
for col in df.columns:
|
32 |
+
text = str(row[col])
|
33 |
+
for match in re.finditer(r'\b\d+\b', text):
|
34 |
+
if not (text[match.start() - 1] == '$' and text[match.end()] == '$'):
|
35 |
+
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
36 |
+
return errors
|
37 |
+
|
38 |
+
def process_file(file):
|
39 |
+
if file.name.endswith('.csv'):
|
40 |
+
df = pd.read_csv(file.name)
|
41 |
+
elif file.name.endswith('.xlsx'):
|
42 |
+
df = pd.read_excel(file.name)
|
43 |
+
else:
|
44 |
+
return "只支持 CSV 和 XLSX 檔案"
|
45 |
+
|
46 |
+
# 執行檢查
|
47 |
+
errors1 = check_spacing_around_dollar(df)
|
48 |
+
errors2 = check_spacing_between_dollars(df)
|
49 |
+
errors3 = check_numbers_surrounded_by_dollar(df)
|
50 |
+
|
51 |
+
return {
|
52 |
+
"第一個檢查": errors1,
|
53 |
+
"第二個檢查": errors2,
|
54 |
+
"第三個檢查": errors3
|
55 |
+
}
|
56 |
+
|
57 |
+
# Gradio 介面
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=process_file,
|
60 |
+
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|
61 |
+
outputs=gr.JSON(label="檢查結果"),
|
62 |
+
title="校對系統",
|
63 |
+
description="這個系統會檢查 CSV 或 XLSX 檔案中的格式錯誤,包括 $ 符號和數字的空格錯誤。"
|
64 |
+
)
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
iface.launch()
|