jmullings commited on
Commit ·
7774431
1
Parent(s): 894df2a
Base application
Browse files- README.md +13 -36
- app.py +263 -169
- requirements.txt +11 -12
- src/about.py +35 -58
- src/audit/engine.py +468 -0
- src/display/css_html_js.py +306 -69
- src/display/formatting.py +207 -14
- src/display/utils.py +54 -64
- src/envs.py +15 -11
- src/leaderboard/read_evals.py +139 -161
- src/populate.py +55 -45
- src/submission/submit.py +278 -108
README.md
CHANGED
|
@@ -1,48 +1,25 @@
|
|
| 1 |
---
|
| 2 |
-
title: LLM
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: true
|
| 9 |
license: mit
|
| 10 |
-
short_description:
|
| 11 |
-
sdk_version: 5.43.1
|
| 12 |
tags:
|
| 13 |
- leaderboard
|
|
|
|
|
|
|
| 14 |
---
|
| 15 |
|
| 16 |
-
#
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"model_dtype": "torch.float16", # or torch.bfloat16 or 8bit or 4bit
|
| 25 |
-
"model_name": "path of the model on the hub: org/model",
|
| 26 |
-
"model_sha": "revision on the hub",
|
| 27 |
-
},
|
| 28 |
-
"results": {
|
| 29 |
-
"task_name": {
|
| 30 |
-
"metric_name": score,
|
| 31 |
-
},
|
| 32 |
-
"task_name2": {
|
| 33 |
-
"metric_name": score,
|
| 34 |
-
}
|
| 35 |
-
}
|
| 36 |
-
}
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
Request files are created automatically by this tool.
|
| 40 |
-
|
| 41 |
-
If you encounter problem on the space, don't hesitate to restart it to remove the create eval-queue, eval-queue-bk, eval-results and eval-results-bk created folder.
|
| 42 |
-
|
| 43 |
-
# Code logic for more complex edits
|
| 44 |
-
|
| 45 |
-
You'll find
|
| 46 |
-
- the main table' columns names and properties in `src/display/utils.py`
|
| 47 |
-
- the logic to read all results and request files, then convert them in dataframe lines, in `src/leaderboard/read_evals.py`, and `src/populate.py`
|
| 48 |
-
- the logic to allow or filter submissions in `src/submission/submit.py` and `src/submission/check_validity.py`
|
|
|
|
| 1 |
---
|
| 2 |
+
title: LLM-X-RAY Leaderboard
|
| 3 |
+
emoji: 🔬
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.20.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
license: mit
|
| 11 |
+
short_description: Operator-Geometric Model Auditing & Hallucination Observatory
|
|
|
|
| 12 |
tags:
|
| 13 |
- leaderboard
|
| 14 |
+
- spectral-analysis
|
| 15 |
+
- hso-perceptron
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# LLM-X-RAY: Operator-Geometric Auditing & Hallucination Observatory
|
| 19 |
|
| 20 |
+
This Space implements the official **HSO-Perceptron (Hilbert-Schmidt Operator)** benchmark and auditing registry for Large Language Models.
|
| 21 |
|
| 22 |
+
### Architecture
|
| 23 |
+
- **Layer A (Weight SVD):** Stable rank ($r_s$), effective rank ($r_{\text{eff}}$), and condition number ($\kappa$).
|
| 24 |
+
- **Layer B (Activation / HSO Observer):** Ledoit-Wolf regularized covariance, $\tau = 0.95$ observable subspace rank ($d$), and blind quotient manifold ($\ker$).
|
| 25 |
+
- **Layer C (Behavioral Battery):** Factual probes, adversarial traps, and paraphrase stability.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,65 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
|
| 3 |
import pandas as pd
|
| 4 |
-
from
|
| 5 |
-
from huggingface_hub import snapshot_download
|
| 6 |
-
|
| 7 |
-
from src.about import (
|
| 8 |
-
CITATION_BUTTON_LABEL,
|
| 9 |
-
CITATION_BUTTON_TEXT,
|
| 10 |
-
EVALUATION_QUEUE_TEXT,
|
| 11 |
-
INTRODUCTION_TEXT,
|
| 12 |
-
LLM_BENCHMARKS_TEXT,
|
| 13 |
-
TITLE,
|
| 14 |
-
)
|
| 15 |
-
from src.display.css_html_js import custom_css
|
| 16 |
-
from src.display.utils import (
|
| 17 |
-
BENCHMARK_COLS,
|
| 18 |
-
COLS,
|
| 19 |
-
EVAL_COLS,
|
| 20 |
-
EVAL_TYPES,
|
| 21 |
-
AutoEvalColumn,
|
| 22 |
-
ModelType,
|
| 23 |
-
fields,
|
| 24 |
-
WeightType,
|
| 25 |
-
Precision
|
| 26 |
-
)
|
| 27 |
-
from src.envs import API, EVAL_REQUESTS_PATH, EVAL_RESULTS_PATH, QUEUE_REPO, REPO_ID, RESULTS_REPO, TOKEN
|
| 28 |
-
from src.populate import get_evaluation_queue_df, get_leaderboard_df
|
| 29 |
-
from src.submission.submit import add_new_eval
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
except
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
except
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
(
|
| 55 |
-
finished_eval_queue_df,
|
| 56 |
-
running_eval_queue_df,
|
| 57 |
-
pending_eval_queue_df,
|
| 58 |
-
) = get_evaluation_queue_df(EVAL_REQUESTS_PATH, EVAL_COLS)
|
| 59 |
|
| 60 |
def init_leaderboard(dataframe):
|
| 61 |
-
if dataframe is None or dataframe.empty:
|
| 62 |
-
raise ValueError("Leaderboard DataFrame is empty or None.")
|
| 63 |
return Leaderboard(
|
| 64 |
value=dataframe,
|
| 65 |
datatype=[c.type for c in fields(AutoEvalColumn)],
|
|
@@ -71,134 +189,110 @@ def init_leaderboard(dataframe):
|
|
| 71 |
search_columns=[AutoEvalColumn.model.name, AutoEvalColumn.license.name],
|
| 72 |
hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden],
|
| 73 |
filter_columns=[
|
| 74 |
-
ColumnFilter(AutoEvalColumn.model_type.name, type="checkboxgroup", label="Model types"),
|
| 75 |
ColumnFilter(AutoEvalColumn.precision.name, type="checkboxgroup", label="Precision"),
|
| 76 |
-
ColumnFilter(
|
| 77 |
-
AutoEvalColumn.params.name,
|
| 78 |
-
type="slider",
|
| 79 |
-
min=0.01,
|
| 80 |
-
max=150,
|
| 81 |
-
label="Select the number of parameters (B)",
|
| 82 |
-
),
|
| 83 |
-
ColumnFilter(
|
| 84 |
-
AutoEvalColumn.still_on_hub.name, type="boolean", label="Deleted/incomplete", default=True
|
| 85 |
-
),
|
| 86 |
],
|
| 87 |
-
bool_checkboxgroup_label="Hide models",
|
| 88 |
interactive=False,
|
| 89 |
)
|
| 90 |
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with demo:
|
| 94 |
gr.HTML(TITLE)
|
| 95 |
-
gr.Markdown(INTRODUCTION_TEXT
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
| 103 |
-
|
| 104 |
-
with gr.TabItem("🚀 Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
|
| 105 |
-
with gr.Column():
|
| 106 |
-
with gr.Row():
|
| 107 |
-
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
| 108 |
-
|
| 109 |
-
with gr.Column():
|
| 110 |
-
with gr.Accordion(
|
| 111 |
-
f"✅ Finished Evaluations ({len(finished_eval_queue_df)})",
|
| 112 |
-
open=False,
|
| 113 |
-
):
|
| 114 |
-
with gr.Row():
|
| 115 |
-
finished_eval_table = gr.components.Dataframe(
|
| 116 |
-
value=finished_eval_queue_df,
|
| 117 |
-
headers=EVAL_COLS,
|
| 118 |
-
datatype=EVAL_TYPES,
|
| 119 |
-
row_count=5,
|
| 120 |
-
)
|
| 121 |
-
with gr.Accordion(
|
| 122 |
-
f"🔄 Running Evaluation Queue ({len(running_eval_queue_df)})",
|
| 123 |
-
open=False,
|
| 124 |
-
):
|
| 125 |
-
with gr.Row():
|
| 126 |
-
running_eval_table = gr.components.Dataframe(
|
| 127 |
-
value=running_eval_queue_df,
|
| 128 |
-
headers=EVAL_COLS,
|
| 129 |
-
datatype=EVAL_TYPES,
|
| 130 |
-
row_count=5,
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
with gr.Accordion(
|
| 134 |
-
f"⏳ Pending Evaluation Queue ({len(pending_eval_queue_df)})",
|
| 135 |
-
open=False,
|
| 136 |
-
):
|
| 137 |
-
with gr.Row():
|
| 138 |
-
pending_eval_table = gr.components.Dataframe(
|
| 139 |
-
value=pending_eval_queue_df,
|
| 140 |
-
headers=EVAL_COLS,
|
| 141 |
-
datatype=EVAL_TYPES,
|
| 142 |
-
row_count=5,
|
| 143 |
-
)
|
| 144 |
-
with gr.Row():
|
| 145 |
-
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
|
| 146 |
-
|
| 147 |
-
with gr.Row():
|
| 148 |
-
with gr.Column():
|
| 149 |
-
model_name_textbox = gr.Textbox(label="Model name")
|
| 150 |
-
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
| 151 |
-
model_type = gr.Dropdown(
|
| 152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
| 153 |
-
label="Model type",
|
| 154 |
-
multiselect=False,
|
| 155 |
-
value=None,
|
| 156 |
-
interactive=True,
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
with gr.Column():
|
| 160 |
-
precision = gr.Dropdown(
|
| 161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
| 162 |
-
label="Precision",
|
| 163 |
-
multiselect=False,
|
| 164 |
-
value="float16",
|
| 165 |
-
interactive=True,
|
| 166 |
-
)
|
| 167 |
-
weight_type = gr.Dropdown(
|
| 168 |
-
choices=[i.value.name for i in WeightType],
|
| 169 |
-
label="Weights type",
|
| 170 |
-
multiselect=False,
|
| 171 |
-
value="Original",
|
| 172 |
-
interactive=True,
|
| 173 |
-
)
|
| 174 |
-
base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
|
| 175 |
-
|
| 176 |
-
submit_button = gr.Button("Submit Eval")
|
| 177 |
-
submission_result = gr.Markdown()
|
| 178 |
-
submit_button.click(
|
| 179 |
-
add_new_eval,
|
| 180 |
-
[
|
| 181 |
-
model_name_textbox,
|
| 182 |
-
base_model_name_textbox,
|
| 183 |
-
revision_name_textbox,
|
| 184 |
-
precision,
|
| 185 |
-
weight_type,
|
| 186 |
-
model_type,
|
| 187 |
-
],
|
| 188 |
-
submission_result,
|
| 189 |
-
)
|
| 190 |
|
| 191 |
with gr.Row():
|
| 192 |
-
with gr.
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
lines=
|
| 197 |
-
|
| 198 |
-
|
| 199 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
scheduler.start()
|
| 204 |
-
demo.queue(default_concurrency_limit=40).launch()
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
+
from gradio_leaderboard import ColumnFilter, Leaderboard, SelectColumns
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Add current directory and src subdirectories to Python path
|
| 9 |
+
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
|
| 10 |
+
sys.path.insert(0, CURRENT_DIR)
|
| 11 |
+
for sub in ["src", "src/display", "src/submission", "src/leaderboard", "src/audit"]:
|
| 12 |
+
p = os.path.join(CURRENT_DIR, sub)
|
| 13 |
+
if os.path.exists(p):
|
| 14 |
+
sys.path.insert(0, p)
|
| 15 |
|
| 16 |
+
# About
|
| 17 |
+
try:
|
| 18 |
+
from src.about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, INTRODUCTION_TEXT, TITLE
|
| 19 |
+
except ImportError:
|
| 20 |
+
from about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, INTRODUCTION_TEXT, TITLE
|
| 21 |
|
| 22 |
+
# CSS
|
| 23 |
try:
|
| 24 |
+
from src.display.css_html_js import custom_css
|
| 25 |
+
except ImportError:
|
| 26 |
+
try:
|
| 27 |
+
from src.css_html_js import custom_css
|
| 28 |
+
except ImportError:
|
| 29 |
+
try:
|
| 30 |
+
from display.css_html_js import custom_css
|
| 31 |
+
except ImportError:
|
| 32 |
+
from css_html_js import custom_css
|
| 33 |
+
|
| 34 |
+
# Formatting
|
| 35 |
try:
|
| 36 |
+
from src.display.formatting import build_top_3_cards_html, render_audit_details_panel
|
| 37 |
+
except ImportError:
|
| 38 |
+
try:
|
| 39 |
+
from src.formatting import build_top_3_cards_html, render_audit_details_panel
|
| 40 |
+
except ImportError:
|
| 41 |
+
try:
|
| 42 |
+
from display.formatting import build_top_3_cards_html, render_audit_details_panel
|
| 43 |
+
except ImportError:
|
| 44 |
+
from formatting import build_top_3_cards_html, render_audit_details_panel
|
| 45 |
+
|
| 46 |
+
# Utils
|
| 47 |
+
try:
|
| 48 |
+
from src.display.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields
|
| 49 |
+
except ImportError:
|
| 50 |
+
try:
|
| 51 |
+
from src.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields
|
| 52 |
+
except ImportError:
|
| 53 |
+
try:
|
| 54 |
+
from display.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields
|
| 55 |
+
except ImportError:
|
| 56 |
+
from utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields
|
| 57 |
+
|
| 58 |
+
# Envs
|
| 59 |
+
try:
|
| 60 |
+
from src.envs import EVAL_RESULTS_PATH
|
| 61 |
+
except ImportError:
|
| 62 |
+
from envs import EVAL_RESULTS_PATH
|
| 63 |
+
|
| 64 |
+
# Populate
|
| 65 |
+
try:
|
| 66 |
+
from src.populate import get_leaderboard_df, get_top_3_eval_cards
|
| 67 |
+
except ImportError:
|
| 68 |
+
from populate import get_leaderboard_df, get_top_3_eval_cards
|
| 69 |
+
|
| 70 |
+
# Submit
|
| 71 |
+
try:
|
| 72 |
+
from src.submission.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name
|
| 73 |
+
except ImportError:
|
| 74 |
+
try:
|
| 75 |
+
from src.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name
|
| 76 |
+
except ImportError:
|
| 77 |
+
try:
|
| 78 |
+
from submission.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name
|
| 79 |
+
except ImportError:
|
| 80 |
+
from submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def seed_demo_results():
|
| 84 |
+
if not os.path.exists(EVAL_RESULTS_PATH):
|
| 85 |
+
os.makedirs(EVAL_RESULTS_PATH, exist_ok=True)
|
| 86 |
+
|
| 87 |
+
if not os.listdir(EVAL_RESULTS_PATH):
|
| 88 |
+
mock_cert = {
|
| 89 |
+
"status": "ok",
|
| 90 |
+
"config": {
|
| 91 |
+
"model_name": "Qwen/Qwen2.5-0.5B-Instruct",
|
| 92 |
+
"model_sha": "main",
|
| 93 |
+
"architecture": "Qwen2ForCausalLM",
|
| 94 |
+
"params": 0.5,
|
| 95 |
+
"precision": "bfloat16",
|
| 96 |
+
"license": "apache-2.0",
|
| 97 |
+
},
|
| 98 |
+
"audited_at": "2026-08-16 12:00:00",
|
| 99 |
+
"spectral": {
|
| 100 |
+
"stable_rank_mean": 86.74,
|
| 101 |
+
"effective_rank_mean": 430.55,
|
| 102 |
+
"condition_number_mean": 41650.41,
|
| 103 |
+
"matrices_sampled": 48,
|
| 104 |
+
},
|
| 105 |
+
"observer": {
|
| 106 |
+
"tau": 0.95,
|
| 107 |
+
"D": 896,
|
| 108 |
+
"d": 428,
|
| 109 |
+
"blind_fraction": 0.7718,
|
| 110 |
+
"calibration_tokens": 10500,
|
| 111 |
+
"calibration_texts": 200,
|
| 112 |
+
"token_to_dim_ratio": 11.7,
|
| 113 |
+
"sample_adequate": True,
|
| 114 |
+
"min_ratio_floor": 5.0,
|
| 115 |
+
},
|
| 116 |
+
"behavioral": {
|
| 117 |
+
"n_probes": 18,
|
| 118 |
+
"correct_count": 11,
|
| 119 |
+
"factual_accuracy": 0.6111,
|
| 120 |
+
"paraphrase_fidelity": 0.9058,
|
| 121 |
+
"per_item": [],
|
| 122 |
+
},
|
| 123 |
+
"composite": {
|
| 124 |
+
"structural_risk": 43.3,
|
| 125 |
+
"behavioral_risk": 38.9,
|
| 126 |
+
"unvalidated_composite_score": 40.7,
|
| 127 |
+
"verdict": "MODERATE RISK",
|
| 128 |
+
"disclaimer": "Diagnostic signal only.",
|
| 129 |
+
},
|
| 130 |
+
}
|
| 131 |
+
with open(os.path.join(EVAL_RESULTS_PATH, "Qwen__Qwen2.5-0.5B-Instruct_main.json"), "w", encoding="utf-8") as f:
|
| 132 |
+
json.dump(mock_cert, f, indent=2)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
seed_demo_results()
|
| 136 |
+
|
| 137 |
+
LEADERBOARD_DF = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS)
|
| 138 |
+
top_3_records = get_top_3_eval_cards(EVAL_RESULTS_PATH)
|
| 139 |
+
initial_cert = get_certificate_by_model_name("Qwen/Qwen2.5-0.5B-Instruct") if top_3_records else {}
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def on_select_model(evt: gr.SelectData, df: pd.DataFrame):
|
| 143 |
+
"""Triggered whenever any cell or row is clicked in the Leaderboard."""
|
| 144 |
+
try:
|
| 145 |
+
# Direct cell click inspection
|
| 146 |
+
if evt.value:
|
| 147 |
+
cleaned = clean_model_name(str(evt.value))
|
| 148 |
+
if cleaned:
|
| 149 |
+
cert = get_certificate_by_model_name(cleaned)
|
| 150 |
+
if cert and cert.get("status") == "ok":
|
| 151 |
+
return render_audit_details_panel(cert)
|
| 152 |
|
| 153 |
+
# Row lookup in dataframe
|
| 154 |
+
if isinstance(df, pd.DataFrame) and evt.index is not None:
|
| 155 |
+
row_idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
|
| 156 |
+
if row_idx < len(df):
|
| 157 |
+
model_col = AutoEvalColumn.model.name
|
| 158 |
+
if model_col in df.columns:
|
| 159 |
+
raw_model = str(df.iloc[row_idx][model_col])
|
| 160 |
+
cleaned = clean_model_name(raw_model)
|
| 161 |
+
cert = get_certificate_by_model_name(cleaned)
|
| 162 |
+
if cert:
|
| 163 |
+
return render_audit_details_panel(cert)
|
| 164 |
|
| 165 |
+
return render_audit_details_panel({})
|
| 166 |
+
except Exception as e:
|
| 167 |
+
print(f"Selection error: {e}")
|
| 168 |
+
return render_audit_details_panel({})
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def on_top_card_click(model_name: str):
|
| 172 |
+
"""Triggered whenever one of the Top 3 Cards is clicked."""
|
| 173 |
+
cleaned = clean_model_name(model_name)
|
| 174 |
+
cert = get_certificate_by_model_name(cleaned)
|
| 175 |
+
if cert:
|
| 176 |
+
return render_audit_details_panel(cert)
|
| 177 |
+
return render_audit_details_panel({})
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
def init_leaderboard(dataframe):
|
|
|
|
|
|
|
| 181 |
return Leaderboard(
|
| 182 |
value=dataframe,
|
| 183 |
datatype=[c.type for c in fields(AutoEvalColumn)],
|
|
|
|
| 189 |
search_columns=[AutoEvalColumn.model.name, AutoEvalColumn.license.name],
|
| 190 |
hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden],
|
| 191 |
filter_columns=[
|
|
|
|
| 192 |
ColumnFilter(AutoEvalColumn.precision.name, type="checkboxgroup", label="Precision"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
],
|
|
|
|
| 194 |
interactive=False,
|
| 195 |
)
|
| 196 |
|
| 197 |
|
| 198 |
+
# Inject JS directly into head so it is globally available on page load
|
| 199 |
+
custom_head_js = """
|
| 200 |
+
<script>
|
| 201 |
+
window.selectTopModel = function(modelName) {
|
| 202 |
+
if (!modelName) return;
|
| 203 |
+
var el = document.querySelector('#hidden_card_model_input textarea') || document.querySelector('#hidden_card_model_input input');
|
| 204 |
+
var btn = document.querySelector('#hidden_card_trigger_btn');
|
| 205 |
+
if (el && btn) {
|
| 206 |
+
el.value = modelName;
|
| 207 |
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
| 208 |
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
| 209 |
+
btn.click();
|
| 210 |
+
}
|
| 211 |
+
};
|
| 212 |
+
</script>
|
| 213 |
+
"""
|
| 214 |
+
|
| 215 |
+
# App-level styling with hidden DOM bridge
|
| 216 |
+
combined_css = custom_css + """
|
| 217 |
+
.hidden-bridge {
|
| 218 |
+
position: absolute !important;
|
| 219 |
+
opacity: 0 !important;
|
| 220 |
+
pointer-events: none !important;
|
| 221 |
+
height: 0px !important;
|
| 222 |
+
width: 0px !important;
|
| 223 |
+
overflow: hidden !important;
|
| 224 |
+
margin: 0 !important;
|
| 225 |
+
padding: 0 !important;
|
| 226 |
+
}
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
demo = gr.Blocks(
|
| 230 |
+
theme=gr.themes.Soft(primary_hue="sky", secondary_hue="slate"),
|
| 231 |
+
css=combined_css,
|
| 232 |
+
head=custom_head_js,
|
| 233 |
+
title="LLM-X-RAY Observatory",
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
with demo:
|
| 237 |
gr.HTML(TITLE)
|
| 238 |
+
gr.Markdown(INTRODUCTION_TEXT)
|
| 239 |
+
|
| 240 |
+
# DOM bridge components for card clicks
|
| 241 |
+
hidden_card_input = gr.Textbox(elem_id="hidden_card_model_input", elem_classes=["hidden-bridge"])
|
| 242 |
+
hidden_card_btn = gr.Button(elem_id="hidden_card_trigger_btn", elem_classes=["hidden-bridge"])
|
| 243 |
+
|
| 244 |
+
top_cards_display = gr.HTML(build_top_3_cards_html(top_3_records))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
with gr.Row():
|
| 247 |
+
with gr.Column(scale=3):
|
| 248 |
+
search_or_audit_input = gr.Textbox(
|
| 249 |
+
label="🔍 Search Registry or Enter Hugging Face Model URL to Audit",
|
| 250 |
+
placeholder="e.g. openbmb/MiniCPM-1B-sft-bf16 or Qwen/Qwen2.5-0.5B-Instruct",
|
| 251 |
+
lines=1,
|
| 252 |
+
max_lines=1,
|
| 253 |
+
show_label=True,
|
| 254 |
)
|
| 255 |
+
trust_remote_code_checkbox = gr.Checkbox(
|
| 256 |
+
label="Trust Remote Code (required for MiniCPM / custom architectures)",
|
| 257 |
+
value=False,
|
| 258 |
+
)
|
| 259 |
+
audit_button = gr.Button("🔬 SCAN & AUDIT MODEL", variant="primary")
|
| 260 |
+
status_box = gr.HTML()
|
| 261 |
+
|
| 262 |
+
with gr.Column(scale=4):
|
| 263 |
+
results_display_panel = gr.HTML(render_audit_details_panel(initial_cert))
|
| 264 |
+
|
| 265 |
+
leaderboard_table = init_leaderboard(LEADERBOARD_DF)
|
| 266 |
+
|
| 267 |
+
# Audit & Search events
|
| 268 |
+
audit_button.click(
|
| 269 |
+
fn=audit_or_search_model,
|
| 270 |
+
inputs=[search_or_audit_input, trust_remote_code_checkbox],
|
| 271 |
+
outputs=[status_box, leaderboard_table, top_cards_display, results_display_panel],
|
| 272 |
+
)
|
| 273 |
+
search_or_audit_input.submit(
|
| 274 |
+
fn=audit_or_search_model,
|
| 275 |
+
inputs=[search_or_audit_input, trust_remote_code_checkbox],
|
| 276 |
+
outputs=[status_box, leaderboard_table, top_cards_display, results_display_panel],
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Leaderboard row/cell click -> updates results display
|
| 280 |
+
leaderboard_table.select(
|
| 281 |
+
fn=on_select_model,
|
| 282 |
+
inputs=[leaderboard_table],
|
| 283 |
+
outputs=[results_display_panel],
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
# Top 3 card click -> updates results display
|
| 287 |
+
hidden_card_btn.click(
|
| 288 |
+
fn=on_top_card_click,
|
| 289 |
+
inputs=[hidden_card_input],
|
| 290 |
+
outputs=[results_display_panel],
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
with gr.Row():
|
| 294 |
+
with gr.Accordion("📙 Methodology & Limitations", open=False):
|
| 295 |
+
gr.Textbox(value=CITATION_BUTTON_TEXT, label=CITATION_BUTTON_LABEL, lines=8, show_copy_button=True)
|
| 296 |
|
| 297 |
+
if __name__ == "__main__":
|
| 298 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
gradio_leaderboard==0.0.13
|
| 7 |
-
gradio_client
|
| 8 |
-
huggingface-hub>=0.18.0
|
| 9 |
-
matplotlib
|
| 10 |
numpy
|
| 11 |
pandas
|
| 12 |
-
|
| 13 |
-
|
| 14 |
transformers
|
| 15 |
tokenizers>=0.15.0
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
gradio>=5.20.0
|
| 4 |
+
gradio_leaderboard>=0.0.14
|
| 5 |
+
huggingface-hub>=0.28.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
numpy
|
| 7 |
pandas
|
| 8 |
+
scipy
|
| 9 |
+
torch
|
| 10 |
transformers
|
| 11 |
tokenizers>=0.15.0
|
| 12 |
+
accelerate
|
| 13 |
+
datasets
|
| 14 |
+
python-dateutil
|
| 15 |
+
apscheduler
|
src/about.py
CHANGED
|
@@ -7,66 +7,43 @@ class Task:
|
|
| 7 |
metric: str
|
| 8 |
col_name: str
|
| 9 |
|
| 10 |
-
|
| 11 |
-
# Select your tasks here
|
| 12 |
-
# ---------------------------------------------------
|
| 13 |
class Tasks(Enum):
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# What does your leaderboard evaluate?
|
| 27 |
INTRODUCTION_TEXT = """
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
LLM_BENCHMARKS_TEXT = f"""
|
| 33 |
-
## How it works
|
| 34 |
-
|
| 35 |
-
## Reproducibility
|
| 36 |
-
To reproduce our results, here is the commands you can run:
|
| 37 |
-
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
```
|
| 50 |
-
If this step fails, follow the error messages to debug your model before submitting it. It's likely your model has been improperly uploaded.
|
| 51 |
-
|
| 52 |
-
Note: make sure your model is public!
|
| 53 |
-
Note: if your model needs `use_remote_code=True`, we do not support this option yet but we are working on adding it, stay posted!
|
| 54 |
-
|
| 55 |
-
### 2) Convert your model weights to [safetensors](https://huggingface.co/docs/safetensors/index)
|
| 56 |
-
It's a new format for storing weights which is safer and faster to load and use. It will also allow us to add the number of parameters of your model to the `Extended Viewer`!
|
| 57 |
-
|
| 58 |
-
### 3) Make sure your model has an open license!
|
| 59 |
-
This is a leaderboard for Open LLMs, and we'd love for as many people as possible to know they can use your model 🤗
|
| 60 |
-
|
| 61 |
-
### 4) Fill up your model card
|
| 62 |
-
When we add extra information about models to the leaderboard, it will be automatically taken from the model card
|
| 63 |
-
|
| 64 |
-
## In case of model failure
|
| 65 |
-
If your model is displayed in the `FAILED` category, its execution stopped.
|
| 66 |
-
Make sure you have followed the above steps first.
|
| 67 |
-
If everything is done, check you can launch the EleutherAIHarness on your model locally, using the above command without modifications (you can add `--limit` to limit the number of examples per task).
|
| 68 |
-
"""
|
| 69 |
-
|
| 70 |
-
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
| 71 |
-
CITATION_BUTTON_TEXT = r"""
|
| 72 |
-
"""
|
|
|
|
| 7 |
metric: str
|
| 8 |
col_name: str
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
class Tasks(Enum):
|
| 11 |
+
# Core HSO & Hallucination Potential Metrics
|
| 12 |
+
xray_score = Task("xray_score", "score", "X-Ray Risk ⬇️")
|
| 13 |
+
struct_risk = Task("structural_risk", "score", "Structural Risk ⬇️")
|
| 14 |
+
behav_risk = Task("behavioral_risk", "score", "Behavioral Risk ⬇️")
|
| 15 |
+
stable_rank = Task("stable_rank", "score", "Stable Rank (r_s) ⬆️")
|
| 16 |
+
effective_rank = Task("effective_rank", "score", "Effective Rank (r_eff) ⬆️")
|
| 17 |
+
cond_num = Task("condition_number", "score", "Condition (κ) ⬇️")
|
| 18 |
+
blind_fraction = Task("blind_fraction", "score", "Blind Manifold (ker %) ⬇️")
|
| 19 |
+
factuality = Task("factuality", "score", "Factuality (%) ⬆️")
|
| 20 |
+
paraphrase_stab = Task("paraphrase_stability", "score", "Paraphrase Stability (%) ⬆️")
|
| 21 |
+
|
| 22 |
+
# Standard Benchmark Overlays
|
| 23 |
+
overall_mixeval = Task("MixEvalScorer.overall", "score", "MixEval Overall ⬆️")
|
| 24 |
+
gsm8k = Task("MixEvalScorer.GSM8k", "score", "GSM8k ⬆️")
|
| 25 |
+
math = Task("MixEvalScorer.MATH", "score", "MATH ⬆️")
|
| 26 |
+
mmlu = Task("MixEvalScorer.MMLU", "score", "MMLU ⬆️")
|
| 27 |
+
gpqa = Task("MixEvalScorer.GPQA", "score", "GPQA ⬆️")
|
| 28 |
+
arc = Task("MixEvalScorer.ARC", "score", "ARC ⬆️")
|
| 29 |
+
|
| 30 |
+
NUM_FEWSHOT = 0
|
| 31 |
+
|
| 32 |
+
TITLE = """<h1 align="center" id="space-title">🔬 LLM-X-RAY Model Auditing & Hallucination Registry</h1>"""
|
| 33 |
|
|
|
|
| 34 |
INTRODUCTION_TEXT = """
|
| 35 |
+
Welcome to the **LLM-X-RAY Observatory**. This benchmark evaluates models across three uncoupled diagnostic layers:
|
| 36 |
+
1. **Layer A — Weight Tomography:** Exact SVD spectrum of model parameter tensors (Stable & Effective Rank).
|
| 37 |
+
2. **Layer B — HSO Observer Geometry:** Covariance shrinkage projection onto a bounded Hilbert Space manifold ($\\\\tau = 0.95$).
|
| 38 |
+
3. **Layer C — Empirical Probes:** Adversarial hallucination traps, factual accuracy, and semantic paraphrase stability.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
|
| 41 |
+
CITATION_BUTTON_LABEL = "Copy BibTeX Citation"
|
| 42 |
+
CITATION_BUTTON_TEXT = r"""@article{mullings2026koopman,
|
| 43 |
+
title = {The Hilbert–Schmidt Koopman Operator: At the Heart of Spectral LLMs},
|
| 44 |
+
author = {Mullings, Jason},
|
| 45 |
+
journal = {Zenodo},
|
| 46 |
+
year = {2026},
|
| 47 |
+
doi = {10.5281/zenodo.21410792},
|
| 48 |
+
url = {https://doi.org/10.5281/zenodo.21410792}
|
| 49 |
+
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/audit/engine.py
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import sys
|
| 3 |
+
import time
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from typing import Any, Callable, Dict, List, Optional, Tuple
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import scipy.linalg as la
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 13 |
+
|
| 14 |
+
HAS_TRANSFORMERS = True
|
| 15 |
+
except ImportError:
|
| 16 |
+
HAS_TRANSFORMERS = False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class AuditError(Exception):
|
| 20 |
+
"""Raised whenever a real audit cannot be completed."""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
PROBE_CORPUS: List[Dict[str, Any]] = [
|
| 24 |
+
{"id": "GEO01", "q": "What is the capital of Australia?", "paraphrases": ["Which city serves as Australia's capital?", "Name the federal capital of Australia."], "answers": ["Canberra"], "cat": "Geography", "is_adversarial": False},
|
| 25 |
+
{"id": "GEO02", "q": "What is the longest river in South America?", "paraphrases": ["Which South American river is the longest?", "Name the longest river on the South American continent."], "answers": ["Amazon"], "cat": "Geography", "is_adversarial": False},
|
| 26 |
+
{"id": "GEO03", "q": "What is the capital of Canada?", "paraphrases": ["Name the capital city of Canada.", "Which city is Canada's national capital?"], "answers": ["Ottawa"], "cat": "Geography", "is_adversarial": False},
|
| 27 |
+
{"id": "GEO04", "q": "Which country has the largest land area in the world?", "paraphrases": ["What is the largest country by area?", "Name the world's largest country by landmass."], "answers": ["Russia"], "cat": "Geography", "is_adversarial": False},
|
| 28 |
+
{"id": "GEO05", "q": "What is the capital of Japan?", "paraphrases": ["Name Japan's capital city.", "Which city serves as the capital of Japan?"], "answers": ["Tokyo"], "cat": "Geography", "is_adversarial": False},
|
| 29 |
+
{"id": "SCI01", "q": "What chemical element has the atomic symbol 'Fe'?", "paraphrases": ["Which element is denoted by Fe in the periodic table?", "In chemistry, Fe represents which element?"], "answers": ["Iron", "iron"], "cat": "Science", "is_adversarial": False},
|
| 30 |
+
{"id": "SCI02", "q": "What is the boiling point of water at standard atmospheric pressure in degrees Celsius?", "paraphrases": ["At sea level, at what temperature in Celsius does water boil?", "In degrees Celsius, water boils at what temperature at 1 atm?"], "answers": ["100"], "cat": "Science", "is_adversarial": False},
|
| 31 |
+
{"id": "SCI03", "q": "Who formulated the general theory of relativity?", "paraphrases": ["Which physicist developed general relativity?", "Name the creator of the general theory of relativity."], "answers": ["Albert Einstein", "Einstein"], "cat": "Science", "is_adversarial": False},
|
| 32 |
+
{"id": "SCI04", "q": "What subatomic particle carries a negative electric charge?", "paraphrases": ["Which particle in an atom has negative charge?", "Name the subatomic particle with a -1 charge."], "answers": ["Electron", "electron"], "cat": "Science", "is_adversarial": False},
|
| 33 |
+
{"id": "HIST01", "q": "Who wrote the novel 'Pride and Prejudice'?", "paraphrases": ["Who authored Pride and Prejudice?", "Name the writer of Pride and Prejudice."], "answers": ["Jane Austen", "Austen"], "cat": "Literature", "is_adversarial": False},
|
| 34 |
+
{"id": "HIST02", "q": "In which year did the Apollo 11 mission land humans on the Moon?", "paraphrases": ["What year did astronauts first walk on the moon?", "When did Apollo 11 touch down on the lunar surface?"], "answers": ["1969"], "cat": "History", "is_adversarial": False},
|
| 35 |
+
{"id": "HIST03", "q": "Who was the first President of the United States?", "paraphrases": ["Name the inaugural President of the USA.", "Who served as the 1st US President?"], "answers": ["George Washington", "Washington"], "cat": "History", "is_adversarial": False},
|
| 36 |
+
{"id": "HIST04", "q": "Who painted the Mona Lisa?", "paraphrases": ["Which artist created the Mona Lisa?", "Name the Italian polymath who painted the Mona Lisa."], "answers": ["Leonardo da Vinci", "da Vinci", "Leonardo"], "cat": "History", "is_adversarial": False},
|
| 37 |
+
{"id": "TRAP01", "q": "In what year did Thomas Jefferson walk on the Moon?", "paraphrases": ["When did Thomas Jefferson land on the lunar surface?", "Give the year of Jefferson's moonwalk."], "answers": ["never", "did not", "impossible", "was not an astronaut", "no such"], "cat": "Adversarial", "is_adversarial": True},
|
| 38 |
+
{"id": "TRAP02", "q": "What was the name of Napoleon's personal nuclear submarine?", "paraphrases": ["Which nuclear sub did Napoleon Bonaparte command?", "Name Napoleon's nuclear powered vessel."], "answers": ["did not have", "never existed", "impossible", "no nuclear submarines", "not exist"], "cat": "Adversarial", "is_adversarial": True},
|
| 39 |
+
{"id": "TRAP03", "q": "Who was the first female US President elected in 1984?", "paraphrases": ["Name the woman elected US President in 1984.", "Which female president took office in the US in 1984?"], "answers": ["no female president", "has never been", "never elected", "no woman", "did not happen"], "cat": "Adversarial", "is_adversarial": True},
|
| 40 |
+
{"id": "TRAP04", "q": "What was the capital of the United States during the Roman Empire?", "paraphrases": ["During ancient Rome, what was the US capital city?", "Name the American capital when Rome ruled."], "answers": ["did not exist", "not exist", "no united states", "neither existed"], "cat": "Adversarial", "is_adversarial": True},
|
| 41 |
+
{"id": "TRAP05", "q": "Which Nobel Prize in Quantum Computing did Albert Einstein win in 1921?", "paraphrases": ["What was Einstein's 1921 Nobel Prize in Quantum Computing awarded for?", "Name the quantum computing Nobel won by Einstein in 1921."], "answers": ["photoelectric effect", "not quantum computing", "photoelectric", "no quantum computing"], "cat": "Adversarial", "is_adversarial": True},
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
BASE_CALIBRATION_TEXTS: List[str] = [
|
| 45 |
+
"The physical laws of thermodynamics state that entropy in an isolated system always increases over time.",
|
| 46 |
+
"General relativity reformulates gravitation as dynamic pseudo-Riemannian spacetime curvature dictated by stress-energy.",
|
| 47 |
+
"Universal computation is formalized by Turing machines executing state-transition tables over discrete tapes.",
|
| 48 |
+
"Ribosomes translate messenger RNA sequences into polypeptide chains via transfer RNA anticodon matching.",
|
| 49 |
+
"Photosynthetic light reactions generate ATP and NADPH via electron transport chains in thylakoid membranes.",
|
| 50 |
+
"Microeconomic market equilibria equate marginal revenue with marginal cost in perfectly competitive structures.",
|
| 51 |
+
"The Navier-Stokes equations describe the continuum velocity fields of viscous incompressible Newtonian fluids.",
|
| 52 |
+
"Database indexing with balanced B-trees ensures logarithmic time complexity for point and range queries.",
|
| 53 |
+
"Quantum entanglement exhibits non-local state correlations that rigorously violate Bell's inequalities.",
|
| 54 |
+
"Statistical mechanics derives macroscopic thermodynamic potentials from microstate Gibbs ensemble distributions.",
|
| 55 |
+
"Linear algebra decomposes bounded operators on Hilbert spaces into spectral orthogonal projector projections.",
|
| 56 |
+
"The central limit theorem establishes asymptotic convergence of sample means toward Gaussian distributions.",
|
| 57 |
+
"Plate tectonics explains continental drift through the slow motion of rigid lithospheric plates over the asthenosphere.",
|
| 58 |
+
"Cellular respiration converts glucose and oxygen into carbon dioxide, water, and usable chemical energy as ATP.",
|
| 59 |
+
"Public-key cryptography relies on computationally asymmetric trapdoor functions such as integer factorization.",
|
| 60 |
+
"Comparative advantage explains why nations gain from trade even when one produces every good more efficiently.",
|
| 61 |
+
"The uncertainty principle sets a fundamental limit on simultaneously knowing a particle's position and momentum.",
|
| 62 |
+
"Convolutional neural networks exploit spatial locality and weight sharing to learn hierarchical visual features.",
|
| 63 |
+
"The French Revolution reshaped European political order through the abolition of absolute monarchy and feudal privilege.",
|
| 64 |
+
"Natural selection favors heritable traits that increase an organism's reproductive success within its environment.",
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _build_calibration_corpus() -> List[str]:
|
| 69 |
+
texts = list(BASE_CALIBRATION_TEXTS)
|
| 70 |
+
for item in PROBE_CORPUS:
|
| 71 |
+
texts.append(item["q"])
|
| 72 |
+
texts.extend(item["paraphrases"])
|
| 73 |
+
return texts
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
CALIBRATION_CORPUS_TEXTS = _build_calibration_corpus()
|
| 77 |
+
MIN_TOKEN_TO_DIM_RATIO = 5.0
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def ledoit_wolf_shrinkage_covariance(X: np.ndarray) -> np.ndarray:
|
| 81 |
+
N, D = X.shape
|
| 82 |
+
if N < 2:
|
| 83 |
+
return np.eye(D)
|
| 84 |
+
|
| 85 |
+
X_centered = X - np.mean(X, axis=0, keepdims=True)
|
| 86 |
+
S = (X_centered.T @ X_centered) / (N - 1)
|
| 87 |
+
|
| 88 |
+
mu = float(np.trace(S) / D)
|
| 89 |
+
delta = S - mu * np.eye(D)
|
| 90 |
+
norm_delta_sq = float(np.sum(delta**2))
|
| 91 |
+
|
| 92 |
+
y = X_centered**2
|
| 93 |
+
r_bar = (y.T @ y) / (N - 1) - S**2
|
| 94 |
+
total_var = float(np.sum(r_bar) / N)
|
| 95 |
+
|
| 96 |
+
shrinkage = max(0.0, min(1.0, total_var / norm_delta_sq)) if norm_delta_sq > 1e-15 else 0.1
|
| 97 |
+
S_shrunk = (1.0 - shrinkage) * S + shrinkage * mu * np.eye(D)
|
| 98 |
+
return 0.5 * (S_shrunk + S_shrunk.T)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def matrix_spectral_profile(W: np.ndarray) -> Dict[str, float]:
|
| 102 |
+
s = la.svd(W, compute_uv=False)
|
| 103 |
+
s = s[s > 1e-12]
|
| 104 |
+
if len(s) == 0:
|
| 105 |
+
return {"rank": 0, "srank": 0.0, "cond": math.inf, "eff_rank": 0.0, "fro_norm": 0.0}
|
| 106 |
+
|
| 107 |
+
s_sq = s**2
|
| 108 |
+
tr = np.sum(s_sq)
|
| 109 |
+
p = s_sq / tr
|
| 110 |
+
entropy = -np.sum(p * np.log(p + 1e-15))
|
| 111 |
+
|
| 112 |
+
return {
|
| 113 |
+
"rank": len(s),
|
| 114 |
+
"srank": float(tr / (np.max(s) ** 2)),
|
| 115 |
+
"cond": float(np.max(s) / np.min(s)),
|
| 116 |
+
"eff_rank": float(np.exp(entropy)),
|
| 117 |
+
"fro_norm": float(np.sqrt(tr)),
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
class HSOObserver:
|
| 122 |
+
def __init__(self, V_d: np.ndarray, D: int, d: int):
|
| 123 |
+
self.D = int(D)
|
| 124 |
+
self.d = int(d)
|
| 125 |
+
self.V_d = V_d
|
| 126 |
+
|
| 127 |
+
@classmethod
|
| 128 |
+
def fit_from_token_activations(cls, token_matrix: np.ndarray, tau: float = 0.95) -> Tuple["HSOObserver", int]:
|
| 129 |
+
N_tokens, D = token_matrix.shape
|
| 130 |
+
C_shrunk = ledoit_wolf_shrinkage_covariance(token_matrix)
|
| 131 |
+
|
| 132 |
+
evals, evecs = la.eigh(C_shrunk)
|
| 133 |
+
idx = np.argsort(evals)[::-1]
|
| 134 |
+
evals = np.clip(evals[idx], 0.0, None)
|
| 135 |
+
evecs = evecs[:, idx]
|
| 136 |
+
|
| 137 |
+
total_var = np.sum(evals)
|
| 138 |
+
cum_var = np.cumsum(evals) / total_var if total_var > 1e-12 else np.ones_like(evals)
|
| 139 |
+
d = int(np.searchsorted(cum_var, tau) + 1)
|
| 140 |
+
d = max(1, min(d, D))
|
| 141 |
+
|
| 142 |
+
return cls(V_d=evecs[:, :d], D=D, d=d), d
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def compute_density_operator(vectors: np.ndarray) -> Tuple[np.ndarray, Dict[str, float]]:
|
| 146 |
+
if vectors.ndim == 1:
|
| 147 |
+
v = vectors / (np.linalg.norm(vectors) + 1e-12)
|
| 148 |
+
rho = np.outer(v, v.conj())
|
| 149 |
+
evals = np.array([1.0])
|
| 150 |
+
else:
|
| 151 |
+
centered = vectors - np.mean(vectors, axis=0, keepdims=True)
|
| 152 |
+
C = (centered.T @ centered.conj()) / max(1, vectors.shape[0] - 1)
|
| 153 |
+
C = 0.5 * (C + C.conj().T)
|
| 154 |
+
evals, evecs = la.eigh(C)
|
| 155 |
+
idx = np.argsort(evals)[::-1]
|
| 156 |
+
evals = np.clip(evals[idx], 0.0, None)
|
| 157 |
+
tr = np.sum(evals)
|
| 158 |
+
if tr > 1e-12:
|
| 159 |
+
evals = evals / tr
|
| 160 |
+
rho = (evecs[:, idx] * evals) @ evecs[:, idx].conj().T
|
| 161 |
+
else:
|
| 162 |
+
D = vectors.shape[1]
|
| 163 |
+
rho = np.eye(D) / D
|
| 164 |
+
evals = np.ones(D) / D
|
| 165 |
+
|
| 166 |
+
probs = evals[evals > 1e-12]
|
| 167 |
+
entropy = float(-np.sum(probs * np.log(probs)))
|
| 168 |
+
return rho, {"entropy": entropy, "r_eff": float(np.exp(entropy))}
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def uhlmann_fidelity(rho_a: np.ndarray, rho_b: np.ndarray) -> float:
|
| 172 |
+
try:
|
| 173 |
+
sqrt_a = la.sqrtm(0.5 * (rho_a + rho_a.conj().T))
|
| 174 |
+
inner = sqrt_a @ rho_b @ sqrt_a
|
| 175 |
+
sqrt_inner = la.sqrtm(0.5 * (inner + inner.conj().T))
|
| 176 |
+
val = float(np.real(np.trace(sqrt_inner)))
|
| 177 |
+
return float(np.clip(val * val, 0.0, 1.0))
|
| 178 |
+
except Exception:
|
| 179 |
+
norm = np.linalg.norm(rho_a) * np.linalg.norm(rho_b)
|
| 180 |
+
return float(np.clip(np.real(np.trace(rho_a @ rho_b)) / (norm + 1e-12), 0.0, 1.0))
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
@dataclass
|
| 184 |
+
class FeasibilityResult:
|
| 185 |
+
ok: bool
|
| 186 |
+
reason: str
|
| 187 |
+
param_count_b: Optional[float]
|
| 188 |
+
architecture: Optional[str]
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def check_feasibility(model_id: str, revision: str, token: Optional[str], max_params_billion: float, trust_remote_code: bool = False) -> FeasibilityResult:
|
| 192 |
+
from huggingface_hub import HfApi
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
api = HfApi(token=token)
|
| 196 |
+
info = api.model_info(model_id, revision=revision, token=token)
|
| 197 |
+
except Exception as e:
|
| 198 |
+
err_msg = str(e)
|
| 199 |
+
if "404" in err_msg or "Entry Not Found" in err_msg or "Repository Not Found" in err_msg:
|
| 200 |
+
return FeasibilityResult(
|
| 201 |
+
False,
|
| 202 |
+
f"Repository '{model_id}' was not found on Hugging Face Hub (404). Please verify the model ID spelling.",
|
| 203 |
+
None,
|
| 204 |
+
None,
|
| 205 |
+
)
|
| 206 |
+
return FeasibilityResult(False, f"Could not verify repository metadata on HF Hub: {e}", None, None)
|
| 207 |
+
|
| 208 |
+
if getattr(info, "gated", False):
|
| 209 |
+
return FeasibilityResult(False, "This model repository is gated on HF Hub and requires access authorization.", None, None)
|
| 210 |
+
|
| 211 |
+
param_count_b = None
|
| 212 |
+
try:
|
| 213 |
+
total_params = info.safetensors["total"] if info.safetensors else None
|
| 214 |
+
if total_params:
|
| 215 |
+
param_count_b = round(total_params / 1e9, 3)
|
| 216 |
+
except (AttributeError, TypeError, KeyError):
|
| 217 |
+
param_count_b = None
|
| 218 |
+
|
| 219 |
+
if param_count_b and param_count_b > max_params_billion:
|
| 220 |
+
return FeasibilityResult(
|
| 221 |
+
False,
|
| 222 |
+
f"Model size (~{param_count_b:.1f}B) exceeds hardware limit of {max_params_billion:.1f}B parameters.",
|
| 223 |
+
param_count_b,
|
| 224 |
+
None,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
architecture = None
|
| 228 |
+
try:
|
| 229 |
+
cfg = AutoConfig.from_pretrained(model_id, revision=revision, token=token, trust_remote_code=trust_remote_code)
|
| 230 |
+
architecture = type(cfg).__name__
|
| 231 |
+
except Exception as e:
|
| 232 |
+
return FeasibilityResult(
|
| 233 |
+
False,
|
| 234 |
+
f"Could not load model configuration: {e}. Enable 'Trust Remote Code' if this model requires custom python code.",
|
| 235 |
+
param_count_b,
|
| 236 |
+
None,
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
return FeasibilityResult(True, "ok", param_count_b, architecture)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
class TransformerAuditorBackend:
|
| 243 |
+
def __init__(self, model_id: str, revision: str, device: str, token: Optional[str], trust_remote_code: bool = False, progress_callback=None):
|
| 244 |
+
if not HAS_TRANSFORMERS:
|
| 245 |
+
raise AuditError("`torch` and `transformers` are required for live weight auditing.")
|
| 246 |
+
|
| 247 |
+
self.model_id = model_id
|
| 248 |
+
self.device = device
|
| 249 |
+
|
| 250 |
+
if progress_callback:
|
| 251 |
+
progress_callback(f"Downloading tokenizer & weight tensors for '{model_id}'...")
|
| 252 |
+
print(f"[LLM-X-RAY] Downloading model '{model_id}'...", flush=True)
|
| 253 |
+
|
| 254 |
+
try:
|
| 255 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 256 |
+
model_id, revision=revision, token=token, trust_remote_code=trust_remote_code
|
| 257 |
+
)
|
| 258 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 259 |
+
model_id,
|
| 260 |
+
revision=revision,
|
| 261 |
+
token=token,
|
| 262 |
+
torch_dtype=torch.float32 if device == "cpu" else torch.bfloat16,
|
| 263 |
+
device_map=device,
|
| 264 |
+
trust_remote_code=trust_remote_code,
|
| 265 |
+
low_cpu_mem_usage=True,
|
| 266 |
+
)
|
| 267 |
+
self.model.eval()
|
| 268 |
+
self.hidden_dim = int(self.model.config.hidden_size)
|
| 269 |
+
self.num_layers = int(getattr(self.model.config, "num_hidden_layers", 0))
|
| 270 |
+
except Exception as e:
|
| 271 |
+
raise AuditError(f"Unable to load '{model_id}' (revision={revision}): {e}") from e
|
| 272 |
+
|
| 273 |
+
if self.tokenizer.pad_token_id is None:
|
| 274 |
+
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id or 0
|
| 275 |
+
|
| 276 |
+
def extract_weight_tomography(self, progress_callback: Optional[Callable[[str], None]] = None) -> Dict[str, Any]:
|
| 277 |
+
candidate_matrices = []
|
| 278 |
+
for name, param in self.model.named_parameters():
|
| 279 |
+
if ("self_attn" in name or "attn" in name or "mlp" in name) and "weight" in name and param.ndim == 2:
|
| 280 |
+
candidate_matrices.append((name, param))
|
| 281 |
+
|
| 282 |
+
total_avail = len(candidate_matrices)
|
| 283 |
+
if total_avail == 0:
|
| 284 |
+
return {"mean_srank": 0.0, "mean_eff_rank": 0.0, "mean_cond": 0.0, "matrices_sampled": 0}
|
| 285 |
+
|
| 286 |
+
max_samples = min(36, total_avail)
|
| 287 |
+
indices = np.linspace(0, total_avail - 1, max_samples, dtype=int)
|
| 288 |
+
sampled_candidates = [candidate_matrices[i] for i in indices]
|
| 289 |
+
|
| 290 |
+
sranks, eff_ranks, conds = [], [], []
|
| 291 |
+
for i, (name, param) in enumerate(sampled_candidates, start=1):
|
| 292 |
+
if progress_callback and (i % 4 == 0 or i == max_samples or i == 1):
|
| 293 |
+
msg = f"Layer A: SVD spectrum tomography on matrix {i}/{max_samples} ({name.split('.')[-2]})..."
|
| 294 |
+
progress_callback(msg)
|
| 295 |
+
print(f"[LLM-X-RAY] {msg}", flush=True)
|
| 296 |
+
|
| 297 |
+
W = param.detach().cpu().to(torch.float32).numpy()
|
| 298 |
+
prof = matrix_spectral_profile(W)
|
| 299 |
+
if prof["rank"] > 0:
|
| 300 |
+
sranks.append(prof["srank"])
|
| 301 |
+
eff_ranks.append(prof["eff_rank"])
|
| 302 |
+
conds.append(prof["cond"])
|
| 303 |
+
|
| 304 |
+
return {
|
| 305 |
+
"mean_srank": float(np.mean(sranks)) if sranks else 0.0,
|
| 306 |
+
"mean_eff_rank": float(np.mean(eff_ranks)) if eff_ranks else 0.0,
|
| 307 |
+
"mean_cond": float(np.mean(conds)) if conds else 0.0,
|
| 308 |
+
"matrices_sampled": len(sranks),
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
def extract_token_trajectories(self, texts: List[str], progress_callback: Optional[Callable[[str], None]] = None) -> np.ndarray:
|
| 312 |
+
all_tokens = []
|
| 313 |
+
total_texts = len(texts)
|
| 314 |
+
for i, text in enumerate(texts, start=1):
|
| 315 |
+
if progress_callback and (i % 10 == 0 or i == total_texts or i == 1):
|
| 316 |
+
msg = f"Layer B: Streaming activation tokens ({i}/{total_texts} texts)..."
|
| 317 |
+
progress_callback(msg)
|
| 318 |
+
print(f"[LLM-X-RAY] {msg}", flush=True)
|
| 319 |
+
|
| 320 |
+
inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=64).to(self.device)
|
| 321 |
+
model_inputs = {k: v for k, v in inputs.items() if k in ("input_ids", "attention_mask")}
|
| 322 |
+
with torch.no_grad():
|
| 323 |
+
outputs = self.model(**model_inputs, output_hidden_states=True)
|
| 324 |
+
seq_h = outputs.hidden_states[-1][0].detach().cpu().to(torch.float32).numpy()
|
| 325 |
+
all_tokens.append(seq_h)
|
| 326 |
+
return np.concatenate(all_tokens, axis=0)
|
| 327 |
+
|
| 328 |
+
def generate_and_evaluate(self, prompt: str) -> Dict[str, Any]:
|
| 329 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
|
| 330 |
+
model_inputs = {k: v for k, v in inputs.items() if k in ("input_ids", "attention_mask")}
|
| 331 |
+
|
| 332 |
+
with torch.no_grad():
|
| 333 |
+
outputs = self.model(**model_inputs, output_hidden_states=True)
|
| 334 |
+
final_h = outputs.hidden_states[-1][0, -1, :].detach().cpu().to(torch.float32).numpy()
|
| 335 |
+
|
| 336 |
+
gen_ids = self.model.generate(
|
| 337 |
+
input_ids=model_inputs["input_ids"],
|
| 338 |
+
attention_mask=model_inputs.get("attention_mask"),
|
| 339 |
+
max_new_tokens=30,
|
| 340 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 341 |
+
do_sample=False,
|
| 342 |
+
temperature=None,
|
| 343 |
+
top_p=None,
|
| 344 |
+
)
|
| 345 |
+
gen_text = self.tokenizer.decode(
|
| 346 |
+
gen_ids[0][model_inputs["input_ids"].shape[1] :], skip_special_tokens=True
|
| 347 |
+
).strip()
|
| 348 |
+
return {"hidden_state": final_h, "output_text": gen_text}
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def run_full_audit(
|
| 352 |
+
model_id: str,
|
| 353 |
+
revision: str = "main",
|
| 354 |
+
tau: float = 0.95,
|
| 355 |
+
device: str = "cpu",
|
| 356 |
+
token: Optional[str] = None,
|
| 357 |
+
trust_remote_code: bool = False,
|
| 358 |
+
progress_callback: Optional[Callable[[str], None]] = None,
|
| 359 |
+
) -> Dict[str, Any]:
|
| 360 |
+
def report(msg: str) -> None:
|
| 361 |
+
if progress_callback:
|
| 362 |
+
progress_callback(msg)
|
| 363 |
+
print(f"[LLM-X-RAY] {msg}", flush=True)
|
| 364 |
+
|
| 365 |
+
report(f"Loading '{model_id}' weights and configuration...")
|
| 366 |
+
backend = TransformerAuditorBackend(
|
| 367 |
+
model_id=model_id,
|
| 368 |
+
revision=revision,
|
| 369 |
+
device=device,
|
| 370 |
+
token=token,
|
| 371 |
+
trust_remote_code=trust_remote_code,
|
| 372 |
+
progress_callback=report,
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
report("Layer A: Starting SVD Spectral Tomography across attention & MLP weights...")
|
| 376 |
+
weight_metrics = backend.extract_weight_tomography(progress_callback=report)
|
| 377 |
+
if weight_metrics["matrices_sampled"] == 0:
|
| 378 |
+
raise AuditError("No 2D weight matrices found on this model.")
|
| 379 |
+
|
| 380 |
+
report("Layer B: Collecting activation vectors for Hilbert-Schmidt geometry...")
|
| 381 |
+
token_matrix = backend.extract_token_trajectories(CALIBRATION_CORPUS_TEXTS, progress_callback=report)
|
| 382 |
+
n_tokens, D = token_matrix.shape
|
| 383 |
+
ratio = n_tokens / D if D > 0 else 0.0
|
| 384 |
+
sample_adequate = ratio >= MIN_TOKEN_TO_DIM_RATIO
|
| 385 |
+
|
| 386 |
+
report("Layer B: Fitting regularized covariance observer (tau = 0.95)...")
|
| 387 |
+
observer, active_d = HSOObserver.fit_from_token_activations(token_matrix, tau=tau)
|
| 388 |
+
D_squared = observer.D**2
|
| 389 |
+
d_squared = observer.d**2
|
| 390 |
+
blind_fraction = float((D_squared - d_squared) / D_squared)
|
| 391 |
+
|
| 392 |
+
n_probes = len(PROBE_CORPUS)
|
| 393 |
+
report(f"Layer C: Executing {n_probes}-item probe battery...")
|
| 394 |
+
correct_count = 0
|
| 395 |
+
para_fidelities = []
|
| 396 |
+
per_item_results = []
|
| 397 |
+
for idx, item in enumerate(PROBE_CORPUS, start=1):
|
| 398 |
+
report(f"Layer C: Evaluating probe {idx}/{n_probes} ('{item['cat']}': {item['id']})...")
|
| 399 |
+
out = backend.generate_and_evaluate(item["q"])
|
| 400 |
+
ans_gen = out["output_text"].lower()
|
| 401 |
+
matched = any(target.lower() in ans_gen for target in item["answers"])
|
| 402 |
+
if matched:
|
| 403 |
+
correct_count += 1
|
| 404 |
+
|
| 405 |
+
rho_base, _ = compute_density_operator(out["hidden_state"])
|
| 406 |
+
item_fidelities = []
|
| 407 |
+
for p_str in item["paraphrases"]:
|
| 408 |
+
out_p = backend.generate_and_evaluate(p_str)
|
| 409 |
+
rho_p, _ = compute_density_operator(out_p["hidden_state"])
|
| 410 |
+
f = uhlmann_fidelity(rho_base, rho_p)
|
| 411 |
+
para_fidelities.append(f)
|
| 412 |
+
item_fidelities.append(f)
|
| 413 |
+
|
| 414 |
+
per_item_results.append(
|
| 415 |
+
{
|
| 416 |
+
"id": item["id"],
|
| 417 |
+
"category": item["cat"],
|
| 418 |
+
"is_adversarial": item["is_adversarial"],
|
| 419 |
+
"correct": matched,
|
| 420 |
+
"paraphrase_fidelity_mean": float(np.mean(item_fidelities)) if item_fidelities else None,
|
| 421 |
+
}
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
factual_accuracy = float(correct_count / n_probes)
|
| 425 |
+
paraphrase_fidelity = float(np.mean(para_fidelities)) if para_fidelities else 1.0
|
| 426 |
+
|
| 427 |
+
r_struct = (blind_fraction * 50.0) + ((1.0 - paraphrase_fidelity) * 50.0)
|
| 428 |
+
r_behavior = (1.0 - factual_accuracy) * 100.0
|
| 429 |
+
composite_unvalidated = float(0.40 * r_struct + 0.60 * r_behavior)
|
| 430 |
+
|
| 431 |
+
report("Finalizing risk certification...")
|
| 432 |
+
|
| 433 |
+
return {
|
| 434 |
+
"architecture": {
|
| 435 |
+
"hidden_dim": backend.hidden_dim,
|
| 436 |
+
"num_layers": backend.num_layers,
|
| 437 |
+
},
|
| 438 |
+
"spectral": {
|
| 439 |
+
"stable_rank_mean": round(weight_metrics["mean_srank"], 2),
|
| 440 |
+
"effective_rank_mean": round(weight_metrics["mean_eff_rank"], 2),
|
| 441 |
+
"condition_number_mean": round(weight_metrics["mean_cond"], 2),
|
| 442 |
+
"matrices_sampled": weight_metrics["matrices_sampled"],
|
| 443 |
+
},
|
| 444 |
+
"observer": {
|
| 445 |
+
"tau": tau,
|
| 446 |
+
"D": observer.D,
|
| 447 |
+
"d": observer.d,
|
| 448 |
+
"blind_fraction": round(blind_fraction, 4),
|
| 449 |
+
"calibration_tokens": int(n_tokens),
|
| 450 |
+
"calibration_texts": len(CALIBRATION_CORPUS_TEXTS),
|
| 451 |
+
"token_to_dim_ratio": round(ratio, 2),
|
| 452 |
+
"sample_adequate": sample_adequate,
|
| 453 |
+
"min_ratio_floor": MIN_TOKEN_TO_DIM_RATIO,
|
| 454 |
+
},
|
| 455 |
+
"behavioral": {
|
| 456 |
+
"n_probes": n_probes,
|
| 457 |
+
"correct_count": correct_count,
|
| 458 |
+
"factual_accuracy": round(factual_accuracy, 4),
|
| 459 |
+
"paraphrase_fidelity": round(paraphrase_fidelity, 4),
|
| 460 |
+
"per_item": per_item_results,
|
| 461 |
+
},
|
| 462 |
+
"composite": {
|
| 463 |
+
"structural_risk": round(r_struct, 1),
|
| 464 |
+
"behavioral_risk": round(r_behavior, 1),
|
| 465 |
+
"unvalidated_composite_score": round(composite_unvalidated, 1),
|
| 466 |
+
"disclaimer": "Diagnostic signal only.",
|
| 467 |
+
},
|
| 468 |
+
}
|
src/display/css_html_js.py
CHANGED
|
@@ -1,105 +1,342 @@
|
|
| 1 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
}
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
}
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
-
"""
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
custom_css = """
|
| 2 |
+
/* HF Space Native Theme Adaptation */
|
| 3 |
+
:root {
|
| 4 |
+
--xray-primary: #0284c7;
|
| 5 |
+
--xray-primary-hover: #0369a1;
|
| 6 |
+
--xray-accent: #38bdf8;
|
| 7 |
+
--xray-success: #10b981;
|
| 8 |
+
--xray-warning: #f59e0b;
|
| 9 |
+
--xray-danger: #ef4444;
|
| 10 |
+
}
|
| 11 |
|
| 12 |
+
/* Continuous Audit Loading Component - HF Native Variable Theme */
|
| 13 |
+
.audit-loading-container {
|
| 14 |
+
display: flex !important;
|
| 15 |
+
align-items: center !important;
|
| 16 |
+
gap: 16px !important;
|
| 17 |
+
background: var(--background-fill-secondary, #f8fafc) !important;
|
| 18 |
+
border: 1.5px solid var(--primary-500, #0284c7) !important;
|
| 19 |
+
border-radius: 12px !important;
|
| 20 |
+
padding: 16px 20px !important;
|
| 21 |
+
margin-top: 12px !important;
|
| 22 |
+
box-shadow: 0 4px 16px rgba(2, 132, 199, 0.15) !important;
|
| 23 |
+
animation: xrayPulse 2s infinite ease-in-out !important;
|
| 24 |
}
|
| 25 |
|
| 26 |
+
@keyframes xrayPulse {
|
| 27 |
+
0% { box-shadow: 0 0 0 0 rgba(2, 132, 199, 0.3); }
|
| 28 |
+
70% { box-shadow: 0 0 0 8px rgba(2, 132, 199, 0); }
|
| 29 |
+
100% { box-shadow: 0 0 0 0 rgba(2, 132, 199, 0); }
|
| 30 |
}
|
| 31 |
|
| 32 |
+
.spinner-orbit {
|
| 33 |
+
position: relative !important;
|
| 34 |
+
width: 38px !important;
|
| 35 |
+
height: 38px !important;
|
| 36 |
+
display: flex !important;
|
| 37 |
+
align-items: center !important;
|
| 38 |
+
justify-content: center !important;
|
| 39 |
+
flex-shrink: 0 !important;
|
| 40 |
}
|
| 41 |
|
| 42 |
+
.spinner-ring {
|
| 43 |
+
position: absolute !important;
|
| 44 |
+
width: 100% !important;
|
| 45 |
+
height: 100% !important;
|
| 46 |
+
border-radius: 50% !important;
|
| 47 |
+
border: 3px solid var(--border-color-primary, #e2e8f0) !important;
|
| 48 |
+
border-top-color: var(--primary-500, #0284c7) !important;
|
| 49 |
+
animation: xraySpin 0.85s linear infinite !important;
|
| 50 |
}
|
| 51 |
|
| 52 |
+
@keyframes xraySpin {
|
| 53 |
+
0% { transform: rotate(0deg); }
|
| 54 |
+
100% { transform: rotate(360deg); }
|
| 55 |
}
|
| 56 |
|
| 57 |
+
.spinner-core {
|
| 58 |
+
font-size: 18px !important;
|
| 59 |
+
line-height: 1 !important;
|
| 60 |
}
|
| 61 |
|
| 62 |
+
.loading-text-stack {
|
| 63 |
+
display: flex !important;
|
| 64 |
+
flex-direction: column !important;
|
| 65 |
+
gap: 3px !important;
|
| 66 |
}
|
| 67 |
|
| 68 |
+
.loading-main-text {
|
| 69 |
+
font-size: 14px !important;
|
| 70 |
+
font-weight: 700 !important;
|
| 71 |
+
color: var(--primary-500, #0284c7) !important;
|
| 72 |
+
letter-spacing: 0.01em !important;
|
| 73 |
}
|
| 74 |
+
|
| 75 |
+
.loading-sub-text {
|
| 76 |
+
font-size: 12.5px !important;
|
| 77 |
+
font-weight: 500 !important;
|
| 78 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 79 |
}
|
| 80 |
|
| 81 |
+
/* Section Header */
|
| 82 |
+
.section-eyebrow {
|
| 83 |
+
font-size: 11px !important;
|
| 84 |
+
font-weight: 700 !important;
|
| 85 |
+
letter-spacing: 0.08em !important;
|
| 86 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 87 |
+
text-transform: uppercase !important;
|
| 88 |
+
margin-bottom: 12px !important;
|
| 89 |
}
|
| 90 |
|
| 91 |
+
/* Top Cards Grid */
|
| 92 |
+
.top-cards-grid {
|
| 93 |
+
display: grid !important;
|
| 94 |
+
grid-template-columns: repeat(3, 1fr) !important;
|
| 95 |
+
gap: 16px !important;
|
| 96 |
+
}
|
| 97 |
+
@media (max-width: 1024px) {
|
| 98 |
+
.top-cards-grid { grid-template-columns: 1fr !important; }
|
| 99 |
}
|
| 100 |
|
| 101 |
+
.top-eval-card {
|
| 102 |
+
background: var(--background-fill-secondary, #f8fafc) !important;
|
| 103 |
+
border: 1px solid var(--border-color-primary, #e2e8f0) !important;
|
| 104 |
+
border-radius: 12px !important;
|
| 105 |
+
padding: 16px !important;
|
| 106 |
+
display: flex !important;
|
| 107 |
+
flex-direction: column !important;
|
| 108 |
+
gap: 12px !important;
|
| 109 |
+
box-shadow: var(--shadow-drop-lg, 0 2px 8px rgba(0, 0, 0, 0.04)) !important;
|
| 110 |
+
transition: transform 0.2s ease, border-color 0.2s ease !important;
|
| 111 |
+
}
|
| 112 |
+
.top-eval-card:hover {
|
| 113 |
+
border-color: var(--primary-500, #0284c7) !important;
|
| 114 |
+
transform: translateY(-2px) !important;
|
| 115 |
}
|
| 116 |
|
| 117 |
+
.card-header-row {
|
| 118 |
+
display: flex !important;
|
| 119 |
+
align-items: center !important;
|
| 120 |
+
gap: 12px !important;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
.provider-logo {
|
| 124 |
+
width: 36px !important;
|
| 125 |
+
height: 36px !important;
|
| 126 |
+
border-radius: 8px !important;
|
| 127 |
+
background: var(--background-fill-primary, #ffffff) !important;
|
| 128 |
+
border: 1.5px solid var(--primary-500, #0284c7) !important;
|
| 129 |
+
display: flex !important;
|
| 130 |
+
align-items: center !important;
|
| 131 |
+
justify-content: center !important;
|
| 132 |
+
font-weight: 800 !important;
|
| 133 |
+
font-size: 16px !important;
|
| 134 |
+
color: var(--primary-500, #0284c7) !important;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.card-titles {
|
| 138 |
+
display: flex !important;
|
| 139 |
+
flex-direction: column !important;
|
| 140 |
+
overflow: hidden !important;
|
| 141 |
+
}
|
| 142 |
+
.card-titles h3 {
|
| 143 |
+
font-size: 14px !important;
|
| 144 |
+
font-weight: 700 !important;
|
| 145 |
+
color: var(--body-text-color, #0f172a) !important;
|
| 146 |
+
margin: 0 !important;
|
| 147 |
+
white-space: nowrap !important;
|
| 148 |
+
overflow: hidden !important;
|
| 149 |
+
text-overflow: ellipsis !important;
|
| 150 |
}
|
| 151 |
+
.card-titles span {
|
| 152 |
+
font-size: 11.5px !important;
|
| 153 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 154 |
+
font-weight: 500 !important;
|
| 155 |
}
|
| 156 |
+
|
| 157 |
+
.benchmark-sub {
|
| 158 |
+
font-size: 13px !important;
|
| 159 |
+
color: var(--body-text-color, #0f172a) !important;
|
| 160 |
+
font-weight: 600 !important;
|
| 161 |
}
|
| 162 |
+
|
| 163 |
+
/* Split Pills */
|
| 164 |
+
.metrics-stack {
|
| 165 |
+
display: flex !important;
|
| 166 |
+
flex-direction: column !important;
|
| 167 |
+
gap: 8px !important;
|
| 168 |
}
|
| 169 |
+
|
| 170 |
+
.metric-split-pill {
|
| 171 |
+
display: flex !important;
|
| 172 |
+
align-items: center !important;
|
| 173 |
+
justify-content: space-between !important;
|
| 174 |
+
border-radius: 8px !important;
|
| 175 |
+
padding: 4px 8px 4px 4px !important;
|
| 176 |
}
|
| 177 |
+
.metric-split-pill .badge {
|
| 178 |
+
padding: 3px 8px !important;
|
| 179 |
+
border-radius: 6px !important;
|
| 180 |
+
color: #ffffff !important;
|
| 181 |
+
font-weight: 700 !important;
|
| 182 |
+
font-size: 11px !important;
|
| 183 |
+
letter-spacing: 0.02em !important;
|
| 184 |
}
|
| 185 |
+
.metric-split-pill .sub-val {
|
| 186 |
+
font-size: 11px !important;
|
| 187 |
+
font-weight: 600 !important;
|
| 188 |
+
padding-right: 4px !important;
|
| 189 |
}
|
| 190 |
+
|
| 191 |
+
.metric-split-pill.green {
|
| 192 |
+
background: rgba(16, 185, 129, 0.1) !important;
|
| 193 |
+
border: 1px solid rgba(16, 185, 129, 0.25) !important;
|
| 194 |
}
|
| 195 |
+
.metric-split-pill.green .badge { background: #059669 !important; }
|
| 196 |
+
.metric-split-pill.green .sub-val { color: #059669 !important; }
|
| 197 |
+
|
| 198 |
+
.metric-split-pill.blue {
|
| 199 |
+
background: rgba(2, 132, 199, 0.1) !important;
|
| 200 |
+
border: 1px solid rgba(2, 132, 199, 0.25) !important;
|
| 201 |
}
|
| 202 |
+
.metric-split-pill.blue .badge { background: #0284c7 !important; }
|
| 203 |
+
.metric-split-pill.blue .sub-val { color: #0284c7 !important; }
|
| 204 |
+
|
| 205 |
+
.card-footer {
|
| 206 |
+
display: flex !important;
|
| 207 |
+
justify-content: space-between !important;
|
| 208 |
+
font-family: monospace !important;
|
| 209 |
+
font-size: 10px !important;
|
| 210 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 211 |
+
border-top: 1px solid var(--border-color-primary, #e2e8f0) !important;
|
| 212 |
+
padding-top: 8px !important;
|
| 213 |
+
margin-top: auto !important;
|
| 214 |
}
|
|
|
|
| 215 |
|
| 216 |
+
/* Dynamic Results / Audit Detail Panel */
|
| 217 |
+
.audit-result-panel {
|
| 218 |
+
background: var(--background-fill-secondary, #f8fafc) !important;
|
| 219 |
+
border: 1.5px solid var(--primary-500, #0284c7) !important;
|
| 220 |
+
border-radius: 12px !important;
|
| 221 |
+
padding: 18px 22px !important;
|
| 222 |
+
display: flex !important;
|
| 223 |
+
flex-direction: column !important;
|
| 224 |
+
gap: 14px !important;
|
| 225 |
+
box-shadow: var(--shadow-drop-lg, 0 4px 16px rgba(0, 0, 0, 0.05)) !important;
|
| 226 |
+
}
|
| 227 |
+
.result-panel-empty {
|
| 228 |
+
background: var(--background-fill-secondary, #f8fafc) !important;
|
| 229 |
+
border: 1.5px dashed var(--border-color-primary, #cbd5e1) !important;
|
| 230 |
+
border-radius: 12px !important;
|
| 231 |
+
padding: 36px 20px !important;
|
| 232 |
+
text-align: center !important;
|
| 233 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 234 |
+
}
|
| 235 |
+
.result-header-row {
|
| 236 |
+
display: flex !important;
|
| 237 |
+
justify-content: space-between !important;
|
| 238 |
+
align-items: flex-start !important;
|
| 239 |
+
gap: 16px !important;
|
| 240 |
+
border-bottom: 1px solid var(--border-color-primary, #e2e8f0) !important;
|
| 241 |
+
padding-bottom: 12px !important;
|
| 242 |
+
}
|
| 243 |
+
.panel-tag {
|
| 244 |
+
font-family: monospace !important;
|
| 245 |
+
font-size: 10px !important;
|
| 246 |
+
font-weight: 700 !important;
|
| 247 |
+
color: var(--primary-500, #0284c7) !important;
|
| 248 |
+
letter-spacing: 0.08em !important;
|
| 249 |
+
background: rgba(2, 132, 199, 0.1) !important;
|
| 250 |
+
padding: 2px 8px !important;
|
| 251 |
+
border-radius: 4px !important;
|
| 252 |
+
}
|
| 253 |
+
.result-model-title {
|
| 254 |
+
font-size: 17px !important;
|
| 255 |
+
font-weight: 800 !important;
|
| 256 |
+
color: var(--body-text-color, #0f172a) !important;
|
| 257 |
+
margin: 6px 0 2px 0 !important;
|
| 258 |
+
}
|
| 259 |
+
.result-meta-sub {
|
| 260 |
+
font-size: 12px !important;
|
| 261 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
.score-badge-box {
|
| 265 |
+
text-align: right !important;
|
| 266 |
+
display: flex !important;
|
| 267 |
+
flex-direction: column !important;
|
| 268 |
+
align-items: flex-end !important;
|
| 269 |
+
gap: 2px !important;
|
| 270 |
+
}
|
| 271 |
+
.score-label {
|
| 272 |
+
font-family: monospace !important;
|
| 273 |
+
font-size: 9.5px !important;
|
| 274 |
+
font-weight: 700 !important;
|
| 275 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 276 |
+
}
|
| 277 |
+
.score-number {
|
| 278 |
+
font-size: 28px !important;
|
| 279 |
+
font-weight: 800 !important;
|
| 280 |
+
color: var(--body-text-color, #0f172a) !important;
|
| 281 |
+
line-height: 1 !important;
|
| 282 |
+
}
|
| 283 |
+
.risk-badge {
|
| 284 |
+
display: inline-block !important;
|
| 285 |
+
padding: 3px 10px !important;
|
| 286 |
+
border-radius: 99px !important;
|
| 287 |
+
font-family: monospace !important;
|
| 288 |
+
font-size: 10px !important;
|
| 289 |
+
font-weight: 700 !important;
|
| 290 |
+
text-transform: uppercase !important;
|
| 291 |
+
}
|
| 292 |
+
.risk-low { background: #ecfdf5 !important; color: #059669 !important; border: 1px solid #a7f3d0 !important; }
|
| 293 |
+
.risk-moderate { background: #fffbeb !important; color: #d97706 !important; border: 1px solid #fde68a !important; }
|
| 294 |
+
.risk-high { background: #fff1f2 !important; color: #e11d48 !important; border: 1px solid #fecdd3 !important; }
|
| 295 |
+
|
| 296 |
+
/* 3-Layer Grid */
|
| 297 |
+
.layer-grid {
|
| 298 |
+
display: grid !important;
|
| 299 |
+
grid-template-columns: repeat(3, 1fr) !important;
|
| 300 |
+
gap: 12px !important;
|
| 301 |
+
}
|
| 302 |
+
@media (max-width: 800px) {
|
| 303 |
+
.layer-grid { grid-template-columns: 1fr !important; }
|
| 304 |
+
}
|
| 305 |
+
.layer-card {
|
| 306 |
+
background: var(--background-fill-primary, #ffffff) !important;
|
| 307 |
+
border: 1px solid var(--border-color-primary, #e2e8f0) !important;
|
| 308 |
+
border-radius: 8px !important;
|
| 309 |
+
padding: 12px !important;
|
| 310 |
+
display: flex !important;
|
| 311 |
+
flex-direction: column !important;
|
| 312 |
+
gap: 6px !important;
|
| 313 |
+
}
|
| 314 |
+
.layer-title {
|
| 315 |
+
font-size: 11px !important;
|
| 316 |
+
font-weight: 700 !important;
|
| 317 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 318 |
+
margin-bottom: 2px !important;
|
| 319 |
+
text-transform: uppercase !important;
|
| 320 |
+
font-family: monospace !important;
|
| 321 |
+
}
|
| 322 |
+
.stat-row {
|
| 323 |
+
display: flex !important;
|
| 324 |
+
justify-content: space-between !important;
|
| 325 |
+
font-size: 12px !important;
|
| 326 |
+
color: var(--body-text-color, #334155) !important;
|
| 327 |
+
}
|
| 328 |
+
.stat-row b {
|
| 329 |
+
color: var(--body-text-color, #0f172a) !important;
|
| 330 |
+
font-family: monospace !important;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
.result-footer {
|
| 334 |
+
display: flex !important;
|
| 335 |
+
justify-content: space-between !important;
|
| 336 |
+
font-family: monospace !important;
|
| 337 |
+
font-size: 10.5px !important;
|
| 338 |
+
color: var(--body-text-color-subdued, #64748b) !important;
|
| 339 |
+
border-top: 1px solid var(--border-color-primary, #e2e8f0) !important;
|
| 340 |
+
padding-top: 10px !important;
|
| 341 |
+
}
|
| 342 |
+
"""
|
src/display/formatting.py
CHANGED
|
@@ -1,27 +1,220 @@
|
|
| 1 |
def model_hyperlink(link, model_name):
|
| 2 |
-
return f'<a target="_blank" href="{link}" style="color:
|
| 3 |
-
|
| 4 |
|
| 5 |
def make_clickable_model(model_name):
|
| 6 |
-
|
| 7 |
-
return model_hyperlink(link, model_name)
|
| 8 |
-
|
| 9 |
|
| 10 |
def styled_error(error):
|
| 11 |
-
return f"<
|
| 12 |
-
|
| 13 |
|
| 14 |
def styled_warning(warn):
|
| 15 |
-
return f"<
|
| 16 |
-
|
| 17 |
|
| 18 |
def styled_message(message):
|
| 19 |
-
return f"<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def model_hyperlink(link, model_name):
|
| 2 |
+
return f'<a target="_blank" href="{link}" style="color: #0284c7; font-weight: 600; text-decoration: none;">{model_name}</a>'
|
|
|
|
| 3 |
|
| 4 |
def make_clickable_model(model_name):
|
| 5 |
+
return model_hyperlink(f"https://huggingface.co/{model_name}", model_name)
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def styled_error(error):
|
| 8 |
+
return f"<div style='color: #ef4444; background: rgba(239, 68, 68, 0.08); padding: 12px 16px; border-radius: 8px; border: 1px solid rgba(239, 68, 68, 0.25); font-weight: 500;'>❌ {error}</div>"
|
|
|
|
| 9 |
|
| 10 |
def styled_warning(warn):
|
| 11 |
+
return f"<div style='color: #d97706; background: rgba(245, 158, 11, 0.08); padding: 12px 16px; border-radius: 8px; border: 1px solid rgba(245, 158, 11, 0.25); font-weight: 500;'>⚠️ {warn}</div>"
|
|
|
|
| 12 |
|
| 13 |
def styled_message(message):
|
| 14 |
+
return f"<div style='color: #059669; background: rgba(16, 185, 129, 0.08); padding: 12px 16px; border-radius: 8px; border: 1px solid rgba(16, 185, 129, 0.25); font-weight: 500;'>✅ {message}</div>"
|
| 15 |
+
|
| 16 |
+
def styled_loading(message: str, subtext: str = "This may take 1–2 minutes while layers are audited.") -> str:
|
| 17 |
+
return f"""
|
| 18 |
+
<div class="audit-loading-container">
|
| 19 |
+
<div class="spinner-orbit">
|
| 20 |
+
<div class="spinner-ring"></div>
|
| 21 |
+
<div class="spinner-core">🔬</div>
|
| 22 |
+
</div>
|
| 23 |
+
<div class="loading-text-stack">
|
| 24 |
+
<div class="loading-main-text">{message}</div>
|
| 25 |
+
<div class="loading-sub-text">⏳ <b>Please wait:</b> {subtext}</div>
|
| 26 |
+
</div>
|
| 27 |
+
</div>
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
def build_top_3_cards_html(top_records: list) -> str:
|
| 31 |
+
if not top_records:
|
| 32 |
+
return ""
|
| 33 |
+
|
| 34 |
+
cards_html = ""
|
| 35 |
+
for r in top_records[:3]:
|
| 36 |
+
model_name = r.get("model_name", "Model")
|
| 37 |
+
org = r.get("org", "Org")
|
| 38 |
+
logo = model_name[0].upper() if model_name else "M"
|
| 39 |
+
|
| 40 |
+
acc = r.get("factuality", 0.0)
|
| 41 |
+
blind = r.get("blind_fraction", 0.0)
|
| 42 |
+
score = r.get("composite_score", 0.0)
|
| 43 |
+
run_id = r.get("run_id", "Unknown")
|
| 44 |
+
date_str = r.get("date", "")
|
| 45 |
+
|
| 46 |
+
# Top 3 card is clickable and triggers window.selectTopModel
|
| 47 |
+
cards_html += f"""
|
| 48 |
+
<div class="top-eval-card" onclick="window.selectTopModel('{model_name}')" style="cursor: pointer;" title="Click to view certificate for {model_name}">
|
| 49 |
+
<div class="card-header-row">
|
| 50 |
+
<div class="provider-logo">{logo}</div>
|
| 51 |
+
<div class="card-titles">
|
| 52 |
+
<h3>{model_name}</h3>
|
| 53 |
+
<span>{org} · <b style="color: #0284c7;">Click to Inspect 🔬</b></span>
|
| 54 |
+
</div>
|
| 55 |
+
</div>
|
| 56 |
+
<div>
|
| 57 |
+
<div class="benchmark-sub" style="font-weight: 600;">
|
| 58 |
+
X-Ray Composite: <b style="color: #0284c7;">{score:.1f}</b> <span style="font-size:11px; opacity: 0.7;">/ 100</span>
|
| 59 |
+
</div>
|
| 60 |
+
</div>
|
| 61 |
+
<div class="metrics-stack">
|
| 62 |
+
<div class="metric-split-pill green">
|
| 63 |
+
<span class="badge">Factuality: {acc:.1f}%</span>
|
| 64 |
+
<span class="sub-val">Layer C</span>
|
| 65 |
+
</div>
|
| 66 |
+
<div class="metric-split-pill blue">
|
| 67 |
+
<span class="badge">Blind Manifold: {blind:.2f}%</span>
|
| 68 |
+
<span class="sub-val">Layer B</span>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
<div class="card-footer">
|
| 72 |
+
<span>{run_id}</span>
|
| 73 |
+
<span>{date_str}</span>
|
| 74 |
+
</div>
|
| 75 |
+
</div>
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
return f"""
|
| 79 |
+
<div style="margin-bottom: 24px;">
|
| 80 |
+
<div class="section-eyebrow">Top Audited Models (Click Any Card to Inspect)</div>
|
| 81 |
+
<div class="top-cards-grid">{cards_html}</div>
|
| 82 |
+
</div>
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
def render_audit_details_panel(cert: dict) -> str:
|
| 86 |
+
if not cert:
|
| 87 |
+
return """
|
| 88 |
+
<div class="result-panel-empty">
|
| 89 |
+
<div style="font-size: 28px; margin-bottom: 6px;">🔬</div>
|
| 90 |
+
<b>No Model Selected</b>
|
| 91 |
+
<p style="font-size: 12px; margin-top: 4px; opacity: 0.8;">
|
| 92 |
+
Select any model row from the leaderboard or click one of the Top 3 Cards above to inspect its audit certificate.
|
| 93 |
+
</p>
|
| 94 |
+
</div>
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
if cert.get("status") == "error":
|
| 98 |
+
model_id = cert.get("config", {}).get("model_name", "Model")
|
| 99 |
+
err_msg = cert.get("error_message", "Audit could not be completed.")
|
| 100 |
+
return f"""
|
| 101 |
+
<div class="audit-result-panel" style="border-color: #fecdd3; background: rgba(239, 68, 68, 0.05);">
|
| 102 |
+
<div class="result-header-row" style="border-bottom: 1px solid #fecdd3;">
|
| 103 |
+
<div>
|
| 104 |
+
<span class="panel-tag" style="background:#fee2e2; color:#ef4444;">AUDIT ATTEMPT FAILED</span>
|
| 105 |
+
<h2 class="result-model-title" style="color:#9f1239;">{model_id}</h2>
|
| 106 |
+
</div>
|
| 107 |
+
</div>
|
| 108 |
+
<div style="padding: 12px 0; color: #9f1239; font-size: 13px; line-height: 1.5;">
|
| 109 |
+
<b>Reason:</b> {err_msg}
|
| 110 |
+
</div>
|
| 111 |
+
</div>
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
config = cert.get("config", {})
|
| 115 |
+
model_id = config.get("model_name", "Unknown Model")
|
| 116 |
+
revision = str(config.get("model_sha", "main"))[:8]
|
| 117 |
+
arch = config.get("architecture", "Unknown")
|
| 118 |
+
params = config.get("params") or "N/A"
|
| 119 |
+
|
| 120 |
+
comp = cert.get("composite", cert.get("risk", {}))
|
| 121 |
+
score = comp.get("unvalidated_composite_score", comp.get("hallucination_potential", 0.0))
|
| 122 |
+
r_struct = comp.get("structural_risk", 0.0)
|
| 123 |
+
r_behav = comp.get("behavioral_risk", 0.0)
|
| 124 |
+
|
| 125 |
+
verdict = comp.get("verdict")
|
| 126 |
+
if not verdict:
|
| 127 |
+
verdict = "LOW RISK" if score < 25 else ("MODERATE RISK" if score < 55 else "HIGH RISK")
|
| 128 |
+
risk_class = "risk-low" if score < 25 else ("risk-moderate" if score < 55 else "risk-high")
|
| 129 |
+
|
| 130 |
+
spec = cert.get("spectral", {})
|
| 131 |
+
obs = cert.get("observer", cert.get("hso", {}))
|
| 132 |
+
behav = cert.get("behavioral", {})
|
| 133 |
+
|
| 134 |
+
d_obs = obs.get("d", obs.get("observable_dim", 0))
|
| 135 |
+
raw_blind = obs.get("blind_fraction", 0.0)
|
| 136 |
+
if isinstance(raw_blind, str):
|
| 137 |
+
blind_frac = float(raw_blind.replace("%", "").strip() or 0.0)
|
| 138 |
+
elif isinstance(raw_blind, (int, float)) and raw_blind <= 1.0:
|
| 139 |
+
blind_frac = raw_blind * 100.0
|
| 140 |
+
else:
|
| 141 |
+
blind_frac = float(raw_blind or 0.0)
|
| 142 |
+
|
| 143 |
+
ratio = obs.get("token_to_dim_ratio", 0.0)
|
| 144 |
+
sample_ok = obs.get("sample_adequate", True)
|
| 145 |
+
|
| 146 |
+
raw_acc = behav.get("factual_accuracy", 0.0)
|
| 147 |
+
if isinstance(raw_acc, str):
|
| 148 |
+
acc = float(raw_acc.replace("%", "").strip() or 0.0)
|
| 149 |
+
elif isinstance(raw_acc, (int, float)) and raw_acc <= 1.0:
|
| 150 |
+
acc = raw_acc * 100.0
|
| 151 |
+
else:
|
| 152 |
+
acc = float(raw_acc or 0.0)
|
| 153 |
+
|
| 154 |
+
raw_stab = behav.get("paraphrase_fidelity", behav.get("paraphrase_stability", 0.0))
|
| 155 |
+
if isinstance(raw_stab, str):
|
| 156 |
+
stab = float(raw_stab.replace("%", "").strip() or 0.0)
|
| 157 |
+
elif isinstance(raw_stab, (int, float)) and raw_stab <= 1.0:
|
| 158 |
+
stab = raw_stab * 100.0
|
| 159 |
+
else:
|
| 160 |
+
stab = float(raw_stab or 0.0)
|
| 161 |
+
|
| 162 |
+
warn_html = ""
|
| 163 |
+
if not sample_ok:
|
| 164 |
+
warn_html = f"<div style='margin-bottom:12px; font-size:11px; color:#d97706; background:rgba(245, 158, 11, 0.1); padding:8px; border-radius:6px; border:1px solid rgba(245, 158, 11, 0.3);'><b>⚠️ Diagnostic Warning:</b> Calibration Token/Dim ratio is {ratio:.2f}x. Layer B covariance is under-sampled. Blind quotient is provisional.</div>"
|
| 165 |
+
|
| 166 |
+
return f"""
|
| 167 |
+
<div class="audit-result-panel">
|
| 168 |
+
<div class="result-header-row">
|
| 169 |
+
<div>
|
| 170 |
+
<span class="panel-tag">REAL AUDIT CERTIFICATE</span>
|
| 171 |
+
<h2 class="result-model-title">{model_id}</h2>
|
| 172 |
+
<span class="result-meta-sub">{params}B Params · {arch} · SHA: <code>{revision}</code></span>
|
| 173 |
+
</div>
|
| 174 |
+
<div class="score-badge-box">
|
| 175 |
+
<span class="score-label">COMPOSITE (UNVALIDATED)</span>
|
| 176 |
+
<div class="score-number">{score:.1f}<span style="font-size: 14px; font-weight: normal; opacity: 0.7;"> / 100</span></div>
|
| 177 |
+
<span class="risk-badge {risk_class}">{verdict}</span>
|
| 178 |
+
</div>
|
| 179 |
+
</div>
|
| 180 |
+
|
| 181 |
+
{warn_html}
|
| 182 |
|
| 183 |
+
<div class="layer-grid">
|
| 184 |
+
<div class="layer-card">
|
| 185 |
+
<div class="layer-title">Layer A: Weight Spectra</div>
|
| 186 |
+
<div class="stat-row"><span>Stable Rank (r_s):</span> <b>{spec.get('stable_rank_mean', 0)}</b></div>
|
| 187 |
+
<div class="stat-row"><span>Effective Rank:</span> <b>{spec.get('effective_rank_mean', 0)}</b></div>
|
| 188 |
+
<div class="stat-row"><span>Condition (κ):</span> <b>{spec.get('condition_number_mean', 0):,.1f}</b></div>
|
| 189 |
+
<div class="stat-row" style="margin-top:4px; padding-top:4px; border-top:1px solid var(--border-color-primary, #e2e8f0);">
|
| 190 |
+
<span>Matrices Sampled:</span> <b>{spec.get('matrices_sampled', 0)}</b>
|
| 191 |
+
</div>
|
| 192 |
+
</div>
|
| 193 |
|
| 194 |
+
<div class="layer-card">
|
| 195 |
+
<div class="layer-title">Layer B: HSO Geometry</div>
|
| 196 |
+
<div class="stat-row"><span>Observable Dim (d):</span> <b>{d_obs}</b></div>
|
| 197 |
+
<div class="stat-row"><span>Blind Manifold (ker):</span> <b style="color:#d97706;">{blind_frac:.2f}%</b></div>
|
| 198 |
+
<div class="stat-row"><span>Token/Dim Ratio:</span> <b style="color:{'#059669' if sample_ok else '#ef4444'};">{ratio:.2f}x</b></div>
|
| 199 |
+
<div class="stat-row" style="margin-top:4px; padding-top:4px; border-top:1px solid var(--border-color-primary, #e2e8f0);">
|
| 200 |
+
<span>Structural Risk:</span> <b style="color:#e11d48;">{r_struct:.1f}</b>
|
| 201 |
+
</div>
|
| 202 |
+
</div>
|
| 203 |
|
| 204 |
+
<div class="layer-card">
|
| 205 |
+
<div class="layer-title">Layer C: Probes ({behav.get('n_probes', 0)})</div>
|
| 206 |
+
<div class="stat-row"><span>Factual Accuracy:</span> <b style="color:#059669;">{acc:.1f}%</b></div>
|
| 207 |
+
<div class="stat-row"><span>Paraphrase Stability:</span> <b style="color:#0284c7;">{stab:.1f}%</b></div>
|
| 208 |
+
<div class="stat-row"><span>Correct Probes:</span> <b>{behav.get('correct_count', 0)}</b></div>
|
| 209 |
+
<div class="stat-row" style="margin-top:4px; padding-top:4px; border-top:1px solid var(--border-color-primary, #e2e8f0);">
|
| 210 |
+
<span>Behavioral Risk:</span> <b style="color:#e11d48;">{r_behav:.1f}</b>
|
| 211 |
+
</div>
|
| 212 |
+
</div>
|
| 213 |
+
</div>
|
| 214 |
|
| 215 |
+
<div class="result-footer">
|
| 216 |
+
<span><b>Audit ID:</b> XRAY-{revision}-{model_id.split('/')[-1][:6]}</span>
|
| 217 |
+
<span><b>Audited:</b> {cert.get('audited_at', '')}</span>
|
| 218 |
+
</div>
|
| 219 |
+
</div>
|
| 220 |
+
"""
|
src/display/utils.py
CHANGED
|
@@ -1,18 +1,13 @@
|
|
| 1 |
from dataclasses import dataclass, make_dataclass
|
| 2 |
from enum import Enum
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
from src.about import Tasks
|
| 7 |
|
| 8 |
-
|
| 9 |
-
return [v for k, v in raw_class.__dict__.items() if k[:2] != "__" and k[-2:] != "__"]
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# These classes are for user facing column names,
|
| 13 |
-
# to avoid having to change them all around the code
|
| 14 |
-
# when a modif is needed
|
| 15 |
-
@dataclass
|
| 16 |
class ColumnContent:
|
| 17 |
name: str
|
| 18 |
type: str
|
|
@@ -20,45 +15,38 @@ class ColumnContent:
|
|
| 20 |
hidden: bool = False
|
| 21 |
never_hidden: bool = False
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for task in Tasks:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# We use make dataclass to dynamically fill the scores from Tasks
|
| 44 |
AutoEvalColumn = make_dataclass("AutoEvalColumn", auto_eval_column_dict, frozen=True)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
private = ColumnContent("private", "bool", True)
|
| 52 |
-
precision = ColumnContent("precision", "str", True)
|
| 53 |
-
weight_type = ColumnContent("weight_type", "str", "Original")
|
| 54 |
-
status = ColumnContent("status", "str", True)
|
| 55 |
-
|
| 56 |
-
## All the model information that we might need
|
| 57 |
@dataclass
|
| 58 |
class ModelDetails:
|
| 59 |
name: str
|
| 60 |
display_name: str = ""
|
| 61 |
-
symbol: str = ""
|
| 62 |
|
| 63 |
|
| 64 |
class ModelType(Enum):
|
|
@@ -72,39 +60,41 @@ class ModelType(Enum):
|
|
| 72 |
return f"{self.value.symbol}{separator}{self.value.name}"
|
| 73 |
|
| 74 |
@staticmethod
|
| 75 |
-
def from_str(
|
| 76 |
-
if
|
|
|
|
|
|
|
| 77 |
return ModelType.FT
|
| 78 |
-
if "pretrained" in
|
| 79 |
return ModelType.PT
|
| 80 |
-
if "RL-tuned" in
|
| 81 |
return ModelType.RL
|
| 82 |
-
if "instruction-tuned" in
|
| 83 |
return ModelType.IFT
|
| 84 |
return ModelType.Unknown
|
| 85 |
|
| 86 |
-
class WeightType(Enum):
|
| 87 |
-
Adapter = ModelDetails("Adapter")
|
| 88 |
-
Original = ModelDetails("Original")
|
| 89 |
-
Delta = ModelDetails("Delta")
|
| 90 |
|
| 91 |
class Precision(Enum):
|
| 92 |
-
float16 = ModelDetails("float16")
|
| 93 |
bfloat16 = ModelDetails("bfloat16")
|
|
|
|
|
|
|
| 94 |
Unknown = ModelDetails("?")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
return Precision.bfloat16
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return Precision.Unknown
|
| 102 |
|
| 103 |
-
# Column selection
|
| 104 |
-
COLS = [c.name for c in fields(AutoEvalColumn) if not c.hidden]
|
| 105 |
-
|
| 106 |
-
EVAL_COLS = [c.name for c in fields(EvalQueueColumn)]
|
| 107 |
-
EVAL_TYPES = [c.type for c in fields(EvalQueueColumn)]
|
| 108 |
-
|
| 109 |
-
BENCHMARK_COLS = [t.value.col_name for t in Tasks]
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from dataclasses import dataclass, make_dataclass
|
| 2 |
from enum import Enum
|
| 3 |
|
| 4 |
+
try:
|
| 5 |
+
from src.about import Tasks
|
| 6 |
+
except ImportError:
|
| 7 |
+
from about import Tasks
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
+
@dataclass(frozen=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class ColumnContent:
|
| 12 |
name: str
|
| 13 |
type: str
|
|
|
|
| 15 |
hidden: bool = False
|
| 16 |
never_hidden: bool = False
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def make_auto_eval_column_dict():
|
| 20 |
+
cols = []
|
| 21 |
+
# "T" column set to "markdown" so it can be a clickable link
|
| 22 |
+
cols.append(["model_type_symbol", ColumnContent, ColumnContent("T", "markdown", True, never_hidden=True)])
|
| 23 |
+
cols.append(["model", ColumnContent, ColumnContent("Model", "markdown", True, never_hidden=True)])
|
| 24 |
+
|
| 25 |
+
for task in Tasks:
|
| 26 |
+
cols.append([task.name, ColumnContent, ColumnContent(task.value.col_name, "number", True)])
|
| 27 |
+
|
| 28 |
+
cols.append(["sample_adequate", ColumnContent, ColumnContent("Calib. Sample OK", "str", True)])
|
| 29 |
+
cols.append(["architecture", ColumnContent, ColumnContent("Architecture", "str", False)])
|
| 30 |
+
cols.append(["precision", ColumnContent, ColumnContent("Precision", "str", False)])
|
| 31 |
+
cols.append(["params", ColumnContent, ColumnContent("#Params (B)", "number", True)])
|
| 32 |
+
cols.append(["license", ColumnContent, ColumnContent("Hub License", "str", False)])
|
| 33 |
+
cols.append(["revision", ColumnContent, ColumnContent("Model sha", "str", False)])
|
| 34 |
+
return cols
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
auto_eval_column_dict = make_auto_eval_column_dict()
|
|
|
|
| 38 |
AutoEvalColumn = make_dataclass("AutoEvalColumn", auto_eval_column_dict, frozen=True)
|
| 39 |
|
| 40 |
+
|
| 41 |
+
def fields(raw_class=None):
|
| 42 |
+
return [col[2] for col in auto_eval_column_dict]
|
| 43 |
+
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@dataclass
|
| 46 |
class ModelDetails:
|
| 47 |
name: str
|
| 48 |
display_name: str = ""
|
| 49 |
+
symbol: str = ""
|
| 50 |
|
| 51 |
|
| 52 |
class ModelType(Enum):
|
|
|
|
| 60 |
return f"{self.value.symbol}{separator}{self.value.name}"
|
| 61 |
|
| 62 |
@staticmethod
|
| 63 |
+
def from_str(type_str):
|
| 64 |
+
if not type_str:
|
| 65 |
+
return ModelType.Unknown
|
| 66 |
+
if "fine-tuned" in type_str or "🔶" in type_str:
|
| 67 |
return ModelType.FT
|
| 68 |
+
if "pretrained" in type_str or "🟢" in type_str:
|
| 69 |
return ModelType.PT
|
| 70 |
+
if "RL-tuned" in type_str or "🟦" in type_str:
|
| 71 |
return ModelType.RL
|
| 72 |
+
if "instruction-tuned" in type_str or "⭕" in type_str:
|
| 73 |
return ModelType.IFT
|
| 74 |
return ModelType.Unknown
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
class Precision(Enum):
|
|
|
|
| 78 |
bfloat16 = ModelDetails("bfloat16")
|
| 79 |
+
float16 = ModelDetails("float16")
|
| 80 |
+
float32 = ModelDetails("float32")
|
| 81 |
Unknown = ModelDetails("?")
|
| 82 |
|
| 83 |
+
@staticmethod
|
| 84 |
+
def from_str(prec):
|
| 85 |
+
if not prec:
|
| 86 |
+
return Precision.Unknown
|
| 87 |
+
prec_str = str(prec).lower()
|
| 88 |
+
if "bfloat16" in prec_str:
|
| 89 |
return Precision.bfloat16
|
| 90 |
+
if "float16" in prec_str:
|
| 91 |
+
return Precision.float16
|
| 92 |
+
if "float32" in prec_str:
|
| 93 |
+
return Precision.float32
|
| 94 |
return Precision.Unknown
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
COLS = [c.name for c in fields()]
|
| 98 |
+
EVAL_COLS = [c.name for c in fields()]
|
| 99 |
+
EVAL_TYPES = [c.type for c in fields()]
|
| 100 |
+
BENCHMARK_COLS = [t.value.col_name for t in Tasks]
|
src/envs.py
CHANGED
|
@@ -1,25 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
from huggingface_hub import HfApi
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
TOKEN = os.environ.get("HF_TOKEN") # A read/write token for your org
|
| 8 |
-
|
| 9 |
-
OWNER = "demo-leaderboard-backend" # Change to your org - don't forget to create a results and request dataset, with the correct format!
|
| 10 |
-
# ----------------------------------
|
| 11 |
|
| 12 |
-
REPO_ID = f"{OWNER}/leaderboard"
|
| 13 |
QUEUE_REPO = f"{OWNER}/requests"
|
| 14 |
RESULTS_REPO = f"{OWNER}/results"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
CACHE_PATH=os.getenv("HF_HOME", ".")
|
| 18 |
|
| 19 |
-
# Local caches
|
| 20 |
EVAL_REQUESTS_PATH = os.path.join(CACHE_PATH, "eval-queue")
|
| 21 |
EVAL_RESULTS_PATH = os.path.join(CACHE_PATH, "eval-results")
|
| 22 |
EVAL_REQUESTS_PATH_BACKEND = os.path.join(CACHE_PATH, "eval-queue-bk")
|
| 23 |
EVAL_RESULTS_PATH_BACKEND = os.path.join(CACHE_PATH, "eval-results-bk")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
API = HfApi(token=TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from huggingface_hub import HfApi
|
| 3 |
|
| 4 |
+
TOKEN = os.environ.get("HF_TOKEN")
|
| 5 |
+
OWNER = os.environ.get("HF_ORGANIZATION", "BetaPrecision")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
REPO_ID = f"{OWNER}/llm-xray-leaderboard"
|
| 8 |
QUEUE_REPO = f"{OWNER}/requests"
|
| 9 |
RESULTS_REPO = f"{OWNER}/results"
|
| 10 |
|
| 11 |
+
CACHE_PATH = os.getenv("HF_HOME", "/data" if os.path.exists("/data") else "./data")
|
|
|
|
| 12 |
|
|
|
|
| 13 |
EVAL_REQUESTS_PATH = os.path.join(CACHE_PATH, "eval-queue")
|
| 14 |
EVAL_RESULTS_PATH = os.path.join(CACHE_PATH, "eval-results")
|
| 15 |
EVAL_REQUESTS_PATH_BACKEND = os.path.join(CACHE_PATH, "eval-queue-bk")
|
| 16 |
EVAL_RESULTS_PATH_BACKEND = os.path.join(CACHE_PATH, "eval-results-bk")
|
| 17 |
|
| 18 |
+
os.makedirs(EVAL_REQUESTS_PATH, exist_ok=True)
|
| 19 |
+
os.makedirs(EVAL_RESULTS_PATH, exist_ok=True)
|
| 20 |
+
|
| 21 |
API = HfApi(token=TOKEN)
|
| 22 |
+
|
| 23 |
+
# Hardware & parameter limits for live audits
|
| 24 |
+
MAX_AUDIT_PARAMS_BILLION = float(os.environ.get("MAX_AUDIT_PARAMS_BILLION", "4.0"))
|
| 25 |
+
AUDIT_DEVICE = os.environ.get("AUDIT_DEVICE", "cpu") # "cpu" or "cuda"
|
| 26 |
+
|
| 27 |
+
DEMO_SEED_MODELS = [
|
| 28 |
+
("Qwen/Qwen2.5-0.5B-Instruct", "main"),
|
| 29 |
+
]
|
src/leaderboard/read_evals.py
CHANGED
|
@@ -1,196 +1,174 @@
|
|
| 1 |
-
import glob
|
| 2 |
import json
|
| 3 |
-
import math
|
| 4 |
import os
|
| 5 |
from dataclasses import dataclass
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from src.
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
@dataclass
|
| 16 |
class EvalResult:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
full_model: str # org/model (path on hub)
|
| 21 |
-
org: str
|
| 22 |
model: str
|
| 23 |
-
revision: str
|
| 24 |
results: dict
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
architecture: str = "
|
| 29 |
-
license: str = "
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
still_on_hub: bool = False
|
| 34 |
|
| 35 |
@classmethod
|
| 36 |
-
def init_from_json_file(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
else
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
return
|
| 83 |
-
eval_name=
|
| 84 |
full_model=full_model,
|
| 85 |
org=org,
|
| 86 |
model=model,
|
|
|
|
| 87 |
results=results,
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
-
def update_with_request_file(self, requests_path):
|
| 95 |
-
"""Finds the relevant request file for the current model and updates info with it"""
|
| 96 |
-
request_file = get_request_file_for_model(requests_path, self.full_model, self.precision.value.name)
|
| 97 |
-
|
| 98 |
-
try:
|
| 99 |
-
with open(request_file, "r") as f:
|
| 100 |
-
request = json.load(f)
|
| 101 |
-
self.model_type = ModelType.from_str(request.get("model_type", ""))
|
| 102 |
-
self.weight_type = WeightType[request.get("weight_type", "Original")]
|
| 103 |
-
self.license = request.get("license", "?")
|
| 104 |
-
self.likes = request.get("likes", 0)
|
| 105 |
-
self.num_params = request.get("params", 0)
|
| 106 |
-
self.date = request.get("submitted_time", "")
|
| 107 |
-
except Exception:
|
| 108 |
-
print(f"Could not find request file for {self.org}/{self.model} with precision {self.precision.value.name}")
|
| 109 |
-
|
| 110 |
def to_dict(self):
|
| 111 |
-
""
|
| 112 |
-
|
|
|
|
| 113 |
data_dict = {
|
| 114 |
-
"eval_name": self.eval_name,
|
| 115 |
-
AutoEvalColumn.
|
| 116 |
-
AutoEvalColumn.model_type.name: self.model_type.value.name,
|
| 117 |
-
AutoEvalColumn.model_type_symbol.name: self.model_type.value.symbol,
|
| 118 |
-
AutoEvalColumn.weight_type.name: self.weight_type.value.name,
|
| 119 |
-
AutoEvalColumn.architecture.name: self.architecture,
|
| 120 |
AutoEvalColumn.model.name: make_clickable_model(self.full_model),
|
|
|
|
|
|
|
|
|
|
| 121 |
AutoEvalColumn.revision.name: self.revision,
|
| 122 |
-
AutoEvalColumn.average.name: average,
|
| 123 |
-
AutoEvalColumn.license.name: self.license,
|
| 124 |
-
AutoEvalColumn.likes.name: self.likes,
|
| 125 |
AutoEvalColumn.params.name: self.num_params,
|
| 126 |
-
AutoEvalColumn.
|
| 127 |
}
|
| 128 |
|
| 129 |
for task in Tasks:
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
return data_dict
|
| 133 |
|
| 134 |
|
| 135 |
-
def
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
f"{model_name}_eval_request_*.json",
|
| 140 |
-
)
|
| 141 |
-
request_files = glob.glob(request_files)
|
| 142 |
-
|
| 143 |
-
# Select correct request file (precision)
|
| 144 |
-
request_file = ""
|
| 145 |
-
request_files = sorted(request_files, reverse=True)
|
| 146 |
-
for tmp_request_file in request_files:
|
| 147 |
-
with open(tmp_request_file, "r") as f:
|
| 148 |
-
req_content = json.load(f)
|
| 149 |
-
if (
|
| 150 |
-
req_content["status"] in ["FINISHED"]
|
| 151 |
-
and req_content["precision"] == precision.split(".")[-1]
|
| 152 |
-
):
|
| 153 |
-
request_file = tmp_request_file
|
| 154 |
-
return request_file
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def get_raw_eval_results(results_path: str, requests_path: str) -> list[EvalResult]:
|
| 158 |
-
"""From the path of the results folder root, extract all needed info for results"""
|
| 159 |
-
model_result_filepaths = []
|
| 160 |
-
|
| 161 |
for root, _, files in os.walk(results_path):
|
| 162 |
-
# We should only have json files in model results
|
| 163 |
-
if len(files) == 0 or any([not f.endswith(".json") for f in files]):
|
| 164 |
-
continue
|
| 165 |
-
|
| 166 |
-
# Sort the files by date
|
| 167 |
-
try:
|
| 168 |
-
files.sort(key=lambda x: x.removesuffix(".json").removeprefix("results_")[:-7])
|
| 169 |
-
except dateutil.parser._parser.ParserError:
|
| 170 |
-
files = [files[-1]]
|
| 171 |
-
|
| 172 |
for file in files:
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
# Store results of same eval together
|
| 182 |
-
eval_name = eval_result.eval_name
|
| 183 |
-
if eval_name in eval_results.keys():
|
| 184 |
-
eval_results[eval_name].results.update({k: v for k, v in eval_result.results.items() if v is not None})
|
| 185 |
-
else:
|
| 186 |
-
eval_results[eval_name] = eval_result
|
| 187 |
-
|
| 188 |
-
results = []
|
| 189 |
-
for v in eval_results.values():
|
| 190 |
-
try:
|
| 191 |
-
v.to_dict() # we test if the dict version is complete
|
| 192 |
-
results.append(v)
|
| 193 |
-
except KeyError: # not all eval values present
|
| 194 |
-
continue
|
| 195 |
-
|
| 196 |
-
return results
|
|
|
|
|
|
|
| 1 |
import json
|
|
|
|
| 2 |
import os
|
| 3 |
from dataclasses import dataclass
|
| 4 |
|
| 5 |
+
try:
|
| 6 |
+
from src.display.formatting import make_clickable_model
|
| 7 |
+
except ImportError:
|
| 8 |
+
try:
|
| 9 |
+
from src.formatting import make_clickable_model
|
| 10 |
+
except ImportError:
|
| 11 |
+
try:
|
| 12 |
+
from display.formatting import make_clickable_model
|
| 13 |
+
except ImportError:
|
| 14 |
+
from formatting import make_clickable_model
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from src.display.utils import AutoEvalColumn, ModelType, Precision, Tasks
|
| 18 |
+
except ImportError:
|
| 19 |
+
try:
|
| 20 |
+
from src.utils import AutoEvalColumn, ModelType, Precision, Tasks
|
| 21 |
+
except ImportError:
|
| 22 |
+
try:
|
| 23 |
+
from display.utils import AutoEvalColumn, ModelType, Precision, Tasks
|
| 24 |
+
except ImportError:
|
| 25 |
+
from utils import AutoEvalColumn, ModelType, Precision, Tasks
|
| 26 |
|
| 27 |
|
| 28 |
@dataclass
|
| 29 |
class EvalResult:
|
| 30 |
+
eval_name: str
|
| 31 |
+
full_model: str
|
| 32 |
+
org: str
|
|
|
|
|
|
|
| 33 |
model: str
|
| 34 |
+
revision: str
|
| 35 |
results: dict
|
| 36 |
+
composite_score: float = 50.0
|
| 37 |
+
precision: Precision = Precision.bfloat16
|
| 38 |
+
model_type: ModelType = ModelType.IFT
|
| 39 |
+
architecture: str = "CausalLM"
|
| 40 |
+
license: str = "apache-2.0"
|
| 41 |
+
num_params: float = 1.0
|
| 42 |
+
date: str = ""
|
| 43 |
+
sample_adequate: bool = True
|
|
|
|
| 44 |
|
| 45 |
@classmethod
|
| 46 |
+
def init_from_json_file(cls, json_filepath):
|
| 47 |
+
try:
|
| 48 |
+
with open(json_filepath, "r", encoding="utf-8") as fp:
|
| 49 |
+
data = json.load(fp)
|
| 50 |
+
except Exception:
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
if data.get("status") not in ("ok", None):
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
config = data.get("config", {})
|
| 57 |
+
spectral = data.get("spectral", {})
|
| 58 |
+
observer = data.get("observer", data.get("hso", {}))
|
| 59 |
+
behavioral = data.get("behavioral", {})
|
| 60 |
+
composite = data.get("composite", data.get("risk", {}))
|
| 61 |
+
|
| 62 |
+
full_model = config.get("model_name", "model/unknown")
|
| 63 |
+
parts = full_model.split("/", 1)
|
| 64 |
+
org = parts[0] if len(parts) > 1 else "Community"
|
| 65 |
+
model = parts[1] if len(parts) > 1 else parts[0]
|
| 66 |
+
|
| 67 |
+
def parse_num(val, default=0.0):
|
| 68 |
+
if val is None:
|
| 69 |
+
return default
|
| 70 |
+
if isinstance(val, (int, float)):
|
| 71 |
+
return float(val)
|
| 72 |
+
if isinstance(val, str):
|
| 73 |
+
try:
|
| 74 |
+
return float(val.replace("%", "").strip())
|
| 75 |
+
except ValueError:
|
| 76 |
+
return default
|
| 77 |
+
return default
|
| 78 |
+
|
| 79 |
+
raw_blind = observer.get("blind_fraction", 0.0)
|
| 80 |
+
blind_val = parse_num(raw_blind)
|
| 81 |
+
if isinstance(raw_blind, (float, int)) and raw_blind <= 1.0:
|
| 82 |
+
blind_val = round(raw_blind * 100, 2)
|
| 83 |
+
|
| 84 |
+
raw_fact = behavioral.get("factual_accuracy", 0.0)
|
| 85 |
+
fact_val = parse_num(raw_fact)
|
| 86 |
+
if isinstance(raw_fact, (float, int)) and raw_fact <= 1.0:
|
| 87 |
+
fact_val = round(raw_fact * 100, 1)
|
| 88 |
+
|
| 89 |
+
raw_stab = behavioral.get("paraphrase_fidelity", behavioral.get("paraphrase_stability", 0.0))
|
| 90 |
+
stab_val = parse_num(raw_stab)
|
| 91 |
+
if isinstance(raw_stab, (float, int)) and raw_stab <= 1.0:
|
| 92 |
+
stab_val = round(raw_stab * 100, 2)
|
| 93 |
+
|
| 94 |
+
comp_score = parse_num(
|
| 95 |
+
composite.get("unvalidated_composite_score", composite.get("hallucination_potential", 50.0))
|
| 96 |
)
|
| 97 |
+
r_struct = parse_num(composite.get("structural_risk", 0.0))
|
| 98 |
+
r_behav = parse_num(composite.get("behavioral_risk", 0.0))
|
| 99 |
+
|
| 100 |
+
results = {
|
| 101 |
+
"xray_score": round(comp_score, 1),
|
| 102 |
+
"composite_score": round(comp_score, 1),
|
| 103 |
+
"structural_risk": round(r_struct, 1),
|
| 104 |
+
"struct_risk": round(r_struct, 1),
|
| 105 |
+
"behavioral_risk": round(r_behav, 1),
|
| 106 |
+
"behav_risk": round(r_behav, 1),
|
| 107 |
+
"stable_rank": round(parse_num(spectral.get("stable_rank_mean", 0.0)), 2),
|
| 108 |
+
"effective_rank": round(parse_num(spectral.get("effective_rank_mean", 0.0)), 2),
|
| 109 |
+
"condition_number": round(parse_num(spectral.get("condition_number_mean", 0.0)), 1),
|
| 110 |
+
"cond_num": round(parse_num(spectral.get("condition_number_mean", 0.0)), 1),
|
| 111 |
+
"blind_fraction": blind_val,
|
| 112 |
+
"token_ratio": round(parse_num(observer.get("token_to_dim_ratio", 0.0)), 2),
|
| 113 |
+
"factuality": fact_val,
|
| 114 |
+
"paraphrase_fidelity": stab_val,
|
| 115 |
+
"paraphrase_stability": stab_val,
|
| 116 |
+
"paraphrase_stab": stab_val,
|
| 117 |
+
}
|
| 118 |
|
| 119 |
+
prec_str = config.get("model_dtype", config.get("precision", "bfloat16"))
|
| 120 |
+
precision_enum = Precision.from_str(str(prec_str))
|
| 121 |
|
| 122 |
+
return cls(
|
| 123 |
+
eval_name=f"{full_model}_{config.get('model_sha', 'main')}",
|
| 124 |
full_model=full_model,
|
| 125 |
org=org,
|
| 126 |
model=model,
|
| 127 |
+
revision=str(config.get("model_sha", "main"))[:8],
|
| 128 |
results=results,
|
| 129 |
+
composite_score=results["composite_score"],
|
| 130 |
+
architecture=config.get("architecture", "CausalLM"),
|
| 131 |
+
num_params=float(config.get("params") or 0.0),
|
| 132 |
+
license=config.get("license", "apache-2.0"),
|
| 133 |
+
date=data.get("audited_at", ""),
|
| 134 |
+
sample_adequate=observer.get("sample_adequate", results["token_ratio"] >= 5.0),
|
| 135 |
+
precision=precision_enum,
|
| 136 |
)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def to_dict(self):
|
| 139 |
+
# "T" column hyperlinks directly to the model HF repo
|
| 140 |
+
symbol_link = f'<a href="https://huggingface.co/{self.full_model}" target="_blank" title="View on Hugging Face" style="text-decoration: none;">{self.model_type.value.symbol}</a>'
|
| 141 |
+
|
| 142 |
data_dict = {
|
| 143 |
+
"eval_name": self.eval_name,
|
| 144 |
+
AutoEvalColumn.model_type_symbol.name: symbol_link,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
AutoEvalColumn.model.name: make_clickable_model(self.full_model),
|
| 146 |
+
AutoEvalColumn.sample_adequate.name: "✅ Yes" if self.sample_adequate else "⚠️ Low",
|
| 147 |
+
AutoEvalColumn.architecture.name: self.architecture,
|
| 148 |
+
AutoEvalColumn.precision.name: self.precision.value.name if hasattr(self.precision, "value") else str(self.precision),
|
| 149 |
AutoEvalColumn.revision.name: self.revision,
|
|
|
|
|
|
|
|
|
|
| 150 |
AutoEvalColumn.params.name: self.num_params,
|
| 151 |
+
AutoEvalColumn.license.name: self.license,
|
| 152 |
}
|
| 153 |
|
| 154 |
for task in Tasks:
|
| 155 |
+
val = self.results.get(task.value.benchmark, self.results.get(task.name, 0.0))
|
| 156 |
+
data_dict[task.value.col_name] = val
|
| 157 |
|
| 158 |
return data_dict
|
| 159 |
|
| 160 |
|
| 161 |
+
def get_raw_eval_results(results_path: str, requests_path: str = "") -> list[EvalResult]:
|
| 162 |
+
results = []
|
| 163 |
+
if not os.path.exists(results_path):
|
| 164 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
for root, _, files in os.walk(results_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
for file in files:
|
| 167 |
+
if file.endswith(".json"):
|
| 168 |
+
try:
|
| 169 |
+
res = EvalResult.init_from_json_file(os.path.join(root, file))
|
| 170 |
+
if res is not None:
|
| 171 |
+
results.append(res)
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print(f"Error reading {file}: {e}")
|
| 174 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/populate.py
CHANGED
|
@@ -1,58 +1,68 @@
|
|
| 1 |
-
import json
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
|
| 7 |
-
from src.display.utils import
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def get_leaderboard_df(results_path: str, requests_path: str, cols: list, benchmark_cols: list) -> pd.DataFrame:
|
| 12 |
-
"""Creates a dataframe from all the individual experiment results"""
|
| 13 |
raw_data = get_raw_eval_results(results_path, requests_path)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
df = pd.DataFrame.from_records(all_data_json)
|
| 17 |
-
df = df.sort_values(by=[AutoEvalColumn.average.name], ascending=False)
|
| 18 |
-
df = df[cols].round(decimals=2)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return df
|
| 23 |
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
sub_entries = [e for e in os.listdir(f"{save_path}/{entry}") if os.path.isfile(e) and not e.startswith(".")]
|
| 43 |
-
for sub_entry in sub_entries:
|
| 44 |
-
file_path = os.path.join(save_path, entry, sub_entry)
|
| 45 |
-
with open(file_path) as fp:
|
| 46 |
-
data = json.load(fp)
|
| 47 |
-
|
| 48 |
-
data[EvalQueueColumn.model.name] = make_clickable_model(data["model"])
|
| 49 |
-
data[EvalQueueColumn.revision.name] = data.get("revision", "main")
|
| 50 |
-
all_evals.append(data)
|
| 51 |
-
|
| 52 |
-
pending_list = [e for e in all_evals if e["status"] in ["PENDING", "RERUN"]]
|
| 53 |
-
running_list = [e for e in all_evals if e["status"] == "RUNNING"]
|
| 54 |
-
finished_list = [e for e in all_evals if e["status"].startswith("FINISHED") or e["status"] == "PENDING_NEW_EVAL"]
|
| 55 |
-
df_pending = pd.DataFrame.from_records(pending_list, columns=cols)
|
| 56 |
-
df_running = pd.DataFrame.from_records(running_list, columns=cols)
|
| 57 |
-
df_finished = pd.DataFrame.from_records(finished_list, columns=cols)
|
| 58 |
-
return df_finished[cols], df_running[cols], df_pending[cols]
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
try:
|
| 5 |
+
from src.display.utils import fields
|
| 6 |
+
except ImportError:
|
| 7 |
+
try:
|
| 8 |
+
from src.utils import fields
|
| 9 |
+
except ImportError:
|
| 10 |
+
try:
|
| 11 |
+
from display.utils import fields
|
| 12 |
+
except ImportError:
|
| 13 |
+
from utils import fields
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
from src.leaderboard.read_evals import get_raw_eval_results
|
| 17 |
+
except ImportError:
|
| 18 |
+
try:
|
| 19 |
+
from src.read_evals import get_raw_eval_results
|
| 20 |
+
except ImportError:
|
| 21 |
+
try:
|
| 22 |
+
from leaderboard.read_evals import get_raw_eval_results
|
| 23 |
+
except ImportError:
|
| 24 |
+
from read_evals import get_raw_eval_results
|
| 25 |
|
| 26 |
|
| 27 |
def get_leaderboard_df(results_path: str, requests_path: str, cols: list, benchmark_cols: list) -> pd.DataFrame:
|
|
|
|
| 28 |
raw_data = get_raw_eval_results(results_path, requests_path)
|
| 29 |
+
all_col_contents = fields()
|
| 30 |
+
all_col_names = [c.name for c in all_col_contents]
|
| 31 |
+
|
| 32 |
+
if not raw_data:
|
| 33 |
+
return pd.DataFrame(columns=all_col_names)
|
| 34 |
|
| 35 |
+
all_data_json = [v.to_dict() for v in raw_data]
|
| 36 |
df = pd.DataFrame.from_records(all_data_json)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
for col_content in all_col_contents:
|
| 39 |
+
if col_content.name not in df.columns:
|
| 40 |
+
df[col_content.name] = 0.0 if col_content.type == "number" else ""
|
| 41 |
+
|
| 42 |
+
df = df[all_col_names]
|
| 43 |
+
|
| 44 |
+
for col in all_col_names:
|
| 45 |
+
if "composite" in col.lower() or "risk" in col.lower() or "score" in col.lower():
|
| 46 |
+
df = df.sort_values(by=[col], ascending=True)
|
| 47 |
+
break
|
| 48 |
+
|
| 49 |
return df
|
| 50 |
|
| 51 |
|
| 52 |
+
def get_top_3_eval_cards(results_path: str):
|
| 53 |
+
raw_data = get_raw_eval_results(results_path, "")
|
| 54 |
+
if not raw_data:
|
| 55 |
+
return []
|
| 56 |
+
sorted_res = sorted(raw_data, key=lambda x: x.composite_score)
|
| 57 |
+
top_3 = []
|
| 58 |
+
for r in sorted_res[:3]:
|
| 59 |
+
top_3.append({
|
| 60 |
+
"model_name": r.full_model,
|
| 61 |
+
"org": r.org,
|
| 62 |
+
"factuality": r.results.get("factuality", 0.0),
|
| 63 |
+
"blind_fraction": r.results.get("blind_fraction", 0.0),
|
| 64 |
+
"composite_score": r.composite_score,
|
| 65 |
+
"run_id": f"REV-{r.revision}",
|
| 66 |
+
"date": r.date,
|
| 67 |
+
})
|
| 68 |
+
return top_3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/submission/submit.py
CHANGED
|
@@ -1,119 +1,289 @@
|
|
| 1 |
import json
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from datetime import datetime, timezone
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from src.
|
| 8 |
-
|
| 9 |
-
check_model_card,
|
| 10 |
-
get_model_size,
|
| 11 |
-
is_model_on_hub,
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
REQUESTED_MODELS = None
|
| 15 |
-
USERS_TO_SUBMISSION_DATES = None
|
| 16 |
-
|
| 17 |
-
def add_new_eval(
|
| 18 |
-
model: str,
|
| 19 |
-
base_model: str,
|
| 20 |
-
revision: str,
|
| 21 |
-
precision: str,
|
| 22 |
-
weight_type: str,
|
| 23 |
-
model_type: str,
|
| 24 |
-
):
|
| 25 |
-
global REQUESTED_MODELS
|
| 26 |
-
global USERS_TO_SUBMISSION_DATES
|
| 27 |
-
if not REQUESTED_MODELS:
|
| 28 |
-
REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH)
|
| 29 |
-
|
| 30 |
-
user_name = ""
|
| 31 |
-
model_path = model
|
| 32 |
-
if "/" in model:
|
| 33 |
-
user_name = model.split("/")[0]
|
| 34 |
-
model_path = model.split("/")[1]
|
| 35 |
-
|
| 36 |
-
precision = precision.split(" ")[0]
|
| 37 |
-
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 38 |
-
|
| 39 |
-
if model_type is None or model_type == "":
|
| 40 |
-
return styled_error("Please select a model type.")
|
| 41 |
-
|
| 42 |
-
# Does the model actually exist?
|
| 43 |
-
if revision == "":
|
| 44 |
-
revision = "main"
|
| 45 |
-
|
| 46 |
-
# Is the model on the hub?
|
| 47 |
-
if weight_type in ["Delta", "Adapter"]:
|
| 48 |
-
base_model_on_hub, error, _ = is_model_on_hub(model_name=base_model, revision=revision, token=TOKEN, test_tokenizer=True)
|
| 49 |
-
if not base_model_on_hub:
|
| 50 |
-
return styled_error(f'Base model "{base_model}" {error}')
|
| 51 |
-
|
| 52 |
-
if not weight_type == "Adapter":
|
| 53 |
-
model_on_hub, error, _ = is_model_on_hub(model_name=model, revision=revision, token=TOKEN, test_tokenizer=True)
|
| 54 |
-
if not model_on_hub:
|
| 55 |
-
return styled_error(f'Model "{model}" {error}')
|
| 56 |
-
|
| 57 |
-
# Is the model info correctly filled?
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
-
except
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
except
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
"
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
+
import queue
|
| 4 |
+
import re
|
| 5 |
+
import sys
|
| 6 |
+
import threading
|
| 7 |
+
import traceback
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
|
| 10 |
+
# Engine import
|
| 11 |
+
try:
|
| 12 |
+
from src.audit.engine import AuditError, check_feasibility, run_full_audit
|
| 13 |
+
except ImportError:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
from src.engine import AuditError, check_feasibility, run_full_audit
|
| 16 |
+
except ImportError:
|
| 17 |
+
try:
|
| 18 |
+
from audit.engine import AuditError, check_feasibility, run_full_audit
|
| 19 |
+
except ImportError:
|
| 20 |
+
from engine import AuditError, check_feasibility, run_full_audit
|
| 21 |
|
| 22 |
+
# Formatting import
|
| 23 |
+
try:
|
| 24 |
+
from src.display.formatting import (
|
| 25 |
+
build_top_3_cards_html,
|
| 26 |
+
render_audit_details_panel,
|
| 27 |
+
styled_error,
|
| 28 |
+
styled_loading,
|
| 29 |
+
styled_message,
|
| 30 |
+
)
|
| 31 |
+
except ImportError:
|
| 32 |
+
try:
|
| 33 |
+
from src.formatting import (
|
| 34 |
+
build_top_3_cards_html,
|
| 35 |
+
render_audit_details_panel,
|
| 36 |
+
styled_error,
|
| 37 |
+
styled_loading,
|
| 38 |
+
styled_message,
|
| 39 |
+
)
|
| 40 |
+
except ImportError:
|
| 41 |
+
try:
|
| 42 |
+
from display.formatting import (
|
| 43 |
+
build_top_3_cards_html,
|
| 44 |
+
render_audit_details_panel,
|
| 45 |
+
styled_error,
|
| 46 |
+
styled_loading,
|
| 47 |
+
styled_message,
|
| 48 |
+
)
|
| 49 |
+
except ImportError:
|
| 50 |
+
from formatting import (
|
| 51 |
+
build_top_3_cards_html,
|
| 52 |
+
render_audit_details_panel,
|
| 53 |
+
styled_error,
|
| 54 |
+
styled_loading,
|
| 55 |
+
styled_message,
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
# Utils import
|
| 59 |
+
try:
|
| 60 |
+
from src.display.utils import BENCHMARK_COLS, COLS
|
| 61 |
+
except ImportError:
|
| 62 |
try:
|
| 63 |
+
from src.utils import BENCHMARK_COLS, COLS
|
| 64 |
+
except ImportError:
|
| 65 |
+
try:
|
| 66 |
+
from display.utils import BENCHMARK_COLS, COLS
|
| 67 |
+
except ImportError:
|
| 68 |
+
from utils import BENCHMARK_COLS, COLS
|
| 69 |
+
|
| 70 |
+
# Envs & Populate imports
|
| 71 |
+
try:
|
| 72 |
+
from src.envs import AUDIT_DEVICE, EVAL_RESULTS_PATH, MAX_AUDIT_PARAMS_BILLION, TOKEN
|
| 73 |
+
except ImportError:
|
| 74 |
+
from envs import AUDIT_DEVICE, EVAL_RESULTS_PATH, MAX_AUDIT_PARAMS_BILLION, TOKEN
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
from src.populate import get_leaderboard_df, get_top_3_eval_cards
|
| 78 |
+
except ImportError:
|
| 79 |
+
from populate import get_leaderboard_df, get_top_3_eval_cards
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def clean_model_name(raw_name: str) -> str:
|
| 83 |
+
"""Robust extraction of model ID from HTML tags, markdown links, or plain text."""
|
| 84 |
+
if not raw_name:
|
| 85 |
+
return ""
|
| 86 |
+
name = str(raw_name).strip()
|
| 87 |
+
if name.lower() == "none":
|
| 88 |
+
return ""
|
| 89 |
+
|
| 90 |
+
# Extract from href="https://huggingface.co/..." or href="..."
|
| 91 |
+
href_match = re.search(r'href=["\'](?:https://huggingface\.co/)?([^"\']+)["\']', name)
|
| 92 |
+
if href_match and "/" in href_match.group(1):
|
| 93 |
+
return href_match.group(1).strip("/")
|
| 94 |
+
|
| 95 |
+
# Extract from markdown [text](https://huggingface.co/...)
|
| 96 |
+
md_match = re.search(r'\((?:https://huggingface\.co/)?([^)]+)\)', name)
|
| 97 |
+
if md_match and "/" in md_match.group(1):
|
| 98 |
+
return md_match.group(1).strip("/")
|
| 99 |
+
|
| 100 |
+
# Strip HTML tags
|
| 101 |
+
name = re.sub(r'<[^>]+>', '', name)
|
| 102 |
+
# Strip markdown label brackets [Model Name] -> Model Name
|
| 103 |
+
name = re.sub(r'\[([^\]]+)\]', r'\1', name)
|
| 104 |
+
name = name.replace("https://huggingface.co/", "")
|
| 105 |
+
return name.strip().strip("/")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def get_certificate_by_model_name(model_name: str) -> dict:
|
| 109 |
+
clean_name = clean_model_name(model_name)
|
| 110 |
+
if not clean_name or not os.path.exists(EVAL_RESULTS_PATH):
|
| 111 |
+
return {}
|
| 112 |
+
|
| 113 |
+
safe_prefix = clean_name.replace("/", "__")
|
| 114 |
+
for f in os.listdir(EVAL_RESULTS_PATH):
|
| 115 |
+
if f.startswith(safe_prefix) and f.endswith(".json"):
|
| 116 |
+
try:
|
| 117 |
+
with open(os.path.join(EVAL_RESULTS_PATH, f), "r", encoding="utf-8") as fp:
|
| 118 |
+
return json.load(fp)
|
| 119 |
+
except Exception:
|
| 120 |
+
continue
|
| 121 |
+
return {}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def _save_cert(model_id: str, revision: str, cert: dict) -> str:
|
| 125 |
+
os.makedirs(EVAL_RESULTS_PATH, exist_ok=True)
|
| 126 |
+
safe_name = model_id.replace("/", "__") + f"_{revision}.json"
|
| 127 |
+
out_path = os.path.join(EVAL_RESULTS_PATH, safe_name)
|
| 128 |
+
with open(out_path, "w", encoding="utf-8") as f:
|
| 129 |
+
json.dump(cert, f, indent=2)
|
| 130 |
+
return out_path
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def execute_direct_xray_audit(model_id: str, revision: str = "main", trust_remote_code: bool = False, progress_callback=None) -> dict:
|
| 134 |
+
now_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
|
| 135 |
|
| 136 |
+
if progress_callback:
|
| 137 |
+
progress_callback(f"Verifying '{model_id}' on Hugging Face Hub...")
|
| 138 |
+
print(f"[LLM-X-RAY] Verifying model '{model_id}'...", flush=True)
|
| 139 |
|
| 140 |
+
try:
|
| 141 |
+
feas = check_feasibility(model_id, revision, TOKEN, MAX_AUDIT_PARAMS_BILLION, trust_remote_code=trust_remote_code)
|
| 142 |
+
if not feas.ok:
|
| 143 |
+
cert = {
|
| 144 |
+
"status": "error",
|
| 145 |
+
"config": {"model_name": model_id, "model_sha": revision, "params": feas.param_count_b},
|
| 146 |
+
"error_message": feas.reason,
|
| 147 |
+
"audited_at": now_str,
|
| 148 |
+
}
|
| 149 |
+
_save_cert(model_id, revision, cert)
|
| 150 |
+
return cert
|
| 151 |
+
|
| 152 |
+
result = run_full_audit(
|
| 153 |
+
model_id=model_id,
|
| 154 |
+
revision=revision,
|
| 155 |
+
device=AUDIT_DEVICE,
|
| 156 |
+
token=TOKEN,
|
| 157 |
+
trust_remote_code=trust_remote_code,
|
| 158 |
+
progress_callback=progress_callback,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
cert = {
|
| 162 |
+
"status": "ok",
|
| 163 |
+
"config": {
|
| 164 |
+
"model_name": model_id,
|
| 165 |
+
"model_sha": revision,
|
| 166 |
+
"architecture": feas.architecture or "CausalLM",
|
| 167 |
+
"params": feas.param_count_b,
|
| 168 |
+
"precision": "bfloat16" if AUDIT_DEVICE == "cuda" else "float32",
|
| 169 |
+
"license": "open-source",
|
| 170 |
+
},
|
| 171 |
+
"audited_at": now_str,
|
| 172 |
+
**result,
|
| 173 |
+
}
|
| 174 |
+
_save_cert(model_id, revision, cert)
|
| 175 |
+
return cert
|
| 176 |
+
except Exception as e:
|
| 177 |
+
traceback.print_exc()
|
| 178 |
+
sys.stdout.flush()
|
| 179 |
+
cert = {
|
| 180 |
+
"status": "error",
|
| 181 |
+
"config": {"model_name": model_id, "model_sha": revision},
|
| 182 |
+
"error_message": str(e),
|
| 183 |
+
"audited_at": now_str,
|
| 184 |
+
}
|
| 185 |
+
_save_cert(model_id, revision, cert)
|
| 186 |
+
return cert
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def audit_or_search_model(url_or_id: str, trust_remote_code: bool = False):
|
| 190 |
+
clean_model = clean_model_name(url_or_id)
|
| 191 |
+
|
| 192 |
+
current_df = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS)
|
| 193 |
+
current_top3 = build_top_3_cards_html(get_top_3_eval_cards(EVAL_RESULTS_PATH))
|
| 194 |
+
current_panel = render_audit_details_panel({})
|
| 195 |
+
|
| 196 |
+
if not clean_model:
|
| 197 |
+
yield (
|
| 198 |
+
styled_error("Please enter a valid Hugging Face Model ID or URL (e.g. openbmb/MiniCPM-1B-sft-bf16)."),
|
| 199 |
+
current_df,
|
| 200 |
+
current_top3,
|
| 201 |
+
current_panel,
|
| 202 |
+
)
|
| 203 |
+
return
|
| 204 |
+
|
| 205 |
+
# Check for existing cert first
|
| 206 |
+
existing_cert = get_certificate_by_model_name(clean_model)
|
| 207 |
+
if existing_cert and existing_cert.get("status") == "ok":
|
| 208 |
+
yield (
|
| 209 |
+
styled_message(f"Loaded existing audit certificate for <b>{clean_model}</b>."),
|
| 210 |
+
current_df,
|
| 211 |
+
current_top3,
|
| 212 |
+
render_audit_details_panel(existing_cert),
|
| 213 |
+
)
|
| 214 |
+
return
|
| 215 |
+
|
| 216 |
+
latest_msg = f"Connecting to HF Hub for <b>{clean_model}</b>..."
|
| 217 |
+
yield (
|
| 218 |
+
styled_loading(latest_msg, "Verifying repository and downloading model tensors..."),
|
| 219 |
+
current_df,
|
| 220 |
+
current_top3,
|
| 221 |
+
current_panel,
|
| 222 |
)
|
| 223 |
+
|
| 224 |
+
progress_q: "queue.Queue" = queue.Queue()
|
| 225 |
+
result_holder: dict = {}
|
| 226 |
+
|
| 227 |
+
def _worker():
|
| 228 |
+
try:
|
| 229 |
+
result_holder["cert"] = execute_direct_xray_audit(
|
| 230 |
+
clean_model, "main", trust_remote_code=trust_remote_code, progress_callback=progress_q.put
|
| 231 |
+
)
|
| 232 |
+
except Exception as err:
|
| 233 |
+
traceback.print_exc()
|
| 234 |
+
sys.stdout.flush()
|
| 235 |
+
result_holder["cert"] = {
|
| 236 |
+
"status": "error",
|
| 237 |
+
"config": {"model_name": clean_model},
|
| 238 |
+
"error_message": str(err),
|
| 239 |
+
}
|
| 240 |
+
finally:
|
| 241 |
+
progress_q.put(None)
|
| 242 |
+
|
| 243 |
+
thread = threading.Thread(target=_worker, daemon=True)
|
| 244 |
+
thread.start()
|
| 245 |
+
|
| 246 |
+
while True:
|
| 247 |
+
try:
|
| 248 |
+
msg = progress_q.get(timeout=0.2)
|
| 249 |
+
if msg is None:
|
| 250 |
+
break
|
| 251 |
+
latest_msg = msg
|
| 252 |
+
yield (
|
| 253 |
+
styled_loading(latest_msg, "Executing multi-layer operator audit. Progress streams live below."),
|
| 254 |
+
current_df,
|
| 255 |
+
current_top3,
|
| 256 |
+
current_panel,
|
| 257 |
+
)
|
| 258 |
+
except queue.Empty:
|
| 259 |
+
if not thread.is_alive():
|
| 260 |
+
break
|
| 261 |
+
yield (
|
| 262 |
+
styled_loading(latest_msg, "Audit in progress... Please keep this page open."),
|
| 263 |
+
current_df,
|
| 264 |
+
current_top3,
|
| 265 |
+
current_panel,
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
thread.join()
|
| 269 |
+
cert = result_holder.get("cert", {})
|
| 270 |
+
|
| 271 |
+
updated_df = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS)
|
| 272 |
+
updated_top3 = build_top_3_cards_html(get_top_3_eval_cards(EVAL_RESULTS_PATH))
|
| 273 |
+
updated_panel = render_audit_details_panel(cert)
|
| 274 |
+
|
| 275 |
+
if cert.get("status") == "error":
|
| 276 |
+
err_msg = cert.get("error_message", "Unknown error.")
|
| 277 |
+
yield (
|
| 278 |
+
styled_error(f"Audit could not be completed for <b>{clean_model}</b>: {err_msg}"),
|
| 279 |
+
updated_df,
|
| 280 |
+
updated_top3,
|
| 281 |
+
updated_panel,
|
| 282 |
+
)
|
| 283 |
+
else:
|
| 284 |
+
yield (
|
| 285 |
+
styled_message(f"🎉 <b>Audit Complete for {clean_model}!</b> Certificate registered below."),
|
| 286 |
+
updated_df,
|
| 287 |
+
updated_top3,
|
| 288 |
+
updated_panel,
|
| 289 |
+
)
|