Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import gradio as grimport pandas as pdimport reimport json# 提取 JSON 格式中的文本部分def extract_text_from_json(text):try:data = json.loads(text)if isinstance(data, dict):if 'question' in data:return data['question']['content']if 'content' in data:return data['content']except json.JSONDecodeError:return textreturn text# 檢查 $ 符號前後是否有空格def check_spacing_around_dollar(df):errors = []for i, row in df.iterrows():for col in df.columns:text = extract_text_from_json(str(row[col]))matches = re.finditer(r'\$\S+|\S+\$', text)for match in matches:if (match.start() > 0 and text[match.start() - 1] != ' ') or (match.end() < len(text) and text[match.end()] != ' '):errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 檢查 $ 符號之間是否有空格def check_spacing_between_dollars(df):errors = []for i, row in df.iterrows():for col in df.columns:text = extract_text_from_json(str(row[col]))matches = re.finditer(r'\$\S+?(?=\$)', text)for match in matches:if text[match.end()] != ' ' and text[match.start() - 1] != ' ':errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 檢查數字前後是否有 $ 符號def check_numbers_surrounded_by_dollar(df):errors = []for i, row in df.iterrows():for col in df.columns:text = extract_text_from_json(str(row[col]))matches = re.finditer(r'\b\d+\b', text)for match in matches:if not (match.start() > 0 and text[match.start() - 1] == '$' and match.end() < len(text) and text[match.end()] == '$'):errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 處理檔案並執行檢查def process_file(file):try:if file.name.endswith('.csv'):df = pd.read_csv(file.name)elif file.name.endswith('.xlsx'):df = pd.read_excel(file.name)else:return "只支持 CSV 和 XLSX 檔案"# 打印 DataFrame 以檢查內容print("讀取的 DataFrame:")print(df.head())# 執行檢查errors1 = check_spacing_around_dollar(df)errors2 = check_spacing_between_dollars(df)errors3 = check_numbers_surrounded_by_dollar(df)return {"第一個檢查": errors1,"第二個檢查": errors2,"第三個檢查": errors3}except Exception as e:return str(e)# Gradio 介面iface = gr.Interface(1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import gradio as grimport pandas as pdimport re# 第一個檢查:檢查$符號前後是否有空格def check_spacing_around_dollar(df):errors = []for i, row in df.iterrows():for col in df.columns:text = str(row[col])matches = list(re.finditer(r'(\s?\$\S*?\s?|\s?\S*?\$)', text))for match in matches:if not (text[match.start() - 1] == ' ' and text[match.end()] == ' '):errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 第二個檢查:檢查$符號之間是否有空格def check_spacing_between_dollars(df):errors = []for i, row in df.iterrows():for col in df.columns:text = str(row[col])matches = list(re.finditer(r'\$\S+?(?=\$)', text))for match in matches:if text[match.end()] != ' ' and text[match.start() - 1] != ' ':errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 第三個檢查:檢查數字前後是否有$def check_numbers_surrounded_by_dollar(df):errors = []for i, row in df.iterrows():for col in df.columns:text = str(row[col])matches = list(re.finditer(r'\b\d+\b', text))for match in matches:if not (text[match.start() - 1] == '$' and text[match.end()] == '$'):errors.append(f"行 {i+1} 列 '{col}': '{text}'")return errors# 處理檔案並執行檢查def process_file(file):if file.name.endswith('.csv'):df = pd.read_csv(file.name)elif file.name.endswith('.xlsx'):df = pd.read_excel(file.name)else:return "只支持 CSV 和 XLSX 檔案"# 執行檢查errors1 = check_spacing_around_dollar(df)errors2 = check_spacing_between_dollars(df)errors3 = check_numbers_surrounded_by_dollar(df)return {"第一個檢查": errors1,"第二個檢查": errors2,"第三個檢查": errors3}# Gradio 介面iface = gr.Interface(
app.py+28-49