-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_generator.py
More file actions
136 lines (120 loc) · 6.3 KB
/
Copy pathreport_generator.py
File metadata and controls
136 lines (120 loc) · 6.3 KB
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
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_CENTER, TA_RIGHT
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, HRFlowable
from scorer import verdict_color, overall_score
import io
from datetime import datetime
def _s(name, **kw):
s = ParagraphStyle(name, fontName="Helvetica", fontSize=10,
textColor=colors.HexColor("#1a1a1a"), leading=16)
for k, v in kw.items(): setattr(s, k, v)
return s
S = {
"logo": _s("logo", fontSize=11, fontName="Helvetica-Bold"),
"eye": _s("eye", fontSize=8, textColor=colors.HexColor("#aaaaaa"), spaceAfter=4),
"title": _s("ttl", fontSize=20, fontName="Helvetica-Bold", spaceAfter=4, leading=26),
"meta": _s("meta", fontSize=9, textColor=colors.HexColor("#888888"), spaceAfter=16),
"sec": _s("sec", fontSize=8, fontName="Helvetica-Bold",
textColor=colors.HexColor("#888888"), spaceAfter=10, leading=12),
"body": _s("body", fontSize=10, spaceAfter=6, leading=16),
"small": _s("sml", fontSize=9, textColor=colors.HexColor("#555555"), leading=14, spaceAfter=4),
"italic": _s("itl", fontSize=9, fontName="Helvetica-Oblique",
textColor=colors.HexColor("#888888"), leading=14, leftIndent=10),
"footer": _s("ftr", fontSize=8, textColor=colors.HexColor("#bbbbbb"), alignment=TA_CENTER),
}
HR = lambda: HRFlowable(width="100%", thickness=0.5, color=colors.HexColor("#e0e0e0"), spaceAfter=10)
HRt = lambda: HRFlowable(width="100%", thickness=0.3, color=colors.HexColor("#f0f0f0"), spaceAfter=6)
SP = lambda h=0.4: Spacer(1, h * cm)
def generate_report(title: str, results: list, language: str = "English") -> io.BytesIO:
buf = io.BytesIO()
doc = SimpleDocTemplate(buf, pagesize=A4,
leftMargin=2.5*cm, rightMargin=2.5*cm,
topMargin=2.5*cm, bottomMargin=2.5*cm)
story = []
# ── Header ────────────────────────────────────────────────────────────────
story.append(Paragraph("VERITY AI", S["logo"]))
story.append(Paragraph("Fact-Checking Report", S["eye"]))
story.append(HR())
story.append(SP(0.2))
story.append(Paragraph(title, S["title"]))
story.append(Paragraph(
f"Generated {datetime.now().strftime('%B %d, %Y at %H:%M')} · Language: {language}",
S["meta"]
))
# ── Overview ──────────────────────────────────────────────────────────────
score = overall_score(results)
n = len(results)
true_n = sum(1 for r in results if r["verdict"].upper() == "TRUE")
false_n= sum(1 for r in results if r["verdict"].upper() == "FALSE")
part_n = sum(1 for r in results if r["verdict"].upper() == "PARTIALLY TRUE")
unv_n = n - true_n - false_n - part_n
score_hex = "4a7c59" if score >= 70 else "c8a040" if score >= 40 else "c0726a"
ov = [[
Paragraph("CREDIBILITY SCORE", S["sec"]),
Paragraph("CLAIMS", S["sec"]),
Paragraph("BREAKDOWN", S["sec"]),
], [
Paragraph(f"<font size='28' color='#{score_hex}'><b>{score}</b></font><font size='14' color='#aaaaaa'>/100</font>", S["body"]),
Paragraph(f"<b>{n}</b> total", S["body"]),
Paragraph(
f"<font color='#4a7c59'>✓ {true_n} true</font> "
f"<font color='#c0726a'>✗ {false_n} false</font> "
f"<font color='#c8a040'>~ {part_n} partial</font> "
f"<font color='#7a7a7a'>? {unv_n} unverifiable</font>",
S["body"]
),
]]
tbl = Table(ov, colWidths=[4*cm, 3.5*cm, 9*cm])
tbl.setStyle(TableStyle([
("VALIGN", (0,0), (-1,-1), "TOP"),
("LINEBELOW", (0,0), (-1,0), 0.5, colors.HexColor("#e0e0e0")),
("LINEBELOW", (0,1), (-1,1), 0.5, colors.HexColor("#e0e0e0")),
("TOPPADDING", (0,0), (-1,-1), 8),
("BOTTOMPADDING",(0,0), (-1,-1), 8),
]))
story.append(tbl)
story.append(SP(0.6))
# ── Claims ────────────────────────────────────────────────────────────────
story.append(Paragraph("VERIFIED CLAIMS", S["sec"]))
story.append(HR())
for i, r in enumerate(results):
verdict = r.get("verdict", "UNVERIFIABLE").upper()
conf = r.get("confidence", 0)
vc = verdict_color(verdict).lstrip("#")
row = [[
Paragraph(f"{i+1}. {r['claim']}", _s(f"cl{i}", fontSize=10, fontName="Helvetica-Bold")),
Paragraph(
f"<font color='#{vc}'><b>{verdict}</b></font> <font color='#aaaaaa'>{conf}%</font>",
ParagraphStyle(f"vd{i}", fontSize=9, fontName="Helvetica-Bold", alignment=TA_RIGHT)
)
]]
t = Table(row, colWidths=[12.5*cm, 4*cm])
t.setStyle(TableStyle([("VALIGN", (0,0), (-1,-1), "TOP")]))
story.append(t)
if r.get("context"):
story.append(Paragraph(f'Context: {r["context"]}', S["italic"]))
story.append(Paragraph(r.get("explanation", ""), S["small"]))
# Sources
used = set(r.get("sources_used", []))
src_line = " · ".join(
f"{s['domain']} ({s['credibility_score']})"
for s in r.get("sources", [])
if s.get("domain") and s["domain"] != "—"
)
if src_line:
story.append(Paragraph(f"Sources: {src_line}", S["italic"]))
story.append(HRt())
story.append(SP(0.15))
# ── Footer ────────────────────────────────────────────────────────────────
story.append(SP(0.8))
story.append(HR())
story.append(Paragraph(
"Generated by Verity AI · For informational purposes only · Not a substitute for professional fact-checking.",
S["footer"]
))
doc.build(story)
buf.seek(0)
return buf