This commit is contained in:
hailin 2025-07-27 19:32:27 +08:00
parent 91194df5d8
commit 9cb53f50f6
1 changed files with 30 additions and 9 deletions

View File

@ -115,22 +115,43 @@ def chat(
out = backend(user, samp, api_suffix)
result_q.put(out)
threading.Thread(target=worker).start()
# threading.Thread(target=worker).start()
thread = threading.Thread(target=worker, daemon=True)
yield "⏳ 正在生成中...", log_state
while True:
# ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止
if not thread.is_alive() and result_q.empty():
return # ← 新增这一行
try:
result = result_q.get(timeout=0.1)
# ★ 不论 /generate纯 str还是 /v1/completionsdict都转成 {"text": ...}
except Empty:
continue
# 统一格式
if isinstance(result, str):
result = {"text": result}
elif not isinstance(result, dict) or "text" not in result:
result = {"text": str(result)}
yield result, log_state
break
except Empty:
continue
yield result, log_state # 第一次真正把模型回复丢给前端
return # ← 把旧的 break 换成 return
# while True:
# try:
# result = result_q.get(timeout=0.1)
# # ★ 不论 /generate纯 str还是 /v1/completionsdict都转成 {"text": ...}
# if isinstance(result, str):
# result = {"text": result}
# elif not isinstance(result, dict) or "text" not in result:
# result = {"text": str(result)}
# yield result, log_state
# break
# except Empty:
# continue
# ────────────────── Gradio UI ──────────────────