Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -27,97 +27,119 @@ from ..database.semantic_export import export_user_interactions
|
|
| 27 |
|
| 28 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 29 |
"""
|
| 30 |
-
Interfaz para el análisis semántico
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
-
#
|
| 33 |
st.session_state.page = 'semantic'
|
| 34 |
-
|
| 35 |
-
# Inicializar estados básicos
|
| 36 |
-
if 'semantic_content' not in st.session_state:
|
| 37 |
-
st.session_state.semantic_content = None
|
| 38 |
-
if 'semantic_analyzed' not in st.session_state:
|
| 39 |
-
st.session_state.semantic_analyzed = False
|
| 40 |
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
with st.container():
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
with cols[0]:
|
| 48 |
uploaded_file = st.file_uploader(
|
| 49 |
-
|
| 50 |
-
type=['txt']
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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 |
-
st.session_state.semantic_result,
|
| 116 |
-
lang_code,
|
| 117 |
-
semantic_t
|
| 118 |
-
)
|
| 119 |
-
elif not uploaded_file:
|
| 120 |
-
st.info("Please upload a text file to begin analysis")
|
| 121 |
|
| 122 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 123 |
"""
|
|
|
|
| 27 |
|
| 28 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 29 |
"""
|
| 30 |
+
Interfaz para el análisis semántico
|
| 31 |
+
Args:
|
| 32 |
+
lang_code: Código del idioma actual
|
| 33 |
+
nlp_models: Modelos de spaCy cargados
|
| 34 |
+
semantic_t: Diccionario de traducciones
|
| 35 |
"""
|
| 36 |
+
# Mantener la página actual
|
| 37 |
st.session_state.page = 'semantic'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Estilos para los botones y controles
|
| 40 |
+
st.markdown("""
|
| 41 |
+
<style>
|
| 42 |
+
.stButton button {
|
| 43 |
+
width: 100%;
|
| 44 |
+
padding: 0.5rem;
|
| 45 |
+
}
|
| 46 |
+
.upload-container {
|
| 47 |
+
display: flex;
|
| 48 |
+
align-items: center;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
""", unsafe_allow_html=True)
|
| 52 |
+
|
| 53 |
+
# Contenedor principal para controles
|
| 54 |
with st.container():
|
| 55 |
+
# Área de carga de archivo y botones
|
| 56 |
+
col_upload, col_analyze, col_export, col_new = st.columns([4,2,2,2])
|
| 57 |
|
| 58 |
+
with col_upload:
|
|
|
|
| 59 |
uploaded_file = st.file_uploader(
|
| 60 |
+
semantic_t.get('file_uploader', 'Upload text file'),
|
| 61 |
+
type=['txt']
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with col_analyze:
|
| 65 |
+
analyze_button = st.button(
|
| 66 |
+
semantic_t.get('analyze_button', 'Analyze'),
|
| 67 |
+
disabled=(uploaded_file is None)
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with col_export:
|
| 71 |
+
export_button = st.button(
|
| 72 |
+
semantic_t.get('export_button', 'Export'),
|
| 73 |
+
disabled=not ('semantic_result' in st.session_state)
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with col_new:
|
| 77 |
+
new_button = st.button(
|
| 78 |
+
semantic_t.get('new_analysis', 'New'),
|
| 79 |
+
disabled=not ('semantic_result' in st.session_state)
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# Línea separadora
|
| 83 |
+
st.markdown("---")
|
| 84 |
+
|
| 85 |
+
# Lógica de análisis
|
| 86 |
+
if uploaded_file is not None and analyze_button:
|
| 87 |
+
try:
|
| 88 |
+
# Procesar el archivo
|
| 89 |
+
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 90 |
+
|
| 91 |
+
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 92 |
+
# Realizar análisis
|
| 93 |
+
analysis_result = perform_semantic_analysis(
|
| 94 |
+
text_content,
|
| 95 |
+
nlp_models[lang_code],
|
| 96 |
+
lang_code
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Guardar resultado
|
| 100 |
+
st.session_state.semantic_result = analysis_result
|
| 101 |
+
|
| 102 |
+
# Guardar en base de datos
|
| 103 |
+
if store_student_semantic_result(
|
| 104 |
+
st.session_state.username,
|
| 105 |
+
text_content,
|
| 106 |
+
analysis_result
|
| 107 |
+
):
|
| 108 |
+
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
| 109 |
+
else:
|
| 110 |
+
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 111 |
+
|
| 112 |
+
# Mostrar resultados
|
| 113 |
+
display_semantic_results(analysis_result, lang_code, semantic_t)
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"Error: {str(e)}")
|
| 117 |
+
|
| 118 |
+
# Manejo de exportación
|
| 119 |
+
if export_button and 'semantic_result' in st.session_state:
|
| 120 |
+
try:
|
| 121 |
+
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 122 |
+
st.download_button(
|
| 123 |
+
label=semantic_t.get('download_pdf', 'Download PDF'),
|
| 124 |
+
data=pdf_buffer,
|
| 125 |
+
file_name="semantic_analysis.pdf",
|
| 126 |
+
mime="application/pdf"
|
| 127 |
+
)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
st.error(f"Error exporting: {str(e)}")
|
| 130 |
+
|
| 131 |
+
# Nuevo análisis
|
| 132 |
+
if new_button:
|
| 133 |
+
if 'semantic_result' in st.session_state:
|
| 134 |
+
del st.session_state.semantic_result
|
| 135 |
+
st.rerun()
|
| 136 |
+
|
| 137 |
+
# Mostrar resultados previos o mensaje inicial
|
| 138 |
+
if 'semantic_result' in st.session_state:
|
| 139 |
+
display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t)
|
| 140 |
+
elif uploaded_file is None:
|
| 141 |
+
st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
|
| 142 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 145 |
"""
|