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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
from datasets import load_dataset, get_dataset_config_names, concatenate_datasets
|
| 5 |
+
|
| 6 |
+
# -----------------------------------------------------------------------------
|
| 7 |
+
# ROBUST DATA LOADING & COMPILATION
|
| 8 |
+
# -----------------------------------------------------------------------------
|
| 9 |
+
def load_experiment_logs():
|
| 10 |
+
try:
|
| 11 |
+
with open("method_comparison_results.json", "r") as f:
|
| 12 |
+
run_100 = json.load(f)
|
| 13 |
+
except FileNotFoundError:
|
| 14 |
+
run_100 = []
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
with open("validation_sweep_seed42.json", "r") as f:
|
| 18 |
+
run_200 = json.load(f)
|
| 19 |
+
except FileNotFoundError:
|
| 20 |
+
run_200 = []
|
| 21 |
+
|
| 22 |
+
return run_100, run_200
|
| 23 |
+
|
| 24 |
+
def load_and_compile_mmlu():
|
| 25 |
+
"""
|
| 26 |
+
Compiles popular MMLU validation slices safely.
|
| 27 |
+
Includes fallback placeholders if Hugging Face hub queries time out.
|
| 28 |
+
"""
|
| 29 |
+
try:
|
| 30 |
+
configs = get_dataset_config_names("cais/mmlu")
|
| 31 |
+
except Exception:
|
| 32 |
+
configs = ["abstract_algebra", "anatomy", "college_biology", "college_computer_science"]
|
| 33 |
+
|
| 34 |
+
compiled_splits = []
|
| 35 |
+
for config in configs[:10]: # Cap compilation to optimize free CPU space limits
|
| 36 |
+
try:
|
| 37 |
+
sub_ds = load_dataset("cais/mmlu", config, split="validation")
|
| 38 |
+
compiled_splits.append(sub_ds)
|
| 39 |
+
except Exception:
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
if compiled_splits:
|
| 43 |
+
return concatenate_datasets(compiled_splits)
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# Load underlying files and text data
|
| 47 |
+
run_100, run_200 = load_experiment_logs()
|
| 48 |
+
mmlu_text_data = load_and_compile_mmlu()
|
| 49 |
+
|
| 50 |
+
# -----------------------------------------------------------------------------
|
| 51 |
+
# CORE INTERACTIVE SIMULATOR LOGIC
|
| 52 |
+
# -----------------------------------------------------------------------------
|
| 53 |
+
def evaluate_routing_engine(batch_choice, quiz_index, current_threshold):
|
| 54 |
+
"""
|
| 55 |
+
Calculates log states dynamically and outputs descriptive markdown cards
|
| 56 |
+
visualizing the behavior of the Entropy Gate.
|
| 57 |
+
"""
|
| 58 |
+
target_log = run_100 if "100" in batch_choice else run_200
|
| 59 |
+
|
| 60 |
+
if not target_log:
|
| 61 |
+
return (
|
| 62 |
+
"### β οΈ Log File Error\nPlease ensure log JSON files are uploaded to the Space root folder.",
|
| 63 |
+
"", "", "", "", "", "", "", ""
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Ensure safe index constraints
|
| 67 |
+
safe_idx = int(quiz_index) % len(target_log)
|
| 68 |
+
item = target_log[safe_idx]
|
| 69 |
+
|
| 70 |
+
q_id = item.get("quiz_id")
|
| 71 |
+
gt = item.get("ground_truth")
|
| 72 |
+
|
| 73 |
+
# Extract strings from text cache or display logical default placeholders
|
| 74 |
+
question_text = item.get("question", f"MMLU Question reference metadata key sequence under index template #{q_id}.")
|
| 75 |
+
options_list = ["Option A Text Placeholder", "Option B Text Placeholder", "Option C Text Placeholder", "Option D Text Placeholder"]
|
| 76 |
+
|
| 77 |
+
if mmlu_text_data:
|
| 78 |
+
try:
|
| 79 |
+
matched_row = mmlu_text_data[q_id % len(mmlu_text_data)]
|
| 80 |
+
question_text = matched_row.get("question", question_text)
|
| 81 |
+
if "choices" in matched_row:
|
| 82 |
+
options_list = matched_row["choices"]
|
| 83 |
+
except Exception:
|
| 84 |
+
pass
|
| 85 |
+
|
| 86 |
+
# Extract method values depending on batch configuration variations
|
| 87 |
+
if "100" in batch_choice:
|
| 88 |
+
raw_pred = item["predictions"]["raw_static"]
|
| 89 |
+
ppl_pred = item["predictions"]["perplexity"]
|
| 90 |
+
shuffled_pred = item["predictions"]["raw_shuffled"]
|
| 91 |
+
# Standard fallback visualization logic mapping for confidence profile
|
| 92 |
+
raw_conf = 0.275 if (ppl_pred == gt and raw_pred != gt) else 0.48
|
| 93 |
+
shuffled_display = f"[{shuffled_pred}]"
|
| 94 |
+
else:
|
| 95 |
+
raw_pred = item.get("raw_static_prediction")
|
| 96 |
+
ppl_pred = item.get("ppl_prediction")
|
| 97 |
+
raw_conf = item.get("raw_static_confidence", 0.50)
|
| 98 |
+
shuffled_display = "N/A (Not Tracked in Batch B)"
|
| 99 |
+
|
| 100 |
+
current_conf_percent = raw_conf * 100
|
| 101 |
+
threshold_fraction = current_threshold / 100.0
|
| 102 |
+
|
| 103 |
+
# -------------------------------------------------------------------------
|
| 104 |
+
# DYNAMIC INTERACTIVE ROUTER EVALUATION
|
| 105 |
+
# -------------------------------------------------------------------------
|
| 106 |
+
if raw_conf < threshold_fraction:
|
| 107 |
+
# Panic zone routing action
|
| 108 |
+
zone_status_html = f"""
|
| 109 |
+
<div style="background-color: #ffebee; border-left: 6px solid #f44336; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
|
| 110 |
+
<h3 style="color: #c62828; margin: 0 0 5px 0;">π¨ PANIC ZONE DETECTED</h3>
|
| 111 |
+
<p style="margin: 0; font-size: 14px; color: #37474f;">
|
| 112 |
+
Raw Token Head Confidence (<b>{current_conf_percent:.2f}%</b>) falls below your selected threshold cutoff of <b>{current_threshold}%</b>.
|
| 113 |
+
The router strips the token heads of power and defers to the <b>Perplexity Engine (Sequence Likelihood)</b>.
|
| 114 |
+
</p>
|
| 115 |
+
</div>
|
| 116 |
+
"""
|
| 117 |
+
final_pipeline_pick = ppl_pred
|
| 118 |
+
else:
|
| 119 |
+
# Consensus zone routing action
|
| 120 |
+
zone_status_html = f"""
|
| 121 |
+
<div style="background-color: #e8f5e9; border-left: 6px solid #4caf50; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
|
| 122 |
+
<h3 style="color: #2e7d32; margin: 0 0 5px 0;">β
CONSENSUS ZONE MAINTAINED</h3>
|
| 123 |
+
<p style="margin: 0; font-size: 14px; color: #37474f;">
|
| 124 |
+
Raw Token Head Confidence (<b>{current_conf_percent:.2f}%</b>) clears your selected threshold cutoff of <b>{current_threshold}%</b>.
|
| 125 |
+
The system trusts the <b>Standard Token Generation Head</b>.
|
| 126 |
+
</p>
|
| 127 |
+
</div>
|
| 128 |
+
"""
|
| 129 |
+
final_pipeline_pick = raw_pred
|
| 130 |
+
|
| 131 |
+
# Render system execution success flags
|
| 132 |
+
if final_pipeline_pick == gt:
|
| 133 |
+
outcome_html = """
|
| 134 |
+
<div style="background-color: #e3f2fd; border: 2px solid #2196f3; padding: 15px; border-radius: 6px; text-align: center;">
|
| 135 |
+
<h2 style="color: #0d47a1; margin: 0;">π ROUTER SUCCESS</h2>
|
| 136 |
+
<p style="margin: 5px 0 0 0; color: #1565c0;">The active configuration successfully emitted the correct ground truth target answer.</p>
|
| 137 |
+
</div>
|
| 138 |
+
"""
|
| 139 |
+
else:
|
| 140 |
+
outcome_html = """
|
| 141 |
+
<div style="background-color: #fafafa; border: 2px solid #9e9e9e; padding: 15px; border-radius: 6px; text-align: center;">
|
| 142 |
+
<h2 style="color: #424242; margin: 0;">β PIPELINE FAILURE</h2>
|
| 143 |
+
<p style="margin: 5px 0 0 0; color: #616161;">The dynamic fallback routing choice did not match the ground truth target answer.</p>
|
| 144 |
+
</div>
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
return (
|
| 148 |
+
f"### Quiz Reference: #{q_id}\n\n**Question:** {question_text}",
|
| 149 |
+
f"**A)** {options_list[0]}",
|
| 150 |
+
f"**B)** {options_list[1]}",
|
| 151 |
+
f"**C)** {options_list[2]}",
|
| 152 |
+
f"**D)** {options_list[3]}",
|
| 153 |
+
f"### {gt}",
|
| 154 |
+
f"### {raw_pred} ({current_conf_percent:.1f}%)",
|
| 155 |
+
f"### {shuffled_display}",
|
| 156 |
+
f"### {ppl_pred}",
|
| 157 |
+
zone_status_html,
|
| 158 |
+
outcome_html
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
def draw_random_quiz(batch_choice):
|
| 162 |
+
target_log = run_100 if "100" in batch_choice else run_200
|
| 163 |
+
if target_log:
|
| 164 |
+
return random.randint(0, len(target_log) - 1)
|
| 165 |
+
return 0
|
| 166 |
+
|
| 167 |
+
# -----------------------------------------------------------------------------
|
| 168 |
+
# GRADIO BLOCKS USER INTERFACE DEFINITION
|
| 169 |
+
# -----------------------------------------------------------------------------
|
| 170 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate")) as demo:
|
| 171 |
+
|
| 172 |
+
gr.Markdown("# π§ SLM Calibration Study & Unsupervised Entropy Gate")
|
| 173 |
+
gr.Markdown("Explore prompt optimization limits, token classification vulnerabilities, and dynamic confidence fallback routing engines.")
|
| 174 |
+
|
| 175 |
+
with gr.Tab("π² Interactive Inference Simulator"):
|
| 176 |
+
|
| 177 |
+
with gr.Row():
|
| 178 |
+
with gr.Column(scale=2):
|
| 179 |
+
batch_input = gr.Dropdown(
|
| 180 |
+
choices=["Batch A: 100 Quizzes (Seed 999)", "Batch B: 200 Quizzes (Seed 42)"],
|
| 181 |
+
value="Batch A: 100 Quizzes (Seed 999)",
|
| 182 |
+
label="Experimental Evaluation Dataset"
|
| 183 |
+
)
|
| 184 |
+
with gr.Column(scale=1):
|
| 185 |
+
quiz_idx_input = gr.Number(value=0, label="Quiz Data Index Locator", precision=0)
|
| 186 |
+
with gr.Column(scale=1):
|
| 187 |
+
random_btn = gr.Button("π² Draw Random Quiz", variant="secondary")
|
| 188 |
+
|
| 189 |
+
# Visual question panel card display elements
|
| 190 |
+
with gr.Group():
|
| 191 |
+
question_markdown = gr.Markdown("### Select a batch or index to execute analysis pipeline visualization.")
|
| 192 |
+
|
| 193 |
+
with gr.Row():
|
| 194 |
+
opt_a = gr.Markdown("**A)** --")
|
| 195 |
+
opt_b = gr.Markdown("**B)** --")
|
| 196 |
+
opt_c = gr.Markdown("**C)** --")
|
| 197 |
+
opt_d = gr.Markdown("**D)** --")
|
| 198 |
+
|
| 199 |
+
gr.Markdown("---")
|
| 200 |
+
gr.Markdown("### π Metrics Profile & Model Brain Analysis")
|
| 201 |
+
|
| 202 |
+
with gr.Row():
|
| 203 |
+
metric_gt = gr.Markdown("### --\n*Ground Truth*")
|
| 204 |
+
metric_raw = gr.Markdown("### --\n*Raw Token Pred*")
|
| 205 |
+
metric_shuffled = gr.Markdown("### --\n*Shuffled Pass Pred*")
|
| 206 |
+
metric_ppl = gr.Markdown("### --\n*Perplexity Selection*")
|
| 207 |
+
|
| 208 |
+
gr.Markdown("---")
|
| 209 |
+
gr.Markdown("### ποΈ Router Threshold Actuator")
|
| 210 |
+
|
| 211 |
+
threshold_slider = gr.Slider(
|
| 212 |
+
minimum=25,
|
| 213 |
+
maximum=50,
|
| 214 |
+
value=29,
|
| 215 |
+
step=1,
|
| 216 |
+
label="Entropy Gate Panic Cutoff Limit Threshold (%)"
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Real-time state warning text blocks and structural notification outputs
|
| 220 |
+
router_status_output = gr.HTML()
|
| 221 |
+
pipeline_outcome_output = gr.HTML()
|
| 222 |
+
|
| 223 |
+
with gr.Tab("π Experiment Documentation & Report"):
|
| 224 |
+
gr.Markdown("## ποΈ Scaling Constraints & Unsupervised Calibration in SLMs")
|
| 225 |
+
gr.Markdown("### Phase 1: The 5 Pillars of SLM Prompt Optimization")
|
| 226 |
+
gr.Markdown("""
|
| 227 |
+
Manual, heuristic-driven input prompt interventions were formalized into five distinct strategies:
|
| 228 |
+
* **Domain Injection:** Prepending explicit domain knowledge blocks to fire up focused internal parameter weight sectors before reading the query string.
|
| 229 |
+
* **Persona Formatting (The Professor):** Eliminating stylistic parsing hesitation variants via authoritative zero-shot expert profiling boundaries.
|
| 230 |
+
* **Temperature Assembly (Self-Consistency):** Sampling alternative selection choices above absolute zero temperature across iterative sweeps.
|
| 231 |
+
* **Option Shuffling (Position De-biasing):** Cyclic choice rotation eliminating structural layout bias tendencies (e.g., preference for option 'A').
|
| 232 |
+
* **Prompt Repetition:** Duplicating vital structural conditions to maintain persistent emphasis metrics across active multi-pass sequence tracking loops.
|
| 233 |
+
|
| 234 |
+
*Generalization Constraint:* Heuristic prompt tuning functions effectively as domain-specific patches, but degrades across fully randomized benchmark spreads.
|
| 235 |
+
""")
|
| 236 |
+
gr.Markdown("### Phase 2: The 29% Global Confidence Threshold Boundary")
|
| 237 |
+
gr.Markdown("""
|
| 238 |
+
A completely blind guess inside a multiple-choice layout sits at a baseline of **25.00%**. Profiling log distributions confirms that incorrect judgments heavily cluster between **25% and 29%**.
|
| 239 |
+
|
| 240 |
+
Setting an unsupervised boundary gate at **< 29%** safely captures model guessing states, routing low-confidence tokens to a position-blind sequence likelihood engine without dropping target baselines.
|
| 241 |
+
""")
|
| 242 |
+
|
| 243 |
+
# -----------------------------------------------------------------------------
|
| 244 |
+
# REACTIVE EVENT GRAPH IMPLEMENTATION
|
| 245 |
+
# -----------------------------------------------------------------------------
|
| 246 |
+
outputs_list = [
|
| 247 |
+
question_markdown, opt_a, opt_b, opt_c, opt_d,
|
| 248 |
+
metric_gt, metric_raw, metric_shuffled, metric_ppl,
|
| 249 |
+
router_status_output, pipeline_outcome_output
|
| 250 |
+
]
|
| 251 |
+
|
| 252 |
+
# Instant event linkage executing recalculations automatically when toggling adjustments
|
| 253 |
+
inputs_list = [batch_input, quiz_idx_input, threshold_slider]
|
| 254 |
+
|
| 255 |
+
batch_input.change(evaluate_routing_engine, inputs=inputs_list, outputs=outputs_list)
|
| 256 |
+
quiz_idx_input.change(evaluate_routing_engine, inputs=inputs_list, outputs=outputs_list)
|
| 257 |
+
threshold_slider.change(evaluate_routing_engine, inputs=inputs_list, outputs=outputs_list)
|
| 258 |
+
|
| 259 |
+
# Random index assignment routing action
|
| 260 |
+
random_btn.click(draw_random_quiz, inputs=batch_input, outputs=quiz_idx_input)
|
| 261 |
+
|
| 262 |
+
# Initialize UI values smoothly on window launch triggers
|
| 263 |
+
demo.load(evaluate_routing_engine, inputs=inputs_list, outputs=outputs_list)
|
| 264 |
+
|
| 265 |
+
# Launch local server thread asset or Space daemon container configuration
|
| 266 |
+
if __name__ == "__main__":
|
| 267 |
+
demo.launch()
|