Spaces:
Sleeping
Sleeping
Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
|
@@ -22,54 +22,30 @@ COLLECTION_NAME = 'student_discourse_analysis'
|
|
| 22 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 23 |
"""
|
| 24 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
| 25 |
-
Args:
|
| 26 |
-
username: Nombre del usuario
|
| 27 |
-
text1: Primer texto analizado (patrón)
|
| 28 |
-
text2: Segundo texto analizado (comparación)
|
| 29 |
-
analysis_result: Resultado del análisis
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
-
#
|
| 33 |
graph1_data = None
|
| 34 |
graph2_data = None
|
| 35 |
combined_graph_data = None
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Convertir base64 a imagen para el gráfico combinado
|
| 56 |
-
img1 = plt.imread(io.BytesIO(base64.b64decode(graph1_data)))
|
| 57 |
-
img2 = plt.imread(io.BytesIO(base64.b64decode(graph2_data)))
|
| 58 |
-
|
| 59 |
-
ax1.imshow(img1)
|
| 60 |
-
ax1.axis('off')
|
| 61 |
-
ax1.set_title("Documento 1: Relaciones Conceptuales")
|
| 62 |
-
|
| 63 |
-
ax2.imshow(img2)
|
| 64 |
-
ax2.axis('off')
|
| 65 |
-
ax2.set_title("Documento 2: Relaciones Conceptuales")
|
| 66 |
-
|
| 67 |
-
# Guardar gráfico combinado en base64
|
| 68 |
-
buf = io.BytesIO()
|
| 69 |
-
fig.savefig(buf, format='png')
|
| 70 |
-
buf.seek(0)
|
| 71 |
-
combined_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 72 |
-
plt.close(fig)
|
| 73 |
|
| 74 |
# Crear documento para MongoDB
|
| 75 |
analysis_document = {
|
|
|
|
| 22 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 23 |
"""
|
| 24 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
+
# Los gráficos ya vienen en bytes, solo necesitamos codificar a base64
|
| 28 |
graph1_data = None
|
| 29 |
graph2_data = None
|
| 30 |
combined_graph_data = None
|
| 31 |
|
| 32 |
+
if 'graph1' in analysis_result and analysis_result['graph1'] is not None:
|
| 33 |
+
try:
|
| 34 |
+
graph1_data = base64.b64encode(analysis_result['graph1']).decode('utf-8')
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.error(f"Error al codificar gráfico 1: {str(e)}")
|
| 37 |
+
|
| 38 |
+
if 'graph2' in analysis_result and analysis_result['graph2'] is not None:
|
| 39 |
+
try:
|
| 40 |
+
graph2_data = base64.b64encode(analysis_result['graph2']).decode('utf-8')
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.error(f"Error al codificar gráfico 2: {str(e)}")
|
| 43 |
+
|
| 44 |
+
if 'combined_graph' in analysis_result and analysis_result['combined_graph'] is not None:
|
| 45 |
+
try:
|
| 46 |
+
combined_graph_data = base64.b64encode(analysis_result['combined_graph']).decode('utf-8')
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logger.error(f"Error al codificar gráfico combinado: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Crear documento para MongoDB
|
| 51 |
analysis_document = {
|