Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -37,42 +37,46 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 37 |
semantic_t: Diccionario de traducciones semánticas
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
-
# 1. Inicializar
|
| 41 |
-
if '
|
| 42 |
-
st.session_state.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
| 49 |
uploaded_file = st.file_uploader(
|
| 50 |
semantic_t.get('semantic_file_uploader', 'Upload a text file for semantic analysis'),
|
| 51 |
type=['txt'],
|
| 52 |
-
key=f"semantic_file_uploader_{st.session_state.
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# 3.
|
| 56 |
-
col1, col2
|
|
|
|
|
|
|
| 57 |
with col1:
|
| 58 |
analyze_button = st.button(
|
| 59 |
-
semantic_t.get('semantic_analyze_button', 'Analyze
|
| 60 |
-
key=f"semantic_analyze_button_{st.session_state.
|
|
|
|
|
|
|
|
|
|
| 61 |
use_container_width=True
|
| 62 |
)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
if analyze_button:
|
| 66 |
-
if uploaded_file is None:
|
| 67 |
-
st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
try:
|
| 71 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 72 |
-
#
|
| 73 |
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 74 |
|
| 75 |
-
#
|
| 76 |
analysis_result = process_semantic_input(
|
| 77 |
text_content,
|
| 78 |
lang_code,
|
|
@@ -80,51 +84,58 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 80 |
semantic_t
|
| 81 |
)
|
| 82 |
|
| 83 |
-
if
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.session_state.semantic_result = analysis_result
|
| 89 |
-
|
| 90 |
-
# 4.4 Incrementar el contador de análisis
|
| 91 |
-
st.session_state.semantic_analysis_counter += 1
|
| 92 |
-
|
| 93 |
-
# 4.5 Guardar en la base de datos
|
| 94 |
-
if store_student_semantic_result(
|
| 95 |
-
st.session_state.username,
|
| 96 |
-
text_content,
|
| 97 |
-
analysis_result['analysis']
|
| 98 |
-
):
|
| 99 |
-
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
st.session_state.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
-
st.error(
|
| 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 |
-
#
|
| 115 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
display_semantic_results(
|
| 117 |
st.session_state.semantic_result,
|
| 118 |
lang_code,
|
| 119 |
semantic_t
|
| 120 |
)
|
| 121 |
-
# 6. Mostrar mensaje inicial
|
| 122 |
else:
|
| 123 |
-
st.info(semantic_t.get('
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 127 |
-
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 128 |
|
| 129 |
#######################################
|
| 130 |
def display_semantic_results(semantic_result, lang_code, semantic_t):
|
|
|
|
| 37 |
semantic_t: Diccionario de traducciones semánticas
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
# 1. Inicializar el estado de la sesión
|
| 41 |
+
if 'semantic_state' not in st.session_state:
|
| 42 |
+
st.session_state.semantic_state = {
|
| 43 |
+
'analysis_count': 0,
|
| 44 |
+
'last_analysis': None,
|
| 45 |
+
'current_file': None
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
# 2. Área de carga de archivo con mensaje informativo
|
| 49 |
+
st.info(semantic_t.get('initial_instruction',
|
| 50 |
+
'Para comenzar un nuevo análisis semántico, cargue un archivo de texto (.txt)'))
|
| 51 |
+
|
| 52 |
uploaded_file = st.file_uploader(
|
| 53 |
semantic_t.get('semantic_file_uploader', 'Upload a text file for semantic analysis'),
|
| 54 |
type=['txt'],
|
| 55 |
+
key=f"semantic_file_uploader_{st.session_state.semantic_state['analysis_count']}"
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# 3. Columnas para los botones y mensajes
|
| 59 |
+
col1, col2 = st.columns([1,4])
|
| 60 |
+
|
| 61 |
+
# 4. Botón de análisis
|
| 62 |
with col1:
|
| 63 |
analyze_button = st.button(
|
| 64 |
+
semantic_t.get('semantic_analyze_button', 'Analyze'),
|
| 65 |
+
key=f"semantic_analyze_button_{st.session_state.semantic_state['analysis_count']}",
|
| 66 |
+
type="primary", # Nuevo en Streamlit 1.39.0
|
| 67 |
+
icon="🔍", # Nuevo en Streamlit 1.39.0
|
| 68 |
+
disabled=uploaded_file is None,
|
| 69 |
use_container_width=True
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# 5. Procesar análisis
|
| 73 |
+
if analyze_button and uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 76 |
+
# Leer contenido del archivo
|
| 77 |
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 78 |
|
| 79 |
+
# Realizar análisis
|
| 80 |
analysis_result = process_semantic_input(
|
| 81 |
text_content,
|
| 82 |
lang_code,
|
|
|
|
| 84 |
semantic_t
|
| 85 |
)
|
| 86 |
|
| 87 |
+
if analysis_result['success']:
|
| 88 |
+
# Guardar resultado
|
| 89 |
+
st.session_state.semantic_result = analysis_result
|
| 90 |
+
st.session_state.semantic_state['analysis_count'] += 1
|
| 91 |
+
st.session_state.semantic_state['current_file'] = uploaded_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Guardar en base de datos
|
| 94 |
+
if store_student_semantic_result(
|
| 95 |
+
st.session_state.username,
|
| 96 |
+
text_content,
|
| 97 |
+
analysis_result['analysis']
|
| 98 |
+
):
|
| 99 |
+
st.success(
|
| 100 |
+
semantic_t.get('analysis_complete',
|
| 101 |
+
'Análisis completado y guardado. Para realizar un nuevo análisis, cargue otro archivo.')
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Mostrar resultados
|
| 105 |
+
display_semantic_results(
|
| 106 |
+
st.session_state.semantic_result,
|
| 107 |
+
lang_code,
|
| 108 |
+
semantic_t
|
| 109 |
+
)
|
| 110 |
+
else:
|
| 111 |
+
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 112 |
else:
|
| 113 |
+
st.error(analysis_result['message'])
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 117 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 118 |
+
|
| 119 |
+
# 6. Mostrar resultados previos o mensaje inicial
|
| 120 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
| 121 |
+
# Mostrar mensaje sobre el análisis actual
|
| 122 |
+
st.info(
|
| 123 |
+
semantic_t.get('current_analysis_message',
|
| 124 |
+
f'Mostrando análisis del archivo: {st.session_state.semantic_state["current_file"]}. '
|
| 125 |
+
'Para realizar un nuevo análisis, cargue otro archivo.')
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
display_semantic_results(
|
| 129 |
st.session_state.semantic_result,
|
| 130 |
lang_code,
|
| 131 |
semantic_t
|
| 132 |
)
|
|
|
|
| 133 |
else:
|
| 134 |
+
st.info(semantic_t.get('upload_prompt', 'Cargue un archivo para comenzar el análisis'))
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
| 138 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 139 |
|
| 140 |
#######################################
|
| 141 |
def display_semantic_results(semantic_result, lang_code, semantic_t):
|