Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -13,14 +13,11 @@ from .semantic_process import (
|
|
| 13 |
process_semantic_input,
|
| 14 |
format_semantic_results
|
| 15 |
)
|
| 16 |
-
|
| 17 |
from ..utils.widget_utils import generate_unique_key
|
| 18 |
-
#
|
| 19 |
from ..database.semantic_mongo_live_db import store_student_semantic_live_result
|
| 20 |
-
#
|
| 21 |
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
| 22 |
|
| 23 |
-
####################################################################################
|
| 24 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
| 25 |
"""
|
| 26 |
Interfaz para el análisis semántico en vivo con proporciones de columna ajustadas
|
|
@@ -32,7 +29,8 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 32 |
'analysis_count': 0,
|
| 33 |
'current_text': '',
|
| 34 |
'last_result': None,
|
| 35 |
-
'text_changed': False
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
# 2. Función para manejar cambios en el texto
|
|
@@ -55,7 +53,7 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 55 |
key="semantic_live_text",
|
| 56 |
value=st.session_state.semantic_live_state.get('current_text', ''),
|
| 57 |
on_change=on_text_change,
|
| 58 |
-
label_visibility="collapsed"
|
| 59 |
)
|
| 60 |
|
| 61 |
# Botón de análisis y procesamiento
|
|
@@ -68,7 +66,13 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 68 |
use_container_width=True
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
if analyze_button and text_input:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 74 |
analysis_result = process_semantic_input(
|
|
@@ -83,17 +87,26 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 83 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 84 |
st.session_state.semantic_live_state['text_changed'] = False
|
| 85 |
|
| 86 |
-
|
|
|
|
| 87 |
st.session_state.username,
|
| 88 |
text_input,
|
| 89 |
-
analysis_result['analysis']
|
|
|
|
| 90 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
st.error(analysis_result.get('message', 'Error en el análisis'))
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
logger.error(f"Error en análisis: {str(e)}")
|
| 96 |
st.error(semantic_t.get('error_processing', 'Error al procesar el texto'))
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Columna derecha: Visualización de resultados
|
| 99 |
with result_col:
|
|
@@ -119,31 +132,31 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 119 |
}
|
| 120 |
.concept-table {
|
| 121 |
display: flex;
|
| 122 |
-
flex-wrap: nowrap;
|
| 123 |
-
gap: 6px;
|
| 124 |
padding: 10px;
|
| 125 |
background-color: #f8f9fa;
|
| 126 |
-
overflow-x: auto;
|
| 127 |
-
white-space: nowrap;
|
| 128 |
}
|
| 129 |
.concept-item {
|
| 130 |
background-color: white;
|
| 131 |
border-radius: 4px;
|
| 132 |
-
padding: 4px 8px;
|
| 133 |
-
display: inline-flex;
|
| 134 |
align-items: center;
|
| 135 |
-
gap: 4px;
|
| 136 |
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 137 |
-
flex-shrink: 0;
|
| 138 |
}
|
| 139 |
.concept-name {
|
| 140 |
font-weight: 500;
|
| 141 |
color: #1f2937;
|
| 142 |
-
font-size: 0.8em;
|
| 143 |
}
|
| 144 |
.concept-freq {
|
| 145 |
color: #6b7280;
|
| 146 |
-
font-size: 0.75em;
|
| 147 |
}
|
| 148 |
.graph-section {
|
| 149 |
padding: 20px;
|
|
@@ -173,28 +186,51 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 173 |
use_container_width=True
|
| 174 |
)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
st.download_button(
|
| 180 |
-
label="📥 " + semantic_t.get('download_graph', "
|
| 181 |
data=analysis['concept_graph'],
|
| 182 |
file_name="semantic_live_graph.png",
|
| 183 |
mime="image/png",
|
| 184 |
use_container_width=True
|
| 185 |
)
|
| 186 |
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
st.markdown("""
|
| 189 |
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
| 190 |
-
- 🎨 Los colores más intensos indican conceptos más centrales
|
| 191 |
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
| 192 |
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
| 193 |
""")
|
| 194 |
else:
|
| 195 |
st.info(semantic_t.get('no_graph', 'No hay datos para mostrar'))
|
|
|
|
|
|
|
| 196 |
|
| 197 |
except Exception as e:
|
| 198 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 199 |
-
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 200 |
-
|
|
|
|
| 13 |
process_semantic_input,
|
| 14 |
format_semantic_results
|
| 15 |
)
|
| 16 |
+
|
| 17 |
from ..utils.widget_utils import generate_unique_key
|
|
|
|
| 18 |
from ..database.semantic_mongo_live_db import store_student_semantic_live_result
|
|
|
|
| 19 |
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
| 20 |
|
|
|
|
| 21 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
| 22 |
"""
|
| 23 |
Interfaz para el análisis semántico en vivo con proporciones de columna ajustadas
|
|
|
|
| 29 |
'analysis_count': 0,
|
| 30 |
'current_text': '',
|
| 31 |
'last_result': None,
|
| 32 |
+
'text_changed': False,
|
| 33 |
+
'pending_analysis': False # Nuevo flag para análisis pendiente
|
| 34 |
}
|
| 35 |
|
| 36 |
# 2. Función para manejar cambios en el texto
|
|
|
|
| 53 |
key="semantic_live_text",
|
| 54 |
value=st.session_state.semantic_live_state.get('current_text', ''),
|
| 55 |
on_change=on_text_change,
|
| 56 |
+
label_visibility="collapsed"
|
| 57 |
)
|
| 58 |
|
| 59 |
# Botón de análisis y procesamiento
|
|
|
|
| 66 |
use_container_width=True
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# 4. Procesar análisis cuando se presiona el botón
|
| 70 |
if analyze_button and text_input:
|
| 71 |
+
st.session_state.semantic_live_state['pending_analysis'] = True
|
| 72 |
+
st.rerun()
|
| 73 |
+
|
| 74 |
+
# 5. Manejar análisis pendiente
|
| 75 |
+
if st.session_state.semantic_live_state.get('pending_analysis', False):
|
| 76 |
try:
|
| 77 |
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 78 |
analysis_result = process_semantic_input(
|
|
|
|
| 87 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 88 |
st.session_state.semantic_live_state['text_changed'] = False
|
| 89 |
|
| 90 |
+
# Guardar en la colección live
|
| 91 |
+
store_result = store_student_semantic_live_result(
|
| 92 |
st.session_state.username,
|
| 93 |
text_input,
|
| 94 |
+
analysis_result['analysis'],
|
| 95 |
+
lang_code
|
| 96 |
)
|
| 97 |
+
|
| 98 |
+
if not store_result:
|
| 99 |
+
st.error(semantic_t.get('error_saving', 'Error al guardar el análisis'))
|
| 100 |
+
else:
|
| 101 |
+
st.success(semantic_t.get('analysis_saved', 'Análisis guardado correctamente'))
|
| 102 |
else:
|
| 103 |
st.error(analysis_result.get('message', 'Error en el análisis'))
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
logger.error(f"Error en análisis: {str(e)}")
|
| 107 |
st.error(semantic_t.get('error_processing', 'Error al procesar el texto'))
|
| 108 |
+
finally:
|
| 109 |
+
st.session_state.semantic_live_state['pending_analysis'] = False
|
| 110 |
|
| 111 |
# Columna derecha: Visualización de resultados
|
| 112 |
with result_col:
|
|
|
|
| 132 |
}
|
| 133 |
.concept-table {
|
| 134 |
display: flex;
|
| 135 |
+
flex-wrap: nowrap;
|
| 136 |
+
gap: 6px;
|
| 137 |
padding: 10px;
|
| 138 |
background-color: #f8f9fa;
|
| 139 |
+
overflow-x: auto;
|
| 140 |
+
white-space: nowrap;
|
| 141 |
}
|
| 142 |
.concept-item {
|
| 143 |
background-color: white;
|
| 144 |
border-radius: 4px;
|
| 145 |
+
padding: 4px 8px;
|
| 146 |
+
display: inline-flex;
|
| 147 |
align-items: center;
|
| 148 |
+
gap: 4px;
|
| 149 |
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 150 |
+
flex-shrink: 0;
|
| 151 |
}
|
| 152 |
.concept-name {
|
| 153 |
font-weight: 500;
|
| 154 |
color: #1f2937;
|
| 155 |
+
font-size: 0.8em;
|
| 156 |
}
|
| 157 |
.concept-freq {
|
| 158 |
color: #6b7280;
|
| 159 |
+
font-size: 0.75em;
|
| 160 |
}
|
| 161 |
.graph-section {
|
| 162 |
padding: 20px;
|
|
|
|
| 186 |
use_container_width=True
|
| 187 |
)
|
| 188 |
|
| 189 |
+
# Controles en dos columnas
|
| 190 |
+
col1, col2 = st.columns([1, 3])
|
| 191 |
+
|
| 192 |
+
with col1:
|
| 193 |
+
# Botón para consultar con el asistente (NUEVO)
|
| 194 |
+
if st.button("💬 Consultar con Asistente",
|
| 195 |
+
key="semantic_live_chat_button",
|
| 196 |
+
use_container_width=True):
|
| 197 |
+
if 'last_result' not in st.session_state.semantic_live_state:
|
| 198 |
+
st.error("Primero complete el análisis semántico")
|
| 199 |
+
else:
|
| 200 |
+
st.session_state.semantic_agent_data = {
|
| 201 |
+
'text': st.session_state.semantic_live_state['current_text'],
|
| 202 |
+
'metrics': analysis,
|
| 203 |
+
'graph_data': analysis.get('concept_graph')
|
| 204 |
+
}
|
| 205 |
+
st.session_state.semantic_agent_active = True
|
| 206 |
+
st.rerun()
|
| 207 |
+
|
| 208 |
+
# Botón de descarga
|
| 209 |
st.download_button(
|
| 210 |
+
label="📥 " + semantic_t.get('download_graph', "Descargar"),
|
| 211 |
data=analysis['concept_graph'],
|
| 212 |
file_name="semantic_live_graph.png",
|
| 213 |
mime="image/png",
|
| 214 |
use_container_width=True
|
| 215 |
)
|
| 216 |
|
| 217 |
+
# Notificación si el agente está activo
|
| 218 |
+
if st.session_state.get('semantic_agent_active', False):
|
| 219 |
+
st.success(semantic_t.get('semantic_agent_ready_message',
|
| 220 |
+
'El agente virtual está listo. Abre el chat en la barra lateral.'))
|
| 221 |
+
|
| 222 |
+
with st.expander("📊 " + semantic_t.get('graph_help', "Interpretación del gráfico")):
|
| 223 |
st.markdown("""
|
| 224 |
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
| 225 |
+
- 🎨 Los colores más intensos indican conceptos más centrales
|
| 226 |
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
| 227 |
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
| 228 |
""")
|
| 229 |
else:
|
| 230 |
st.info(semantic_t.get('no_graph', 'No hay datos para mostrar'))
|
| 231 |
+
else:
|
| 232 |
+
st.info(semantic_t.get('analysis_prompt', 'Realice un análisis para ver los resultados'))
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 236 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|