Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -32,7 +32,8 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 32 |
st.session_state.semantic_live_state = {
|
| 33 |
'analysis_count': 0,
|
| 34 |
'last_analysis': None,
|
| 35 |
-
'current_text': ''
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
# 2. Crear dos columnas
|
|
@@ -46,13 +47,17 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 46 |
text_input = st.text_area(
|
| 47 |
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 48 |
height=400,
|
| 49 |
-
key=
|
|
|
|
| 50 |
)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
# Botón de análisis
|
| 53 |
analyze_button = st.button(
|
| 54 |
semantic_t.get('analyze_button', 'Analizar'),
|
| 55 |
-
key=
|
| 56 |
type="primary",
|
| 57 |
icon="🔍",
|
| 58 |
disabled=not text_input,
|
|
@@ -76,8 +81,8 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 76 |
)
|
| 77 |
|
| 78 |
if analysis_result['success']:
|
| 79 |
-
# Guardar resultado
|
| 80 |
-
st.session_state.
|
| 81 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 82 |
|
| 83 |
# Guardar en base de datos
|
|
@@ -87,37 +92,116 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 87 |
analysis_result['analysis']
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
|
|
|
| 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. Crear dos columnas
|
|
|
|
| 47 |
text_input = st.text_area(
|
| 48 |
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 49 |
height=400,
|
| 50 |
+
key="semantic_live_text",
|
| 51 |
+
value=st.session_state.semantic_live_state.get('current_text', '')
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Actualizar el texto actual en el estado
|
| 55 |
+
st.session_state.semantic_live_state['current_text'] = text_input
|
| 56 |
+
|
| 57 |
# Botón de análisis
|
| 58 |
analyze_button = st.button(
|
| 59 |
semantic_t.get('analyze_button', 'Analizar'),
|
| 60 |
+
key="semantic_live_analyze",
|
| 61 |
type="primary",
|
| 62 |
icon="🔍",
|
| 63 |
disabled=not text_input,
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
if analysis_result['success']:
|
| 84 |
+
# Guardar resultado en el estado
|
| 85 |
+
st.session_state.semantic_live_state['last_result'] = analysis_result
|
| 86 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 87 |
|
| 88 |
# Guardar en base de datos
|
|
|
|
| 92 |
analysis_result['analysis']
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Mostrar resultados (ya sea nuevos o previos)
|
| 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(
|
| 104 |
+
"""
|
| 105 |
+
<style>
|
| 106 |
+
.concept-table {
|
| 107 |
+
display: flex;
|
| 108 |
+
flex-wrap: wrap;
|
| 109 |
+
gap: 10px;
|
| 110 |
+
margin-bottom: 20px;
|
| 111 |
+
}
|
| 112 |
+
.concept-item {
|
| 113 |
+
background-color: #f0f2f6;
|
| 114 |
+
border-radius: 5px;
|
| 115 |
+
padding: 8px 12px;
|
| 116 |
+
display: flex;
|
| 117 |
+
align-items: center;
|
| 118 |
+
gap: 8px;
|
| 119 |
+
}
|
| 120 |
+
.concept-name {
|
| 121 |
+
font-weight: bold;
|
| 122 |
+
}
|
| 123 |
+
.concept-freq {
|
| 124 |
+
color: #666;
|
| 125 |
+
font-size: 0.9em;
|
| 126 |
+
}
|
| 127 |
+
</style>
|
| 128 |
+
""",
|
| 129 |
+
unsafe_allow_html=True
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Crear la visualización horizontal de conceptos
|
| 133 |
+
concepts_html = '<div class="concept-table">'
|
| 134 |
+
for concept, freq in analysis['key_concepts']:
|
| 135 |
+
concepts_html += f'''
|
| 136 |
+
<div class="concept-item">
|
| 137 |
+
<span class="concept-name">{concept}</span>
|
| 138 |
+
<span class="concept-freq">({freq:.2f})</span>
|
| 139 |
+
</div>
|
| 140 |
+
'''
|
| 141 |
+
concepts_html += '</div>'
|
| 142 |
+
st.markdown(concepts_html, unsafe_allow_html=True)
|
| 143 |
+
|
| 144 |
+
# Mostrar gráfico de conceptos
|
| 145 |
+
if 'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
| 146 |
+
try:
|
| 147 |
+
# Container para el grafo con estilos mejorados
|
| 148 |
+
st.markdown(
|
| 149 |
+
"""
|
| 150 |
+
<style>
|
| 151 |
+
.graph-container {
|
| 152 |
+
background-color: white;
|
| 153 |
+
border-radius: 10px;
|
| 154 |
+
padding: 20px;
|
| 155 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 156 |
+
margin: 10px 0;
|
| 157 |
+
}
|
| 158 |
+
</style>
|
| 159 |
+
""",
|
| 160 |
+
unsafe_allow_html=True
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
with st.container():
|
| 164 |
+
st.markdown('<div class="graph-container">', unsafe_allow_html=True)
|
| 165 |
+
|
| 166 |
+
# Mostrar grafo
|
| 167 |
+
st.image(analysis['concept_graph'], use_column_width=True)
|
| 168 |
+
|
| 169 |
+
# Leyenda del grafo
|
| 170 |
+
st.caption(semantic_t.get(
|
| 171 |
+
'graph_description',
|
| 172 |
+
'Visualización de relaciones entre conceptos clave identificados en el texto.'
|
| 173 |
+
))
|
| 174 |
+
|
| 175 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 176 |
+
|
| 177 |
+
# Contenedor para botones
|
| 178 |
+
col1, col2 = st.columns([1,4])
|
| 179 |
+
with col1:
|
| 180 |
+
st.download_button(
|
| 181 |
+
label="📥 " + semantic_t.get('download_graph', "Download"),
|
| 182 |
+
data=analysis['concept_graph'],
|
| 183 |
+
file_name="semantic_live_graph.png",
|
| 184 |
+
mime="image/png",
|
| 185 |
+
use_container_width=True
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Expandible con la interpretación
|
| 189 |
+
with st.expander("📊 " + semantic_t.get('graph_help', "Graph Interpretation")):
|
| 190 |
+
st.markdown("""
|
| 191 |
+
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
| 192 |
+
- 🎨 Los colores más intensos indican conceptos más centrales en el texto
|
| 193 |
+
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
| 194 |
+
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
| 195 |
+
""")
|
| 196 |
+
except Exception as e:
|
| 197 |
+
logger.error(f"Error displaying graph: {str(e)}")
|
| 198 |
+
st.error(semantic_t.get('graph_error', 'Error displaying the graph'))
|
| 199 |
+
else:
|
| 200 |
+
st.info(semantic_t.get('no_graph', 'No graph available'))
|
| 201 |
+
|
| 202 |
+
except Exception as e:
|
| 203 |
+
logger.error(f"Error en análisis semántico en vivo: {str(e)}")
|
| 204 |
+
st.error(semantic_t.get('error_processing', f'Error al procesar el texto: {str(e)}'))
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|