Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# 模型
|
| 5 |
-
|
| 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
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# --- 核心修复:构造 Gradio 6.0 强制要求的字典格式 ---
|
| 14 |
messages = [{"role": "system", "content": system_message}]
|
| 15 |
-
for
|
| 16 |
-
|
| 17 |
-
messages.append(
|
| 18 |
|
| 19 |
-
# 添加当前用户输入
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 40 |
|
| 41 |
-
# 极客样式
|
| 42 |
style = """
|
| 43 |
-
.gradio-container { background: #
|
| 44 |
-
.message
|
|
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
with gr.Blocks(fill_height=True) as demo:
|
| 48 |
-
gr.HTML("<div
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column(scale=1):
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
gr.Markdown("---")
|
| 55 |
-
gr.Markdown("
|
| 56 |
|
| 57 |
with gr.Column(scale=3):
|
| 58 |
-
#
|
| 59 |
-
chatbot = gr.Chatbot(
|
| 60 |
with gr.Row():
|
| 61 |
-
msg = gr.Textbox(placeholder="输入指令
|
| 62 |
send = gr.Button("EXE", scale=1)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
msg.submit(ghost_chat, [msg, chatbot, sys_msg
|
| 66 |
-
send.click(ghost_chat, [msg, chatbot, sys_msg
|
| 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)
|