Update modules/database/database.py
Browse files- modules/database/database.py +23 -30
modules/database/database.py
CHANGED
|
@@ -13,7 +13,8 @@ from matplotlib.figure import Figure
|
|
| 13 |
import bcrypt
|
| 14 |
print(f"Bcrypt version: {bcrypt.__version__}")
|
| 15 |
import uuid
|
| 16 |
-
|
|
|
|
| 17 |
logging.basicConfig(level=logging.DEBUG)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
|
@@ -288,43 +289,35 @@ def store_semantic_result(username, text, analysis_result):
|
|
| 288 |
|
| 289 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
| 290 |
try:
|
| 291 |
-
#
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
# Convertir la figura combinada a una imagen base64
|
| 308 |
-
buf = io.BytesIO()
|
| 309 |
-
fig.savefig(buf, format='png')
|
| 310 |
-
buf.seek(0)
|
| 311 |
-
img_str = base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 312 |
-
|
| 313 |
-
# Cerrar las figuras para liberar memoria
|
| 314 |
-
plt.close(fig)
|
| 315 |
-
plt.close(analysis_result['graph1'])
|
| 316 |
-
plt.close(analysis_result['graph2'])
|
| 317 |
|
| 318 |
# Convertir los conceptos clave a listas de tuplas
|
| 319 |
-
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['
|
| 320 |
-
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['
|
| 321 |
|
| 322 |
analysis_document = {
|
| 323 |
'username': username,
|
| 324 |
'timestamp': datetime.utcnow(),
|
| 325 |
'text1': text1,
|
| 326 |
'text2': text2,
|
| 327 |
-
'
|
|
|
|
|
|
|
| 328 |
'key_concepts1': key_concepts1,
|
| 329 |
'key_concepts2': key_concepts2,
|
| 330 |
'analysis_type': 'discourse'
|
|
|
|
| 13 |
import bcrypt
|
| 14 |
print(f"Bcrypt version: {bcrypt.__version__}")
|
| 15 |
import uuid
|
| 16 |
+
import plotly.graph_objects as go # Para manejar el diagrama de Sankey
|
| 17 |
+
import numpy as np # Puede ser necesario para algunas operaciones
|
| 18 |
logging.basicConfig(level=logging.DEBUG)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
|
|
|
| 289 |
|
| 290 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
| 291 |
try:
|
| 292 |
+
# Convertir los grafos individuales a imágenes base64
|
| 293 |
+
buf1 = io.BytesIO()
|
| 294 |
+
analysis_result['graph1'].savefig(buf1, format='png')
|
| 295 |
+
buf1.seek(0)
|
| 296 |
+
img_str1 = base64.b64encode(buf1.getvalue()).decode('utf-8')
|
| 297 |
+
|
| 298 |
+
buf2 = io.BytesIO()
|
| 299 |
+
analysis_result['graph2'].savefig(buf2, format='png')
|
| 300 |
+
buf2.seek(0)
|
| 301 |
+
img_str2 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
| 302 |
+
|
| 303 |
+
# Convertir el diagrama de Sankey a imagen base64
|
| 304 |
+
buf_sankey = io.BytesIO()
|
| 305 |
+
analysis_result['sankey_diagram'].write_image(buf_sankey, format='png')
|
| 306 |
+
buf_sankey.seek(0)
|
| 307 |
+
img_str_sankey = base64.b64encode(buf_sankey.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
# Convertir los conceptos clave a listas de tuplas
|
| 310 |
+
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts1']]
|
| 311 |
+
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts2']]
|
| 312 |
|
| 313 |
analysis_document = {
|
| 314 |
'username': username,
|
| 315 |
'timestamp': datetime.utcnow(),
|
| 316 |
'text1': text1,
|
| 317 |
'text2': text2,
|
| 318 |
+
'graph1': img_str1,
|
| 319 |
+
'graph2': img_str2,
|
| 320 |
+
'sankey_diagram': img_str_sankey,
|
| 321 |
'key_concepts1': key_concepts1,
|
| 322 |
'key_concepts2': key_concepts2,
|
| 323 |
'analysis_type': 'discourse'
|