Spaces:
Runtime error
Runtime error
Upload app.py
Browse files207 unmodified lines20820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525630 unmodified lines287288289247290291292293294295296297298299252253254255300301302303304305306307308309310311312313314315207 unmodified lines except Exception as e:
print(f"[extract_model_reply 錯誤] {e}")
return full_text.strip()
# === 放在 safe_parse_json 之後:用「正則」擷取 full_text 中最後一個完整 JSON 物件 ===
try:
import regex as re2 # 第三方 regex,支援遞迴 (?R)
except Exception:
re2 = None
def extract_last_json_block(text: str) -> Optional[str]:
"""
以 regex 擷取最後一個平衡的大括號 JSON 物件:
- 優先使用第三方 `regex` 的遞迴 (?R) 來比對平衡大括號
- 若無法使用 `regex`,改用手動堆疊法做 fallback
回傳:最後一個 JSON 物件字串;若找不到回傳 None
"""
try:
s = _strip_code_fences(text)
# 1) 使用 regex (?R) 遞迴:{\n ... { ... } ... \n}
if re2 is not None:
pattern = re2.compile(r"\{(?:[^{}]|(?R))*\}", flags=re2.DOTALL)
matches = [m.group(0) for m in pattern.finditer(s)]
return matches[-1] if matches else None
# 2) 無 regex 模組 → 手動掃描平衡大括號
blocks = []
depth = 0
start = None
for i, ch in enumerate(s):
if ch == "{":
if depth == 0:
start = i
depth += 1
elif ch == "}":
if depth > 0:
depth -= 1
if depth == 0 and start is not None:
blocks.append(s[start:i+1])
start = None
return blocks[-1] if blocks else None
except Exception as e:
print(f"[JSON-EXTRACT][ERROR] {e}")
return None
def call_llm(messages: List[dict], model: str, logs: List[str]) -> dict:
"""30 unmodified lines print("torch.no_grad")
# 解碼生成內容後
full_text = _hf_tok.decode(out_ids[0], skip_special_tokens=True)
gen_text = extract_model_reply(full_text, prompt)
# ★ 使用 regex/堆疊法:從 full_text 擷取「最後一個」完整 JSON 物件
candidate = extract_last_json_block(full_text)
gen_text = candidate if candidate is not None else full_text # 若找不到就用原文(後續 safe_parse_json 仍會嘗試)
logs.append(f"[LOCAL LLM] raw_len={len(gen_text)}")
logs.append(f"[LOCAL LLM] gen_text={gen_text}")
logs.append(f"[LOCAL LLM] prompt={prompt}")
logs.append(f"[LOCAL LLM] full_text={full_text}")
# ★ 強韌解析:剝掉前置描述,只取第一個 {...}
data = safe_parse_json(full_text)
logs.append("[LOCAL LLM] JSON 解析成功")
# 強韌解析
try:
data = safe_parse_json(gen_text)
logs.append("[LOCAL LLM] JSON 解析成功")
return data
except Exception as jerr:
logs.append(f"[LOCAL LLM] JSON 解析失敗:{jerr}")
return {
"符合情況": "部分符合",
"原因": [f"模型輸出非合法 JSON:{str(jerr)}"],
"改進建議": ["請調整提示詞,要求嚴格輸出 JSON(雙引號、無註解、無多餘文字)。"],
"規則逐點檢核": []
}
return data
except Exception as e:
logs.append(f"[LOCAL LLM] JSON 解析失敗:{e}")app.py+62-5