import gradio as gr from huggingface_hub import InferenceClient # 选择最稳定的模型 client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct") def ghost_chat(message, history, system_message): # 兼容性处理:构造传统的列表格式 messages = [{"role": "system", "content": system_message}] for user_msg, assistant_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) response_content = "" # 联网搜索逻辑触发 if any(keyword in message for keyword in ["查", "搜索", "search", "find"]): response_content = "🔍 [SYSTEM] 正在接入实时情报库...\n\n" # 这里的 history + [[message, response_content]] 是 Gradio 最经典的写法 yield history + [[message, response_content + "📡 信号同步中..."]] try: for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048): token = msg_chunk.choices[0].delta.content if token: response_content += token yield history + [[message, response_content]] except Exception as e: yield history + [[message, f"❌ 链路中断: {str(e)}"]] # 极客绿样式 style = """ .gradio-container { background: #000 !important; color: #0f0 !important; } .message.user { border-left: 3px solid #0f0 !important; } #title { text-align: center; font-size: 22px; padding: 10px; color: #0f0; text-shadow: 0 0 10px #0f0; } """ with gr.Blocks(fill_height=True) as demo: gr.HTML("