This commit is contained in:
parent
d33a596dfa
commit
452a2ed902
67
meta_ui.py
67
meta_ui.py
|
|
@ -152,31 +152,64 @@ def chat(
|
||||||
# yield "⏳ 正在生成中...", log_state
|
# yield "⏳ 正在生成中...", log_state
|
||||||
|
|
||||||
|
|
||||||
while True:
|
if api_suffix == "/v1/chat/completions":
|
||||||
# ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止
|
while True:
|
||||||
if not thread.is_alive() and result_q.empty():
|
if not thread.is_alive() and result_q.empty():
|
||||||
break
|
break
|
||||||
# return # ← 新增这一行
|
try:
|
||||||
|
result = result_q.get(timeout=0.1)
|
||||||
|
except Empty:
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
if isinstance(result, str):
|
||||||
result = result_q.get(timeout=0.1)
|
result = {"text": result}
|
||||||
except Empty:
|
elif not isinstance(result, dict) or "text" not in result:
|
||||||
continue
|
result = {"text": str(result)}
|
||||||
|
|
||||||
|
history.append({"role": "assistant", "content": result["text"]})
|
||||||
|
yield result["text"], history # ✅ 显示模型输出,同时更新 history
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
while thread.is_alive():
|
||||||
|
try:
|
||||||
|
result = result_q.get(timeout=0.1)
|
||||||
|
break
|
||||||
|
except Empty:
|
||||||
|
continue
|
||||||
|
|
||||||
# 统一格式
|
|
||||||
if isinstance(result, str):
|
if isinstance(result, str):
|
||||||
result = {"text": result}
|
result = {"text": result}
|
||||||
elif not isinstance(result, dict) or "text" not in result:
|
elif not isinstance(result, dict) or "text" not in result:
|
||||||
result = {"text": str(result)}
|
result = {"text": str(result)}
|
||||||
|
|
||||||
# yield result, log_state # 第一次真正把模型回复丢给前端
|
yield result["text"], log_state # ✅ 其它接口只输出文本,不更新 history
|
||||||
if api_suffix == "/v1/chat/completions":
|
return
|
||||||
history.append({"role": "assistant", "content": result["text"]})
|
|
||||||
yield result["text"], None
|
|
||||||
else:
|
|
||||||
yield result["text"], log_state
|
|
||||||
|
|
||||||
# return # ← 把旧的 break 换成 return
|
# while True:
|
||||||
|
# # ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止
|
||||||
|
# if not thread.is_alive() and result_q.empty():
|
||||||
|
# break
|
||||||
|
# # return # ← 新增这一行
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# result = result_q.get(timeout=0.1)
|
||||||
|
# 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 # 第一次真正把模型回复丢给前端
|
||||||
|
# if api_suffix == "/v1/chat/completions":
|
||||||
|
# history.append({"role": "assistant", "content": result["text"]})
|
||||||
|
# yield result["text"], None
|
||||||
|
# else:
|
||||||
|
# yield result["text"], log_state
|
||||||
|
|
||||||
|
# return # ← 把旧的 break 换成 return
|
||||||
|
|
||||||
# ────────────────── Gradio UI ──────────────────
|
# ────────────────── Gradio UI ──────────────────
|
||||||
with gr.Blocks(title="调试界面") as demo:
|
with gr.Blocks(title="调试界面") as demo:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue