Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import re
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if not (text[match.start() - 1] == ' ' and text[match.end()] == ' '):
|
14 |
-
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
15 |
-
return errors
|
16 |
|
17 |
-
|
18 |
-
def check_spacing_between_dollars(df):
|
19 |
-
errors = []
|
20 |
-
for i, row in df.iterrows():
|
21 |
-
for col in df.columns:
|
22 |
-
text = str(row[col])
|
23 |
-
matches = list(re.finditer(r'\$\S+?(?=\$)', text))
|
24 |
-
for match in matches:
|
25 |
-
if text[match.end()] != ' ' and text[match.start() - 1] != ' ':
|
26 |
-
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
27 |
-
return errors
|
28 |
-
|
29 |
-
# 第三個檢查:檢查數字前後是否有$
|
30 |
-
def check_numbers_surrounded_by_dollar(df):
|
31 |
errors = []
|
32 |
for i, row in df.iterrows():
|
33 |
for col in df.columns:
|
34 |
-
text = str(row[col])
|
35 |
-
matches =
|
36 |
for match in matches:
|
37 |
-
if
|
38 |
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
39 |
return errors
|
40 |
|
41 |
-
# 處理檔案並執行檢查
|
42 |
def process_file(file):
|
43 |
if file.name.endswith('.csv'):
|
44 |
df = pd.read_csv(file.name)
|
@@ -58,7 +42,6 @@ def process_file(file):
|
|
58 |
"第三個檢查": errors3
|
59 |
}
|
60 |
|
61 |
-
# Gradio 介面
|
62 |
iface = gr.Interface(
|
63 |
fn=process_file,
|
64 |
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import re
|
4 |
+
import json
|
5 |
|
6 |
+
def extract_text_from_json(text):
|
7 |
+
try:
|
8 |
+
data = json.loads(text)
|
9 |
+
if isinstance(data, dict) and 'question' in data:
|
10 |
+
return data['question']['content']
|
11 |
+
except json.JSONDecodeError:
|
12 |
+
return text
|
13 |
+
return text
|
|
|
|
|
|
|
14 |
|
15 |
+
def check_spacing_around_dollar(df):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
errors = []
|
17 |
for i, row in df.iterrows():
|
18 |
for col in df.columns:
|
19 |
+
text = extract_text_from_json(str(row[col]))
|
20 |
+
matches = re.finditer(r'(\$\S+|\S+\$)', text)
|
21 |
for match in matches:
|
22 |
+
if (match.start() > 0 and text[match.start() - 1] != ' ') or (match.end() < len(text) and text[match.end()] != ' '):
|
23 |
errors.append(f"行 {i+1} 列 '{col}': '{text}'")
|
24 |
return errors
|
25 |
|
|
|
26 |
def process_file(file):
|
27 |
if file.name.endswith('.csv'):
|
28 |
df = pd.read_csv(file.name)
|
|
|
42 |
"第三個檢查": errors3
|
43 |
}
|
44 |
|
|
|
45 |
iface = gr.Interface(
|
46 |
fn=process_file,
|
47 |
inputs=gr.File(label="上傳 CSV 或 XLSX 檔案"),
|