Spaces:
Running on Zero
Running on Zero
File size: 20,193 Bytes
2cdd95e | 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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | """
scripts/evaluate_shadow_metrics.py
==================================
Phase 4 Shadow Mode Analytics & Metric Evaluation Script.
Computes:
1. Field-level agreement rates (email, phone, name F1, title F1, skills Jaccard, exp delta)
2. Slice breakdown (PDF vs DOCX, page count, single vs two-column, length tiers)
3. SLM invalid JSON and timeout error rates
4. Validity-rate comparison (Legacy vs SLM: email, phone, name, dates)
5. Grounding / Hallucination failure rate and ungrounded entities breakdown
6. Latency distribution (p50, p95, min, max) and queue-wait impact
"""
import argparse
import json
import logging
import math
import os
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
CURRENT_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = CURRENT_DIR.parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("evaluate_shadow_metrics")
def percentile(values: List[float], p: float) -> float:
"""Computes the p-th percentile (0 <= p <= 100) from a list of floats."""
if not values:
return 0.0
sorted_vals = sorted(values)
k = (len(sorted_vals) - 1) * (p / 100.0)
f = math.floor(k)
c = math.ceil(k)
if f == c:
return sorted_vals[int(k)]
d0 = sorted_vals[int(f)] * (c - k)
d1 = sorted_vals[int(c)] * (k - f)
return round(d0 + d1, 2)
def fetch_shadow_logs_from_supabase(limit: int = 1000) -> List[Dict[str, Any]]:
"""Fetches recent shadow log rows from Supabase."""
from app.storage.resume_storage import get_supabase_client, is_supabase_configured
if not is_supabase_configured():
logger.warning("Supabase is not configured; cannot fetch remote shadow logs.")
return []
client = get_supabase_client()
if not client:
return []
from app.services.extraction_service import _get_table_ref
table = _get_table_ref(client, "extraction_shadow_logs")
res = table.select("*").order("createdAt", desc=True).limit(limit).execute()
return res.data if res and res.data else []
def analyze_shadow_logs(logs: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Analyzes a list of shadow log records and generates comprehensive metrics."""
if not logs:
return {"error": "No shadow logs available to analyze.", "total_runs": 0}
total_runs = len(logs)
# 1. Field-Level Agreements
email_matches = 0
phone_matches = 0
name_f1s: List[float] = []
title_f1s: List[float] = []
skills_jaccards: List[float] = []
skills_precisions: List[float] = []
skills_recalls: List[float] = []
exp_deltas: List[float] = []
edu_matches = 0
work_matches = 0
# 2. Validity Comparisons
slm_email_valid = 0
slm_phone_valid = 0
slm_name_plausible = 0
slm_dates_parse = 0
leg_email_valid = 0
leg_phone_valid = 0
leg_name_plausible = 0
leg_dates_parse = 0
# 3. Errors & Grounding
invalid_json_count = 0
timeout_count = 0
grounding_failures = 0
ungrounded_entities_tally: Dict[str, int] = {}
# 4. Latencies
latencies: List[float] = []
# 5. Slices
slices: Dict[str, Dict[str, Any]] = {}
for row in logs:
# Harmonize field agreements
agreements = row.get("fieldAgreements") or row.get("agreementScores") or {}
if isinstance(agreements, str):
try:
agreements = json.loads(agreements)
except Exception:
agreements = {}
if agreements.get("emailExactMatch"):
email_matches += 1
if agreements.get("phoneExactMatch"):
phone_matches += 1
name_f1s.append(float(agreements.get("nameTokenF1", 0.0)))
title_f1s.append(float(agreements.get("titleTokenF1", 0.0)))
skills_jaccards.append(float(agreements.get("skillsJaccard", 0.0)))
skills_precisions.append(float(agreements.get("skillsPrecision", 0.0)))
skills_recalls.append(float(agreements.get("skillsRecall", 0.0)))
exp_deltas.append(float(agreements.get("expDeltaYears", 0.0)))
if agreements.get("educationCountMatch"):
edu_matches += 1
if agreements.get("workExperienceCountMatch"):
work_matches += 1
# Validity Flags (SLM)
validity = row.get("validityFlags") or {}
if isinstance(validity, str):
try:
validity = json.loads(validity)
except Exception:
validity = {}
if validity.get("emailValid", True):
slm_email_valid += 1
if validity.get("phoneValid", True):
slm_phone_valid += 1
if validity.get("namePlausible", True):
slm_name_plausible += 1
if validity.get("datesParse", True):
slm_dates_parse += 1
# Legacy validity check from legacyOutput if present
leg_out = row.get("legacyOutput") or {}
if isinstance(leg_out, str):
try:
leg_out = json.loads(leg_out)
except Exception:
leg_out = {}
if leg_out:
from app.services.shadow_evaluator import compute_validity_flags
leg_val = compute_validity_flags(leg_out)
if leg_val.get("emailValid"):
leg_email_valid += 1
if leg_val.get("phoneValid"):
leg_phone_valid += 1
if leg_val.get("namePlausible"):
leg_name_plausible += 1
if leg_val.get("datesParse"):
leg_dates_parse += 1
else:
# Fallback assumption parity
leg_email_valid += 1 if validity.get("emailValid") else 0
leg_phone_valid += 1 if validity.get("phoneValid") else 0
leg_name_plausible += 1 if validity.get("namePlausible") else 0
leg_dates_parse += 1 if validity.get("datesParse") else 0
# Errors
errs = row.get("errorFlags") or row.get("discrepancies") or []
if isinstance(errs, str):
try:
errs = json.loads(errs)
except Exception:
errs = [errs]
is_valid_json = row.get("isValidJson")
if is_valid_json is False or "json_invalid" in errs or "json_parse_error" in errs:
invalid_json_count += 1
if "timeout" in errs:
timeout_count += 1
# Grounding
grounding = row.get("groundingFlags") or {}
if isinstance(grounding, str):
try:
grounding = json.loads(grounding)
except Exception:
grounding = {}
if grounding.get("allGrounded") is False:
grounding_failures += 1
for ent in grounding.get("ungroundedEntities", []):
prefix = ent.split(":")[0] if ":" in ent else "entity"
ungrounded_entities_tally[prefix] = ungrounded_entities_tally.get(prefix, 0) + 1
# Latency
lat = row.get("slmLatencyMs") or row.get("shadowDurationMs")
if lat is not None:
try:
latencies.append(float(lat))
except (ValueError, TypeError):
pass
# Slice Tracking
doc_format = row.get("docFormat") or ("docx" if "docx" in str(row.get("filename", "")).lower() else "pdf")
page_count = row.get("pageCount") or 1
is_two_col = bool(row.get("isTwoColumn", False))
char_len = int(row.get("charLength") or len(str(leg_out.get("resumeText") or "")))
length_tier = "short (<2k)" if char_len < 2000 else ("medium (2k-5k)" if char_len <= 5000 else "long (>5k)")
slice_key = f"{doc_format.upper()} | {page_count}p | {'2-Col' if is_two_col else '1-Col'} | {length_tier}"
if slice_key not in slices:
slices[slice_key] = {"count": 0, "title_f1s": [], "skills_jaccards": [], "latencies": []}
slices[slice_key]["count"] += 1
slices[slice_key]["title_f1s"].append(float(agreements.get("titleTokenF1", 0.0)))
slices[slice_key]["skills_jaccards"].append(float(agreements.get("skillsJaccard", 0.0)))
if lat is not None:
try:
slices[slice_key]["latencies"].append(float(lat))
except Exception:
pass
# Aggregate Slices
slice_breakdown = []
for sk, sdata in slices.items():
c = sdata["count"]
slice_breakdown.append({
"slice": sk,
"sample_count": c,
"avg_title_f1": round(sum(sdata["title_f1s"]) / c, 4) if c else 0.0,
"avg_skills_jaccard": round(sum(sdata["skills_jaccards"]) / c, 4) if c else 0.0,
"avg_latency_ms": round(sum(sdata["latencies"]) / len(sdata["latencies"]), 1) if sdata["latencies"] else 0.0,
})
def _avg(arr: List[float], decimals: int = 4) -> float:
return round(sum(arr) / len(arr), decimals) if arr else 0.0
return {
"generatedAt": datetime.now(timezone.utc).isoformat(),
"totalRuns": total_runs,
"agreements": {
"emailExactMatchPct": round((email_matches / total_runs) * 100, 2),
"phoneExactMatchPct": round((phone_matches / total_runs) * 100, 2),
"avgNameTokenF1": _avg(name_f1s, 4),
"avgTitleTokenF1": _avg(title_f1s, 4),
"avgSkillsJaccard": _avg(skills_jaccards, 4),
"avgSkillsPrecision": _avg(skills_precisions, 4),
"avgSkillsRecall": _avg(skills_recalls, 4),
"avgExpDeltaYears": _avg(exp_deltas, 2),
"educationCountMatchPct": round((edu_matches / total_runs) * 100, 2),
"workExperienceCountMatchPct": round((work_matches / total_runs) * 100, 2),
},
"validityComparison": {
"email": {
"legacyValidPct": round((leg_email_valid / total_runs) * 100, 2),
"slmValidPct": round((slm_email_valid / total_runs) * 100, 2),
},
"phone": {
"legacyValidPct": round((leg_phone_valid / total_runs) * 100, 2),
"slmValidPct": round((slm_phone_valid / total_runs) * 100, 2),
},
"name": {
"legacyValidPct": round((leg_name_plausible / total_runs) * 100, 2),
"slmValidPct": round((slm_name_plausible / total_runs) * 100, 2),
},
"dates": {
"legacyValidPct": round((leg_dates_parse / total_runs) * 100, 2),
"slmValidPct": round((slm_dates_parse / total_runs) * 100, 2),
},
},
"errors": {
"invalidJsonCount": invalid_json_count,
"invalidJsonRatePct": round((invalid_json_count / total_runs) * 100, 3),
"timeoutCount": timeout_count,
"timeoutRatePct": round((timeout_count / total_runs) * 100, 3),
"totalErrorRatePct": round(((invalid_json_count + timeout_count) / total_runs) * 100, 3),
},
"grounding": {
"groundingFailureCount": grounding_failures,
"groundingFailureRatePct": round((grounding_failures / total_runs) * 100, 3),
"groundedRatePct": round(((total_runs - grounding_failures) / total_runs) * 100, 3),
"ungroundedEntitiesBreakdown": ungrounded_entities_tally,
},
"latency": {
"p50Ms": percentile(latencies, 50.0),
"p95Ms": percentile(latencies, 95.0),
"minMs": round(min(latencies), 1) if latencies else 0.0,
"maxMs": round(max(latencies), 1) if latencies else 0.0,
"meanMs": round(sum(latencies) / len(latencies), 1) if latencies else 0.0,
},
"sliceBreakdown": slice_breakdown,
}
def generate_markdown_report(metrics: Dict[str, Any]) -> str:
"""Formats metrics dictionary into a clean markdown evaluation report."""
agr = metrics.get("agreements", {})
val = metrics.get("validityComparison", {})
err = metrics.get("errors", {})
gro = metrics.get("grounding", {})
lat = metrics.get("latency", {})
slices = metrics.get("sliceBreakdown", [])
md = [
"# ATS Resume Extraction: Phase 4 Shadow Mode Evaluation Report",
f"**Generated:** {metrics.get('generatedAt')} | **Total Shadow Evaluations:** {metrics.get('totalRuns')}",
"",
"## 1. Field-Level Agreement Rates (Legacy vs SLM)",
"| Field | Metric Type | Score / Rate |",
"| :--- | :--- | :--- |",
f"| **Email** | Exact String Match | **{agr.get('emailExactMatchPct')}%** |",
f"| **Phone** | Normalized Digits Match | **{agr.get('phoneExactMatchPct')}%** |",
f"| **Candidate Name** | Token F1 | **{agr.get('avgNameTokenF1')}** |",
f"| **Job Title** | Token F1 | **{agr.get('avgTitleTokenF1')}** |",
f"| **Skills** | Jaccard Overlap | **{agr.get('avgSkillsJaccard')}** (Prec: {agr.get('avgSkillsPrecision')}, Rec: {agr.get('avgSkillsRecall')}) |",
f"| **Total Experience** | Mean Absolute Delta | **{agr.get('avgExpDeltaYears')} years** |",
f"| **Education Count** | Exact Match | **{agr.get('educationCountMatchPct')}%** |",
f"| **Work Experience Count** | Exact Match | **{agr.get('workExperienceCountMatchPct')}%** |",
"",
"## 2. Validity-Rate Comparison (Legacy vs Fine-Tuned SLM)",
"| Field Check | Legacy Extractor | Fine-Tuned SLM | Delta |",
"| :--- | :--- | :--- | :--- |",
f"| **Valid RFC Email** | {val.get('email', {}).get('legacyValidPct')}% | {val.get('email', {}).get('slmValidPct')}% | {round(val.get('email', {}).get('slmValidPct', 0) - val.get('email', {}).get('legacyValidPct', 0), 2)}% |",
f"| **Valid Phone (>= 7 digits)** | {val.get('phone', {}).get('legacyValidPct')}% | {val.get('phone', {}).get('slmValidPct')}% | {round(val.get('phone', {}).get('slmValidPct', 0) - val.get('phone', {}).get('legacyValidPct', 0), 2)}% |",
f"| **Plausible Name (Non-blank/Valid)** | {val.get('name', {}).get('legacyValidPct')}% | {val.get('name', {}).get('slmValidPct')}% | {round(val.get('name', {}).get('slmValidPct', 0) - val.get('name', {}).get('legacyValidPct', 0), 2)}% |",
f"| **Dates Parse Successfully** | {val.get('dates', {}).get('legacyValidPct')}% | {val.get('dates', {}).get('slmValidPct')}% | {round(val.get('dates', {}).get('slmValidPct', 0) - val.get('dates', {}).get('legacyValidPct', 0), 2)}% |",
"",
"## 3. Reliability, Grounding & Error Rates",
"| Quality Gate | Value | Target Status |",
"| :--- | :--- | :--- |",
f"| **Invalid JSON Rate** | {err.get('invalidJsonRatePct')}% ({err.get('invalidJsonCount')} / {metrics.get('totalRuns')}) | {'PASS (<1%)' if err.get('invalidJsonRatePct', 0) < 1.0 else 'WARN'} |",
f"| **Upstream Timeout Rate** | {err.get('timeoutRatePct')}% ({err.get('timeoutCount')} / {metrics.get('totalRuns')}) | {'PASS (<1%)' if err.get('timeoutRatePct', 0) < 1.0 else 'WARN'} |",
f"| **Combined Error Rate** | {err.get('totalErrorRatePct')}% | {'PASS (<1%)' if err.get('totalErrorRatePct', 0) < 1.0 else 'FAIL'} |",
f"| **Grounding Failure Rate** | {gro.get('groundingFailureRatePct')}% | {'PASS (<2%)' if gro.get('groundingFailureRatePct', 0) < 2.0 else 'WARN'} |",
f"| **Overall Grounded Extractions** | {gro.get('groundedRatePct')}% | Supported by Resume Text |",
"",
"## 4. Latency Profile (Single Free-Tier vCPU Worker)",
f"- **Median Latency (p50):** {lat.get('p50Ms')} ms",
f"- **95th Percentile (p95):** {lat.get('p95Ms')} ms",
f"- **Mean Latency:** {lat.get('meanMs')} ms",
f"- **Range:** {lat.get('minMs')} ms – {lat.get('maxMs')} ms",
"",
"## 5. Slice Breakdown (Format, Layout & Length)",
"| Slice | Samples | Title F1 | Skills Jaccard | Avg Latency |",
"| :--- | :--- | :--- | :--- | :--- |",
]
for s in slices:
md.append(f"| {s['slice']} | {s['sample_count']} | {s['avg_title_f1']} | {s['avg_skills_jaccard']} | {s['avg_latency_ms']} ms |")
return "\n".join(md)
def main():
parser = argparse.ArgumentParser(description="Evaluate ATS Shadow Mode extraction metrics.")
parser.add_argument("--from-file", type=str, help="Path to local JSON file of shadow logs.")
parser.add_argument("--limit", type=int, default=500, help="Max records to pull from Supabase.")
parser.add_argument("--export-report", action="store_true", default=True, help="Save report to reports/ dir.")
args = parser.parse_args()
logs = []
if args.from_file and os.path.exists(args.from_file):
logger.info("Loading shadow logs from local file: %s", args.from_file)
with open(args.from_file, "r", encoding="utf-8") as f:
logs = json.load(f)
else:
logger.info("Fetching shadow logs from Supabase (limit=%d)...", args.limit)
logs = fetch_shadow_logs_from_supabase(limit=args.limit)
if not logs:
logger.warning("No logs found. Generating synthetic shadow evaluation fixture for demonstration/testing.")
from app.services.shadow_evaluator import (
compute_field_agreements,
compute_validity_flags,
compute_grounding_flags,
)
sample_leg = {
"candidateName": "David Chen",
"email": "david.chen@example.com",
"phone": "+1-415-555-0199",
"jobTitle": "Senior Distributed Systems Engineer",
"skills": ["Go", "Kubernetes", "gRPC", "Docker", "PostgreSQL"],
"totalExperienceYears": 6.5,
"education": [{"degree": "B.S. Computer Science"}],
"workExperience": [{"title": "Senior Distributed Systems Engineer", "company": "CloudTech", "durationYears": 4.0}],
"resumeText": "David Chen | david.chen@example.com | (415) 555-0199\nCloudTech Senior Distributed Systems Engineer\nSkills: Go, Kubernetes, gRPC, Docker, PostgreSQL",
}
sample_slm = {
"candidateName": "David Chen",
"email": "david.chen@example.com",
"phone": "(415) 555-0199",
"jobTitle": "Senior Distributed Systems Engineer (Go/K8s)",
"skills": ["Go", "Kubernetes", "gRPC", "Docker", "PostgreSQL", "Kafka"],
"totalExperienceYears": 6.5,
"education": [{"degree": "B.S. Computer Science"}],
"workExperience": [{"title": "Senior Distributed Systems Engineer", "company": "CloudTech", "durationYears": 4.0}],
}
agreements = compute_field_agreements(sample_leg, sample_slm)
validity = compute_validity_flags(sample_slm)
grounding = compute_grounding_flags(sample_slm, sample_leg["resumeText"])
logs = [{
"fileHash": "test_hash_12345",
"legacyVersion": "ats-legacy:heuristic-v1",
"slmVersion": "unsloth-qwen2.5-1.5b-q4_k_m",
"legacyOutput": sample_leg,
"slmOutput": sample_slm,
"fieldAgreements": agreements,
"validityFlags": validity,
"groundingFlags": grounding,
"slmLatencyMs": 1420.5,
"docFormat": "pdf",
"pageCount": 2,
"isTwoColumn": False,
"charLength": 2400,
"isValidJson": True,
"errorFlags": [],
}]
metrics = analyze_shadow_logs(logs)
report_md = generate_markdown_report(metrics)
print("\n" + report_md + "\n")
if args.export_report:
reports_dir = PROJECT_ROOT / "reports"
reports_dir.mkdir(parents=True, exist_ok=True)
rep_path = reports_dir / "shadow_evaluation_report.md"
json_path = reports_dir / "shadow_evaluation_summary.json"
with open(rep_path, "w", encoding="utf-8") as f:
f.write(report_md)
with open(json_path, "w", encoding="utf-8") as f:
json.dump(metrics, f, indent=2)
logger.info("Saved evaluation report to %s and %s", rep_path, json_path)
if __name__ == "__main__":
main()
|