Spaces:
Sleeping
Sleeping
Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
|
@@ -30,37 +30,35 @@ def fig_to_bytes(fig):
|
|
| 30 |
logger.error(f"Error en fig_to_bytes: {str(e)}")
|
| 31 |
return None
|
| 32 |
|
|
|
|
|
|
|
| 33 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 34 |
"""
|
| 35 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
| 36 |
"""
|
| 37 |
try:
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
graph2_data = None
|
| 41 |
-
combined_graph_data = None
|
| 42 |
-
|
| 43 |
-
if 'graph1' in analysis_result and analysis_result['graph1'] is not None:
|
| 44 |
try:
|
| 45 |
-
# Convertir
|
| 46 |
graph1_bytes = fig_to_bytes(analysis_result['graph1'])
|
| 47 |
if graph1_bytes:
|
| 48 |
-
graph1_data = base64.b64encode(graph1_bytes).decode('utf-8')
|
| 49 |
-
# Guardar los bytes también para el botón de descarga
|
| 50 |
analysis_result['graph1_bytes'] = graph1_bytes
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
logger.error(f"Error al
|
| 53 |
|
| 54 |
-
if 'graph2' in analysis_result
|
| 55 |
try:
|
| 56 |
-
# Convertir
|
| 57 |
graph2_bytes = fig_to_bytes(analysis_result['graph2'])
|
| 58 |
if graph2_bytes:
|
| 59 |
-
graph2_data = base64.b64encode(graph2_bytes).decode('utf-8')
|
| 60 |
-
# Guardar los bytes también para el botón de descarga
|
| 61 |
analysis_result['graph2_bytes'] = graph2_bytes
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
-
logger.error(f"Error al
|
| 64 |
|
| 65 |
# Crear documento para MongoDB
|
| 66 |
analysis_document = {
|
|
@@ -71,27 +69,23 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
| 71 |
'analysis_type': 'discourse',
|
| 72 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
| 73 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
| 74 |
-
'
|
| 75 |
-
'
|
| 76 |
-
'graph1_bytes': analysis_result.get('graph1_bytes'),
|
| 77 |
-
'graph2_bytes': analysis_result.get('graph2_bytes')
|
| 78 |
}
|
| 79 |
|
| 80 |
# Insertar en MongoDB
|
| 81 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
| 82 |
-
if result:
|
| 83 |
-
logger.
|
| 84 |
-
return
|
| 85 |
|
| 86 |
-
logger.
|
| 87 |
-
return
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
| 91 |
return False
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
#################################################################################
|
| 96 |
def get_student_discourse_analysis(username, limit=10):
|
| 97 |
"""
|
|
|
|
| 30 |
logger.error(f"Error en fig_to_bytes: {str(e)}")
|
| 31 |
return None
|
| 32 |
|
| 33 |
+
|
| 34 |
+
#################################################################################
|
| 35 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 36 |
"""
|
| 37 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
# Primero convertir las figuras a bytes
|
| 41 |
+
if 'graph1' in analysis_result:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
+
# Convertir y guardar bytes del grafo 1
|
| 44 |
graph1_bytes = fig_to_bytes(analysis_result['graph1'])
|
| 45 |
if graph1_bytes:
|
|
|
|
|
|
|
| 46 |
analysis_result['graph1_bytes'] = graph1_bytes
|
| 47 |
+
analysis_result['graph1_base64'] = base64.b64encode(graph1_bytes).decode('utf-8')
|
| 48 |
+
plt.close(analysis_result['graph1']) # Cerrar la figura
|
| 49 |
except Exception as e:
|
| 50 |
+
logger.error(f"Error al procesar gráfico 1: {str(e)}")
|
| 51 |
|
| 52 |
+
if 'graph2' in analysis_result:
|
| 53 |
try:
|
| 54 |
+
# Convertir y guardar bytes del grafo 2
|
| 55 |
graph2_bytes = fig_to_bytes(analysis_result['graph2'])
|
| 56 |
if graph2_bytes:
|
|
|
|
|
|
|
| 57 |
analysis_result['graph2_bytes'] = graph2_bytes
|
| 58 |
+
analysis_result['graph2_base64'] = base64.b64encode(graph2_bytes).decode('utf-8')
|
| 59 |
+
plt.close(analysis_result['graph2']) # Cerrar la figura
|
| 60 |
except Exception as e:
|
| 61 |
+
logger.error(f"Error al procesar gráfico 2: {str(e)}")
|
| 62 |
|
| 63 |
# Crear documento para MongoDB
|
| 64 |
analysis_document = {
|
|
|
|
| 69 |
'analysis_type': 'discourse',
|
| 70 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
| 71 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
| 72 |
+
'graph1_base64': analysis_result.get('graph1_base64'),
|
| 73 |
+
'graph2_base64': analysis_result.get('graph2_base64'),
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
# Insertar en MongoDB
|
| 77 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
| 78 |
+
if not result:
|
| 79 |
+
logger.error("No se pudo insertar el documento en MongoDB")
|
| 80 |
+
return False
|
| 81 |
|
| 82 |
+
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
|
| 83 |
+
return True
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
| 87 |
return False
|
| 88 |
|
|
|
|
|
|
|
| 89 |
#################################################################################
|
| 90 |
def get_student_discourse_analysis(username, limit=10):
|
| 91 |
"""
|