FlashCode-Lab commited on
Commit
6d3c4ea
·
verified ·
1 Parent(s): 12ae7dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -48
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:工具集成逻辑 (Vulnerability Scanner)
9
- def security_tool_scanner(target_type, content):
10
  if target_type == "Code":
11
- return f"🔍 [静态扫描] 发现 {len(content)//50} 个潜在注入点... 状态:待确认"
12
- return "🌐 [资产识别] 目标服务响应正常,检测到 OpenSSH 8.2..."
13
 
14
- # 核心聊天逻辑
15
- def chat_engine(message, history, system_message, file_content):
16
- # C: RAG 逻辑处理 - 如果有上传文件,将其加入上下文
17
- context = ""
18
- if file_content is not None:
19
- context = f"\n[参考私有知识库内容]: {file_content[:500]}..." # 简单模拟读取
20
-
21
- formatted_history = [{"role": "system", "content": system_message + context}]
22
- for msg in history:
23
- formatted_history.append(msg)
24
 
25
- formatted_history.append({"role": "user", "content": message})
26
 
27
- # B: 视觉反馈 - 模拟黑客终端的“思考”过程
28
- yield history + [{"role": "assistant", "content": "SYSTEM: Accessing Kernel... Analyzing...", "metadata": {"title": "⚡ 核心注入中..."}}]
29
-
30
  response = ""
31
- for msg_chunk in client.chat_completion(formatted_history, stream=True, max_tokens=2048):
 
32
  token = msg_chunk.choices[0].delta.content
33
  if token:
34
  response += token
35
- yield history + [{"role": "user", "content": message}, {"role": "assistant", "content": response}]
36
 
37
- # --- B: 极客 UI 样式 (Kali Green 风格) ---
38
  terminal_css = """
39
- .gradio-container { background-color: #050505 !important; color: #00ff41 !important; font-family: 'Courier New', monospace !important; }
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
- with gr.Blocks(fill_height=True, css=terminal_css) as demo:
47
- # 头部状态栏
48
- with gr.Row(elem_id="terminal-header"):
49
- gr.Markdown("### 💀 BLACK-CORE OS v6.0 | SESSION: ACTIVE | ENCRYPTION: AES-256")
50
- status_light = gr.HighlightedText(value=[("SYSTEM", "ONLINE"), ("VPN", "CONNECTED")], color_map={"ONLINE": "green", "CONNECTED": "blue"})
51
 
52
  with gr.Row():
53
- # 左栏:A+C 工具
54
  with gr.Column(scale=1, min_width=300):
55
  with gr.Group():
56
  gr.Markdown("#### [A] 自动化审计工具")
57
- tool_type = gr.Dropdown(["Code", "Network", "Social"], label="扫描类型")
58
- scan_btn = gr.Button("RUN SCAN", variant="primary")
59
- scan_output = gr.Textbox(label="扫描日志", interactive=False, placeholder="等待指令...")
60
- scan_btn.click(security_tool_scanner, [tool_type, scan_output], scan_output)
61
 
62
  with gr.Group():
63
  gr.Markdown("#### [C] 私有知识库 (RAG)")
64
- file_input = gr.File(label="上传审计报告/文档", file_types=[".txt", ".pdf", ".py"])
65
- vector_status = gr.Markdown("状态: 知识库未挂载")
66
- file_input.change(lambda: "状态: 实时向量索引已建立 ✅", None, vector_status)
67
 
68
- # 右栏:核心交互
69
  with gr.Column(scale=3):
70
- chatbot = gr.Chatbot(type="messages", height="65vh", show_label=False)
 
71
 
72
  with gr.Row():
73
- input_text = gr.Textbox(placeholder="root@kali:~# 输入渗透指令...", scale=8, container=False)
74
- submit_btn = gr.Button("EXE", scale=1, variant="primary")
75
 
76
- with gr.Accordion("B: 系统内核配置", open=False):
77
- sys_msg = gr.Textbox(value="你是一个拥有最高权限的黑盒测试专家。", label="System Kernel")
78
 
79
- # 绑定逻辑
80
- input_text.submit(chat_engine, [input_text, chatbot, sys_msg, file_input], [chatbot])
81
- submit_btn.click(chat_engine, [input_text, chatbot, sys_msg, file_input], [chatbot])
82
 
 
83
  if __name__ == "__main__":
84
- demo.launch(ssr=True)
 
 
 
 
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
+ )