Update modules/discourse/discourse_live_interface.py
Browse files
modules/discourse/discourse_live_interface.py
CHANGED
@@ -18,6 +18,8 @@ from ..utils.widget_utils import generate_unique_key
|
|
18 |
from ..database.discourse_mongo_db import store_student_discourse_result
|
19 |
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
20 |
|
|
|
|
|
21 |
def fig_to_bytes(fig):
|
22 |
"""Convierte una figura de matplotlib a bytes."""
|
23 |
try:
|
@@ -29,70 +31,13 @@ def fig_to_bytes(fig):
|
|
29 |
logger.error(f"Error en fig_to_bytes: {str(e)}")
|
30 |
return None
|
31 |
|
|
|
32 |
def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
33 |
"""
|
34 |
Interfaz para el análisis del discurso en vivo
|
35 |
"""
|
36 |
try:
|
37 |
-
#
|
38 |
-
if 'discourse_live_state' not in st.session_state:
|
39 |
-
st.session_state.discourse_live_state = {
|
40 |
-
'analysis_count': 0,
|
41 |
-
'current_text1': '',
|
42 |
-
'current_text2': '',
|
43 |
-
'last_result': None,
|
44 |
-
'text_changed': False
|
45 |
-
}
|
46 |
-
|
47 |
-
# 2. Función para manejar cambios en los textos
|
48 |
-
def on_text1_change():
|
49 |
-
current_text = st.session_state.discourse_live_text1
|
50 |
-
st.session_state.discourse_live_state['current_text1'] = current_text
|
51 |
-
st.session_state.discourse_live_state['text_changed'] = True
|
52 |
-
|
53 |
-
def on_text2_change():
|
54 |
-
current_text = st.session_state.discourse_live_text2
|
55 |
-
st.session_state.discourse_live_state['current_text2'] = current_text
|
56 |
-
st.session_state.discourse_live_state['text_changed'] = True
|
57 |
-
|
58 |
-
# 3. Crear columnas con proporción 1:3
|
59 |
-
input_col, result_col = st.columns([1, 3])
|
60 |
-
|
61 |
-
# Columna izquierda: Entrada de textos
|
62 |
-
with input_col:
|
63 |
-
st.subheader(discourse_t.get('enter_text', 'Ingrese sus textos'))
|
64 |
-
|
65 |
-
# Primer área de texto
|
66 |
-
st.markdown("**Texto 1 (Patrón)**")
|
67 |
-
text_input1 = st.text_area(
|
68 |
-
"Texto 1",
|
69 |
-
height=250,
|
70 |
-
key="discourse_live_text1",
|
71 |
-
value=st.session_state.discourse_live_state.get('current_text1', ''),
|
72 |
-
on_change=on_text1_change,
|
73 |
-
label_visibility="collapsed"
|
74 |
-
)
|
75 |
-
|
76 |
-
# Segundo área de texto
|
77 |
-
st.markdown("**Texto 2 (Comparación)**")
|
78 |
-
text_input2 = st.text_area(
|
79 |
-
"Texto 2",
|
80 |
-
height=250,
|
81 |
-
key="discourse_live_text2",
|
82 |
-
value=st.session_state.discourse_live_state.get('current_text2', ''),
|
83 |
-
on_change=on_text2_change,
|
84 |
-
label_visibility="collapsed"
|
85 |
-
)
|
86 |
-
|
87 |
-
# Botón de análisis
|
88 |
-
analyze_button = st.button(
|
89 |
-
discourse_t.get('analyze_button', 'Analizar'),
|
90 |
-
key="discourse_live_analyze",
|
91 |
-
type="primary",
|
92 |
-
icon="🔍",
|
93 |
-
disabled=not (text_input1 and text_input2),
|
94 |
-
use_container_width=True
|
95 |
-
)
|
96 |
|
97 |
if analyze_button and text_input1 and text_input2:
|
98 |
try:
|
@@ -106,31 +51,36 @@ def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
|
106 |
)
|
107 |
|
108 |
if result['success']:
|
109 |
-
#
|
110 |
-
|
111 |
-
result
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
st.session_state.discourse_live_state['last_result'] = result
|
116 |
st.session_state.discourse_live_state['analysis_count'] += 1
|
117 |
st.session_state.discourse_live_state['text_changed'] = False
|
118 |
|
119 |
# Guardar en base de datos
|
120 |
-
store_student_discourse_result(
|
121 |
st.session_state.username,
|
122 |
text_input1,
|
123 |
text_input2,
|
124 |
result
|
125 |
)
|
|
|
|
|
|
|
126 |
else:
|
127 |
st.error(result.get('message', 'Error en el análisis'))
|
128 |
|
129 |
except Exception as e:
|
130 |
logger.error(f"Error en análisis: {str(e)}")
|
131 |
-
st.error(discourse_t.get('error_processing', 'Error al procesar el texto'))
|
132 |
|
133 |
-
|
134 |
# Columna derecha: Visualización de resultados
|
135 |
with result_col:
|
136 |
st.subheader(discourse_t.get('live_results', 'Resultados en vivo'))
|
@@ -138,12 +88,14 @@ def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
|
138 |
if 'last_result' in st.session_state.discourse_live_state and \
|
139 |
st.session_state.discourse_live_state['last_result'] is not None:
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
discourse_t
|
146 |
-
|
|
|
|
|
147 |
|
148 |
elif st.session_state.discourse_live_state.get('text_changed', False):
|
149 |
st.info(discourse_t.get('changes_pending',
|
@@ -154,4 +106,4 @@ def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
|
154 |
|
155 |
except Exception as e:
|
156 |
logger.error(f"Error general en interfaz del discurso en vivo: {str(e)}")
|
157 |
-
st.error(discourse_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
18 |
from ..database.discourse_mongo_db import store_student_discourse_result
|
19 |
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
20 |
|
21 |
+
|
22 |
+
#####################################################################################################
|
23 |
def fig_to_bytes(fig):
|
24 |
"""Convierte una figura de matplotlib a bytes."""
|
25 |
try:
|
|
|
31 |
logger.error(f"Error en fig_to_bytes: {str(e)}")
|
32 |
return None
|
33 |
|
34 |
+
#################################################################################################
|
35 |
def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
36 |
"""
|
37 |
Interfaz para el análisis del discurso en vivo
|
38 |
"""
|
39 |
try:
|
40 |
+
# [Código anterior igual hasta el análisis]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if analyze_button and text_input1 and text_input2:
|
43 |
try:
|
|
|
51 |
)
|
52 |
|
53 |
if result['success']:
|
54 |
+
# Asegurarnos de que ambos gráficos se procesen correctamente
|
55 |
+
for graph_key in ['graph1', 'graph2']:
|
56 |
+
if graph_key in result and result[graph_key] is not None:
|
57 |
+
bytes_key = f'{graph_key}_bytes'
|
58 |
+
graph_bytes = fig_to_bytes(result[graph_key])
|
59 |
+
if graph_bytes:
|
60 |
+
result[bytes_key] = graph_bytes
|
61 |
+
plt.close(result[graph_key]) # Cerrar la figura después de convertirla
|
62 |
|
63 |
st.session_state.discourse_live_state['last_result'] = result
|
64 |
st.session_state.discourse_live_state['analysis_count'] += 1
|
65 |
st.session_state.discourse_live_state['text_changed'] = False
|
66 |
|
67 |
# Guardar en base de datos
|
68 |
+
store_result = store_student_discourse_result(
|
69 |
st.session_state.username,
|
70 |
text_input1,
|
71 |
text_input2,
|
72 |
result
|
73 |
)
|
74 |
+
|
75 |
+
if not store_result:
|
76 |
+
logger.warning("No se pudo guardar el análisis en la base de datos")
|
77 |
else:
|
78 |
st.error(result.get('message', 'Error en el análisis'))
|
79 |
|
80 |
except Exception as e:
|
81 |
logger.error(f"Error en análisis: {str(e)}")
|
82 |
+
st.error(discourse_t.get('error_processing', f'Error al procesar el texto: {str(e)}'))
|
83 |
|
|
|
84 |
# Columna derecha: Visualización de resultados
|
85 |
with result_col:
|
86 |
st.subheader(discourse_t.get('live_results', 'Resultados en vivo'))
|
|
|
88 |
if 'last_result' in st.session_state.discourse_live_state and \
|
89 |
st.session_state.discourse_live_state['last_result'] is not None:
|
90 |
|
91 |
+
result = st.session_state.discourse_live_state['last_result']
|
92 |
+
|
93 |
+
# Verificar que tenemos ambos gráficos antes de mostrarlos
|
94 |
+
if all(key in result for key in ['graph1', 'graph2', 'graph1_bytes', 'graph2_bytes']):
|
95 |
+
display_discourse_results(result, lang_code, discourse_t)
|
96 |
+
else:
|
97 |
+
logger.error(f"Faltan gráficos en el resultado: {list(result.keys())}")
|
98 |
+
st.error(discourse_t.get('missing_graphs', 'Error: No se pudieron generar todos los gráficos'))
|
99 |
|
100 |
elif st.session_state.discourse_live_state.get('text_changed', False):
|
101 |
st.info(discourse_t.get('changes_pending',
|
|
|
106 |
|
107 |
except Exception as e:
|
108 |
logger.error(f"Error general en interfaz del discurso en vivo: {str(e)}")
|
109 |
+
st.error(discourse_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|