File size: 2,396 Bytes
0176f7d 9de93da 0176f7d 9de93da 0176f7d 9de93da 0176f7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | # app.py
import os
import gradio as gr
from pathlib import Path
# ======================
# 配置区(按需修改路径)
# ======================
# 根目录:你的所有数据放在这个目录下(相对于 app.py)
DATA_ROOT = Path("../outputs") # 例如:data/exp1/chart1.png, data/results/metrics.jsonl
# 支持的图片格式
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".svg"}
# ======================
# 辅助函数
# ======================
def find_files(root: Path, extensions: set):
"""递归查找指定后缀的文件"""
files = []
for ext in extensions:
files.extend(root.rglob(f"*{ext}"))
return sorted(files)
def get_relative_path(file: Path):
"""获取相对于 DATA_ROOT 的路径,用于显示"""
try:
return str(file.relative_to(DATA_ROOT))
except ValueError:
return str(file)
# ======================
# Gradio 应用
# ======================
with gr.Blocks(title="Red Team Experiment Dashboard") as demo:
gr.Markdown("## 🔴 Red Team Attack Results Dashboard")
gr.Markdown(f"All charts and raw data from `{DATA_ROOT}` directory.")
# === 图表展示区 ===
image_files = find_files(DATA_ROOT, IMAGE_EXTENSIONS)
if image_files:
gr.Markdown("### 📊 Charts")
# 按父文件夹分组
folders = {}
for img in image_files:
folder = img.parent
if folder not in folders:
folders[folder] = []
folders[folder].append(img)
for folder, imgs in folders.items():
folder_name = get_relative_path(folder)
gr.Markdown(f"#### Folder: `{folder_name}/`")
with gr.Row():
for img in imgs:
label = get_relative_path(img)
gr.Image(str(img), label=label, interactive=False, height=300)
else:
gr.Markdown("### 📊 Charts\n> No images found.")
# === JSONL 下载区 ===
jsonl_files = find_files(DATA_ROOT, {".jsonl"})
if jsonl_files:
gr.Markdown("### 📥 Raw Data (JSONL)")
for jf in jsonl_files:
rel_path = get_relative_path(jf)
gr.File(value=str(jf), label=rel_path, interactive=False)
else:
gr.Markdown("### 📥 Raw Data (JSONL)\n> No .jsonl files found.")
demo.launch() |