|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Generate a PDF summary of safety audit reports for Shielded RecRL. |
| 4 | +
|
| 5 | +This script reads all safety report JSON files and creates a PDF summary. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + python generate_pdf_summary.py |
| 9 | +""" |
| 10 | +import pathlib |
| 11 | +import os |
| 12 | +import json |
| 13 | +import sys |
| 14 | + |
| 15 | +def generate_summary_pdf(): |
| 16 | + try: |
| 17 | + # Check for reportlab |
| 18 | + from reportlab.lib.pagesizes import letter |
| 19 | + from reportlab.pdfgen import canvas |
| 20 | + from reportlab.lib import colors |
| 21 | + from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
| 22 | + from reportlab.platypus import Paragraph, Spacer |
| 23 | + except ImportError: |
| 24 | + print("Error: reportlab package not installed. Install with: pip install reportlab") |
| 25 | + return False |
| 26 | + |
| 27 | + root = pathlib.Path(os.getenv("PROJ", ".")) |
| 28 | + reports_path = root / "docs" |
| 29 | + output_file = reports_path / "safety_summary.pdf" |
| 30 | + |
| 31 | + # Collect all report files |
| 32 | + report_files = list(reports_path.glob("safety_report_*.json")) |
| 33 | + if not report_files: |
| 34 | + print("Error: No safety report files found in", reports_path) |
| 35 | + return False |
| 36 | + |
| 37 | + # Load policy thresholds |
| 38 | + try: |
| 39 | + thresholds_file = reports_path / "policy_thresholds.yaml" |
| 40 | + if thresholds_file.exists(): |
| 41 | + import yaml |
| 42 | + with open(thresholds_file) as f: |
| 43 | + thresholds = yaml.safe_load(f) |
| 44 | + else: |
| 45 | + thresholds = None |
| 46 | + except Exception as e: |
| 47 | + print(f"Warning: Could not load thresholds: {e}") |
| 48 | + thresholds = None |
| 49 | + |
| 50 | + # Create the PDF |
| 51 | + c = canvas.Canvas(str(output_file), pagesize=letter) |
| 52 | + width, height = letter |
| 53 | + |
| 54 | + # Title |
| 55 | + c.setFont("Helvetica-Bold", 16) |
| 56 | + c.drawString(50, height - 50, "Shielded RecRL: Safety Audit Summary") |
| 57 | + c.setFont("Helvetica", 10) |
| 58 | + c.drawString(50, height - 70, f"Generated on {time.strftime('%Y-%m-%d %H:%M:%S')}") |
| 59 | + |
| 60 | + # Header line |
| 61 | + c.line(50, height - 80, width - 50, height - 80) |
| 62 | + |
| 63 | + # Policy thresholds section if available |
| 64 | + y_pos = height - 100 |
| 65 | + if thresholds: |
| 66 | + c.setFont("Helvetica-Bold", 12) |
| 67 | + c.drawString(50, y_pos, "Policy Thresholds:") |
| 68 | + y_pos -= 20 |
| 69 | + c.setFont("Helvetica", 10) |
| 70 | + for key, value in thresholds.items(): |
| 71 | + c.drawString(70, y_pos, f"{key}: {value}") |
| 72 | + y_pos -= 15 |
| 73 | + y_pos -= 10 |
| 74 | + |
| 75 | + # Report summaries |
| 76 | + c.setFont("Helvetica-Bold", 12) |
| 77 | + c.drawString(50, y_pos, "Safety Reports:") |
| 78 | + y_pos -= 20 |
| 79 | + |
| 80 | + for report_file in sorted(report_files): |
| 81 | + try: |
| 82 | + with open(report_file) as f: |
| 83 | + report = json.load(f) |
| 84 | + |
| 85 | + # Extract dataset name from filename |
| 86 | + dataset = report_file.stem.replace('safety_report_', '') |
| 87 | + |
| 88 | + # Dataset header |
| 89 | + c.setFont("Helvetica-Bold", 11) |
| 90 | + c.drawString(50, y_pos, f"Dataset: {dataset}") |
| 91 | + y_pos -= 20 |
| 92 | + |
| 93 | + # Toxicity metrics |
| 94 | + c.setFont("Helvetica", 10) |
| 95 | + if 'tox' in report: |
| 96 | + tox_mean = report['tox'].get('mean', 'N/A') |
| 97 | + tox_p95 = report['tox'].get('p95', 'N/A') |
| 98 | + c.drawString(70, y_pos, f"Toxicity: mean={tox_mean:.4f}, p95={tox_p95:.4f}") |
| 99 | + y_pos -= 15 |
| 100 | + |
| 101 | + # Popularity bias |
| 102 | + if 'pop' in report: |
| 103 | + gini_base = report['pop'].get('gini_base', 'N/A') |
| 104 | + gini_new = report['pop'].get('gini_new', 'N/A') |
| 105 | + delta = report['pop'].get('delta', 'N/A') |
| 106 | + c.drawString(70, y_pos, f"Gini: base={gini_base:.4f}, new={gini_new:.4f}, delta={delta:.4f}") |
| 107 | + y_pos -= 15 |
| 108 | + |
| 109 | + # Gender parity (ml25m only) |
| 110 | + if 'parity' in report and report['parity']: |
| 111 | + male = report['parity'].get('male_rate', 0) |
| 112 | + female = report['parity'].get('female_rate', 0) |
| 113 | + gap = abs(male - female) |
| 114 | + c.drawString(70, y_pos, f"Gender: M={male:.4f}, F={female:.4f}, gap={gap:.4f}") |
| 115 | + y_pos -= 15 |
| 116 | + |
| 117 | + # Privacy |
| 118 | + if 'privacy' in report: |
| 119 | + priv = report['privacy'] |
| 120 | + c.drawString(70, y_pos, f"Privacy leakage rate: {priv:.6f}") |
| 121 | + y_pos -= 25 |
| 122 | + |
| 123 | + # Check for new page if needed |
| 124 | + if y_pos < 100: |
| 125 | + c.showPage() |
| 126 | + y_pos = height - 50 |
| 127 | + c.setFont("Helvetica-Bold", 12) |
| 128 | + c.drawString(50, y_pos, "Safety Reports (continued):") |
| 129 | + y_pos -= 30 |
| 130 | + |
| 131 | + except Exception as e: |
| 132 | + c.setFont("Helvetica-Italic", 10) |
| 133 | + c.drawString(70, y_pos, f"Error processing {report_file.name}: {str(e)}") |
| 134 | + y_pos -= 20 |
| 135 | + |
| 136 | + # Summary |
| 137 | + if y_pos < 150: |
| 138 | + c.showPage() |
| 139 | + y_pos = height - 50 |
| 140 | + |
| 141 | + c.setFont("Helvetica-Bold", 12) |
| 142 | + c.drawString(50, y_pos, "Summary:") |
| 143 | + y_pos -= 20 |
| 144 | + c.setFont("Helvetica", 10) |
| 145 | + c.drawString(70, y_pos, f"Total reports processed: {len(report_files)}") |
| 146 | + |
| 147 | + # Save the PDF |
| 148 | + c.save() |
| 149 | + print(f"PDF summary saved to {output_file}") |
| 150 | + return True |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + import time |
| 154 | + success = generate_summary_pdf() |
| 155 | + sys.exit(0 if success else 1) |
0 commit comments