Spaces:
Sleeping
Sleeping
Phase 3:Bug3附帶——上傳驗證強化(檔名字首/每組至少1檔/Metadata群組數一致性)
Browse files- run_format_check 新增檔名字首合法性檢查(情境1/2僅接受A/B,情境3/4
接受A/B/C)與每組至少1個檔案的完整性檢查(A、B為必要,C視情境選填)
- on_metadata_confirm 新增群組數量與實際上傳分組數量的一致性檢查
(僅情境3/4會顯示此選項),時序上無法併入格式驗證,另開第二檢查點
三項皆已用 Playwright 實測:情境1漏傳B組、字首不符(C_xx.txt)、
情境3群組數選錯,皆正確攔下並提示;修正後皆能正常通過進入下一步。
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1493 unmodified lines149414951496149714981499150015011502150315041505150615071508150915107 unmodified lines15181519152015211522152315241834 unmodified lines3359336033613362336333641493 unmodified linesdef run_format_check(state: dict):"""檢查所有上傳檔案是否為 ckip_segmentation_sumin 產生的格式。通過 → CALCULATING;不通過 → UPLOAD_WAIT(要求重新上傳)。返回 (reply_text, new_state, has_errors)"""failed = {}for fp in state["uploaded_files"]:ok, reason = validate_ckip_format(Path(fp))if not ok:failed[Path(fp).name] = reasonif not failed:state["state"] = "CALCULATING"state["cleaned_files"] = state["uploaded_files"]sys_p = (7 unmodified linesmsg = "[OK] 所有檔案均符合 ckip_segmentation_sumin 斷詞格式,即將開始計算文本指標……"return msg, state, False# 有檔案不符fail_list = "\n".join([f"- **{fn}**:{reason}" for fn, reason in failed.items()])sys_p = ("你是重譯文本分析系統的工作程序 Agent,請用繁體中文告知使用者:"1834 unmodified linesif state is None:yield chat_history, log_history, state, gr.update(), _todo_update(state), gr.update()returnstate["user_metadata"] = _compile_metadata(state, title, author, lang, pubyear,version, ngroups, method, group_df, indiv_df)log_history = _log_append(log_history, "✅ 已記錄語料 Metadata,開始分析")1493 unmodified lines1494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415357 unmodified lines15431544154515461547154815491834 unmodified lines33843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734081493 unmodified linesdef run_format_check(state: dict):"""檢查所有上傳檔案是否為 ckip_segmentation_sumin 產生的格式、檔名字首是否合法、以及每組是否至少各有1個檔案。通過 → CALCULATING;不通過 → UPLOAD_WAIT(要求重新上傳)。返回 (reply_text, new_state, has_errors)"""scenario = state.get("scenario", "")# 情境1/2固定二群(A/B)、情境3/4可二群或三群(A/B/C),C是否用到要等# Metadata表單才知道,所以這裡的「允許字首」比「必要字首」寬鬆一點allowed_prefixes = {"A", "B", "C"} if scenario in ("3", "4") else {"A", "B"}required_prefixes = {"A", "B"}failed = {}for fp in state["uploaded_files"]:name = Path(fp).nameprefix = name[0].upper() if name else ""if prefix not in allowed_prefixes:allowed_str = "、".join(f"{p}_xx.txt" for p in sorted(allowed_prefixes))failed[name] = f"檔名字首不符——本情境僅接受 {allowed_str} 格式"continueok, reason = validate_ckip_format(Path(fp))if not ok:failed[name] = reasonif not failed:present_prefixes = {Path(fp).name[0].upper() for fp in state["uploaded_files"]}missing = required_prefixes - present_prefixesif missing:present_str = "、".join(sorted(present_prefixes)) or "無"missing_str = "、".join(sorted(missing))msg = (f"[WARNING] 請確認 {'、'.join(sorted(required_prefixes))} 兩組皆已上傳,"f"目前偵測到:{present_str},缺少:{missing_str}。")state["state"] = "UPLOAD_WAIT"return msg, state, Truestate["state"] = "CALCULATING"state["cleaned_files"] = state["uploaded_files"]sys_p = (7 unmodified linesmsg = "[OK] 所有檔案均符合 ckip_segmentation_sumin 斷詞格式,即將開始計算文本指標……"return msg, state, False# 有檔案不符(格式或字首)fail_list = "\n".join([f"- **{fn}**:{reason}" for fn, reason in failed.items()])sys_p = ("你是重譯文本分析系統的工作程序 Agent,請用繁體中文告知使用者:"1834 unmodified linesif state is None:yield chat_history, log_history, state, gr.update(), _todo_update(state), gr.update()return# 群組數量跟實際上傳分組是否一致:只有情境3/4才會顯示這個選項,# 情境1/2固定二群沒有歧義不用檢查。時序上這個檢查沒辦法併進# run_format_check(使用者選二群/三群是在格式驗證通過之後才填),# 所以在這裡另開一道檢查點,不一致就擋下、留在表單讓使用者確認if state.get("scenario") in ("3", "4"):actual_prefixes = {Path(fp).name[0].upper() for fp in state.get("cleaned_files", [])}expected_count = 3 if ngroups == "三群" else 2if len(actual_prefixes) != expected_count:warn_msg = (f"[WARNING] 您選擇「{ngroups}」,但實際上傳的檔案偵測到 "f"{len(actual_prefixes)} 組({'、'.join(sorted(actual_prefixes)) or '無'}),""請確認群組數量與上傳檔案是否一致後再送出。")chat_history = chat_history + [_assistant_msg(warn_msg)]log_history = _log_append(log_history, "⚠️ 群組數量與上傳檔案不一致")yield chat_history, log_history, state, gr.update(visible=True), _todo_update(state), gr.update()returnstate["user_metadata"] = _compile_metadata(state, title, author, lang, pubyear,version, ngroups, method, group_df, indiv_df)log_history = _log_append(log_history, "✅ 已記錄語料 Metadata,開始分析")
app_agent.py+47-3