Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ================================================================
|
| 2 |
-
# 教育大模型MIA攻防研究 - Gradio演示系统 v6.
|
| 3 |
-
# 整合了算法流程图 + 伪代码 + 详尽数据分析 + 完整结论
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
@@ -81,6 +81,9 @@ COLORS = {
|
|
| 81 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 82 |
}
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
def apply_light_style(fig, ax_or_axes):
|
| 85 |
fig.patch.set_facecolor(COLORS['bg'])
|
| 86 |
axes = ax_or_axes if hasattr(ax_or_axes, '__iter__') else [ax_or_axes]
|
|
@@ -215,20 +218,70 @@ def fig_auc_bar():
|
|
| 215 |
ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10, loc='upper right'); plt.tight_layout()
|
| 216 |
return fig
|
| 217 |
|
| 218 |
-
def
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
N = len(
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
return fig
|
| 233 |
|
| 234 |
def fig_loss_dist():
|
|
@@ -615,7 +668,6 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 615 |
d_a = gr.Textbox(label="💡 标准回答 (Ground Truth)", lines=6, interactive=False)
|
| 616 |
d_btn.click(cb_sample, [d_src], [d_meta, d_q, d_a])
|
| 617 |
|
| 618 |
-
# ===== 新增:算法原理 Tab =====
|
| 619 |
with gr.Tab("🧠 算法原理"):
|
| 620 |
gr.Markdown("## 算法流程图与伪代码")
|
| 621 |
|
|
@@ -666,8 +718,26 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 666 |
gr.Markdown("## 🔍 多维度攻防效果对比分析")
|
| 667 |
gr.Markdown(f"### 1️⃣ 攻击成功率全景对比 (AUC)\n\n> 柱子越短 = AUC越低 = 防御越有效。基线AUC={bl_auc:.4f},标签平滑最低降至{gm('smooth_eps_0.2','auc'):.4f},输出扰动最低降至{gm('perturbation_0.03','auc'):.4f}。")
|
| 668 |
gr.Plot(value=fig_auc_bar())
|
| 669 |
-
|
| 670 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
gr.Markdown("### 3️⃣ ROC曲线对比\n\n> 曲线越贴近对角线=攻击越接近随机猜测=防御越有效。左图标签平滑,右图输出扰动。")
|
| 672 |
gr.Plot(value=fig_roc_curves())
|
| 673 |
gr.Markdown(f"### 4️⃣ 低误报率下的攻击能力\n\n> 基线 TPR@5%FPR={gm('baseline','tpr_at_5fpr'):.4f},防御后显著下降。这是衡量攻击危害的最严格指标。")
|
|
@@ -682,7 +752,6 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 682 |
with gr.Accordion("📉 Loss分布直方图(输出扰动 6组)", open=False):
|
| 683 |
gr.Plot(value=fig_perturb_dist())
|
| 684 |
|
| 685 |
-
# 每个模型详尽分析
|
| 686 |
with gr.Accordion("📖 每个模型/参数详细分析", open=False):
|
| 687 |
detail_md = "## 逐一详细分析\n\n"
|
| 688 |
detail_md += f"""\
|
|
@@ -751,7 +820,6 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 751 |
e_b.click(cb_eval, [e_m], [e_r])
|
| 752 |
|
| 753 |
with gr.Tab("📝 研究结论"):
|
| 754 |
-
# 融入了新提供的详细结论
|
| 755 |
gr.Markdown(f"""\
|
| 756 |
## 核心研究发现
|
| 757 |
|
|
@@ -794,4 +862,4 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 794 |
|
| 795 |
""")
|
| 796 |
|
| 797 |
-
demo.launch(theme=gr.themes.Soft(), css=CSS)
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
+
# 教育大模型MIA攻防研究 - Gradio演示系统 v6.1 Final (苹果风)
|
| 3 |
+
# 整合了双雷达图 + 算法流程图 + 伪代码 + 详尽数据分析 + 完整结论
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 81 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 82 |
}
|
| 83 |
|
| 84 |
+
# 图表宽度配置 (为了适配双雷达图)
|
| 85 |
+
CHART_W = 14
|
| 86 |
+
|
| 87 |
def apply_light_style(fig, ax_or_axes):
|
| 88 |
fig.patch.set_facecolor(COLORS['bg'])
|
| 89 |
axes = ax_or_axes if hasattr(ax_or_axes, '__iter__') else [ax_or_axes]
|
|
|
|
| 218 |
ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10, loc='upper right'); plt.tight_layout()
|
| 219 |
return fig
|
| 220 |
|
| 221 |
+
def fig_radar():
|
| 222 |
+
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 223 |
+
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1',
|
| 224 |
+
'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 225 |
+
N = len(ms)
|
| 226 |
+
ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 227 |
+
|
| 228 |
+
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7),
|
| 229 |
+
subplot_kw=dict(polar=True))
|
| 230 |
+
fig.patch.set_facecolor('white')
|
| 231 |
+
|
| 232 |
+
# --- 左图: 5个标签平滑模型 ---
|
| 233 |
+
ls_cfgs = [
|
| 234 |
+
("Baseline", "baseline", '#F04438'),
|
| 235 |
+
("LS(e=0.02)", "smooth_eps_0.02", '#B2DDFF'),
|
| 236 |
+
("LS(e=0.05)", "smooth_eps_0.05", '#84CAFF'),
|
| 237 |
+
("LS(e=0.1)", "smooth_eps_0.1", '#2E90FA'),
|
| 238 |
+
("LS(e=0.2)", "smooth_eps_0.2", '#7A5AF8'),
|
| 239 |
+
]
|
| 240 |
+
|
| 241 |
+
# --- 右图: Baseline + 6个输出扰动 ---
|
| 242 |
+
op_cfgs = [
|
| 243 |
+
("Baseline", "baseline", '#F04438'),
|
| 244 |
+
("OP(s=0.005)", "perturbation_0.005", '#A6F4C5'),
|
| 245 |
+
("OP(s=0.01)", "perturbation_0.01", '#6CE9A6'),
|
| 246 |
+
("OP(s=0.015)", "perturbation_0.015", '#32D583'),
|
| 247 |
+
("OP(s=0.02)", "perturbation_0.02", '#12B76A'),
|
| 248 |
+
("OP(s=0.025)", "perturbation_0.025", '#039855'),
|
| 249 |
+
("OP(s=0.03)", "perturbation_0.03", '#027A48'),
|
| 250 |
+
]
|
| 251 |
+
|
| 252 |
+
for ax_idx, (ax, cfgs, title) in enumerate([
|
| 253 |
+
(axes[0], ls_cfgs, 'Label Smoothing (5 models)'),
|
| 254 |
+
(axes[1], op_cfgs, 'Output Perturbation (7 configs)')
|
| 255 |
+
]):
|
| 256 |
+
ax.set_facecolor('white')
|
| 257 |
+
|
| 258 |
+
# 计算归一化用的最大值(基于当前子图的配置)
|
| 259 |
+
mx = []
|
| 260 |
+
for i, m_key in enumerate(mk):
|
| 261 |
+
val_max = max(gm(k, m_key) for _, k, _ in cfgs)
|
| 262 |
+
mx.append(val_max if val_max > 0 else 1)
|
| 263 |
+
|
| 264 |
+
for nm, ky, cl in cfgs:
|
| 265 |
+
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]
|
| 266 |
+
v += [v[0]] # 闭合
|
| 267 |
+
lw = 2.8 if ky == 'baseline' else 1.8
|
| 268 |
+
alpha_fill = 0.10 if ky == 'baseline' else 0.04
|
| 269 |
+
ax.plot(ag, v, 'o-', lw=lw, label=nm, color=cl, ms=5,
|
| 270 |
+
alpha=0.95 if ky == 'baseline' else 0.85)
|
| 271 |
+
ax.fill(ag, v, alpha=alpha_fill, color=cl)
|
| 272 |
+
|
| 273 |
+
ax.set_xticks(ag[:-1])
|
| 274 |
+
ax.set_xticklabels(ms, fontsize=9, color=COLORS['text'])
|
| 275 |
+
ax.set_yticklabels([])
|
| 276 |
+
ax.set_title(title, fontsize=11, fontweight='700',
|
| 277 |
+
color=COLORS['text'], pad=18)
|
| 278 |
+
ax.legend(loc='upper right',
|
| 279 |
+
bbox_to_anchor=(1.35 if ax_idx == 1 else 1.30, 1.12),
|
| 280 |
+
fontsize=8, framealpha=0.9, edgecolor=COLORS['grid'])
|
| 281 |
+
ax.spines['polar'].set_color(COLORS['grid'])
|
| 282 |
+
ax.grid(color=COLORS['grid'], alpha=0.5)
|
| 283 |
+
|
| 284 |
+
plt.tight_layout()
|
| 285 |
return fig
|
| 286 |
|
| 287 |
def fig_loss_dist():
|
|
|
|
| 668 |
d_a = gr.Textbox(label="💡 标准回答 (Ground Truth)", lines=6, interactive=False)
|
| 669 |
d_btn.click(cb_sample, [d_src], [d_meta, d_q, d_a])
|
| 670 |
|
|
|
|
| 671 |
with gr.Tab("🧠 算法原理"):
|
| 672 |
gr.Markdown("## 算法流程图与伪代码")
|
| 673 |
|
|
|
|
| 718 |
gr.Markdown("## 🔍 多维度攻防效果对比分析")
|
| 719 |
gr.Markdown(f"### 1️⃣ 攻击成功率全景对比 (AUC)\n\n> 柱子越短 = AUC越低 = 防御越有效。基线AUC={bl_auc:.4f},标签平滑最低降至{gm('smooth_eps_0.2','auc'):.4f},输出扰动最低降至{gm('perturbation_0.03','auc'):.4f}。")
|
| 720 |
gr.Plot(value=fig_auc_bar())
|
| 721 |
+
|
| 722 |
+
# --- 整合双雷达图及配套讲解文本 ---
|
| 723 |
+
gr.Markdown(f"""\
|
| 724 |
+
### 2️⃣ 多指标雷达图对比(全部11组实验)
|
| 725 |
+
|
| 726 |
+
> **左图:标签平滑系列5个模型**
|
| 727 |
+
> - 红色(Baseline)面积最大 = 攻击全面有效
|
| 728 |
+
> - 随着ε从0.02−0.2增大,雷达面积逐步缩小 = 防御逐步增强
|
| 729 |
+
> - 特别注意 TPR@1%FPR 和 LossGap 两个轴,缩小最显著
|
| 730 |
+
>
|
| 731 |
+
> **右图:输出扰动系列7个配置**
|
| 732 |
+
> - 红色(Baseline)同样是最大的
|
| 733 |
+
> - 随着σ从0.005−0.03增大,绿色系雷达逐步缩小
|
| 734 |
+
> - OP在LossGap和TPR@5%维度上降幅尤其明显
|
| 735 |
+
>
|
| 736 |
+
> **结论:** 两种防御均在所有维度上全面压制攻击能力,不是只降低了某一个指标。
|
| 737 |
+
""")
|
| 738 |
+
gr.Plot(value=fig_radar())
|
| 739 |
+
# ---------------------------------
|
| 740 |
+
|
| 741 |
gr.Markdown("### 3️⃣ ROC曲线对比\n\n> 曲线越贴近对角线=攻击越接近随机猜测=防御越有效。左图标签平滑,右图输出扰动。")
|
| 742 |
gr.Plot(value=fig_roc_curves())
|
| 743 |
gr.Markdown(f"### 4️⃣ 低误报率下的攻击能力\n\n> 基线 TPR@5%FPR={gm('baseline','tpr_at_5fpr'):.4f},防御后显著下降。这是衡量攻击危害的最严格指标。")
|
|
|
|
| 752 |
with gr.Accordion("📉 Loss分布直方图(输出扰动 6组)", open=False):
|
| 753 |
gr.Plot(value=fig_perturb_dist())
|
| 754 |
|
|
|
|
| 755 |
with gr.Accordion("📖 每个模型/参数详细分析", open=False):
|
| 756 |
detail_md = "## 逐一详细分析\n\n"
|
| 757 |
detail_md += f"""\
|
|
|
|
| 820 |
e_b.click(cb_eval, [e_m], [e_r])
|
| 821 |
|
| 822 |
with gr.Tab("📝 研究结论"):
|
|
|
|
| 823 |
gr.Markdown(f"""\
|
| 824 |
## 核心研究发现
|
| 825 |
|
|
|
|
| 862 |
|
| 863 |
""")
|
| 864 |
|
| 865 |
+
demo.launch(theme=gr.themes.Soft(), css=CSS)
|