Spaces:
Running on Zero
Running on Zero
File size: 15,994 Bytes
2276e5e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | """
Benchmark Test Script for Plain-English Translator
Compares SMC outputs from different models against Claude Opus 4.5 benchmarks.
"""
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import random
import json
from datetime import datetime
# ============================================================================
# CLAUDE OPUS 4.5 BENCHMARK TRANSLATIONS
# ============================================================================
BENCHMARKS = {
"Legal": {
"Force Majeure clause and why it might void our contract":
"This is a 'nobody's fault' escape hatch. If something huge and uncontrollable happens—like a massive earthquake, a war, or a pandemic—neither of us can be blamed for not keeping our promises. It's like if you promised to meet a friend but a tornado blocked every road; you couldn't get there, but it wasn't your fault.",
"Why we need to add an indemnification clause to protect your business":
"This is a 'you cover me, I cover you' promise. If someone sues us because of something your side did wrong, you agree to pay for our defense and any costs. Think of it like agreeing that whoever spills the milk has to clean it up and pay for a new carton.",
"What happens if the other party breaches the non-compete agreement":
"They promised not to start a competing business or work for your rivals. If they break that promise, you can take them to court and ask a judge to make them stop and possibly pay you money for the customers you lost."
},
"Medical": {
"Your MRI shows a benign lesion that we should monitor":
"We found a small spot on your scan, but it's not cancer—it's harmless. Think of it like a freckle on your skin. We just want to check on it every few months to make sure it stays the same size and doesn't change.",
"The etiology of your chronic fatigue syndrome":
"We're trying to find the root cause of why you feel exhausted all the time. It's like being a detective figuring out why a car won't start—is it the battery, the fuel, or something else? Your tiredness could come from a virus you had, stress, or how your immune system is working.",
"Why we're recommending prophylactic treatment given your comorbidities":
"Because you have several health conditions at once, we want to give you medicine now to prevent a problem before it happens. It's like putting on sunscreen before going to the beach—we're protecting you ahead of time because you're at higher risk."
},
"Financial": {
"How compound interest and amortization affect your mortgage payments":
"Your loan grows because you pay interest on interest—money owed on money already owed. Your monthly payment is split between paying down what you borrowed and paying the bank for lending it to you. Early on, most goes to the bank; later, more chips away at what you owe.",
"Why we recommend diversifying your portfolio with low-liquidity assets":
"Don't put all your eggs in one basket. We suggest putting some money into things that are harder to sell quickly—like real estate or private businesses—because they often grow more over time, even though you can't turn them into cash overnight like stocks.",
"The tax implications of depreciation on your rental property":
"The government lets you pretend your rental building loses value each year on paper, even if it's actually worth more. This 'paper loss' reduces the income you report, so you pay less in taxes now. It's like getting a discount for wear and tear that hasn't really happened yet."
},
"Technical/Engineering": {
"Why our API has high latency and how microservices could help":
"Our system is slow to respond because everything runs through one big, overloaded program. Imagine one cashier serving an entire grocery store. Breaking it into smaller, specialized services is like opening more checkout lanes—each handles one type of task faster.",
"The difference between synchronous and asynchronous processing":
"Synchronous means waiting in line—you can't order your coffee until the person ahead finishes. Asynchronous means you order, step aside, and they call your name when it's ready. The second way lets more people order at once without everyone standing around waiting.",
"Why we need to refactor the legacy codebase before adding new features":
"Our old code is like a cluttered garage—you can barely find anything, and adding new stuff just makes the mess worse. We need to organize and clean it up first. Otherwise, every new feature takes twice as long and breaks things we didn't expect."
}
}
# ============================================================================
# JARGON DICTIONARIES
# ============================================================================
JARGON_DICTIONARIES = {
"Legal": [
"liability", "liable", "indemnify", "indemnification", "breach",
"statute", "damages", "negligence", "herein", "aforementioned",
"plaintiff", "defendant", "jurisdiction", "arbitration", "tort",
"fiduciary", "escrow", "lien", "deposition", "stipulation",
"injunction", "subpoena", "affidavit", "adjudicate", "appellant"
],
"Medical": [
"prognosis", "diagnosis", "etiology", "pathology", "contraindicated",
"idiopathic", "nosocomial", "comorbidity", "prophylactic", "benign",
"malignant", "metastasis", "hemorrhage", "ischemia", "infarction",
"edema", "necrosis", "lesion", "syndrome", "acute", "chronic",
"bilateral", "unilateral", "subcutaneous", "intravenous"
],
"Financial": [
"amortization", "liquidity", "collateral", "derivative", "equity",
"fiduciary", "hedge", "leverage", "portfolio", "securities",
"dividend", "depreciation", "liability", "asset", "accrual",
"arbitrage", "capitalization", "yield", "maturity", "principal",
"compound", "annuity", "underwriting", "insolvency", "solvency"
],
"Technical/Engineering": [
"algorithm", "bandwidth", "latency", "throughput", "scalability",
"deprecated", "refactor", "polymorphism", "encapsulation", "abstraction",
"iteration", "recursion", "synchronous", "asynchronous", "protocol",
"middleware", "backend", "frontend", "deployment", "infrastructure",
"microservices", "containerization", "orchestration", "API", "SDK"
]
}
# ============================================================================
# MODELS TO TEST
# ============================================================================
MODELS = {
"TinyLlama-1.1B": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
"Qwen2-0.5B": "Qwen/Qwen2-0.5B-Instruct",
"Gemma-2-2B": "google/gemma-2-2b-it",
}
# ============================================================================
# SMC FUNCTIONS
# ============================================================================
def is_safe(text: str, banned_words: list) -> bool:
text_lower = text.lower()
for word in banned_words:
word_lower = word.lower()
if (f" {word_lower} " in f" {text_lower} " or
f" {word_lower}." in f" {text_lower}" or
f" {word_lower}," in f" {text_lower}" or
f" {word_lower}?" in f" {text_lower}" or
f" {word_lower}!" in f" {text_lower}" or
text_lower.startswith(f"{word_lower} ") or
text_lower.endswith(f" {word_lower}")):
return False
return True
def find_jargon_used(text: str, banned_words: list) -> list:
text_lower = text.lower()
found = []
for word in banned_words:
word_lower = word.lower()
if (f" {word_lower} " in f" {text_lower} " or
f" {word_lower}." in f" {text_lower}" or
f" {word_lower}," in f" {text_lower}" or
f" {word_lower}?" in f" {text_lower}" or
f" {word_lower}!" in f" {text_lower}" or
text_lower.startswith(f"{word_lower} ") or
text_lower.endswith(f" {word_lower}")):
found.append(word)
return list(set(found))
def smc_translate(concept, profession, tokenizer, model, num_particles=5, max_steps=20, tokens_per_step=4):
banned_words = JARGON_DICTIONARIES.get(profession, [])
prompt = f"""You are an expert {profession.lower()} professional explaining a concept to a client with no background in your field.
Rules:
- Explain as if talking to a curious 10-year-old
- Use a concrete, relatable real-world example to illustrate the concept
- Avoid redundancy (don't say "X is Y such as Y")
- Keep it concise: 2-3 sentences max
Concept to explain: {concept}
Simple explanation with example:"""
particles = [prompt]
for step in range(max_steps):
candidates = []
for particle in particles:
inputs = tokenizer(particle, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=tokens_per_step,
num_return_sequences=3,
do_sample=True,
temperature=0.8,
top_p=0.9,
pad_token_id=tokenizer.eos_token_id
)
for out in outputs:
decoded = tokenizer.decode(out, skip_special_tokens=True)
candidates.append(decoded)
valid_candidates = [c for c in candidates if is_safe(c, banned_words)]
if valid_candidates:
unique_candidates = list(set(valid_candidates))
random.shuffle(unique_candidates)
particles = unique_candidates[:num_particles]
else:
break
current_text = particles[0].split("Simple explanation with example:")[-1].strip()
# Only stop if we have a good amount of text ending with punctuation
if current_text.endswith(('.', '!', '?')) and len(current_text) > 100:
break
final_text = particles[0].split("Simple explanation with example:")[-1].strip()
jargon_found = find_jargon_used(final_text, banned_words)
return final_text, jargon_found
def grade_output(output, benchmark, jargon_found, profession):
"""Grade the output on multiple criteria."""
scores = {}
# 1. Jargon-free (0-25 points)
if len(jargon_found) == 0:
scores['jargon_free'] = 25
else:
scores['jargon_free'] = max(0, 25 - (len(jargon_found) * 10))
# 2. Has example/analogy (0-25 points)
example_indicators = ['like', 'imagine', 'think of', 'for example', 'such as', 'similar to', 'as if']
has_example = any(ind in output.lower() for ind in example_indicators)
scores['has_example'] = 25 if has_example else 0
# 3. Appropriate length (0-25 points) - not too short, not too long
word_count = len(output.split())
if 20 <= word_count <= 100:
scores['length'] = 25
elif 10 <= word_count < 20 or 100 < word_count <= 150:
scores['length'] = 15
else:
scores['length'] = 5
# 4. Coherence - ends properly (0-25 points)
if output.strip().endswith(('.', '!', '?')):
scores['coherence'] = 25
elif len(output) > 30:
scores['coherence'] = 15
else:
scores['coherence'] = 5
total = sum(scores.values())
return scores, total
# ============================================================================
# MAIN TEST RUNNER
# ============================================================================
def run_benchmark_tests(models_to_test=None):
"""Run all benchmark tests and return results."""
if models_to_test is None:
models_to_test = list(MODELS.keys())
results = {
"timestamp": datetime.now().isoformat(),
"models": {},
"summary": {}
}
# Load models
loaded_models = {}
loaded_tokenizers = {}
for model_name in models_to_test:
if model_name not in MODELS:
print(f"⚠️ Unknown model: {model_name}, skipping...")
continue
model_id = MODELS[model_name]
print(f"\n📦 Loading {model_name}...")
try:
loaded_tokenizers[model_name] = AutoTokenizer.from_pretrained(model_id)
loaded_models[model_name] = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.float16
)
print(f"✅ {model_name} loaded successfully")
except Exception as e:
print(f"❌ Failed to load {model_name}: {e}")
continue
# Run tests
for model_name in loaded_models.keys():
print(f"\n{'='*60}")
print(f"🧪 Testing {model_name}")
print('='*60)
results["models"][model_name] = {
"tests": [],
"total_score": 0,
"max_possible": 0
}
tokenizer = loaded_tokenizers[model_name]
model = loaded_models[model_name]
for profession, examples in BENCHMARKS.items():
for concept, benchmark in examples.items():
print(f"\n📝 {profession}: {concept[:50]}...")
try:
output, jargon_found = smc_translate(
concept, profession, tokenizer, model,
num_particles=5, max_steps=25, tokens_per_step=6
)
scores, total = grade_output(output, benchmark, jargon_found, profession)
test_result = {
"profession": profession,
"concept": concept,
"benchmark": benchmark,
"output": output,
"jargon_found": jargon_found,
"scores": scores,
"total_score": total
}
results["models"][model_name]["tests"].append(test_result)
results["models"][model_name]["total_score"] += total
results["models"][model_name]["max_possible"] += 100
print(f" Output: {output[:100]}...")
print(f" Jargon found: {jargon_found if jargon_found else 'None ✅'}")
print(f" Score: {total}/100")
except Exception as e:
print(f" ❌ Error: {e}")
results["models"][model_name]["tests"].append({
"profession": profession,
"concept": concept,
"error": str(e),
"total_score": 0
})
results["models"][model_name]["max_possible"] += 100
# Calculate summary
print(f"\n{'='*60}")
print("📊 FINAL RESULTS")
print('='*60)
for model_name, data in results["models"].items():
pct = (data["total_score"] / data["max_possible"] * 100) if data["max_possible"] > 0 else 0
results["summary"][model_name] = {
"total_score": data["total_score"],
"max_possible": data["max_possible"],
"percentage": round(pct, 1)
}
print(f"\n{model_name}:")
print(f" Total Score: {data['total_score']}/{data['max_possible']} ({pct:.1f}%)")
# Save results
with open("benchmark_results.json", "w") as f:
json.dump(results, f, indent=2)
print(f"\n💾 Results saved to benchmark_results.json")
return results
if __name__ == "__main__":
import sys
# Allow specifying which models to test via command line
if len(sys.argv) > 1:
models_to_test = sys.argv[1:]
else:
# Default: test all models
models_to_test = None
results = run_benchmark_tests(models_to_test)
|