Download test/test_eval_run_summary_csv.py from seanpedrickcase/document_redaction: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/spaces/seanpedrickcase/document_redaction/resolve/main/test/test_eval_run_summary_csv.py
- Command line
-
hf download hf://spaces/seanpedrickcase/document_redaction/test/test_eval_run_summary_csv.py
-
curl -L -o test_eval_run_summary_csv.py https://huggingface.co/spaces/seanpedrickcase/document_redaction/resolve/main/test/test_eval_run_summary_csv.py
10 kB
| """Tests for eval_run_summary_csv append log.""" | |
| from __future__ import annotations | |
| import csv | |
| import json | |
| import sys | |
| from pathlib import Path | |
| _REPO = Path(__file__).resolve().parents[1] | |
| _EVAL = _REPO / "agent-redact" / "eval" | |
| if str(_EVAL) not in sys.path: | |
| sys.path.insert(0, str(_EVAL)) | |
| from eval_run_summary_csv import ( # noqa: E402 | |
| SUMMARY_CSV_COLUMNS, | |
| append_eval_run_summary_row, | |
| rebuild_eval_run_summary_csv, | |
| run_stack_from_dir, | |
| ) | |
| def test_append_eval_run_summary_row(tmp_path, monkeypatch): | |
| monkeypatch.setenv("AGENT_EVAL_DEEPEVAL_TRACING", "1") | |
| monkeypatch.setenv( | |
| "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv") | |
| ) | |
| run_dir = tmp_path / "run_20261104_122626" | |
| run_dir.mkdir() | |
| summary = { | |
| "document_name": "doc.pdf", | |
| "gold_case": "/gold/example_live", | |
| "overall_pass": False, | |
| "workflow_incomplete": False, | |
| "eval_turn_kind": "initial_task", | |
| "follow_up_steer_count": 0, | |
| "metrics": [ | |
| {"name": "policy_strict", "pass": False, "score": 0.0, "skipped": False}, | |
| {"name": "tool_correctness", "pass": True, "score": 1.0, "skipped": False}, | |
| {"name": "task_completion", "pass": True, "score": 0.9, "skipped": False}, | |
| ], | |
| } | |
| (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8") | |
| (run_dir / "policy_gate.json").write_text( | |
| json.dumps({"pass_strict": False}), encoding="utf-8" | |
| ) | |
| (run_dir / "gold_geometry.json").write_text( | |
| json.dumps({"f1": 0.9, "recall": 0.88, "precision": 0.92}), | |
| encoding="utf-8", | |
| ) | |
| (run_dir / "ocr_gold.json").write_text( | |
| json.dumps({"skipped": False, "f1": 0.95, "recall": 0.94, "precision": 0.96}), | |
| encoding="utf-8", | |
| ) | |
| (run_dir / "policy_extraction.json").write_text( | |
| json.dumps( | |
| { | |
| "must_redact": {"f1": 1.0}, | |
| "deny_list": {"f1": 0.8}, | |
| } | |
| ), | |
| encoding="utf-8", | |
| ) | |
| out = append_eval_run_summary_row( | |
| summary=summary, | |
| run_dir=run_dir, | |
| task_instructions="Redact names", | |
| run_id="gradio_abc_20261104_122626", | |
| ) | |
| assert out is not None | |
| assert out.is_file() | |
| with out.open(encoding="utf-8", newline="") as handle: | |
| rows = list(csv.DictReader(handle)) | |
| assert len(rows) == 1 | |
| row = rows[0] | |
| assert row["run_id"] == "gradio_abc_20261104_122626" | |
| assert row["document_name"] == "doc.pdf" | |
| assert row["gold_case_path"] == "/gold/example_live" | |
| assert row["run_timestamp_utc"] == "2026-11-04T12:26:26Z" | |
| assert row["policy_strict_pass"] == "False" | |
| assert row["tool_correctness_pass"] == "True" | |
| assert row["pass_strict_from_gate"] == "false" | |
| assert float(row["gold_geometry_f1"]) == 0.9 | |
| assert row["gold_geometry_skipped"] == "False" | |
| assert float(row["ocr_gold_f1"]) == 0.95 | |
| assert row["ocr_gold_skipped"] == "False" | |
| assert float(row["policy_extraction_must_redact_f1"]) == 1.0 | |
| assert float(row["policy_extraction_deny_list_f1"]) == 0.8 | |
| assert "eval_summary.json" in row["output_files"] | |
| assert set(row.keys()) == set(SUMMARY_CSV_COLUMNS) | |
| def test_run_stack_from_artifacts_and_csv_columns(tmp_path, monkeypatch): | |
| monkeypatch.delenv("AGENT_DEFAULT_MODEL", raising=False) | |
| monkeypatch.delenv("AGENT_DEFAULT_PROVIDER", raising=False) | |
| monkeypatch.setenv("AGENT_EVAL_DEEPEVAL_TRACING", "1") | |
| monkeypatch.setenv( | |
| "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv") | |
| ) | |
| run_dir = tmp_path / "run_20261104_170815" | |
| run_dir.mkdir() | |
| (run_dir / "gradio_run_meta.json").write_text( | |
| json.dumps( | |
| { | |
| "orchestrator": "langgraph", | |
| "model_name": "llama/gemma-4-31b", | |
| "ocr_method": "paddle", | |
| "pii_method": "aws_comprehend", | |
| "pii_llm_model": "amazon.nova-pro-v1:0", | |
| "chosen_local_ocr_model": "hybrid-paddle-vlm", | |
| "chosen_ocr_model": "qwen_3_5_27b", | |
| "encourage_vlm_faces": True, | |
| "encourage_vlm_signatures": False, | |
| } | |
| ), | |
| encoding="utf-8", | |
| ) | |
| summary = { | |
| "document_name": "doc.pdf", | |
| "overall_pass": True, | |
| "workflow_incomplete": False, | |
| "metrics": [], | |
| } | |
| (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8") | |
| stack = run_stack_from_dir(run_dir) | |
| assert stack["model_name"] == "llama/gemma-4-31b" | |
| assert stack["ocr_method"] == "paddle" | |
| assert stack["chosen_local_ocr_model"] == "hybrid-paddle-vlm" | |
| assert stack["chosen_ocr_model"] == "qwen_3_5_27b" | |
| append_eval_run_summary_row( | |
| summary=summary, | |
| run_dir=run_dir, | |
| run_id="gradio_test", | |
| ) | |
| with (tmp_path / "eval_run_summary.csv").open( | |
| encoding="utf-8", newline="" | |
| ) as handle: | |
| row = next(csv.DictReader(handle)) | |
| assert row["model_name"] == "llama/gemma-4-31b" | |
| assert row["orchestrator"] == "langgraph" | |
| assert row["agent_orchestrator"] == "langgraph" | |
| assert row["agent_model_name"] == "llama/gemma-4-31b" | |
| assert row["ocr_method"] == "paddle" | |
| assert row["pii_method"] == "aws_comprehend" | |
| assert row["pii_llm_model"] == "amazon.nova-pro-v1:0" | |
| assert row["chosen_local_ocr_model"] == "hybrid-paddle-vlm" | |
| assert row["chosen_ocr_model"] == "qwen_3_5_27b" | |
| assert row["encourage_vlm_faces"] == "true" | |
| assert row["encourage_vlm_signatures"] == "false" | |
| def test_main_app_row_does_not_inherit_agent_env(tmp_path, monkeypatch): | |
| """Non-agentic (main_app) rows must not pick up ambient AGENT_* env values.""" | |
| monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV", "1") | |
| monkeypatch.setenv( | |
| "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv") | |
| ) | |
| monkeypatch.setenv("AGENT_DEFAULT_PROVIDER", "amazon-bedrock") | |
| monkeypatch.setenv("AGENT_DEFAULT_MODEL", "amazon.nova-pro-v1:0") | |
| monkeypatch.setenv("AGENT_ORCHESTRATOR", "pi") | |
| run_dir = tmp_path / "run_20261104_180000" | |
| run_dir.mkdir() | |
| (run_dir / "app_run_meta.json").write_text( | |
| json.dumps( | |
| { | |
| "document_name": "doc.pdf", | |
| "ocr_method": "AWS Textract", | |
| "pii_method": "AWS Comprehend", | |
| "chosen_ocr_model": "", | |
| "eval_profile": "app_gradio", | |
| } | |
| ), | |
| encoding="utf-8", | |
| ) | |
| summary = { | |
| "document_name": "doc.pdf", | |
| "overall_pass": True, | |
| "metrics": [], | |
| "eval_turn_kind": "app_redact", | |
| } | |
| (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8") | |
| append_eval_run_summary_row( | |
| summary=summary, | |
| run_dir=run_dir, | |
| run_id="app_test", | |
| run_source="main_app", | |
| ) | |
| with (tmp_path / "eval_run_summary.csv").open( | |
| encoding="utf-8", newline="" | |
| ) as handle: | |
| row = next(csv.DictReader(handle)) | |
| assert row["run_source"] == "main_app" | |
| assert row["orchestrator"] == "" | |
| assert row["model_name"] == "" | |
| assert row["agent_orchestrator"] == "" | |
| assert row["agent_model_name"] == "" | |
| assert row["ocr_method"] == "AWS Textract" | |
| assert row["pii_method"] == "AWS Comprehend" | |
| assert row["pii_llm_model"] == "" | |
| def test_agent_row_falls_back_to_agent_env(tmp_path, monkeypatch): | |
| """Agentic rows without meta still resolve orchestrator/model from AGENT_* env.""" | |
| monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV", "1") | |
| monkeypatch.setenv( | |
| "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv") | |
| ) | |
| monkeypatch.setenv("AGENT_DEFAULT_PROVIDER", "llama-cpp") | |
| monkeypatch.setenv("AGENT_DEFAULT_MODEL", "unsloth/Inkling-2-Mini-GGUF") | |
| monkeypatch.setenv("AGENT_ORCHESTRATOR", "pi") | |
| run_dir = tmp_path / "run_20261104_181500" | |
| run_dir.mkdir() | |
| summary = {"document_name": "doc.pdf", "overall_pass": True, "metrics": []} | |
| (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8") | |
| append_eval_run_summary_row( | |
| summary=summary, | |
| run_dir=run_dir, | |
| run_id="agent_test", | |
| run_source="agent_gradio", | |
| ) | |
| with (tmp_path / "eval_run_summary.csv").open( | |
| encoding="utf-8", newline="" | |
| ) as handle: | |
| row = next(csv.DictReader(handle)) | |
| assert row["orchestrator"] == "pi" | |
| assert row["model_name"] == "llama-cpp/unsloth/Inkling-2-Mini-GGUF" | |
| assert row["agent_orchestrator"] == "pi" | |
| assert row["agent_model_name"] == "llama-cpp/unsloth/Inkling-2-Mini-GGUF" | |
| def test_run_stack_manifest_fallback(tmp_path): | |
| run_dir = tmp_path / "run_manifest" | |
| run_dir.mkdir() | |
| (run_dir / "manifest.json").write_text( | |
| json.dumps( | |
| { | |
| "ocr_method": "pymupdf", | |
| "pii_method": "spacy", | |
| "run_model_name": "openai/gpt-4o", | |
| "run_chosen_ocr_model": "gemma_3_27b", | |
| } | |
| ), | |
| encoding="utf-8", | |
| ) | |
| stack = run_stack_from_dir(run_dir) | |
| assert stack["ocr_method"] == "pymupdf" | |
| assert stack["pii_method"] == "spacy" | |
| assert stack["model_name"] == "openai/gpt-4o" | |
| assert stack["chosen_ocr_model"] == "gemma_3_27b" | |
| def test_rebuild_from_runs(tmp_path, monkeypatch): | |
| monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "rebuilt.csv")) | |
| runs = tmp_path / "runs" | |
| run_dir = runs / "gradio_x_20261104_100000" | |
| run_dir.mkdir(parents=True) | |
| summary = { | |
| "document_name": "a.pdf", | |
| "overall_pass": True, | |
| "metrics": [], | |
| } | |
| (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8") | |
| count = rebuild_eval_run_summary_csv( | |
| search_root=runs, dest=tmp_path / "rebuilt.csv" | |
| ) | |
| assert count == 1 | |
| text = (tmp_path / "rebuilt.csv").read_text(encoding="utf-8") | |
| assert "a.pdf" in text | |