Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -31,7 +31,7 @@ from ..database.semantic_export import export_user_interactions
|
|
| 31 |
|
| 32 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 33 |
"""
|
| 34 |
-
Interfaz para el análisis semántico con controles
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
# Inicializar estados
|
|
@@ -42,26 +42,22 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 42 |
if 'semantic_analysis_done' not in st.session_state:
|
| 43 |
st.session_state.semantic_analysis_done = False
|
| 44 |
|
| 45 |
-
# Contenedor principal
|
| 46 |
with st.container():
|
| 47 |
-
|
| 48 |
-
st.
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
with col_file:
|
| 53 |
uploaded_file = st.file_uploader(
|
| 54 |
-
semantic_t.get('file_uploader', 'Upload
|
| 55 |
type=['txt'],
|
| 56 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}",
|
| 57 |
on_change=lambda: handle_file_upload(uploaded_file)
|
| 58 |
)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
col1, col2, col3, col4 = st.columns([2, 2, 2, 6])
|
| 63 |
-
|
| 64 |
-
with col1:
|
| 65 |
analyze_button = st.button(
|
| 66 |
semantic_t.get('analyze_button', 'Analyze Text'),
|
| 67 |
disabled=not st.session_state.semantic_file_content,
|
|
@@ -69,7 +65,8 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 69 |
key="analyze_semantic"
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
export_button = st.button(
|
| 74 |
semantic_t.get('export_button', 'Export Analysis'),
|
| 75 |
disabled=not st.session_state.semantic_analysis_done,
|
|
@@ -77,55 +74,46 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 77 |
key="export_semantic"
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
disabled=not st.session_state.semantic_analysis_done,
|
| 84 |
use_container_width=True,
|
| 85 |
-
key="
|
| 86 |
)
|
| 87 |
|
| 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 |
-
else:
|
| 120 |
-
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 121 |
-
else:
|
| 122 |
-
st.error(analysis_result['message'])
|
| 123 |
-
except Exception as e:
|
| 124 |
-
logger.error(f"Error en an��lisis semántico: {str(e)}")
|
| 125 |
-
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 126 |
-
|
| 127 |
-
# Manejo de exportación
|
| 128 |
-
if export_button and st.session_state.semantic_analysis_done:
|
| 129 |
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 130 |
st.download_button(
|
| 131 |
label=semantic_t.get('download_pdf', 'Download PDF'),
|
|
@@ -134,41 +122,25 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 134 |
mime="application/pdf",
|
| 135 |
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
| 136 |
)
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
lang_code,
|
| 150 |
-
semantic_t
|
| 151 |
-
)
|
| 152 |
-
elif not st.session_state.semantic_file_content:
|
| 153 |
-
st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 157 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 158 |
|
| 159 |
-
def handle_file_upload(uploaded_file):
|
| 160 |
-
"""Maneja la carga de archivos y mantiene el estado"""
|
| 161 |
-
if uploaded_file is not None:
|
| 162 |
-
try:
|
| 163 |
-
content = uploaded_file.getvalue().decode('utf-8')
|
| 164 |
-
st.session_state.semantic_file_content = content
|
| 165 |
-
st.session_state.page = 'semantic' # Mantener en la página semántica
|
| 166 |
-
except Exception as e:
|
| 167 |
-
logger.error(f"Error al cargar archivo: {str(e)}")
|
| 168 |
-
st.error("Error al cargar el archivo. Asegúrese de que es un archivo de texto válido.")
|
| 169 |
-
st.session_state.semantic_file_content = None
|
| 170 |
-
else:
|
| 171 |
-
st.session_state.semantic_file_content = None
|
| 172 |
|
| 173 |
# [Resto del código igual...] ###############################################################################################################
|
| 174 |
|
|
|
|
| 31 |
|
| 32 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 33 |
"""
|
| 34 |
+
Interfaz para el análisis semántico con controles alineados horizontalmente
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
# Inicializar estados
|
|
|
|
| 42 |
if 'semantic_analysis_done' not in st.session_state:
|
| 43 |
st.session_state.semantic_analysis_done = False
|
| 44 |
|
| 45 |
+
# Contenedor principal para la fila de controles
|
| 46 |
with st.container():
|
| 47 |
+
# Crear una fila con cuatro columnas de igual ancho
|
| 48 |
+
col1, col2, col3, col4 = st.columns([3, 1, 1, 1])
|
| 49 |
|
| 50 |
+
# Columna 1: Carga de archivo
|
| 51 |
+
with col1:
|
|
|
|
| 52 |
uploaded_file = st.file_uploader(
|
| 53 |
+
semantic_t.get('file_uploader', 'Upload TXT file'),
|
| 54 |
type=['txt'],
|
| 55 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}",
|
| 56 |
on_change=lambda: handle_file_upload(uploaded_file)
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Columna 2: Botón de análisis
|
| 60 |
+
with col2:
|
|
|
|
|
|
|
|
|
|
| 61 |
analyze_button = st.button(
|
| 62 |
semantic_t.get('analyze_button', 'Analyze Text'),
|
| 63 |
disabled=not st.session_state.semantic_file_content,
|
|
|
|
| 65 |
key="analyze_semantic"
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# Columna 3: Botón de exportación
|
| 69 |
+
with col3:
|
| 70 |
export_button = st.button(
|
| 71 |
semantic_t.get('export_button', 'Export Analysis'),
|
| 72 |
disabled=not st.session_state.semantic_analysis_done,
|
|
|
|
| 74 |
key="export_semantic"
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Columna 4: Botón de nuevo análisis
|
| 78 |
+
with col4:
|
| 79 |
+
new_analysis_button = st.button(
|
| 80 |
+
semantic_t.get('new_analysis_button', 'New Analysis'),
|
| 81 |
disabled=not st.session_state.semantic_analysis_done,
|
| 82 |
use_container_width=True,
|
| 83 |
+
key="new_semantic"
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Separador sutil
|
| 87 |
+
st.markdown("<hr style='margin: 1em 0; padding: 0; opacity: 0.3'>", unsafe_allow_html=True)
|
| 88 |
+
|
| 89 |
+
# Procesar análisis
|
| 90 |
+
if analyze_button and st.session_state.semantic_file_content:
|
| 91 |
+
try:
|
| 92 |
+
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 93 |
+
doc = nlp_models[lang_code](st.session_state.semantic_file_content)
|
| 94 |
+
|
| 95 |
+
# Usar spacy-streamlit para las visualizaciones
|
| 96 |
+
st.markdown("### Semantic Analysis Results")
|
| 97 |
+
|
| 98 |
+
# Visualizar entidades nombradas
|
| 99 |
+
spacy_streamlit.visualize_ner(
|
| 100 |
+
doc,
|
| 101 |
+
labels=nlp_models[lang_code].get_pipe("ner").labels
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Visualizar dependencias sintácticas
|
| 105 |
+
spacy_streamlit.visualize_parser(doc)
|
| 106 |
+
|
| 107 |
+
st.session_state.semantic_analysis_done = True
|
| 108 |
+
st.session_state.semantic_result = {'doc': doc}
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 112 |
+
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 113 |
+
|
| 114 |
+
# Manejo de exportación
|
| 115 |
+
if export_button and st.session_state.semantic_analysis_done:
|
| 116 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 118 |
st.download_button(
|
| 119 |
label=semantic_t.get('download_pdf', 'Download PDF'),
|
|
|
|
| 122 |
mime="application/pdf",
|
| 123 |
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
| 124 |
)
|
| 125 |
+
except Exception as e:
|
| 126 |
+
st.error(f"Error exporting analysis: {str(e)}")
|
| 127 |
|
| 128 |
+
# Manejo de nuevo análisis
|
| 129 |
+
if new_analysis_button:
|
| 130 |
+
st.session_state.semantic_file_content = None
|
| 131 |
+
st.session_state.semantic_analysis_done = False
|
| 132 |
+
st.session_state.semantic_result = None
|
| 133 |
+
st.session_state.semantic_analysis_counter += 1
|
| 134 |
+
st.rerun()
|
| 135 |
+
|
| 136 |
+
# Mostrar mensaje inicial si no hay archivo
|
| 137 |
+
if not st.session_state.semantic_file_content and not st.session_state.semantic_analysis_done:
|
| 138 |
+
st.info(semantic_t.get('initial_message', 'Upload a TXT file to begin analysis'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 142 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# [Resto del código igual...] ###############################################################################################################
|
| 146 |
|