Spaces:
Sleeping
Sleeping
Update utils/pdf_export.py
Browse files- utils/pdf_export.py +22 -62
utils/pdf_export.py
CHANGED
@@ -1,95 +1,55 @@
|
|
1 |
import os
|
2 |
-
from datetime import datetime
|
3 |
-
from typing import List, Dict
|
4 |
from reportlab.lib.pagesizes import letter
|
5 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
6 |
from reportlab.lib.styles import getSampleStyleSheet
|
7 |
-
from reportlab.lib import colors
|
8 |
-
|
9 |
-
def export_report_to_pdf(
|
10 |
-
query: str,
|
11 |
-
summary: str,
|
12 |
-
citations: List[Dict],
|
13 |
-
structures: List[Dict],
|
14 |
-
funding_data: List[Dict],
|
15 |
-
regulation_data: List[Dict],
|
16 |
-
filename: str = None
|
17 |
-
) -> str:
|
18 |
-
"""
|
19 |
-
Export a structured GENESIS-AI research report to a PDF file.
|
20 |
-
|
21 |
-
Args:
|
22 |
-
query: The original research query.
|
23 |
-
summary: AI-generated research summary.
|
24 |
-
citations: List of citations (dicts with 'url' or 'id').
|
25 |
-
structures: List of molecular structures info.
|
26 |
-
funding_data: Funding records.
|
27 |
-
regulation_data: Regulatory insights.
|
28 |
-
filename: Optional custom filename.
|
29 |
-
Returns:
|
30 |
-
str: Full path to generated PDF.
|
31 |
-
"""
|
32 |
-
if not filename:
|
33 |
-
filename = f"genesis_report_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.pdf"
|
34 |
|
|
|
|
|
35 |
filepath = os.path.join(os.getcwd(), filename)
|
36 |
doc = SimpleDocTemplate(filepath, pagesize=letter)
|
37 |
styles = getSampleStyleSheet()
|
38 |
story = []
|
39 |
|
40 |
# Title
|
41 |
-
story.append(Paragraph(f"
|
42 |
story.append(Spacer(1, 12))
|
|
|
|
|
43 |
story.append(Paragraph(f"<b>Query:</b> {query}", styles["Normal"]))
|
44 |
story.append(Spacer(1, 12))
|
45 |
|
46 |
# Summary
|
47 |
-
story.append(Paragraph("<b>Summary
|
48 |
-
story.append(Paragraph(summary
|
49 |
story.append(Spacer(1, 12))
|
50 |
|
51 |
# Citations
|
52 |
if citations:
|
53 |
-
story.append(Paragraph("<b>Citations
|
54 |
-
for
|
55 |
-
|
56 |
-
story.append(Paragraph(url, styles["Normal"]))
|
57 |
story.append(Spacer(1, 12))
|
58 |
|
59 |
# Structures
|
60 |
if structures:
|
61 |
-
story.append(Paragraph("<b>Structures
|
62 |
-
for
|
63 |
-
story.append(Paragraph(str(
|
64 |
story.append(Spacer(1, 12))
|
65 |
|
66 |
-
# Funding
|
67 |
if funding_data:
|
68 |
-
story.append(Paragraph("<b>Funding Data
|
69 |
-
|
70 |
-
|
71 |
-
table_data.append([
|
72 |
-
fund.get("company", ""),
|
73 |
-
fund.get("amount", ""),
|
74 |
-
", ".join(fund.get("investors", []))
|
75 |
-
])
|
76 |
-
table = Table(table_data, repeatRows=1)
|
77 |
-
table.setStyle(TableStyle([
|
78 |
-
("BACKGROUND", (0,0), (-1,0), colors.grey),
|
79 |
-
("TEXTCOLOR", (0,0), (-1,0), colors.whitesmoke),
|
80 |
-
("ALIGN", (0,0), (-1,-1), "LEFT"),
|
81 |
-
("GRID", (0,0), (-1,-1), 0.5, colors.black),
|
82 |
-
]))
|
83 |
-
story.append(table)
|
84 |
story.append(Spacer(1, 12))
|
85 |
|
86 |
-
# Regulation
|
87 |
if regulation_data:
|
88 |
-
story.append(Paragraph("<b>Regulatory
|
89 |
-
for
|
90 |
-
story.append(Paragraph(str(
|
91 |
story.append(Spacer(1, 12))
|
92 |
|
93 |
-
# Build PDF
|
94 |
doc.build(story)
|
95 |
return filepath
|
|
|
1 |
import os
|
|
|
|
|
2 |
from reportlab.lib.pagesizes import letter
|
3 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
4 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def export_report_to_pdf(query, summary, citations, structures, funding_data, regulation_data, filename="output.pdf"):
|
7 |
+
"""Export research report to PDF."""
|
8 |
filepath = os.path.join(os.getcwd(), filename)
|
9 |
doc = SimpleDocTemplate(filepath, pagesize=letter)
|
10 |
styles = getSampleStyleSheet()
|
11 |
story = []
|
12 |
|
13 |
# Title
|
14 |
+
story.append(Paragraph(f"GENESIS-AI Research Report", styles["Title"]))
|
15 |
story.append(Spacer(1, 12))
|
16 |
+
|
17 |
+
# Query
|
18 |
story.append(Paragraph(f"<b>Query:</b> {query}", styles["Normal"]))
|
19 |
story.append(Spacer(1, 12))
|
20 |
|
21 |
# Summary
|
22 |
+
story.append(Paragraph("<b>Summary:</b>", styles["Heading2"]))
|
23 |
+
story.append(Paragraph(summary, styles["Normal"]))
|
24 |
story.append(Spacer(1, 12))
|
25 |
|
26 |
# Citations
|
27 |
if citations:
|
28 |
+
story.append(Paragraph("<b>Citations:</b>", styles["Heading2"]))
|
29 |
+
for c in citations:
|
30 |
+
story.append(Paragraph(f"{c.get('type', '')}: {c.get('url', '')}", styles["Normal"]))
|
|
|
31 |
story.append(Spacer(1, 12))
|
32 |
|
33 |
# Structures
|
34 |
if structures:
|
35 |
+
story.append(Paragraph("<b>Structures:</b>", styles["Heading2"]))
|
36 |
+
for s in structures:
|
37 |
+
story.append(Paragraph(str(s), styles["Normal"]))
|
38 |
story.append(Spacer(1, 12))
|
39 |
|
40 |
+
# Funding
|
41 |
if funding_data:
|
42 |
+
story.append(Paragraph("<b>Funding Data:</b>", styles["Heading2"]))
|
43 |
+
for f in funding_data:
|
44 |
+
story.append(Paragraph(str(f), styles["Normal"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
story.append(Spacer(1, 12))
|
46 |
|
47 |
+
# Regulation
|
48 |
if regulation_data:
|
49 |
+
story.append(Paragraph("<b>Regulatory Info:</b>", styles["Heading2"]))
|
50 |
+
for r in regulation_data:
|
51 |
+
story.append(Paragraph(str(r), styles["Normal"]))
|
52 |
story.append(Spacer(1, 12))
|
53 |
|
|
|
54 |
doc.build(story)
|
55 |
return filepath
|