Spaces:
Running
Running
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -29,21 +29,17 @@ from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
|
| 29 |
|
| 30 |
|
| 31 |
###############################
|
|
|
|
|
|
|
| 32 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 33 |
-
"""
|
| 34 |
-
Interfaz para el análisis semántico
|
| 35 |
-
Args:
|
| 36 |
-
lang_code: Código del idioma actual
|
| 37 |
-
nlp_models: Modelos de spaCy cargados
|
| 38 |
-
semantic_t: Diccionario de traducciones semánticas
|
| 39 |
-
"""
|
| 40 |
try:
|
| 41 |
# 1. Inicializar el estado de la sesión
|
| 42 |
if 'semantic_state' not in st.session_state:
|
| 43 |
st.session_state.semantic_state = {
|
| 44 |
'analysis_count': 0,
|
| 45 |
'last_analysis': None,
|
| 46 |
-
'current_file': None
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
# 2. Área de carga de archivo con mensaje informativo
|
|
@@ -56,28 +52,13 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 56 |
key=f"semantic_file_uploader_{st.session_state.semantic_state['analysis_count']}"
|
| 57 |
)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# 4. Botón de análisis
|
| 63 |
-
with col1:
|
| 64 |
-
analyze_button = st.button(
|
| 65 |
-
semantic_t.get('semantic_analyze_button', 'Analyze'),
|
| 66 |
-
key=f"semantic_analyze_button_{st.session_state.semantic_state['analysis_count']}",
|
| 67 |
-
type="primary", # Nuevo en Streamlit 1.39.0
|
| 68 |
-
icon="🔍", # Nuevo en Streamlit 1.39.0
|
| 69 |
-
disabled=uploaded_file is None,
|
| 70 |
-
use_container_width=True
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
# 5. Procesar análisis
|
| 74 |
-
if analyze_button and uploaded_file is not None:
|
| 75 |
try:
|
| 76 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 77 |
-
#
|
| 78 |
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 79 |
|
| 80 |
-
# Realizar análisis
|
| 81 |
analysis_result = process_semantic_input(
|
| 82 |
text_content,
|
| 83 |
lang_code,
|
|
@@ -92,30 +73,51 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 92 |
st.session_state.semantic_state['current_file'] = uploaded_file.name
|
| 93 |
|
| 94 |
# Guardar en base de datos
|
| 95 |
-
|
| 96 |
st.session_state.username,
|
| 97 |
text_content,
|
| 98 |
analysis_result['analysis']
|
| 99 |
-
)
|
|
|
|
|
|
|
| 100 |
st.success(
|
| 101 |
semantic_t.get('analysis_complete',
|
| 102 |
'Análisis completado y guardado. Para realizar un nuevo análisis, cargue otro archivo.')
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
# Mostrar resultados
|
| 106 |
-
display_semantic_results(
|
| 107 |
-
st.session_state.semantic_result,
|
| 108 |
-
lang_code,
|
| 109 |
-
semantic_t
|
| 110 |
-
)
|
| 111 |
else:
|
| 112 |
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 113 |
else:
|
| 114 |
st.error(analysis_result['message'])
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 118 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# 6. Mostrar resultados previos o mensaje inicial
|
| 121 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
|
@@ -138,6 +140,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 138 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 139 |
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 140 |
|
|
|
|
| 141 |
#######################################
|
| 142 |
def display_semantic_results(semantic_result, lang_code, semantic_t):
|
| 143 |
"""
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
###############################
|
| 32 |
+
|
| 33 |
+
# En semantic_interface.py
|
| 34 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
# 1. Inicializar el estado de la sesión
|
| 37 |
if 'semantic_state' not in st.session_state:
|
| 38 |
st.session_state.semantic_state = {
|
| 39 |
'analysis_count': 0,
|
| 40 |
'last_analysis': None,
|
| 41 |
+
'current_file': None,
|
| 42 |
+
'pending_analysis': False # Nuevo flag para controlar el análisis pendiente
|
| 43 |
}
|
| 44 |
|
| 45 |
# 2. Área de carga de archivo con mensaje informativo
|
|
|
|
| 52 |
key=f"semantic_file_uploader_{st.session_state.semantic_state['analysis_count']}"
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Verificar si hay un archivo cargado y un análisis pendiente
|
| 56 |
+
if uploaded_file is not None and st.session_state.semantic_state.get('pending_analysis', False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 59 |
+
# Realizar análisis
|
| 60 |
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 61 |
|
|
|
|
| 62 |
analysis_result = process_semantic_input(
|
| 63 |
text_content,
|
| 64 |
lang_code,
|
|
|
|
| 73 |
st.session_state.semantic_state['current_file'] = uploaded_file.name
|
| 74 |
|
| 75 |
# Guardar en base de datos
|
| 76 |
+
storage_success = store_student_semantic_result(
|
| 77 |
st.session_state.username,
|
| 78 |
text_content,
|
| 79 |
analysis_result['analysis']
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if storage_success:
|
| 83 |
st.success(
|
| 84 |
semantic_t.get('analysis_complete',
|
| 85 |
'Análisis completado y guardado. Para realizar un nuevo análisis, cargue otro archivo.')
|
| 86 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 89 |
else:
|
| 90 |
st.error(analysis_result['message'])
|
| 91 |
+
|
| 92 |
+
# Restablecer el flag de análisis pendiente
|
| 93 |
+
st.session_state.semantic_state['pending_analysis'] = False
|
| 94 |
+
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 97 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 98 |
+
# Restablecer el flag de análisis pendiente en caso de error
|
| 99 |
+
st.session_state.semantic_state['pending_analysis'] = False
|
| 100 |
+
|
| 101 |
+
# 3. Columnas para los botones y mensajes
|
| 102 |
+
col1, col2 = st.columns([1,4])
|
| 103 |
+
|
| 104 |
+
# 4. Botón de análisis
|
| 105 |
+
with col1:
|
| 106 |
+
analyze_button = st.button(
|
| 107 |
+
semantic_t.get('semantic_analyze_button', 'Analyze'),
|
| 108 |
+
key=f"semantic_analyze_button_{st.session_state.semantic_state['analysis_count']}",
|
| 109 |
+
type="primary",
|
| 110 |
+
icon="🔍",
|
| 111 |
+
disabled=uploaded_file is None,
|
| 112 |
+
use_container_width=True
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# 5. Procesar análisis
|
| 116 |
+
if analyze_button and uploaded_file is not None:
|
| 117 |
+
# En lugar de realizar el análisis inmediatamente, establecer el flag
|
| 118 |
+
st.session_state.semantic_state['pending_analysis'] = True
|
| 119 |
+
# Forzar la recarga de la aplicación
|
| 120 |
+
st.rerun()
|
| 121 |
|
| 122 |
# 6. Mostrar resultados previos o mensaje inicial
|
| 123 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
|
|
|
| 140 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 141 |
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 142 |
|
| 143 |
+
|
| 144 |
#######################################
|
| 145 |
def display_semantic_results(semantic_result, lang_code, semantic_t):
|
| 146 |
"""
|