Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import gradio as
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
import json
|
|
@@ -23,7 +23,7 @@ def check_spacing_around_dollar(df):
|
|
| 23 |
for i, row in df.iterrows():
|
| 24 |
for col in df.columns:
|
| 25 |
text = extract_text_from_json(str(row[col]))
|
| 26 |
-
matches = re.finditer(r'
|
| 27 |
for match in matches:
|
| 28 |
if (match.start() > 0 and text[match.start() - 1] != ' ') or (match.end() < len(text) and text[match.end()] != ' '):
|
| 29 |
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
|
@@ -55,79 +55,32 @@ def check_numbers_surrounded_by_dollar(df):
|
|
| 55 |
|
| 56 |
# 處理檔案並執行檢查
|
| 57 |
def process_file(file):
|
| 58 |
-
if file.name.endswith('.csv'):
|
| 59 |
-
df = pd.read_csv(file.name)
|
| 60 |
-
elif file.name.endswith('.xlsx'):
|
| 61 |
-
df = pd.read_excel(file.name)
|
| 62 |
-
else:
|
| 63 |
-
return "只支持 CSV 和 XLSX 檔案"
|
| 64 |
-
|
| 65 |
-
# 執行檢查
|
| 66 |
-
errors1 = check_spacing_around_dollar(df)
|
| 67 |
-
errors2 = check_spacing_between_dollars(df)
|
| 68 |
-
errors3 = check_numbers_surrounded_by_dollar(df)
|
| 69 |
-
|
| 70 |
-
return {
|
| 71 |
-
"第一個檢查": errors1,
|
| 72 |
-
"第二個檢查": errors2,
|
| 73 |
-
"第三個檢查": errors3
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
# Gradio 介面
|
| 77 |
-
iface = gr.Interface(
|
| 78 |
-
fn=process_file,
|
| 79 |
-
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|
| 80 |
-
outputs=gr.JSON(label="檢查結果"),
|
| 81 |
-
title="校對系統",
|
| 82 |
-
description="這個系統會檢查 CSV 或 XLSX 檔案中的格式錯誤,包括 $ 符號和數字的空格錯誤。"
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
if __name__ == "__main__":
|
| 86 |
-
iface.launch()
|
| 87 |
-
|
| 88 |
-
import pandas as pd
|
| 89 |
-
import re
|
| 90 |
-
import json
|
| 91 |
-
|
| 92 |
-
def extract_text_from_json(text):
|
| 93 |
try:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
else:
|
| 118 |
-
return "只支持 CSV 和 XLSX 檔案"
|
| 119 |
-
|
| 120 |
-
# 執行檢查
|
| 121 |
-
errors1 = check_spacing_around_dollar(df)
|
| 122 |
-
errors2 = check_spacing_between_dollars(df)
|
| 123 |
-
errors3 = check_numbers_surrounded_by_dollar(df)
|
| 124 |
-
|
| 125 |
-
return {
|
| 126 |
-
"第一個檢查": errors1,
|
| 127 |
-
"第二個檢查": errors2,
|
| 128 |
-
"第三個檢查": errors3
|
| 129 |
-
}
|
| 130 |
|
|
|
|
| 131 |
iface = gr.Interface(
|
| 132 |
fn=process_file,
|
| 133 |
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
import json
|
|
|
|
| 23 |
for i, row in df.iterrows():
|
| 24 |
for col in df.columns:
|
| 25 |
text = extract_text_from_json(str(row[col]))
|
| 26 |
+
matches = re.finditer(r'\$\S+|\S+\$', text)
|
| 27 |
for match in matches:
|
| 28 |
if (match.start() > 0 and text[match.start() - 1] != ' ') or (match.end() < len(text) and text[match.end()] != ' '):
|
| 29 |
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
|
|
|
| 55 |
|
| 56 |
# 處理檔案並執行檢查
|
| 57 |
def process_file(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
try:
|
| 59 |
+
if file.name.endswith('.csv'):
|
| 60 |
+
df = pd.read_csv(file.name)
|
| 61 |
+
elif file.name.endswith('.xlsx'):
|
| 62 |
+
df = pd.read_excel(file.name)
|
| 63 |
+
else:
|
| 64 |
+
return "只支持 CSV 和 XLSX 檔案"
|
| 65 |
|
| 66 |
+
# 打印 DataFrame 以檢查內容
|
| 67 |
+
print("讀取的 DataFrame:")
|
| 68 |
+
print(df.head())
|
| 69 |
+
|
| 70 |
+
# 執行檢查
|
| 71 |
+
errors1 = check_spacing_around_dollar(df)
|
| 72 |
+
errors2 = check_spacing_between_dollars(df)
|
| 73 |
+
errors3 = check_numbers_surrounded_by_dollar(df)
|
| 74 |
+
|
| 75 |
+
return {
|
| 76 |
+
"第一個檢查": errors1,
|
| 77 |
+
"第二個檢查": errors2,
|
| 78 |
+
"第三個檢查": errors3
|
| 79 |
+
}
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Gradio 介面
|
| 84 |
iface = gr.Interface(
|
| 85 |
fn=process_file,
|
| 86 |
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|