File size: 1,436 Bytes
8f1213e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)

    # also produce the cheap attention-only version as a fallback view
    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()