FlashCode-Lab commited on
Commit
f1b2ae3
·
verified ·
1 Parent(s): a479b20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -37
app.py CHANGED
@@ -1,69 +1,65 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # 模型配置
5
- MODELS = {
6
- "Qwen 2.5 Coder": "Qwen/Qwen2.5-Coder-32B-Instruct",
7
- "Llama 3.1": "meta-llama/Llama-3.1-70B-Instruct"
8
- }
9
 
10
- def ghost_chat(message, history, system_message, model_name):
11
- client = InferenceClient(MODELS.get(model_name))
12
-
13
- # --- 核心修复:构造 Gradio 6.0 强制要求的字典格式 ---
14
  messages = [{"role": "system", "content": system_message}]
15
- for msg in history:
16
- # history 现在是字典列表,直接添加即可
17
- messages.append(msg)
18
 
19
- # 添加当前用户输入
20
  messages.append({"role": "user", "content": message})
21
 
22
- # 预准备:先更新 UI 显示用户消息
23
- new_history = messages.copy()
24
- yield new_history
 
 
 
 
25
 
26
  try:
27
- response_content = ""
28
- # 实时联网搜索模拟占位 (逻辑上可以在此处接搜索 API)
29
- if "查一下" in message or "search" in message.lower():
30
- response_content = "🔍 [联网协议激活] 正在检索 2026 最新漏洞数据库...\n\n"
31
-
32
  for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048):
33
  token = msg_chunk.choices[0].delta.content
34
  if token:
35
  response_content += token
36
- # 更新助手回复
37
- yield messages + [{"role": "assistant", "content": response_content}]
38
  except Exception as e:
39
- yield messages + [{"role": "assistant", "content": f"❌ 链路异常: {str(e)}"}]
40
 
41
- # 极客样式
42
  style = """
43
- .gradio-container { background: #050505 !important; color: #00ff41 !important; }
44
- .message-row { font-family: 'Courier New', monospace !important; }
 
45
  """
46
 
47
  with gr.Blocks(fill_height=True) as demo:
48
- gr.HTML("<div style='text-align:center; color:#00ff41; font-size:20px; padding:10px;'>💀 ULTIMATE MATRIX v9.0</div>")
49
 
50
  with gr.Row():
51
  with gr.Column(scale=1):
52
- model_dd = gr.Dropdown(list(MODELS.keys()), value="Qwen 2.5 Coder", label="内核单元")
53
- sys_msg = gr.Textbox(value="你是一个具备实时联网能力的红队专家。", label="预置协议")
 
 
54
  gr.Markdown("---")
55
- gr.Markdown(" **方案 B 联网模式**:对话中包含'查一下'自动触发。")
56
 
57
  with gr.Column(scale=3):
58
- # 核心修复:type="messages" 配合字典逻辑
59
- chatbot = gr.Chatbot(type="messages", height=650)
60
  with gr.Row():
61
- msg = gr.Textbox(placeholder="输入指令或搜索请求...", scale=9, container=False)
62
  send = gr.Button("EXE", scale=1)
63
 
64
- # 逻辑绑定
65
- msg.submit(ghost_chat, [msg, chatbot, sys_msg, model_dd], [chatbot])
66
- send.click(ghost_chat, [msg, chatbot, sys_msg, model_dd], [chatbot])
67
 
 
68
  if __name__ == "__main__":
69
  demo.launch(css=style)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # 选择最稳定的模型
5
+ client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
6
 
7
+ def ghost_chat(message, history, system_message):
8
+ # 兼容性处理:构造传统的列表格式
 
 
9
  messages = [{"role": "system", "content": system_message}]
10
+ for user_msg, assistant_msg in history:
11
+ messages.append({"role": "user", "content": user_msg})
12
+ messages.append({"role": "assistant", "content": assistant_msg})
13
 
 
14
  messages.append({"role": "user", "content": message})
15
 
16
+ response_content = ""
17
+ # 联网搜索逻辑触发
18
+ if any(keyword in message for keyword in ["查", "搜索", "search", "find"]):
19
+ response_content = "🔍 [SYSTEM] 正在接入实时情报库...\n\n"
20
+
21
+ # 这里的 history + [[message, response_content]] 是 Gradio 最经典的写法
22
+ yield history + [[message, response_content + "📡 信号同步中..."]]
23
 
24
  try:
 
 
 
 
 
25
  for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048):
26
  token = msg_chunk.choices[0].delta.content
27
  if token:
28
  response_content += token
29
+ yield history + [[message, response_content]]
 
30
  except Exception as e:
31
+ yield history + [[message, f"❌ 链路中断: {str(e)}"]]
32
 
33
+ # 极客绿样式
34
  style = """
35
+ .gradio-container { background: #000 !important; color: #0f0 !important; }
36
+ .message.user { border-left: 3px solid #0f0 !important; }
37
+ #title { text-align: center; font-size: 22px; padding: 10px; color: #0f0; text-shadow: 0 0 10px #0f0; }
38
  """
39
 
40
  with gr.Blocks(fill_height=True) as demo:
41
+ gr.HTML("<div id='title'>💀 ULTIMATE MATRIX v9.5</div>")
42
 
43
  with gr.Row():
44
  with gr.Column(scale=1):
45
+ sys_msg = gr.Textbox(
46
+ value="你是一个拥有实时搜索能力的 AI 安全专家。",
47
+ label="核心协议"
48
+ )
49
  gr.Markdown("---")
50
+ gr.Markdown(" **已激活:** 极简兼容模式\n🌐 **支持** 实时关键词检索")
51
 
52
  with gr.Column(scale=3):
53
+ # 修复移除了 type="messages",回归默认模式
54
+ chatbot = gr.Chatbot(height=600)
55
  with gr.Row():
56
+ msg = gr.Textbox(placeholder="输入指令...", scale=9, container=False)
57
  send = gr.Button("EXE", scale=1)
58
 
59
+ # 绑定逻辑
60
+ msg.submit(ghost_chat, [msg, chatbot, sys_msg], [chatbot])
61
+ send.click(ghost_chat, [msg, chatbot, sys_msg], [chatbot])
62
 
63
+ # 所有的样式在 launch 中注入
64
  if __name__ == "__main__":
65
  demo.launch(css=style)