Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -143,117 +143,133 @@ def display_semantic_results(semantic_result, lang_code, semantic_t):
|
|
| 143 |
"""
|
| 144 |
Muestra los resultados del análisis semántico de conceptos clave.
|
| 145 |
"""
|
| 146 |
-
# Verificar resultado
|
| 147 |
if semantic_result is None or not semantic_result['success']:
|
| 148 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
| 149 |
return
|
| 150 |
|
| 151 |
analysis = semantic_result['analysis']
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
}
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
)
|
| 178 |
-
else:
|
| 179 |
-
st.info(semantic_t.get('no_concepts', 'No key concepts found'))
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
st.markdown(
|
| 188 |
-
"""
|
| 189 |
-
<style>
|
| 190 |
-
.semantic-graph-container {
|
| 191 |
-
background-color: white;
|
| 192 |
-
border-radius: 10px;
|
| 193 |
-
padding: 20px;
|
| 194 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 195 |
-
margin: 10px 0;
|
| 196 |
-
}
|
| 197 |
-
.stImage {
|
| 198 |
-
display: flex;
|
| 199 |
-
justify-content: center;
|
| 200 |
-
align-items: center;
|
| 201 |
-
}
|
| 202 |
-
.stImage > img {
|
| 203 |
-
max-width: 100%;
|
| 204 |
-
height: auto;
|
| 205 |
-
object-fit: contain;
|
| 206 |
-
}
|
| 207 |
-
</style>
|
| 208 |
-
""",
|
| 209 |
unsafe_allow_html=True
|
| 210 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
graph_bytes
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
f'<img src="data:image/png;base64,{graph_base64}" alt="Concept Graph" style="width:100%;"/>',
|
| 223 |
-
unsafe_allow_html=True
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
# Agregar leyenda o descripción si es necesario
|
| 227 |
-
st.caption(semantic_t.get(
|
| 228 |
-
'graph_description',
|
| 229 |
-
'El grosor de las líneas indica la fuerza de la relación. '
|
| 230 |
-
'Los colores indican la centralidad del concepto.'
|
| 231 |
-
))
|
| 232 |
-
|
| 233 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 234 |
-
|
| 235 |
-
# Agregar botón de descarga
|
| 236 |
-
st.download_button(
|
| 237 |
-
label=semantic_t.get('download_graph', "Download Graph"),
|
| 238 |
-
data=graph_bytes,
|
| 239 |
-
file_name="semantic_graph.png",
|
| 240 |
-
mime="image/png",
|
| 241 |
-
)
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
st.
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
# Añadir información adicional sobre la interpretación del grafo
|
| 250 |
-
with st.expander(semantic_t.get('graph_help', "How to interpret this graph")):
|
| 251 |
-
st.markdown("""
|
| 252 |
-
- Las flechas indican la dirección de la relación entre conceptos
|
| 253 |
-
- Los colores más intensos indican conceptos más centrales en el texto
|
| 254 |
-
- El tamaño de los nodos representa la frecuencia del concepto
|
| 255 |
-
- El grosor de las líneas indica la fuerza de la conexión
|
| 256 |
-
""")
|
| 257 |
|
| 258 |
########################################################################################
|
| 259 |
'''
|
|
|
|
| 143 |
"""
|
| 144 |
Muestra los resultados del análisis semántico de conceptos clave.
|
| 145 |
"""
|
|
|
|
| 146 |
if semantic_result is None or not semantic_result['success']:
|
| 147 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
| 148 |
return
|
| 149 |
|
| 150 |
analysis = semantic_result['analysis']
|
| 151 |
|
| 152 |
+
# Mostrar conceptos clave en formato horizontal
|
| 153 |
+
st.subheader(semantic_t.get('key_concepts', 'Key Concepts'))
|
| 154 |
+
if 'key_concepts' in analysis and analysis['key_concepts']:
|
| 155 |
+
# Crear tabla de conceptos
|
| 156 |
+
df = pd.DataFrame(
|
| 157 |
+
analysis['key_concepts'],
|
| 158 |
+
columns=[
|
| 159 |
+
semantic_t.get('concept', 'Concept'),
|
| 160 |
+
semantic_t.get('frequency', 'Frequency')
|
| 161 |
+
]
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Convertir DataFrame a formato horizontal
|
| 165 |
+
st.write(
|
| 166 |
+
"""
|
| 167 |
+
<style>
|
| 168 |
+
.concept-table {
|
| 169 |
+
display: flex;
|
| 170 |
+
flex-wrap: wrap;
|
| 171 |
+
gap: 10px;
|
| 172 |
+
margin-bottom: 20px;
|
| 173 |
+
}
|
| 174 |
+
.concept-item {
|
| 175 |
+
background-color: #f0f2f6;
|
| 176 |
+
border-radius: 5px;
|
| 177 |
+
padding: 8px 12px;
|
| 178 |
+
display: flex;
|
| 179 |
+
align-items: center;
|
| 180 |
+
gap: 8px;
|
| 181 |
+
}
|
| 182 |
+
.concept-name {
|
| 183 |
+
font-weight: bold;
|
| 184 |
+
}
|
| 185 |
+
.concept-freq {
|
| 186 |
+
color: #666;
|
| 187 |
+
font-size: 0.9em;
|
| 188 |
+
}
|
| 189 |
+
</style>
|
| 190 |
+
<div class="concept-table">
|
| 191 |
+
""" +
|
| 192 |
+
''.join([
|
| 193 |
+
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
| 194 |
+
f'<span class="concept-freq">({freq:.2f})</span></div>'
|
| 195 |
+
for concept, freq in df.values
|
| 196 |
+
]) +
|
| 197 |
+
"</div>",
|
| 198 |
+
unsafe_allow_html=True
|
| 199 |
+
)
|
| 200 |
+
else:
|
| 201 |
+
st.info(semantic_t.get('no_concepts', 'No key concepts found'))
|
| 202 |
|
| 203 |
+
# Gráfico de conceptos
|
| 204 |
+
st.subheader(semantic_t.get('concept_graph', 'Concepts Graph'))
|
| 205 |
+
if 'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
| 206 |
+
try:
|
| 207 |
+
# Container para el grafo con estilos mejorados
|
| 208 |
+
st.markdown(
|
| 209 |
+
"""
|
| 210 |
+
<style>
|
| 211 |
+
.graph-container {
|
| 212 |
+
background-color: white;
|
| 213 |
+
border-radius: 10px;
|
| 214 |
+
padding: 20px;
|
| 215 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 216 |
+
margin: 10px 0;
|
| 217 |
+
}
|
| 218 |
+
.button-container {
|
| 219 |
+
display: flex;
|
| 220 |
+
gap: 10px;
|
| 221 |
+
margin: 10px 0;
|
| 222 |
+
}
|
| 223 |
+
</style>
|
| 224 |
+
""",
|
| 225 |
+
unsafe_allow_html=True
|
| 226 |
)
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
with st.container():
|
| 229 |
+
st.markdown('<div class="graph-container">', unsafe_allow_html=True)
|
| 230 |
+
|
| 231 |
+
# Mostrar grafo
|
| 232 |
+
graph_bytes = analysis['concept_graph']
|
| 233 |
+
graph_base64 = base64.b64encode(graph_bytes).decode()
|
| 234 |
st.markdown(
|
| 235 |
+
f'<img src="data:image/png;base64,{graph_base64}" alt="Concept Graph" style="width:100%;"/>',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
unsafe_allow_html=True
|
| 237 |
)
|
| 238 |
+
|
| 239 |
+
# Leyenda del grafo
|
| 240 |
+
st.caption(semantic_t.get(
|
| 241 |
+
'graph_description',
|
| 242 |
+
'Visualización de relaciones entre conceptos clave identificados en el texto.'
|
| 243 |
+
))
|
| 244 |
+
|
| 245 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 246 |
|
| 247 |
+
# Contenedor para botones
|
| 248 |
+
col1, col2 = st.columns([1,4])
|
| 249 |
+
with col1:
|
| 250 |
+
st.download_button(
|
| 251 |
+
label="📥 " + semantic_t.get('download_graph', "Download"),
|
| 252 |
+
data=graph_bytes,
|
| 253 |
+
file_name="semantic_graph.png",
|
| 254 |
+
mime="image/png",
|
| 255 |
+
use_container_width=True
|
| 256 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
# Expandible con la interpretación
|
| 259 |
+
with st.expander("📊 " + semantic_t.get('graph_help', "Graph Interpretation")):
|
| 260 |
+
st.markdown("""
|
| 261 |
+
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
| 262 |
+
- 🎨 Los colores más intensos indican conceptos más centrales en el texto
|
| 263 |
+
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
| 264 |
+
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
| 265 |
+
""")
|
| 266 |
+
|
| 267 |
+
except Exception as e:
|
| 268 |
+
logger.error(f"Error displaying graph: {str(e)}")
|
| 269 |
+
st.error(semantic_t.get('graph_error', 'Error displaying the graph'))
|
| 270 |
+
else:
|
| 271 |
+
st.info(semantic_t.get('no_graph', 'No concept graph available'))
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
########################################################################################
|
| 275 |
'''
|