File size: 1,541 Bytes
350392a
 
 
 
 
 
b828a40
 
 
350392a
 
 
b828a40
350392a
 
 
 
 
 
b828a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Page rendering using a lightweight template.
"""

from pathlib import Path
from string import Template
from typing import Any, Dict

from core.escaping import escape_json_for_script, escape_text_node


_TEMPLATE_PATH = Path(__file__).resolve().parent / "templates" / "page.html.tmpl"
_ASSETS_DIR = Path(__file__).resolve().parent / "assets"


def render_page(context: Dict[str, str]) -> str:
    template_text = _TEMPLATE_PATH.read_text(encoding="utf-8")
    template = Template(template_text)
    return template.safe_substitute(context)


def render_comparison_page(model: Dict[str, Any]) -> str:
    """Render the current presentation without changing evaluation data."""
    meta = model["meta"]
    return render_page(
        {
            "page_title": "Compression-Lens: Model Comparison",
            "model_a": escape_text_node(meta["model_a"]),
            "model_b": escape_text_node(meta["model_b"]),
            "rwkv_compression": f'{meta["avg_compression"]["rwkv"]:.2f}',
            "qwen_compression": f'{meta["avg_compression"]["qwen"]:.2f}',
            "mean_difference": f'{meta["avg_delta_compression"]:+.2f}',
            "document_meta": f'{len(model["text"].encode("utf-8")):,} bytes · {len(model["tokens"]):,} segments',
            "style_block": "\n".join((_ASSETS_DIR / name).read_text(encoding="utf-8") for name in ("theme.css", "main.css")),
            "render_model_json": escape_json_for_script(model),
            "script_body": (_ASSETS_DIR / "main.js").read_text(encoding="utf-8"),
        }
    )