Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
# 初始化
|
| 6 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 7 |
|
| 8 |
-
# 模拟 A:工具集成逻辑
|
| 9 |
-
def security_tool_scanner(target_type
|
| 10 |
if target_type == "Code":
|
| 11 |
-
return
|
| 12 |
-
return "🌐 [资产识别] 目标服务响应正常
|
| 13 |
|
| 14 |
-
# 核心聊天逻辑
|
| 15 |
-
def chat_engine(message, history, system_message
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
formatted_history = [{"role": "system", "content": system_message + context}]
|
| 22 |
-
for msg in history:
|
| 23 |
-
formatted_history.append(msg)
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
# B: 视觉反馈 - 模拟黑客终端的“思考”过程
|
| 28 |
-
yield history + [{"role": "assistant", "content": "SYSTEM: Accessing Kernel... Analyzing...", "metadata": {"title": "⚡ 核心注入中..."}}]
|
| 29 |
-
|
| 30 |
response = ""
|
| 31 |
-
|
|
|
|
| 32 |
token = msg_chunk.choices[0].delta.content
|
| 33 |
if token:
|
| 34 |
response += token
|
| 35 |
-
yield history + [
|
| 36 |
|
| 37 |
-
#
|
| 38 |
terminal_css = """
|
| 39 |
-
.gradio-container { background-color: #050505 !important; color: #00ff41 !important; font-family: '
|
| 40 |
.message.user { border-left: 3px solid #00ff41 !important; background: #0a1a0a !important; }
|
| 41 |
-
footer { visibility: hidden; }
|
| 42 |
#terminal-header { border-bottom: 2px solid #00ff41; padding-bottom: 10px; margin-bottom: 20px; }
|
| 43 |
-
.gr-button-primary { background: #00ff41 !important; color: black !important; border: none !important; }
|
| 44 |
"""
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
status_light = gr.HighlightedText(value=[("SYSTEM", "ONLINE"), ("VPN", "CONNECTED")], color_map={"ONLINE": "green", "CONNECTED": "blue"})
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
-
# 左栏:
|
| 54 |
with gr.Column(scale=1, min_width=300):
|
| 55 |
with gr.Group():
|
| 56 |
gr.Markdown("#### [A] 自动化审计工具")
|
| 57 |
-
tool_type = gr.Dropdown(["Code", "Network"
|
| 58 |
-
scan_btn = gr.Button("RUN SCAN"
|
| 59 |
-
scan_output = gr.Textbox(label="扫描日志", interactive=False
|
| 60 |
-
scan_btn.click(security_tool_scanner, [tool_type
|
| 61 |
|
| 62 |
with gr.Group():
|
| 63 |
gr.Markdown("#### [C] 私有知识库 (RAG)")
|
| 64 |
-
file_input = gr.File(label="上传审计报告
|
| 65 |
-
vector_status = gr.Markdown("状态:
|
| 66 |
-
file_input.change(lambda: "状态: 实时向量索引已建立 ✅", None, vector_status)
|
| 67 |
|
| 68 |
-
# 右栏:
|
| 69 |
with gr.Column(scale=3):
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
-
input_text = gr.Textbox(placeholder="root@kali:~# 输入
|
| 74 |
-
submit_btn = gr.Button("EXE", scale=1
|
| 75 |
|
| 76 |
-
with gr.Accordion("
|
| 77 |
-
sys_msg = gr.Textbox(value="你是一个
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
input_text.submit(chat_engine, [input_text, chatbot, sys_msg
|
| 81 |
-
submit_btn.click(chat_engine, [input_text, chatbot, sys_msg
|
| 82 |
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
# 初始化
|
| 5 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 6 |
|
| 7 |
+
# 模拟 A:工具集成逻辑
|
| 8 |
+
def security_tool_scanner(target_type):
|
| 9 |
if target_type == "Code":
|
| 10 |
+
return "🔍 [静态扫描] 发现 3 个潜在注入点... 状态:待确认"
|
| 11 |
+
return "🌐 [资产识别] 目标服务响应正常。"
|
| 12 |
|
| 13 |
+
# 核心聊天逻辑 (适配旧版 Chatbot 格式)
|
| 14 |
+
def chat_engine(message, history, system_message):
|
| 15 |
+
messages = [{"role": "system", "content": system_message}]
|
| 16 |
+
# 兼容处理:将历史记录转为 API 需要的格式
|
| 17 |
+
for user_msg, assistant_msg in history:
|
| 18 |
+
messages.append({"role": "user", "content": user_msg})
|
| 19 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
messages.append({"role": "user", "content": message})
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
response = ""
|
| 24 |
+
# 流式输出
|
| 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 += token
|
| 29 |
+
yield history + [[message, response]]
|
| 30 |
|
| 31 |
+
# B: 极客 UI 样式
|
| 32 |
terminal_css = """
|
| 33 |
+
.gradio-container { background-color: #050505 !important; color: #00ff41 !important; font-family: 'monospace' !important; }
|
| 34 |
.message.user { border-left: 3px solid #00ff41 !important; background: #0a1a0a !important; }
|
|
|
|
| 35 |
#terminal-header { border-bottom: 2px solid #00ff41; padding-bottom: 10px; margin-bottom: 20px; }
|
|
|
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
# --- 核心 UI 布局 ---
|
| 39 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 40 |
+
# 顶部状态栏
|
| 41 |
+
gr.Markdown("### 💀 BLACK-CORE OS v6.0 | SESSION: ACTIVE", elem_id="terminal-header")
|
|
|
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
+
# 左栏:工具与知识库
|
| 45 |
with gr.Column(scale=1, min_width=300):
|
| 46 |
with gr.Group():
|
| 47 |
gr.Markdown("#### [A] 自动化审计工具")
|
| 48 |
+
tool_type = gr.Dropdown(["Code", "Network"], label="扫描类型")
|
| 49 |
+
scan_btn = gr.Button("RUN SCAN")
|
| 50 |
+
scan_output = gr.Textbox(label="扫描日志", interactive=False)
|
| 51 |
+
scan_btn.click(security_tool_scanner, [tool_type], scan_output)
|
| 52 |
|
| 53 |
with gr.Group():
|
| 54 |
gr.Markdown("#### [C] 私有知识库 (RAG)")
|
| 55 |
+
file_input = gr.File(label="上传审计报告")
|
| 56 |
+
vector_status = gr.Markdown("状态: 待命")
|
|
|
|
| 57 |
|
| 58 |
+
# 右栏:对话区
|
| 59 |
with gr.Column(scale=3):
|
| 60 |
+
# 修复点:移除了 type="messages",回归最稳定的 Chatbot 定义
|
| 61 |
+
chatbot = gr.Chatbot(height="65vh", show_label=False)
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
+
input_text = gr.Textbox(placeholder="root@kali:~# 输入指令...", scale=8, container=False)
|
| 65 |
+
submit_btn = gr.Button("EXE", scale=1)
|
| 66 |
|
| 67 |
+
with gr.Accordion("内核配置", open=False):
|
| 68 |
+
sys_msg = gr.Textbox(value="你是一个渗透测试专家。", label="System Kernel")
|
| 69 |
|
| 70 |
+
# 逻辑绑定
|
| 71 |
+
input_text.submit(chat_engine, [input_text, chatbot, sys_msg], [chatbot])
|
| 72 |
+
submit_btn.click(chat_engine, [input_text, chatbot, sys_msg], [chatbot])
|
| 73 |
|
| 74 |
+
# --- 修复点:所有样式参数必须在 launch 中 ---
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
demo.launch(
|
| 77 |
+
theme=gr.themes.Soft(primary_hue="green"),
|
| 78 |
+
css=terminal_css
|
| 79 |
+
)
|