AIdeaText commited on
Commit
83f1e70
·
verified ·
1 Parent(s): bbf8ddd

Update modules/database/discourse_mongo_db.py

Browse files
modules/database/discourse_mongo_db.py CHANGED
@@ -19,34 +19,49 @@ logger = logging.getLogger(__name__)
19
 
20
  COLLECTION_NAME = 'student_discourse_analysis'
21
 
 
 
 
 
 
 
 
 
 
 
 
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 = {
52
  'username': username,
@@ -58,7 +73,8 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
58
  'key_concepts2': analysis_result.get('key_concepts2', []),
59
  'graph1': graph1_data,
60
  'graph2': graph2_data,
61
- 'combined_graph': combined_graph_data
 
62
  }
63
 
64
  # Insertar en MongoDB
@@ -120,10 +136,6 @@ def get_student_discourse_analysis(username, limit=10):
120
  logger.error(f"Error recuperando análisis del discurso: {str(e)}")
121
  return []
122
  #####################################################################################
123
-
124
-
125
-
126
-
127
 
128
  def get_student_discourse_data(username):
129
  """
 
19
 
20
  COLLECTION_NAME = 'student_discourse_analysis'
21
 
22
+ def fig_to_bytes(fig):
23
+ """Convierte una figura de matplotlib a bytes."""
24
+ try:
25
+ buf = io.BytesIO()
26
+ fig.savefig(buf, format='png', dpi=300, bbox_inches='tight')
27
+ buf.seek(0)
28
+ return buf.getvalue()
29
+ except Exception as e:
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
+ # Convertir figuras a bytes antes de codificar
39
  graph1_data = None
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 figura a bytes
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 codificar gráfico 1: {str(e)}")
53
 
54
  if 'graph2' in analysis_result and analysis_result['graph2'] is not None:
55
  try:
56
+ # Convertir figura a bytes
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 codificar gráfico 2: {str(e)}")
64
 
 
 
 
 
 
 
65
  # Crear documento para MongoDB
66
  analysis_document = {
67
  'username': username,
 
73
  'key_concepts2': analysis_result.get('key_concepts2', []),
74
  'graph1': graph1_data,
75
  'graph2': graph2_data,
76
+ 'graph1_bytes': analysis_result.get('graph1_bytes'),
77
+ 'graph2_bytes': analysis_result.get('graph2_bytes')
78
  }
79
 
80
  # Insertar en MongoDB
 
136
  logger.error(f"Error recuperando análisis del discurso: {str(e)}")
137
  return []
138
  #####################################################################################
 
 
 
 
139
 
140
  def get_student_discourse_data(username):
141
  """