refactor(报告): 图片改为独立JPG文件引用,不再base64内嵌
- 测试结果图(258张)和原图(3张)以JPG存入 static/bench/{matrix,hairstyle,orig}/
- 报告HTML改为 <img src> 引用相对路径,体积从 ~45MB 降至 ~30KB
- 报告和图片均部署在 static/ 下,可通过URL直接访问
This commit is contained in:
+19
-26
@@ -23,36 +23,29 @@ MODEL_LABEL = {
|
||||
MODEL_ORDER = ["4b-fp8", "9b-Q4", "9b-Q5", "9b-fp8"]
|
||||
|
||||
|
||||
def img_b64(path, max_w=320, quality=72):
|
||||
"""读图片,缩放到 max_w 宽、JPEG 降质后转 base64 data URI(压缩体积便于入 git)。"""
|
||||
if not path or not os.path.isfile(path):
|
||||
def img_src(path):
|
||||
"""把绝对路径转成报告里的相对 URL(报告在 static/,图片在 static/bench/)。"""
|
||||
if not path:
|
||||
return None
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
im = Image.open(path).convert("RGB")
|
||||
if im.width > max_w:
|
||||
nh = round(im.height * max_w / im.width)
|
||||
im = im.resize((max_w, nh), Image.LANCZOS)
|
||||
buf = BytesIO()
|
||||
im.save(buf, format="JPEG", quality=quality, optimize=True)
|
||||
b = base64.b64encode(buf.getvalue()).decode()
|
||||
return f"data:image/jpeg;base64,{b}"
|
||||
p = str(path)
|
||||
# benchmark_out/matrix/xxx.jpg -> bench/matrix/xxx.jpg
|
||||
if "benchmark_out/matrix/" in p:
|
||||
return "bench/matrix/" + os.path.basename(p)
|
||||
if "benchmark_out/hairstyle/" in p:
|
||||
return "bench/hairstyle/" + os.path.basename(p)
|
||||
return None
|
||||
|
||||
|
||||
def thumb(data_uri, alt="", cls=""):
|
||||
if not data_uri:
|
||||
def thumb(src, alt="", cls=""):
|
||||
if not src:
|
||||
return f'<div class="na {cls}">⚠ 失败</div>'
|
||||
return f'<img class="{cls}" src="{data_uri}" alt="{alt}" loading="lazy">'
|
||||
return f'<img class="{cls}" src="{src}" alt="{alt}" loading="lazy">'
|
||||
|
||||
|
||||
def main():
|
||||
data = json.load(open(RESULTS, encoding="utf-8"))
|
||||
# 预读原图 base64(3 张图)
|
||||
orig_b64 = {}
|
||||
for c in data:
|
||||
ip = c["img_path"]
|
||||
if c["img"] not in orig_b64:
|
||||
orig_b64[c["img"]] = img_b64(ip, 200)
|
||||
# 原图相对路径映射(图片在 static/bench/orig/)
|
||||
ORIG_SRC = {"asdf": "bench/orig/asdf.jpg", "qwer": "bench/orig/qwer.jpg", "girl5": "bench/orig/girl5.jpg"}
|
||||
|
||||
# 统计:每个模型的平均耗时、平均峰值显存
|
||||
stats = {}
|
||||
@@ -76,11 +69,11 @@ def main():
|
||||
# 3 次结果图
|
||||
run_cells = []
|
||||
for i, r in enumerate(c["runs"]):
|
||||
uri = img_b64(r["grown_path"]) if not r["error"] else None
|
||||
if uri:
|
||||
src = img_src(r["grown_path"]) if not r["error"] else None
|
||||
if src:
|
||||
run_cells.append(
|
||||
f'<div class="run-cell"><div class="run-label">第{i+1}次 · {r["elapsed"]:.1f}s</div>'
|
||||
f'{thumb(uri, f"r{i+1}", "result-img")}</div>'
|
||||
f'{thumb(src, f"r{i+1}", "result-img")}</div>'
|
||||
)
|
||||
else:
|
||||
run_cells.append(
|
||||
@@ -88,7 +81,7 @@ def main():
|
||||
f'<div class="na">⚠ {r["error"][:30] if r["error"] else ""}</div></div>'
|
||||
)
|
||||
|
||||
orig = orig_b64.get(c["img"])
|
||||
orig = ORIG_SRC.get(c["img"])
|
||||
rows_html.append(f'''
|
||||
<div class="combo-row">
|
||||
<div class="cell-model">{MODEL_LABEL.get(m, m)}<div class="cell-sub">res={RES_LABEL.get(rlabel, rlabel)}</div></div>
|
||||
|
||||
Reference in New Issue
Block a user