Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
#modules/semantic/semantic_interface.py
|
| 2 |
-
# Importaciones necesarias
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit_float import *
|
| 5 |
from streamlit_antd_components import *
|
| 6 |
from streamlit.components.v1 import html
|
|
|
|
| 7 |
import io
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
|
@@ -25,9 +25,25 @@ from ..utils.widget_utils import generate_unique_key
|
|
| 25 |
from ..database.semantic_mongo_db import store_student_semantic_result
|
| 26 |
from ..database.semantic_export import export_user_interactions
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 33 |
"""
|
|
@@ -90,23 +106,36 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 90 |
if analyze_button and st.session_state.semantic_file_content:
|
| 91 |
try:
|
| 92 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 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 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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)}'))
|
|
@@ -133,20 +162,23 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 133 |
st.session_state.semantic_analysis_counter += 1
|
| 134 |
st.rerun()
|
| 135 |
|
| 136 |
-
# Mostrar
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 147 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 148 |
"""
|
| 149 |
-
Muestra los resultados del análisis semántico
|
| 150 |
"""
|
| 151 |
if result is None or not result['success']:
|
| 152 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
|
@@ -193,21 +225,4 @@ def display_semantic_results(result, lang_code, semantic_t):
|
|
| 193 |
# Columna 2: Gráfico de entidades
|
| 194 |
with col2:
|
| 195 |
st.subheader(semantic_t.get('entity_graph', 'Entities Graph'))
|
| 196 |
-
st.image(analysis['entity_graph'])
|
| 197 |
-
|
| 198 |
-
# Botón de exportación al final
|
| 199 |
-
col1, col2, col3 = st.columns([2,1,2])
|
| 200 |
-
with col2:
|
| 201 |
-
if st.button(
|
| 202 |
-
semantic_t.get('export_button', 'Export Analysis'),
|
| 203 |
-
key=f"semantic_export_{st.session_state.semantic_analysis_counter}",
|
| 204 |
-
use_container_width=True
|
| 205 |
-
):
|
| 206 |
-
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 207 |
-
st.download_button(
|
| 208 |
-
label=semantic_t.get('download_pdf', 'Download PDF'),
|
| 209 |
-
data=pdf_buffer,
|
| 210 |
-
file_name="semantic_analysis.pdf",
|
| 211 |
-
mime="application/pdf",
|
| 212 |
-
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
| 213 |
-
)
|
|
|
|
| 1 |
#modules/semantic/semantic_interface.py
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from streamlit_float import *
|
| 4 |
from streamlit_antd_components import *
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
+
import spacy_streamlit
|
| 7 |
import io
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
|
|
|
| 25 |
from ..database.semantic_mongo_db import store_student_semantic_result
|
| 26 |
from ..database.semantic_export import export_user_interactions
|
| 27 |
|
| 28 |
+
def handle_file_upload(uploaded_file):
|
| 29 |
+
"""
|
| 30 |
+
Maneja la carga de archivos y mantiene el estado
|
| 31 |
+
Args:
|
| 32 |
+
uploaded_file: Archivo subido a través del file_uploader
|
| 33 |
+
"""
|
| 34 |
+
try:
|
| 35 |
+
if uploaded_file is not None:
|
| 36 |
+
content = uploaded_file.getvalue().decode('utf-8')
|
| 37 |
+
st.session_state.semantic_file_content = content
|
| 38 |
+
st.session_state.page = 'semantic' # Mantener en la página semántica
|
| 39 |
+
logger.info(f"Archivo cargado exitosamente: {uploaded_file.name}")
|
| 40 |
+
else:
|
| 41 |
+
st.session_state.semantic_file_content = None
|
| 42 |
+
logger.info("No se ha cargado ningún archivo")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.error(f"Error al cargar archivo: {str(e)}")
|
| 45 |
+
st.error("Error al cargar el archivo. Asegúrese de que es un archivo de texto válido.")
|
| 46 |
+
st.session_state.semantic_file_content = None
|
| 47 |
|
| 48 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 49 |
"""
|
|
|
|
| 106 |
if analyze_button and st.session_state.semantic_file_content:
|
| 107 |
try:
|
| 108 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 109 |
+
analysis_result = process_semantic_input(
|
| 110 |
+
st.session_state.semantic_file_content,
|
| 111 |
+
lang_code,
|
| 112 |
+
nlp_models,
|
| 113 |
+
semantic_t
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
+
if analysis_result['success']:
|
| 117 |
+
st.session_state.semantic_result = analysis_result
|
| 118 |
+
st.session_state.semantic_analysis_done = True
|
| 119 |
+
st.session_state.semantic_analysis_counter += 1
|
| 120 |
+
|
| 121 |
+
# Guardar en la base de datos
|
| 122 |
+
if store_student_semantic_result(
|
| 123 |
+
st.session_state.username,
|
| 124 |
+
st.session_state.semantic_file_content,
|
| 125 |
+
analysis_result['analysis']
|
| 126 |
+
):
|
| 127 |
+
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
| 128 |
+
# Mostrar resultados
|
| 129 |
+
display_semantic_results(
|
| 130 |
+
analysis_result,
|
| 131 |
+
lang_code,
|
| 132 |
+
semantic_t
|
| 133 |
+
)
|
| 134 |
+
else:
|
| 135 |
+
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 136 |
+
else:
|
| 137 |
+
st.error(analysis_result['message'])
|
| 138 |
+
|
| 139 |
except Exception as e:
|
| 140 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 141 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
|
|
|
| 162 |
st.session_state.semantic_analysis_counter += 1
|
| 163 |
st.rerun()
|
| 164 |
|
| 165 |
+
# Mostrar resultados previos o mensaje inicial
|
| 166 |
+
elif st.session_state.semantic_analysis_done and 'semantic_result' in st.session_state:
|
| 167 |
+
display_semantic_results(
|
| 168 |
+
st.session_state.semantic_result,
|
| 169 |
+
lang_code,
|
| 170 |
+
semantic_t
|
| 171 |
+
)
|
| 172 |
+
elif not st.session_state.semantic_file_content:
|
| 173 |
st.info(semantic_t.get('initial_message', 'Upload a TXT file to begin analysis'))
|
| 174 |
|
| 175 |
except Exception as e:
|
| 176 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 177 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 180 |
"""
|
| 181 |
+
Muestra los resultados del análisis semántico
|
| 182 |
"""
|
| 183 |
if result is None or not result['success']:
|
| 184 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
|
|
|
| 225 |
# Columna 2: Gráfico de entidades
|
| 226 |
with col2:
|
| 227 |
st.subheader(semantic_t.get('entity_graph', 'Entities Graph'))
|
| 228 |
+
st.image(analysis['entity_graph'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|