From 452a2ed90239445b33ecd48b50c850db95051d2b Mon Sep 17 00:00:00 2001 From: hailin Date: Fri, 1 Aug 2025 11:30:15 +0800 Subject: [PATCH] . --- meta_ui.py | 67 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/meta_ui.py b/meta_ui.py index 00d3156..1e4e7c8 100644 --- a/meta_ui.py +++ b/meta_ui.py @@ -152,31 +152,64 @@ def chat( # yield "⏳ 正在生成中...", log_state - while True: - # ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止 - if not thread.is_alive() and result_q.empty(): - break - # return # ← 新增这一行 + if api_suffix == "/v1/chat/completions": + while True: + if not thread.is_alive() and result_q.empty(): + break + try: + result = result_q.get(timeout=0.1) + except Empty: + continue - 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)} + + 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): 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 + yield result["text"], log_state # ✅ 其它接口只输出文本,不更新 history + return + + # while True: + # # ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止 + # if not thread.is_alive() and result_q.empty(): + # break + # # return # ← 新增这一行 - # return # ← 把旧的 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 ────────────────── with gr.Blocks(title="调试界面") as demo: