Spaces:
Build error
Build error
Create report_gen.py
Browse files- report_gen.py +44 -0
report_gen.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
from reportlab.lib import colors
|
| 3 |
+
from reportlab.lib.pagesizes import A4
|
| 4 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 5 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 6 |
+
from reportlab.lib.units import mm
|
| 7 |
+
|
| 8 |
+
def generate_pdf(pages, answers):
|
| 9 |
+
buffer = BytesIO()
|
| 10 |
+
doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=20*mm, leftMargin=20*mm, topMargin=20*mm, bottomMargin=20*mm)
|
| 11 |
+
|
| 12 |
+
# Define custom colors
|
| 13 |
+
dark_purple = colors.Color(0.2, 0, 0.4) # Very dark purple for titles
|
| 14 |
+
light_purple = colors.Color(0.6, 0.4, 0.8) # Lighter purple for answers
|
| 15 |
+
medium_gray = colors.Color(0.5, 0.5, 0.5) # Medium gray for descriptions
|
| 16 |
+
|
| 17 |
+
styles = getSampleStyleSheet()
|
| 18 |
+
styles.add(ParagraphStyle(name='SectionTitle', fontName='Helvetica', fontSize=16, spaceAfter=6, textColor=dark_purple))
|
| 19 |
+
styles.add(ParagraphStyle(name='Description', fontName='Helvetica', fontSize=12, spaceAfter=6, textColor=medium_gray))
|
| 20 |
+
styles.add(ParagraphStyle(name='Answer', fontName='Helvetica', fontSize=12, spaceAfter=12, textColor=light_purple))
|
| 21 |
+
styles.add(ParagraphStyle(name='MainTitle', fontName='Helvetica-Bold', fontSize=16, spaceAfter=24, textColor=dark_purple))
|
| 22 |
+
|
| 23 |
+
story = [Paragraph("AI Trust and Opacity Evaluation", styles['MainTitle'])]
|
| 24 |
+
|
| 25 |
+
for page in pages[:-1]: # Skip the last page
|
| 26 |
+
if 'input_key' in page and page['input_key'] is not None:
|
| 27 |
+
story.append(Paragraph(page['title'], styles['SectionTitle']))
|
| 28 |
+
story.append(Paragraph(page['content'], styles['Description']))
|
| 29 |
+
|
| 30 |
+
answer = answers.get(page['input_key'], "")
|
| 31 |
+
if isinstance(answer, list):
|
| 32 |
+
answer = ', '.join(answer)
|
| 33 |
+
|
| 34 |
+
if page.get('input_type') == 'combined':
|
| 35 |
+
option = answers.get(page['input_key'], "")
|
| 36 |
+
conclusion = answers.get(f"{page['input_key']}_conclusion", "")
|
| 37 |
+
answer = f"Option selected: {option}\n\nConclusion: {conclusion}"
|
| 38 |
+
|
| 39 |
+
story.append(Paragraph(f"<b>Answer:</b> {answer}", styles['Answer']))
|
| 40 |
+
story.append(Spacer(1, 12))
|
| 41 |
+
|
| 42 |
+
doc.build(story)
|
| 43 |
+
buffer.seek(0)
|
| 44 |
+
return buffer
|