Spaces:
Sleeping
Sleeping
Create semantic_export.py
Browse files
modules/database/semantic_export.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
from reportlab.lib import colors
|
| 3 |
+
from reportlab.lib.pagesizes import letter
|
| 4 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, PageBreak
|
| 5 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 6 |
+
from reportlab.lib.units import cm
|
| 7 |
+
from svglib.svglib import svg2rlg
|
| 8 |
+
from reportlab.graphics import renderPM
|
| 9 |
+
import base64
|
| 10 |
+
import cairosvg
|
| 11 |
+
from reportlab.graphics import renderPDF
|
| 12 |
+
from reportlab.lib.utils import ImageReader
|
| 13 |
+
|
| 14 |
+
#importaciones locales
|
| 15 |
+
from .morphosintax_mongo_db import get_student_morphosyntax_data
|
| 16 |
+
from .chat_db import get_chat_history
|
| 17 |
+
|
| 18 |
+
# Placeholder para el logo
|
| 19 |
+
LOGO_PATH = "assets\img\logo_92x92.png" # Reemplaza esto con la ruta real de tu logo
|
| 20 |
+
|
| 21 |
+
# Definir el tamaño de página carta manualmente (612 x 792 puntos)
|
| 22 |
+
LETTER_SIZE = (612, 792)
|
| 23 |
+
|
| 24 |
+
def add_logo(canvas, doc):
|
| 25 |
+
logo = Image(LOGO_PATH, width=2*cm, height=2*cm)
|
| 26 |
+
logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - 0.5*cm)
|
| 27 |
+
|
| 28 |
+
def export_user_interactions(username, analysis_type):
|
| 29 |
+
# Obtener historial de chat (que ahora incluye los análisis morfosintácticos)
|
| 30 |
+
chat_history = get_chat_history(username, analysis_type)
|
| 31 |
+
|
| 32 |
+
# Crear un PDF
|
| 33 |
+
buffer = BytesIO()
|
| 34 |
+
doc = SimpleDocTemplate(
|
| 35 |
+
buffer,
|
| 36 |
+
pagesize=letter,
|
| 37 |
+
rightMargin=2*cm,
|
| 38 |
+
leftMargin=2*cm,
|
| 39 |
+
topMargin=2*cm,
|
| 40 |
+
bottomMargin=2*cm
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
story = []
|
| 44 |
+
styles = getSampleStyleSheet()
|
| 45 |
+
|
| 46 |
+
# Título
|
| 47 |
+
story.append(Paragraph(f"Interacciones de {username} - Análisis {analysis_type}", styles['Title']))
|
| 48 |
+
story.append(Spacer(1, 0.5*cm))
|
| 49 |
+
|
| 50 |
+
# Historial del chat y análisis
|
| 51 |
+
for entry in chat_history:
|
| 52 |
+
for message in entry['messages']:
|
| 53 |
+
role = message['role']
|
| 54 |
+
content = message['content']
|
| 55 |
+
story.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", styles['BodyText']))
|
| 56 |
+
story.append(Spacer(1, 0.25*cm))
|
| 57 |
+
|
| 58 |
+
# Si hay visualizaciones (diagramas SVG), convertirlas a imagen y añadirlas
|
| 59 |
+
if 'visualizations' in message and message['visualizations']:
|
| 60 |
+
for svg in message['visualizations']:
|
| 61 |
+
drawing = svg2rlg(BytesIO(svg.encode('utf-8')))
|
| 62 |
+
img_data = BytesIO()
|
| 63 |
+
renderPM.drawToFile(drawing, img_data, fmt="PNG")
|
| 64 |
+
img_data.seek(0)
|
| 65 |
+
img = Image(img_data, width=15*cm, height=7.5*cm)
|
| 66 |
+
story.append(img)
|
| 67 |
+
story.append(Spacer(1, 0.5*cm))
|
| 68 |
+
|
| 69 |
+
story.append(PageBreak())
|
| 70 |
+
|
| 71 |
+
# Construir el PDF
|
| 72 |
+
doc.build(story)
|
| 73 |
+
buffer.seek(0)
|
| 74 |
+
return buffer
|
| 75 |
+
|
| 76 |
+
# Uso en Streamlit:
|
| 77 |
+
# pdf_buffer = export_user_interactions(username, 'morphosyntax')
|
| 78 |
+
# st.download_button(label="Descargar PDF", data=pdf_buffer, file_name="interacciones.pdf", mime="application/pdf")
|