Spaces:
Sleeping
Sleeping
Update utils/pdf_export.py
Browse files- utils/pdf_export.py +83 -7
utils/pdf_export.py
CHANGED
@@ -1,19 +1,95 @@
|
|
1 |
-
|
|
|
|
|
2 |
from reportlab.lib.pagesizes import letter
|
3 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
4 |
from reportlab.lib.styles import getSampleStyleSheet
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
def export_to_pdf(content: str, filename: str = "output.pdf"):
|
8 |
-
"""Export given text content to a PDF file."""
|
9 |
filepath = os.path.join(os.getcwd(), filename)
|
10 |
doc = SimpleDocTemplate(filepath, pagesize=letter)
|
11 |
styles = getSampleStyleSheet()
|
12 |
story = []
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
story.append(Spacer(1, 12))
|
17 |
|
|
|
18 |
doc.build(story)
|
19 |
return filepath
|
|
|
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, Table, TableStyle
|
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"<b>GENESIS-AI Research Report</b>", styles["Title"]))
|
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</b>", styles["Heading2"]))
|
48 |
+
story.append(Paragraph(summary or "No summary generated.", styles["Normal"]))
|
49 |
+
story.append(Spacer(1, 12))
|
50 |
+
|
51 |
+
# Citations
|
52 |
+
if citations:
|
53 |
+
story.append(Paragraph("<b>Citations</b>", styles["Heading2"]))
|
54 |
+
for cite in citations:
|
55 |
+
url = cite.get("url", "")
|
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</b>", styles["Heading2"]))
|
62 |
+
for struct in structures:
|
63 |
+
story.append(Paragraph(str(struct), styles["Normal"]))
|
64 |
+
story.append(Spacer(1, 12))
|
65 |
+
|
66 |
+
# Funding Data (Table)
|
67 |
+
if funding_data:
|
68 |
+
story.append(Paragraph("<b>Funding Data</b>", styles["Heading2"]))
|
69 |
+
table_data = [["Company", "Amount", "Investor"]]
|
70 |
+
for fund in funding_data:
|
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 Data
|
87 |
+
if regulation_data:
|
88 |
+
story.append(Paragraph("<b>Regulatory Insights</b>", styles["Heading2"]))
|
89 |
+
for reg in regulation_data:
|
90 |
+
story.append(Paragraph(str(reg), styles["Normal"]))
|
91 |
story.append(Spacer(1, 12))
|
92 |
|
93 |
+
# Build PDF
|
94 |
doc.build(story)
|
95 |
return filepath
|