Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,100 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
# 初始化
|
| 5 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
if
|
| 10 |
-
return "
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# 核心
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 32 |
-
|
| 33 |
-
.gradio-container { background
|
| 34 |
-
.message.user { border
|
| 35 |
-
|
|
|
|
|
|
|
| 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
|
| 46 |
-
with gr.
|
| 47 |
-
gr.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
with gr.Accordion("内核
|
| 68 |
-
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
#
|
| 75 |
if __name__ == "__main__":
|
| 76 |
demo.launch(
|
| 77 |
-
theme=gr.themes.
|
| 78 |
-
css=
|
| 79 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
+
# 初始化强力模型
|
| 6 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 7 |
|
| 8 |
+
# --- [A] 实战化:代码漏洞扫描逻辑 ---
|
| 9 |
+
def run_security_scan(code):
|
| 10 |
+
if not code or len(code) < 5:
|
| 11 |
+
return "⚠️ 请在对话框或下方输入代码以供扫描..."
|
| 12 |
+
|
| 13 |
+
findings = []
|
| 14 |
+
# 简单的正则审计模拟 (SQLi, XSS, Hardcoded Secrets)
|
| 15 |
+
if re.search(r"exec\(|eval\(", code): findings.append("🔴 发现危险函数: eval/exec (代码注入风险)")
|
| 16 |
+
if re.search(r"select.*from.*where.*=.*['\"]", code, re.I): findings.append("🟠 发现可疑 SQL 查询 (潜在 SQL 注入)")
|
| 17 |
+
if re.search(r"api_key|password|secret", code, re.I): findings.append("🟡 发现硬编码敏感词 (泄露风险)")
|
| 18 |
+
|
| 19 |
+
if not findings:
|
| 20 |
+
return "✅ 静态扫描完成:未发现明显基础漏洞。建议进行深度动态分析。"
|
| 21 |
+
return " \n".join(findings)
|
| 22 |
+
|
| 23 |
+
# --- [C] 智能处理:读取上传文件 ---
|
| 24 |
+
def process_file(file):
|
| 25 |
+
if file is None: return ""
|
| 26 |
+
try:
|
| 27 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 28 |
+
content = f.read()
|
| 29 |
+
return f"\n[已加载私有文档内容]:\n{content[:1000]}" # 限制长度防止爆 Token
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"\n[文件读取失败]: {str(e)}"
|
| 32 |
|
| 33 |
+
# --- 核心交互引擎 ---
|
| 34 |
+
def ghost_chat(message, history, system_message, file_obj):
|
| 35 |
+
# 结合文件内容
|
| 36 |
+
file_context = process_file(file_obj)
|
| 37 |
+
combined_system = f"{system_message}\n{file_context}"
|
| 38 |
+
|
| 39 |
+
messages = [{"role": "system", "content": combined_system}]
|
| 40 |
for user_msg, assistant_msg in history:
|
| 41 |
messages.append({"role": "user", "content": user_msg})
|
| 42 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 43 |
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
+
|
| 46 |
response = ""
|
| 47 |
+
# 模拟“幽灵”协议加载动画
|
| 48 |
+
yield history + [[message, "📡 连接加密节点... 注入协议..."]]
|
| 49 |
+
|
| 50 |
for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048):
|
| 51 |
token = msg_chunk.choices[0].delta.content
|
| 52 |
if token:
|
| 53 |
response += token
|
| 54 |
yield history + [[message, response]]
|
| 55 |
|
| 56 |
+
# --- [B] 视觉增强:极客黑绿 2.0 ---
|
| 57 |
+
custom_style = """
|
| 58 |
+
.gradio-container { background: #020202 !important; color: #00ff41 !important; }
|
| 59 |
+
.message.user { border: 1px solid #00ff41 !important; border-radius: 5px !important; }
|
| 60 |
+
.message.bot { background: #050505 !important; }
|
| 61 |
+
#scan-console { font-family: 'Courier New', monospace; background: #001100; border: 1px dashed #00ff41; }
|
| 62 |
+
#title-area { text-shadow: 0 0 10px #00ff41; text-align: center; }
|
| 63 |
"""
|
| 64 |
|
|
|
|
| 65 |
with gr.Blocks(fill_height=True) as demo:
|
| 66 |
+
gr.Markdown("# 💀 GHOST-PROTOCOL v7.0", elem_id="title-area")
|
|
|
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
+
# 左侧边栏:情报与工具
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
with gr.Tab("🛡️ 审计中心"):
|
| 72 |
+
code_input = gr.Textbox(label="代码片段扫描", lines=5, placeholder="粘贴���码进行快速审计...")
|
| 73 |
+
scan_btn = gr.Button("START AUDIT", variant="primary")
|
| 74 |
+
scan_res = gr.Textbox(label="扫描结果", elem_id="scan-console", interactive=False)
|
| 75 |
+
scan_btn.click(run_security_scan, [code_input], scan_res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
with gr.Tab("📂 数据注入"):
|
| 78 |
+
file_up = gr.File(label="私有知识库 (TXT/PY/MD)")
|
| 79 |
+
gr.Markdown("ℹ️ 上传后 AI 将自动获取文件上下文")
|
| 80 |
+
|
| 81 |
+
# 右侧主控台
|
| 82 |
+
with gr.Column(scale=2):
|
| 83 |
+
chatbot = gr.Chatbot(height="70vh", show_share_button=False)
|
| 84 |
with gr.Row():
|
| 85 |
+
msg = gr.Textbox(placeholder="Execute command...", scale=7, container=False)
|
| 86 |
+
send = gr.Button("SEND", scale=1)
|
| 87 |
|
| 88 |
+
with gr.Accordion("内核控制", open=False):
|
| 89 |
+
sys_set = gr.Textbox(value="你是一个顶尖的红队专家,擅长漏洞挖掘和代码审计。", label="System Kernel")
|
| 90 |
|
| 91 |
+
# 交互绑定
|
| 92 |
+
msg.submit(ghost_chat, [msg, chatbot, sys_set, file_up], [chatbot])
|
| 93 |
+
send.click(ghost_chat, [msg, chatbot, sys_set, file_up], [chatbot])
|
| 94 |
|
| 95 |
+
# 启动配置
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch(
|
| 98 |
+
theme=gr.themes.Monochrome(),
|
| 99 |
+
css=custom_style
|
| 100 |
)
|