Upload 4 files
Browse files
modules/utils/export_utils.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from reportlab.lib.pagesizes import letter
|
| 3 |
+
from reportlab.pdfgen import canvas
|
| 4 |
+
from docx import Document
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
def export_data(user_data, t, format='pdf'):
|
| 8 |
+
if format == 'pdf':
|
| 9 |
+
return export_to_pdf(user_data, t)
|
| 10 |
+
elif format == 'docx':
|
| 11 |
+
return export_to_docx(user_data, t)
|
| 12 |
+
else:
|
| 13 |
+
raise ValueError(f"Unsupported format: {format}")
|
| 14 |
+
|
| 15 |
+
def export_to_pdf(user_data, t):
|
| 16 |
+
buffer = io.BytesIO()
|
| 17 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 18 |
+
width, height = letter
|
| 19 |
+
|
| 20 |
+
# T铆tulo
|
| 21 |
+
c.setFont("Helvetica-Bold", 16)
|
| 22 |
+
c.drawString(50, height - 50, t['analysis_report'])
|
| 23 |
+
|
| 24 |
+
# Resumen
|
| 25 |
+
c.setFont("Helvetica", 12)
|
| 26 |
+
c.drawString(50, height - 80, f"{t['morpho_analyses']}: {len(user_data['morphosyntax_analyses'])}")
|
| 27 |
+
c.drawString(50, height - 100, f"{t['semantic_analyses']}: {len(user_data['semantic_analyses'])}")
|
| 28 |
+
c.drawString(50, height - 120, f"{t['discourse_analyses']}: {len(user_data['discourse_analyses'])}")
|
| 29 |
+
|
| 30 |
+
# Aqu铆 agregar铆as m谩s detalles de los an谩lisis...
|
| 31 |
+
|
| 32 |
+
c.save()
|
| 33 |
+
buffer.seek(0)
|
| 34 |
+
return buffer
|
| 35 |
+
|
| 36 |
+
def export_to_docx(user_data, t):
|
| 37 |
+
doc = Document()
|
| 38 |
+
doc.add_heading(t['analysis_report'], 0)
|
| 39 |
+
|
| 40 |
+
doc.add_paragraph(f"{t['morpho_analyses']}: {len(user_data['morphosyntax_analyses'])}")
|
| 41 |
+
doc.add_paragraph(f"{t['semantic_analyses']}: {len(user_data['semantic_analyses'])}")
|
| 42 |
+
doc.add_paragraph(f"{t['discourse_analyses']}: {len(user_data['discourse_analyses'])}")
|
| 43 |
+
|
| 44 |
+
# Aqu铆 agregar铆as m谩s detalles de los an谩lisis...
|
| 45 |
+
|
| 46 |
+
buffer = io.BytesIO()
|
| 47 |
+
doc.save(buffer)
|
| 48 |
+
buffer.seek(0)
|
| 49 |
+
return buffer
|
| 50 |
+
|
| 51 |
+
def display_export_options(t):
|
| 52 |
+
format = st.radio(t['select_export_format'], ['PDF', 'DOCX'])
|
| 53 |
+
if st.button(t['export']):
|
| 54 |
+
user_data = st.session_state.user_data
|
| 55 |
+
if format == 'PDF':
|
| 56 |
+
buffer = export_data(user_data, t, format='pdf')
|
| 57 |
+
st.download_button(
|
| 58 |
+
label=t['download_pdf'],
|
| 59 |
+
data=buffer,
|
| 60 |
+
file_name="analysis_report.pdf",
|
| 61 |
+
mime="application/pdf"
|
| 62 |
+
)
|
| 63 |
+
elif format == 'DOCX':
|
| 64 |
+
buffer = export_data(user_data, t, format='docx')
|
| 65 |
+
st.download_button(
|
| 66 |
+
label=t['download_docx'],
|
| 67 |
+
data=buffer,
|
| 68 |
+
file_name="analysis_report.docx",
|
| 69 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 70 |
+
)
|
modules/utils/spacy_utils.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/spacy_utils.py
|
| 2 |
+
import spacy
|
| 3 |
+
|
| 4 |
+
def load_spacy_models():
|
| 5 |
+
return {
|
| 6 |
+
'es': spacy.load("es_core_news_lg"),
|
| 7 |
+
'en': spacy.load("en_core_web_lg"),
|
| 8 |
+
'uk': spacy.load("uk_core_news_lg")
|
| 9 |
+
}
|
modules/utils/svg_to_png_converter.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from svglib.svglib import svg2rlg
|
| 3 |
+
from reportlab.graphics import renderPM
|
| 4 |
+
from pymongo import MongoClient
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# Asume que tienes una funci贸n para obtener la conexi贸n a MongoDB
|
| 8 |
+
from ..database.mongo_db import get_mongodb
|
| 9 |
+
|
| 10 |
+
def convert_svg_to_png(svg_string):
|
| 11 |
+
"""Convierte una cadena SVG a una imagen PNG."""
|
| 12 |
+
drawing = svg2rlg(io.BytesIO(svg_string.encode('utf-8')))
|
| 13 |
+
png_bio = io.BytesIO()
|
| 14 |
+
renderPM.drawToFile(drawing, png_bio, fmt="PNG")
|
| 15 |
+
return png_bio.getvalue()
|
| 16 |
+
|
| 17 |
+
def save_png_to_database(username, analysis_id, png_data):
|
| 18 |
+
"""Guarda la imagen PNG en la base de datos."""
|
| 19 |
+
client = get_mongodb()
|
| 20 |
+
db = client['aideatext_db'] # Aseg煤rate de usar el nombre correcto de tu base de datos
|
| 21 |
+
collection = db['png_diagrams']
|
| 22 |
+
|
| 23 |
+
png_base64 = base64.b64encode(png_data).decode('utf-8')
|
| 24 |
+
|
| 25 |
+
document = {
|
| 26 |
+
'username': username,
|
| 27 |
+
'analysis_id': analysis_id,
|
| 28 |
+
'png_data': png_base64
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
result = collection.insert_one(document)
|
| 32 |
+
return result.inserted_id
|
| 33 |
+
|
| 34 |
+
def process_and_save_svg_diagrams(username, analysis_id, svg_diagrams):
|
| 35 |
+
"""Procesa una lista de diagramas SVG, los convierte a PNG y los guarda en la base de datos."""
|
| 36 |
+
png_ids = []
|
| 37 |
+
for svg in svg_diagrams:
|
| 38 |
+
png_data = convert_svg_to_png(svg)
|
| 39 |
+
png_id = save_png_to_database(username, analysis_id, png_data)
|
| 40 |
+
png_ids.append(png_id)
|
| 41 |
+
return png_ids
|
| 42 |
+
|
| 43 |
+
# Funci贸n para recuperar PNGs de la base de datos
|
| 44 |
+
def get_png_diagrams(username, analysis_id):
|
| 45 |
+
"""Recupera los diagramas PNG de la base de datos para un an谩lisis espec铆fico."""
|
| 46 |
+
client = get_mongodb()
|
| 47 |
+
db = client['aideatext_db']
|
| 48 |
+
collection = db['png_diagrams']
|
| 49 |
+
|
| 50 |
+
diagrams = collection.find({'username': username, 'analysis_id': analysis_id})
|
| 51 |
+
return [base64.b64decode(doc['png_data']) for doc in diagrams]
|
modules/utils/widget_utils.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/utils/widget_utils.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
def generate_unique_key(module_name, element_type="input", username=None):
|
| 5 |
+
# Si el nombre de usuario no se pasa expl铆citamente, lo toma de session_state
|
| 6 |
+
username = username or st.session_state.username
|
| 7 |
+
return f"{module_name}_{element_type}_{username}"
|