Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -30,111 +30,150 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 30 |
st.session_state.semantic_live_state['current_text'] = current_text
|
| 31 |
st.session_state.semantic_live_state['text_changed'] = True
|
| 32 |
|
| 33 |
-
# 3.
|
| 34 |
-
st.
|
| 35 |
-
|
| 36 |
-
text_input = st.text_area(
|
| 37 |
-
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 38 |
-
height=300,
|
| 39 |
-
key="semantic_live_text",
|
| 40 |
-
value=st.session_state.semantic_live_state.get('current_text', ''),
|
| 41 |
-
on_change=on_text_change,
|
| 42 |
-
label_visibility="collapsed"
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
semantic_t.get('
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
nlp_models,
|
| 62 |
-
semantic_t
|
| 63 |
-
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Guardar en MongoDB (colección live)
|
| 72 |
-
store_result = store_student_semantic_live_result(
|
| 73 |
-
st.session_state.username,
|
| 74 |
text_input,
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
)
|
| 78 |
-
|
| 79 |
-
if
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
else:
|
| 82 |
-
st.
|
| 83 |
-
else:
|
| 84 |
-
st.error(analysis_result.get('message', 'Error en el análisis'))
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
| 95 |
-
|
| 96 |
-
if 'key_concepts' in analysis and analysis['key_concepts'] and \
|
| 97 |
-
'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
| 98 |
-
|
| 99 |
-
# Mostrar conceptos clave
|
| 100 |
-
st.subheader(semantic_t.get('key_concepts', 'Conceptos Clave'))
|
| 101 |
-
concepts_html = """
|
| 102 |
-
<div style="display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px;">
|
| 103 |
-
""" + ''.join([
|
| 104 |
-
f'<div style="background-color: #f0f2f6; border-radius: 5px; padding: 6px 10px; display: inline-flex; align-items: center; gap: 6px;">'
|
| 105 |
-
f'<span style="font-weight: bold;">{concept}</span>'
|
| 106 |
-
f'<span style="color: #666; font-size: 0.9em;">({freq:.2f})</span></div>'
|
| 107 |
-
for concept, freq in analysis['key_concepts']
|
| 108 |
-
]) + "</div>"
|
| 109 |
-
st.markdown(concepts_html, unsafe_allow_html=True)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
st.subheader(semantic_t.get('concept_network', 'Red de Conceptos'))
|
| 113 |
-
st.image(
|
| 114 |
-
analysis['concept_graph'],
|
| 115 |
-
use_container_width=True
|
| 116 |
-
)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
}
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
else:
|
| 136 |
-
st.info(semantic_t.get('
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
-
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 140 |
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
|
| 30 |
st.session_state.semantic_live_state['current_text'] = current_text
|
| 31 |
st.session_state.semantic_live_state['text_changed'] = True
|
| 32 |
|
| 33 |
+
# 3. Crear columnas para el diseño (1:3 ratio)
|
| 34 |
+
input_col, result_col = st.columns([1, 3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Columna izquierda: Entrada de texto y botón
|
| 37 |
+
with input_col:
|
| 38 |
+
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
| 39 |
+
|
| 40 |
+
text_input = st.text_area(
|
| 41 |
+
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 42 |
+
height=400, # Altura aumentada
|
| 43 |
+
key="semantic_live_text",
|
| 44 |
+
value=st.session_state.semantic_live_state.get('current_text', ''),
|
| 45 |
+
on_change=on_text_change,
|
| 46 |
+
label_visibility="collapsed"
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
analyze_button = st.button(
|
| 50 |
+
semantic_t.get('analyze_button', 'Analizar'),
|
| 51 |
+
key="semantic_live_analyze",
|
| 52 |
+
type="primary",
|
| 53 |
+
disabled=not text_input,
|
| 54 |
+
use_container_width=True
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# 4. Procesar análisis cuando se presiona el botón
|
| 58 |
+
if analyze_button and text_input:
|
| 59 |
+
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 60 |
+
try:
|
| 61 |
+
analysis_result = process_semantic_input(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
text_input,
|
| 63 |
+
lang_code,
|
| 64 |
+
nlp_models,
|
| 65 |
+
semantic_t
|
| 66 |
)
|
| 67 |
+
|
| 68 |
+
if analysis_result['success']:
|
| 69 |
+
# Guardar resultado en sesión y base de datos
|
| 70 |
+
st.session_state.semantic_live_state['last_result'] = analysis_result
|
| 71 |
+
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 72 |
+
st.session_state.semantic_live_state['text_changed'] = False
|
| 73 |
+
|
| 74 |
+
# Guardar en MongoDB (colección live)
|
| 75 |
+
store_result = store_student_semantic_live_result(
|
| 76 |
+
st.session_state.username,
|
| 77 |
+
text_input,
|
| 78 |
+
analysis_result['analysis'],
|
| 79 |
+
lang_code
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if not store_result:
|
| 83 |
+
st.error(semantic_t.get('error_saving', 'Error al guardar el análisis'))
|
| 84 |
+
else:
|
| 85 |
+
st.success(semantic_t.get('analysis_saved', 'Análisis guardado correctamente'))
|
| 86 |
+
st.rerun() # Forzar actualización para mostrar resultados
|
| 87 |
else:
|
| 88 |
+
st.error(analysis_result.get('message', 'Error en el análisis'))
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"Error en análisis: {str(e)}", exc_info=True)
|
| 92 |
+
st.error(semantic_t.get('error_processing', f'Error al procesar el texto: {str(e)}'))
|
| 93 |
|
| 94 |
+
# Columna derecha: Resultados
|
| 95 |
+
with result_col:
|
| 96 |
+
if 'last_result' in st.session_state.semantic_live_state and \
|
| 97 |
+
st.session_state.semantic_live_state['last_result'] is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
if 'key_concepts' in analysis and analysis['key_concepts'] and \
|
| 102 |
+
'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
| 103 |
+
|
| 104 |
+
# Mostrar conceptos clave en formato horizontal
|
| 105 |
+
st.subheader(semantic_t.get('key_concepts', 'Conceptos Clave'))
|
| 106 |
+
concepts_html = """
|
| 107 |
+
<style>
|
| 108 |
+
.concept-container {
|
| 109 |
+
display: flex;
|
| 110 |
+
flex-wrap: wrap;
|
| 111 |
+
gap: 8px;
|
| 112 |
+
margin-bottom: 20px;
|
| 113 |
+
max-width: 100%;
|
| 114 |
+
overflow-x: auto;
|
| 115 |
}
|
| 116 |
+
.concept-item {
|
| 117 |
+
background-color: #f0f2f6;
|
| 118 |
+
border-radius: 5px;
|
| 119 |
+
padding: 6px 10px;
|
| 120 |
+
display: inline-flex;
|
| 121 |
+
align-items: center;
|
| 122 |
+
gap: 6px;
|
| 123 |
+
white-space: nowrap;
|
| 124 |
+
}
|
| 125 |
+
.concept-name {
|
| 126 |
+
font-weight: bold;
|
| 127 |
+
}
|
| 128 |
+
.concept-freq {
|
| 129 |
+
color: #666;
|
| 130 |
+
font-size: 0.9em;
|
| 131 |
+
}
|
| 132 |
+
</style>
|
| 133 |
+
<div class="concept-container">
|
| 134 |
+
""" + ''.join([
|
| 135 |
+
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
| 136 |
+
f'<span class="concept-freq">({freq:.2f})</span></div>'
|
| 137 |
+
for concept, freq in analysis['key_concepts']
|
| 138 |
+
]) + "</div>"
|
| 139 |
+
st.markdown(concepts_html, unsafe_allow_html=True)
|
| 140 |
+
|
| 141 |
+
# Mostrar gráfico con controles
|
| 142 |
+
st.subheader(semantic_t.get('concept_network', 'Red de Conceptos'))
|
| 143 |
+
st.image(
|
| 144 |
+
analysis['concept_graph'],
|
| 145 |
+
use_container_width=True
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Controles debajo del gráfico
|
| 149 |
+
btn_col1, btn_col2 = st.columns([1, 3])
|
| 150 |
+
with btn_col1:
|
| 151 |
+
if st.button("💬 Consultar con Asistente", key="semantic_live_chat_button"):
|
| 152 |
+
st.session_state.semantic_agent_data = {
|
| 153 |
+
'text': st.session_state.semantic_live_state['current_text'],
|
| 154 |
+
'metrics': analysis,
|
| 155 |
+
'graph_data': analysis.get('concept_graph')
|
| 156 |
+
}
|
| 157 |
+
st.session_state.semantic_agent_active = True
|
| 158 |
+
st.rerun()
|
| 159 |
+
|
| 160 |
+
# Notificación si el agente está activo
|
| 161 |
+
if st.session_state.get('semantic_agent_active', False):
|
| 162 |
+
st.success(semantic_t.get('semantic_agent_ready_message', 'El agente virtual está listo. Abre el chat en la barra lateral.'))
|
| 163 |
+
|
| 164 |
+
# Información de interpretación
|
| 165 |
+
with st.expander("📊 Interpretación del gráfico"):
|
| 166 |
+
st.markdown("""
|
| 167 |
+
- 🔀 **Flechas**: Dirección de las relaciones entre conceptos
|
| 168 |
+
- 🎨 **Colores**: Intensidad indica centralidad del concepto
|
| 169 |
+
- ⭕ **Tamaño**: Representa frecuencia del concepto
|
| 170 |
+
- ↔️ **Grosor**: Fuerza de la conexión entre conceptos
|
| 171 |
+
""")
|
| 172 |
+
else:
|
| 173 |
+
st.info(semantic_t.get('no_results', 'No hay resultados para mostrar'))
|
| 174 |
else:
|
| 175 |
+
st.info(semantic_t.get('analysis_prompt', 'Realice un análisis para ver los resultados'))
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
+
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}", exc_info=True)
|
| 179 |
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|