Update modules/discourse/discourse_live_interface.py
Browse files
modules/discourse/discourse_live_interface.py
CHANGED
@@ -32,17 +32,63 @@ def fig_to_bytes(fig):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if analyze_button and text_input1 and text_input2:
|
43 |
try:
|
44 |
with st.spinner(discourse_t.get('processing', 'Procesando...')):
|
45 |
-
# Realizar análisis
|
46 |
result = perform_discourse_analysis(
|
47 |
text_input1,
|
48 |
text_input2,
|
@@ -51,29 +97,24 @@ def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
|
51 |
)
|
52 |
|
53 |
if result['success']:
|
54 |
-
#
|
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])
|
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 |
-
|
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 |
|
@@ -89,17 +130,11 @@ def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
|
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',
|
102 |
-
'Los textos han cambiado. Presione Analizar para ver los nuevos resultados.'))
|
103 |
else:
|
104 |
st.info(discourse_t.get('initial_message',
|
105 |
'Ingrese los textos y presione Analizar para ver los resultados.'))
|
|
|
32 |
return None
|
33 |
|
34 |
#################################################################################################
|
35 |
+
|
36 |
def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
37 |
"""
|
38 |
Interfaz para el análisis del discurso en vivo
|
39 |
"""
|
40 |
try:
|
41 |
+
if 'discourse_live_state' not in st.session_state:
|
42 |
+
st.session_state.discourse_live_state = {
|
43 |
+
'analysis_count': 0,
|
44 |
+
'current_text1': '',
|
45 |
+
'current_text2': '',
|
46 |
+
'last_result': None,
|
47 |
+
'text_changed': False
|
48 |
+
}
|
49 |
+
|
50 |
+
# Crear columnas con proporción 1:3
|
51 |
+
input_col, result_col = st.columns([1, 3])
|
52 |
+
|
53 |
+
# Columna izquierda: Entrada de textos
|
54 |
+
with input_col:
|
55 |
+
st.subheader(discourse_t.get('enter_text', 'Ingrese sus textos'))
|
56 |
+
|
57 |
+
# Primer área de texto
|
58 |
+
st.markdown("**Texto 1 (Patrón)**")
|
59 |
+
text_input1 = st.text_area(
|
60 |
+
"Texto 1",
|
61 |
+
height=250,
|
62 |
+
key="discourse_live_text1",
|
63 |
+
value=st.session_state.discourse_live_state.get('current_text1', ''),
|
64 |
+
label_visibility="collapsed"
|
65 |
+
)
|
66 |
+
st.session_state.discourse_live_state['current_text1'] = text_input1
|
67 |
+
|
68 |
+
# Segundo área de texto
|
69 |
+
st.markdown("**Texto 2 (Comparación)**")
|
70 |
+
text_input2 = st.text_area(
|
71 |
+
"Texto 2",
|
72 |
+
height=250,
|
73 |
+
key="discourse_live_text2",
|
74 |
+
value=st.session_state.discourse_live_state.get('current_text2', ''),
|
75 |
+
label_visibility="collapsed"
|
76 |
+
)
|
77 |
+
st.session_state.discourse_live_state['current_text2'] = text_input2
|
78 |
+
|
79 |
+
# Botón de análisis
|
80 |
+
analyze_button = st.button(
|
81 |
+
discourse_t.get('analyze_button', 'Analizar'),
|
82 |
+
key="discourse_live_analyze",
|
83 |
+
type="primary",
|
84 |
+
icon="🔍",
|
85 |
+
disabled=not (text_input1 and text_input2),
|
86 |
+
use_container_width=True
|
87 |
+
)
|
88 |
|
89 |
if analyze_button and text_input1 and text_input2:
|
90 |
try:
|
91 |
with st.spinner(discourse_t.get('processing', 'Procesando...')):
|
|
|
92 |
result = perform_discourse_analysis(
|
93 |
text_input1,
|
94 |
text_input2,
|
|
|
97 |
)
|
98 |
|
99 |
if result['success']:
|
100 |
+
# Procesar ambos gráficos
|
101 |
for graph_key in ['graph1', 'graph2']:
|
102 |
if graph_key in result and result[graph_key] is not None:
|
103 |
bytes_key = f'{graph_key}_bytes'
|
104 |
graph_bytes = fig_to_bytes(result[graph_key])
|
105 |
if graph_bytes:
|
106 |
result[bytes_key] = graph_bytes
|
107 |
+
plt.close(result[graph_key])
|
108 |
|
109 |
st.session_state.discourse_live_state['last_result'] = result
|
110 |
st.session_state.discourse_live_state['analysis_count'] += 1
|
|
|
111 |
|
112 |
+
store_student_discourse_result(
|
|
|
113 |
st.session_state.username,
|
114 |
text_input1,
|
115 |
text_input2,
|
116 |
result
|
117 |
)
|
|
|
|
|
|
|
118 |
else:
|
119 |
st.error(result.get('message', 'Error en el análisis'))
|
120 |
|
|
|
130 |
st.session_state.discourse_live_state['last_result'] is not None:
|
131 |
|
132 |
result = st.session_state.discourse_live_state['last_result']
|
133 |
+
if all(key in result for key in ['graph1', 'graph2']):
|
|
|
|
|
134 |
display_discourse_results(result, lang_code, discourse_t)
|
135 |
else:
|
136 |
logger.error(f"Faltan gráficos en el resultado: {list(result.keys())}")
|
137 |
st.error(discourse_t.get('missing_graphs', 'Error: No se pudieron generar todos los gráficos'))
|
|
|
|
|
|
|
|
|
138 |
else:
|
139 |
st.info(discourse_t.get('initial_message',
|
140 |
'Ingrese los textos y presione Analizar para ver los resultados.'))
|