File size: 2,919 Bytes
c58df45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from io import BytesIO
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import cm
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
import base64
import cairosvg
from reportlab.graphics import renderPDF
from reportlab.lib.utils import ImageReader

#importaciones locales
from .morphosintax_mongo_db import get_student_morphosyntax_data
from .chat_db import get_chat_history

# Placeholder para el logo
LOGO_PATH = "assets\img\logo_92x92.png" # Reemplaza esto con la ruta real de tu logo

# Definir el tamaño de página carta manualmente (612 x 792 puntos)
LETTER_SIZE = (612, 792)

def add_logo(canvas, doc):
    logo = Image(LOGO_PATH, width=2*cm, height=2*cm)
    logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - 0.5*cm)

def export_user_interactions(username, analysis_type):
    # Obtener historial de chat (que ahora incluye los análisis morfosintácticos)
    chat_history = get_chat_history(username, analysis_type)

    # Crear un PDF
    buffer = BytesIO()
    doc = SimpleDocTemplate(
        buffer,
        pagesize=letter,
        rightMargin=2*cm,
        leftMargin=2*cm,
        topMargin=2*cm,
        bottomMargin=2*cm
    )

    story = []
    styles = getSampleStyleSheet()

    # Título
    story.append(Paragraph(f"Interacciones de {username} - Análisis {analysis_type}", styles['Title']))
    story.append(Spacer(1, 0.5*cm))

    # Historial del chat y análisis
    for entry in chat_history:
        for message in entry['messages']:
            role = message['role']
            content = message['content']
            story.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", styles['BodyText']))
            story.append(Spacer(1, 0.25*cm))

            # Si hay visualizaciones (diagramas SVG), convertirlas a imagen y añadirlas
            if 'visualizations' in message and message['visualizations']:
                for svg in message['visualizations']:
                    drawing = svg2rlg(BytesIO(svg.encode('utf-8')))
                    img_data = BytesIO()
                    renderPM.drawToFile(drawing, img_data, fmt="PNG")
                    img_data.seek(0)
                    img = Image(img_data, width=15*cm, height=7.5*cm)
                    story.append(img)
                    story.append(Spacer(1, 0.5*cm))

        story.append(PageBreak())

    # Construir el PDF
    doc.build(story)
    buffer.seek(0)
    return buffer

# Uso en Streamlit:
# pdf_buffer = export_user_interactions(username, 'morphosyntax')
# st.download_button(label="Descargar PDF", data=pdf_buffer, file_name="interacciones.pdf", mime="application/pdf")