shihman commited on
Commit
98421b8
·
verified ·
1 Parent(s): a9d6e69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -240,24 +240,24 @@ def check_csv_and_run(file, key):
240
  required_columns = {"Question", "Context", "Answer"}
241
  actual_columns = set(df.columns)
242
 
243
- # if actual_columns != required_columns:
244
- # return pd.DataFrame([{"錯誤訊息": f"欄位錯誤:應包含欄位 {required_columns},實際為 {actual_columns}"}]), None
245
 
246
- # if df.shape[0] == 0:
247
- # return pd.DataFrame([{"錯誤訊息": "檔案中沒有資料列!"}]), None
248
 
249
- # invalid_rows = df[df["Question"].notnull() & (df["Answer"].isnull() | df["Context"].isnull())]
250
- # if len(invalid_rows) > 0:
251
- # missing_questions = "\n".join(f"- {q}" for q in invalid_rows["Question"].tolist())
252
- # return pd.DataFrame([{"錯誤訊息": f"發現 {len(invalid_rows)} 筆資料中 Answer 或 Context 為空:\n{missing_questions}"}]), None
253
 
254
- # # check eval context
255
- # try:
256
- # for i, val in df["Context"].dropna().items():
257
- # if not isinstance(eval(val), list):
258
- # return pd.DataFrame([{"錯誤訊息": f"第 {i + 1} 筆 Context 欄格式錯誤,請確認其內容應為 list"}]), None
259
- # except Exception as e:
260
- # return pd.DataFrame([{"錯誤訊息": f"Context 欄格式解析錯誤,請確認其為有效的 list 格式,例如 ['A', 'B']:{str(e)}"}]), None
261
 
262
  # 若上傳之待評估檔案無錯誤,執行評估
263
  try:
 
240
  required_columns = {"Question", "Context", "Answer"}
241
  actual_columns = set(df.columns)
242
 
243
+ if actual_columns != required_columns:
244
+ return pd.DataFrame([{"錯誤訊息": f"欄位錯誤:應包含欄位 {required_columns},實際為 {actual_columns}"}]), None
245
 
246
+ if df.shape[0] == 0:
247
+ return pd.DataFrame([{"錯誤訊息": "檔案中沒有資料列!"}]), None
248
 
249
+ invalid_rows = df[df["Question"].notnull() & (df["Answer"].isnull() | df["Context"].isnull())]
250
+ if len(invalid_rows) > 0:
251
+ missing_questions = "\n".join(f"- {q}" for q in invalid_rows["Question"].tolist())
252
+ return pd.DataFrame([{"錯誤訊息": f"發現 {len(invalid_rows)} 筆資料中 Answer 或 Context 為空:\n{missing_questions}"}]), None
253
 
254
+ # check eval context
255
+ try:
256
+ for i, val in df["Context"].dropna().items():
257
+ if not isinstance(eval(val), list):
258
+ return pd.DataFrame([{"錯誤訊息": f"第 {i + 1} 筆 Context 欄格式錯誤,請確認其內容應為 list"}]), None
259
+ except Exception as e:
260
+ return pd.DataFrame([{"錯誤訊息": f"Context 欄格式解析錯誤,請確認其為有效的 list 格式,例如 ['A', 'B']:{str(e)}"}]), None
261
 
262
  # 若上傳之待評估檔案無錯誤,執行評估
263
  try: