| """Script 08: Generate XAI report (cross-attention + IG + drilldown plots).""" |
| import argparse |
| import sys |
| from pathlib import Path |
|
|
| import pandas as pd |
|
|
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
|
|
| from src.utils import setup_logging |
| from src import config as cfg |
| from src.explainer import explain_examples |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--n_examples", type=int, default=10) |
| parser.add_argument("--method", choices=["ig", "attention"], default="ig", |
| help="ig = Integrated Gradients (needs captum); " |
| "attention = BERT [CLS]-attention (no extra deps).") |
| args = parser.parse_args() |
|
|
| setup_logging() |
| test_df = pd.read_parquet(cfg.TEST_PATH) |
|
|
| print(f"Generating {args.method} explanations for {args.n_examples} examples...") |
| out = cfg.REPORT_DIR / f"explanation_{args.method}.html" |
| explain_examples(test_df, n_examples=args.n_examples, method=args.method, |
| output_path=out) |
|
|
| |
| if args.method == "ig": |
| out2 = cfg.REPORT_DIR / "explanation_attention.html" |
| explain_examples(test_df, n_examples=args.n_examples, method="attention", |
| output_path=out2) |
|
|
| print(f"\nDone. Open reports/explanation_*.html in your browser.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|