Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -34,75 +34,64 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 34 |
semantic_t: Diccionario de traducciones semánticas
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
-
# Inicializar el estado
|
| 38 |
-
input_key = f"semantic_input_{lang_code}"
|
| 39 |
-
if input_key not in st.session_state:
|
| 40 |
-
st.session_state[input_key] = ""
|
| 41 |
-
|
| 42 |
if 'semantic_analysis_counter' not in st.session_state:
|
| 43 |
st.session_state.semantic_analysis_counter = 0
|
| 44 |
|
| 45 |
-
# Campo de entrada de texto con key única
|
| 46 |
-
#text_input = st.text_area(
|
| 47 |
-
# semantic_t.get('text_input_label', 'Enter text to analyze'),
|
| 48 |
-
# height=150,
|
| 49 |
-
# placeholder=semantic_t.get('text_input_placeholder', 'Enter your text here...'),
|
| 50 |
-
# value=st.session_state[input_key],
|
| 51 |
-
# key=f"semantic_text_area_{st.session_state.semantic_analysis_counter}"
|
| 52 |
-
#)
|
| 53 |
-
|
| 54 |
# Opción para cargar archivo con key única
|
| 55 |
uploaded_file = st.file_uploader(
|
| 56 |
-
semantic_t.get('file_uploader', '
|
| 57 |
type=['txt'],
|
| 58 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
|
| 59 |
)
|
| 60 |
|
| 61 |
# Botón de análisis con key única
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
if analyze_button:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
st.session_state.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
analysis_result,
|
| 94 |
-
lang_code,
|
| 95 |
-
semantic_t
|
| 96 |
-
)
|
| 97 |
-
else:
|
| 98 |
-
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 99 |
else:
|
| 100 |
-
st.error(
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.
|
|
|
|
|
|
|
| 106 |
|
| 107 |
# Mostrar resultados previos
|
| 108 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
|
@@ -112,7 +101,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 112 |
semantic_t
|
| 113 |
)
|
| 114 |
else:
|
| 115 |
-
st.info(semantic_t.get('initial_message', '
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
|
@@ -120,7 +109,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
| 120 |
|
| 121 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 122 |
"""
|
| 123 |
-
Muestra los resultados del análisis semántico
|
| 124 |
"""
|
| 125 |
if result is None or not result['success']:
|
| 126 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
|
@@ -128,37 +117,60 @@ def display_semantic_results(result, lang_code, semantic_t):
|
|
| 128 |
|
| 129 |
analysis = result['analysis']
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
# Botón de exportación
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
semantic_t: Diccionario de traducciones semánticas
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
+
# Inicializar el estado si no existe
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if 'semantic_analysis_counter' not in st.session_state:
|
| 39 |
st.session_state.semantic_analysis_counter = 0
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Opción para cargar archivo con key única
|
| 42 |
uploaded_file = st.file_uploader(
|
| 43 |
+
semantic_t.get('file_uploader', 'Upload a text file for analysis'),
|
| 44 |
type=['txt'],
|
| 45 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
|
| 46 |
)
|
| 47 |
|
| 48 |
# Botón de análisis con key única
|
| 49 |
+
col1, col2, col3 = st.columns([2,1,2])
|
| 50 |
+
with col2:
|
| 51 |
+
analyze_button = st.button(
|
| 52 |
+
semantic_t.get('analyze_button', 'Analyze text'),
|
| 53 |
+
key=f"semantic_analyze_button_{st.session_state.semantic_analysis_counter}",
|
| 54 |
+
use_container_width=True
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
if analyze_button and uploaded_file is not None:
|
| 58 |
+
try:
|
| 59 |
+
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 60 |
+
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 61 |
+
|
| 62 |
+
analysis_result = process_semantic_input(
|
| 63 |
+
text_content,
|
| 64 |
+
lang_code,
|
| 65 |
+
nlp_models,
|
| 66 |
+
semantic_t
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if analysis_result['success']:
|
| 70 |
+
st.session_state.semantic_result = analysis_result
|
| 71 |
+
st.session_state.semantic_analysis_counter += 1
|
| 72 |
|
| 73 |
+
# Guardar en la base de datos
|
| 74 |
+
if store_student_semantic_result(
|
| 75 |
+
st.session_state.username,
|
| 76 |
+
text_content,
|
| 77 |
+
analysis_result['analysis']
|
| 78 |
+
):
|
| 79 |
+
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
| 80 |
+
# Mostrar resultados
|
| 81 |
+
display_semantic_results(
|
| 82 |
+
analysis_result,
|
| 83 |
+
lang_code,
|
| 84 |
+
semantic_t
|
| 85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
+
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
| 88 |
+
else:
|
| 89 |
+
st.error(analysis_result['message'])
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"Error en análisis semántico: {str(e)}")
|
| 92 |
+
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 93 |
+
elif analyze_button:
|
| 94 |
+
st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
|
| 95 |
|
| 96 |
# Mostrar resultados previos
|
| 97 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
|
|
|
| 101 |
semantic_t
|
| 102 |
)
|
| 103 |
else:
|
| 104 |
+
st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
|
|
|
| 109 |
|
| 110 |
def display_semantic_results(result, lang_code, semantic_t):
|
| 111 |
"""
|
| 112 |
+
Muestra los resultados del análisis semántico en tabs
|
| 113 |
"""
|
| 114 |
if result is None or not result['success']:
|
| 115 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
|
|
|
| 117 |
|
| 118 |
analysis = result['analysis']
|
| 119 |
|
| 120 |
+
# Crear tabs para los resultados
|
| 121 |
+
tab1, tab2 = st.tabs([
|
| 122 |
+
semantic_t.get('concepts_tab', 'Key Concepts Analysis'),
|
| 123 |
+
semantic_t.get('entities_tab', 'Entities Analysis')
|
| 124 |
+
])
|
| 125 |
+
|
| 126 |
+
# Tab 1: Conceptos Clave
|
| 127 |
+
with tab1:
|
| 128 |
+
col1, col2 = st.columns(2)
|
| 129 |
+
|
| 130 |
+
# Columna 1: Lista de conceptos
|
| 131 |
+
with col1:
|
| 132 |
+
st.subheader(semantic_t.get('key_concepts', 'Key Concepts'))
|
| 133 |
+
concept_text = "\n".join([
|
| 134 |
+
f"• {concept} ({frequency:.2f})"
|
| 135 |
+
for concept, frequency in analysis['key_concepts']
|
| 136 |
+
])
|
| 137 |
+
st.markdown(concept_text)
|
| 138 |
+
|
| 139 |
+
# Columna 2: Gráfico de conceptos
|
| 140 |
+
with col2:
|
| 141 |
+
st.subheader(semantic_t.get('concept_graph', 'Concepts Graph'))
|
| 142 |
+
st.image(analysis['concept_graph'])
|
| 143 |
+
|
| 144 |
+
# Tab 2: Entidades
|
| 145 |
+
with tab2:
|
| 146 |
+
col1, col2 = st.columns(2)
|
| 147 |
+
|
| 148 |
+
# Columna 1: Lista de entidades
|
| 149 |
+
with col1:
|
| 150 |
+
st.subheader(semantic_t.get('identified_entities', 'Identified Entities'))
|
| 151 |
+
if 'entities' in analysis:
|
| 152 |
+
for entity_type, entities in analysis['entities'].items():
|
| 153 |
+
st.markdown(f"**{entity_type}**")
|
| 154 |
+
st.markdown("• " + "\n• ".join(entities))
|
| 155 |
+
|
| 156 |
+
# Columna 2: Gráfico de entidades
|
| 157 |
+
with col2:
|
| 158 |
+
st.subheader(semantic_t.get('entity_graph', 'Entities Graph'))
|
| 159 |
+
st.image(analysis['entity_graph'])
|
| 160 |
|
| 161 |
+
# Botón de exportación al final
|
| 162 |
+
col1, col2, col3 = st.columns([2,1,2])
|
| 163 |
+
with col2:
|
| 164 |
+
if st.button(
|
| 165 |
+
semantic_t.get('export_button', 'Export Analysis'),
|
| 166 |
+
key=f"semantic_export_{st.session_state.semantic_analysis_counter}",
|
| 167 |
+
use_container_width=True
|
| 168 |
+
):
|
| 169 |
+
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 170 |
+
st.download_button(
|
| 171 |
+
label=semantic_t.get('download_pdf', 'Download PDF'),
|
| 172 |
+
data=pdf_buffer,
|
| 173 |
+
file_name="semantic_analysis.pdf",
|
| 174 |
+
mime="application/pdf",
|
| 175 |
+
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
| 176 |
+
)
|