Spaces:
Runtime error
Runtime error
Upload app.py
Browse files183 unmodified lines18418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321429 unmodified lines244245246223247248249250183 unmodified lines if "'" in repaired and '"' not in repaired:
repaired = repaired.replace("'", '"')
return _json.loads(repaired)
def extract_model_reply(full_text, prompt):
"""
從模型完整輸出中,移除 prompt 和任何 system、assistant 等前置內容
"""
try:
# 如果模型有把 prompt 或 system 一起回顯,先找最後一次 user 提問位置
markers = ["user", "User", "使用者", prompt.strip()]
last_pos = -1
for m in markers:
pos = full_text.rfind(m)
if pos > last_pos:
last_pos = pos
# 從最後 marker 後面開始取內容
if last_pos != -1:
reply = full_text[last_pos + len(markers[-1]):]
else:
reply = full_text
# 移除多餘空白與換行
return reply.strip()
except Exception as e:
print(f"[extract_model_reply 錯誤] {e}")
return full_text.strip()
def call_llm(messages: List[dict], model: str, logs: List[str]) -> dict:
"""
保留原名稱 call_llm,但改為本地 LLaMA。29 unmodified lines print("torch.no_grad")
# 解碼生成內容後
full_text = _hf_tok.decode(out_ids[0], skip_special_tokens=True)
gen_text = full_text.replace(prompt,"",1).strip()
gen_text = extract_model_reply(full_text, prompt)
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}")app.py+25-1