Spaces:
Running
Running
Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -27,77 +27,91 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 27 |
semantic_t: Diccionario de traducciones semánticas
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
-
# 1. Inicializar el estado de la sesión
|
| 31 |
if 'semantic_live_state' not in st.session_state:
|
| 32 |
st.session_state.semantic_live_state = {
|
| 33 |
'analysis_count': 0,
|
| 34 |
-
'last_analysis': None,
|
| 35 |
'current_text': '',
|
| 36 |
-
'last_result': None
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
-
# 2.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
# Mostrar conceptos clave en formato horizontal
|
| 102 |
if 'key_concepts' in analysis and analysis['key_concepts']:
|
| 103 |
st.markdown("""
|
|
@@ -131,7 +145,6 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 131 |
</style>
|
| 132 |
""", unsafe_allow_html=True)
|
| 133 |
|
| 134 |
-
# Crear la visualización horizontal de conceptos
|
| 135 |
concepts_html = ['<div class="concept-table">']
|
| 136 |
concepts_html.extend(
|
| 137 |
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
|
@@ -142,25 +155,23 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 142 |
|
| 143 |
st.markdown(''.join(concepts_html), unsafe_allow_html=True)
|
| 144 |
|
| 145 |
-
# Mostrar gráfico de conceptos
|
| 146 |
if 'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
| 147 |
-
# Container para el grafo con estilos mejorados
|
| 148 |
-
st.markdown("""
|
| 149 |
-
<style>
|
| 150 |
-
.graph-container {
|
| 151 |
-
background-color: white;
|
| 152 |
-
border-radius: 10px;
|
| 153 |
-
padding: 20px;
|
| 154 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 155 |
-
margin: 10px 0;
|
| 156 |
-
}
|
| 157 |
-
</style>
|
| 158 |
-
""", unsafe_allow_html=True)
|
| 159 |
-
|
| 160 |
with st.container():
|
| 161 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
# Mostrar grafo
|
| 164 |
st.image(
|
| 165 |
analysis['concept_graph'],
|
| 166 |
use_container_width=True,
|
|
@@ -172,32 +183,29 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 172 |
|
| 173 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
else:
|
| 195 |
st.info(semantic_t.get('no_graph', 'No graph available'))
|
| 196 |
|
| 197 |
-
except Exception as e:
|
| 198 |
-
logger.error(f"Error processing or displaying results: {str(e)}")
|
| 199 |
-
st.error(semantic_t.get('error_processing', f'Error al procesar o mostrar resultados: {str(e)}'))
|
| 200 |
-
|
| 201 |
except Exception as e:
|
| 202 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 203 |
-
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
|
|
|
| 27 |
semantic_t: Diccionario de traducciones semánticas
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
+
# 1. Inicializar el estado de la sesión de manera más robusta
|
| 31 |
if 'semantic_live_state' not in st.session_state:
|
| 32 |
st.session_state.semantic_live_state = {
|
| 33 |
'analysis_count': 0,
|
|
|
|
| 34 |
'current_text': '',
|
| 35 |
+
'last_result': None,
|
| 36 |
+
'text_changed': False
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# 2. Función callback para manejar cambios en el texto
|
| 40 |
+
def on_text_change():
|
| 41 |
+
st.session_state.semantic_live_state['text_changed'] = True
|
| 42 |
+
|
| 43 |
+
# 3. Función callback para el análisis
|
| 44 |
+
def analyze_text():
|
| 45 |
+
if not st.session_state.semantic_live_text:
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 49 |
+
analysis_result = process_semantic_input(
|
| 50 |
+
st.session_state.semantic_live_text,
|
| 51 |
+
lang_code,
|
| 52 |
+
nlp_models,
|
| 53 |
+
semantic_t
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if analysis_result['success']:
|
| 57 |
+
st.session_state.semantic_live_state['last_result'] = analysis_result
|
| 58 |
+
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 59 |
+
st.session_state.semantic_live_state['text_changed'] = False
|
| 60 |
+
|
| 61 |
+
# Guardar en base de datos
|
| 62 |
+
store_student_semantic_result(
|
| 63 |
+
st.session_state.username,
|
| 64 |
+
st.session_state.semantic_live_text,
|
| 65 |
+
analysis_result['analysis']
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return analysis_result
|
| 69 |
+
|
| 70 |
+
# 4. Crear contenedor principal para mantener el layout estable
|
| 71 |
+
with st.container():
|
| 72 |
+
# Crear dos columnas principales
|
| 73 |
+
input_col, result_col = st.columns(2)
|
| 74 |
+
|
| 75 |
+
# Columna izquierda: Entrada de texto
|
| 76 |
+
with input_col:
|
| 77 |
+
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
| 78 |
+
|
| 79 |
+
# Área de texto para input usando session state
|
| 80 |
+
text_input = st.text_area(
|
| 81 |
+
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 82 |
+
height=400,
|
| 83 |
+
key="semantic_live_text",
|
| 84 |
+
on_change=on_text_change
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Botón de análisis
|
| 88 |
+
if st.button(
|
| 89 |
+
semantic_t.get('analyze_button', 'Analizar'),
|
| 90 |
+
key="semantic_live_analyze",
|
| 91 |
+
type="primary",
|
| 92 |
+
icon="🔍",
|
| 93 |
+
disabled=not text_input,
|
| 94 |
+
use_container_width=True
|
| 95 |
+
):
|
| 96 |
+
analysis_result = analyze_text()
|
| 97 |
+
if analysis_result and not analysis_result.get('success'):
|
| 98 |
+
st.error(analysis_result.get('message', 'Error en el análisis'))
|
| 99 |
+
|
| 100 |
+
# Columna derecha: Visualización de resultados
|
| 101 |
+
with result_col:
|
| 102 |
+
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
| 103 |
+
|
| 104 |
+
# Mostrar resultados si existen
|
| 105 |
if 'last_result' in st.session_state.semantic_live_state and \
|
| 106 |
st.session_state.semantic_live_state['last_result'] is not None:
|
| 107 |
|
| 108 |
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
| 109 |
|
| 110 |
+
# Mostrar advertencia si el texto ha cambiado
|
| 111 |
+
if st.session_state.semantic_live_state.get('text_changed'):
|
| 112 |
+
st.warning(semantic_t.get('text_changed_warning',
|
| 113 |
+
'El texto ha cambiado. Presione Analizar para actualizar los resultados.'))
|
| 114 |
+
|
| 115 |
# Mostrar conceptos clave en formato horizontal
|
| 116 |
if 'key_concepts' in analysis and analysis['key_concepts']:
|
| 117 |
st.markdown("""
|
|
|
|
| 145 |
</style>
|
| 146 |
""", unsafe_allow_html=True)
|
| 147 |
|
|
|
|
| 148 |
concepts_html = ['<div class="concept-table">']
|
| 149 |
concepts_html.extend(
|
| 150 |
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
|
|
|
| 155 |
|
| 156 |
st.markdown(''.join(concepts_html), unsafe_allow_html=True)
|
| 157 |
|
| 158 |
+
# Mostrar gráfico de conceptos en un contenedor fijo
|
| 159 |
if 'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
with st.container():
|
| 161 |
+
st.markdown("""
|
| 162 |
+
<style>
|
| 163 |
+
.graph-container {
|
| 164 |
+
background-color: white;
|
| 165 |
+
border-radius: 10px;
|
| 166 |
+
padding: 20px;
|
| 167 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 168 |
+
margin: 10px 0;
|
| 169 |
+
min-height: 400px;
|
| 170 |
+
}
|
| 171 |
+
</style>
|
| 172 |
+
<div class="graph-container">
|
| 173 |
+
""", unsafe_allow_html=True)
|
| 174 |
|
|
|
|
| 175 |
st.image(
|
| 176 |
analysis['concept_graph'],
|
| 177 |
use_container_width=True,
|
|
|
|
| 183 |
|
| 184 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 185 |
|
| 186 |
+
# Contenedor para botones en la parte inferior del gráfico
|
| 187 |
+
button_col, spacer_col = st.columns([1,4])
|
| 188 |
+
with button_col:
|
| 189 |
+
st.download_button(
|
| 190 |
+
label="📥 " + semantic_t.get('download_graph', "Download"),
|
| 191 |
+
data=analysis['concept_graph'],
|
| 192 |
+
file_name="semantic_live_graph.png",
|
| 193 |
+
mime="image/png",
|
| 194 |
+
use_container_width=True
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# Expandible con la interpretación
|
| 198 |
+
with st.expander("📊 " + semantic_t.get('graph_help', "Graph Interpretation")):
|
| 199 |
+
st.markdown("""
|
| 200 |
+
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
| 201 |
+
- 🎨 Los colores más intensos indican conceptos más centrales en el texto
|
| 202 |
+
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
| 203 |
+
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
| 204 |
+
""")
|
| 205 |
else:
|
| 206 |
st.info(semantic_t.get('no_graph', 'No graph available'))
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
except Exception as e:
|
| 209 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 210 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 211 |
+
|