Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
@@ -38,26 +38,55 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
38 |
semantic_t: Diccionario de traducciones semánticas
|
39 |
"""
|
40 |
try:
|
41 |
-
# Inicializar
|
42 |
if 'semantic_analysis_counter' not in st.session_state:
|
43 |
st.session_state.semantic_analysis_counter = 0
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
with col2:
|
55 |
-
analyze_button = st.button(
|
56 |
-
semantic_t.get('analyze_button', 'Analyze text'),
|
57 |
-
key=f"semantic_analyze_button_{st.session_state.semantic_analysis_counter}",
|
58 |
-
use_container_width=True
|
59 |
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if analyze_button and uploaded_file is not None:
|
62 |
try:
|
63 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
@@ -81,6 +110,8 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
81 |
analysis_result['analysis']
|
82 |
):
|
83 |
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
|
|
|
|
84 |
# Mostrar resultados
|
85 |
display_semantic_results(
|
86 |
analysis_result,
|
@@ -94,8 +125,6 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
94 |
except Exception as e:
|
95 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
96 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
97 |
-
elif analyze_button:
|
98 |
-
st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
|
99 |
|
100 |
# Mostrar resultados previos
|
101 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
@@ -111,70 +140,4 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
111 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
112 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
113 |
|
114 |
-
|
115 |
-
"""
|
116 |
-
Muestra los resultados del análisis semántico en tabs
|
117 |
-
"""
|
118 |
-
if result is None or not result['success']:
|
119 |
-
st.warning(semantic_t.get('no_results', 'No results available'))
|
120 |
-
return
|
121 |
-
|
122 |
-
analysis = result['analysis']
|
123 |
-
|
124 |
-
# Crear tabs para los resultados
|
125 |
-
tab1, tab2 = st.tabs([
|
126 |
-
semantic_t.get('concepts_tab', 'Key Concepts Analysis'),
|
127 |
-
semantic_t.get('entities_tab', 'Entities Analysis')
|
128 |
-
])
|
129 |
-
|
130 |
-
# Tab 1: Conceptos Clave
|
131 |
-
with tab1:
|
132 |
-
col1, col2 = st.columns(2)
|
133 |
-
|
134 |
-
# Columna 1: Lista de conceptos
|
135 |
-
with col1:
|
136 |
-
st.subheader(semantic_t.get('key_concepts', 'Key Concepts'))
|
137 |
-
concept_text = "\n".join([
|
138 |
-
f"• {concept} ({frequency:.2f})"
|
139 |
-
for concept, frequency in analysis['key_concepts']
|
140 |
-
])
|
141 |
-
st.markdown(concept_text)
|
142 |
-
|
143 |
-
# Columna 2: Gráfico de conceptos
|
144 |
-
with col2:
|
145 |
-
st.subheader(semantic_t.get('concept_graph', 'Concepts Graph'))
|
146 |
-
st.image(analysis['concept_graph'])
|
147 |
-
|
148 |
-
# Tab 2: Entidades
|
149 |
-
with tab2:
|
150 |
-
col1, col2 = st.columns(2)
|
151 |
-
|
152 |
-
# Columna 1: Lista de entidades
|
153 |
-
with col1:
|
154 |
-
st.subheader(semantic_t.get('identified_entities', 'Identified Entities'))
|
155 |
-
if 'entities' in analysis:
|
156 |
-
for entity_type, entities in analysis['entities'].items():
|
157 |
-
st.markdown(f"**{entity_type}**")
|
158 |
-
st.markdown("• " + "\n• ".join(entities))
|
159 |
-
|
160 |
-
# Columna 2: Gráfico de entidades
|
161 |
-
with col2:
|
162 |
-
st.subheader(semantic_t.get('entity_graph', 'Entities Graph'))
|
163 |
-
st.image(analysis['entity_graph'])
|
164 |
-
|
165 |
-
# Botón de exportación al final
|
166 |
-
col1, col2, col3 = st.columns([2,1,2])
|
167 |
-
with col2:
|
168 |
-
if st.button(
|
169 |
-
semantic_t.get('export_button', 'Export Analysis'),
|
170 |
-
key=f"semantic_export_{st.session_state.semantic_analysis_counter}",
|
171 |
-
use_container_width=True
|
172 |
-
):
|
173 |
-
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
174 |
-
st.download_button(
|
175 |
-
label=semantic_t.get('download_pdf', 'Download PDF'),
|
176 |
-
data=pdf_buffer,
|
177 |
-
file_name="semantic_analysis.pdf",
|
178 |
-
mime="application/pdf",
|
179 |
-
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
180 |
-
)
|
|
|
38 |
semantic_t: Diccionario de traducciones semánticas
|
39 |
"""
|
40 |
try:
|
41 |
+
# Inicializar estados si no existen
|
42 |
if 'semantic_analysis_counter' not in st.session_state:
|
43 |
st.session_state.semantic_analysis_counter = 0
|
44 |
+
if 'semantic_current_file' not in st.session_state:
|
45 |
+
st.session_state.semantic_current_file = None
|
46 |
+
if 'semantic_page' not in st.session_state:
|
47 |
+
st.session_state.semantic_page = 'semantic'
|
48 |
|
49 |
+
# Contenedor fijo para los controles
|
50 |
+
with st.container():
|
51 |
+
st.markdown("### Controls")
|
52 |
+
# Opción para cargar archivo con key única
|
53 |
+
uploaded_file = st.file_uploader(
|
54 |
+
semantic_t.get('file_uploader', 'Upload a text file for analysis'),
|
55 |
+
type=['txt'],
|
56 |
+
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}",
|
57 |
+
on_change=lambda: setattr(st.session_state, 'semantic_current_file', uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
58 |
)
|
59 |
|
60 |
+
# Botón de análisis deshabilitado si no hay archivo
|
61 |
+
col1, col2, col3 = st.columns([1,2,1])
|
62 |
+
with col1:
|
63 |
+
analyze_button = st.button(
|
64 |
+
semantic_t.get('analyze_button', 'Analyze text'),
|
65 |
+
key=f"semantic_analyze_button_{st.session_state.semantic_analysis_counter}",
|
66 |
+
disabled=not uploaded_file,
|
67 |
+
use_container_width=True
|
68 |
+
)
|
69 |
+
|
70 |
+
# Botón de exportación solo visible si hay resultados
|
71 |
+
if 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
72 |
+
export_button = st.button(
|
73 |
+
semantic_t.get('export_button', 'Export Analysis'),
|
74 |
+
key=f"semantic_export_{st.session_state.semantic_analysis_counter}",
|
75 |
+
use_container_width=True
|
76 |
+
)
|
77 |
+
if export_button:
|
78 |
+
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
79 |
+
st.download_button(
|
80 |
+
label=semantic_t.get('download_pdf', 'Download PDF'),
|
81 |
+
data=pdf_buffer,
|
82 |
+
file_name="semantic_analysis.pdf",
|
83 |
+
mime="application/pdf",
|
84 |
+
key=f"semantic_download_{st.session_state.semantic_analysis_counter}"
|
85 |
+
)
|
86 |
+
|
87 |
+
st.markdown("---") # Separador
|
88 |
+
|
89 |
+
# Procesar el análisis cuando se presiona el botón
|
90 |
if analyze_button and uploaded_file is not None:
|
91 |
try:
|
92 |
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
|
|
110 |
analysis_result['analysis']
|
111 |
):
|
112 |
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
113 |
+
# Asegurar que nos mantenemos en la página semántica
|
114 |
+
st.session_state.page = 'semantic'
|
115 |
# Mostrar resultados
|
116 |
display_semantic_results(
|
117 |
analysis_result,
|
|
|
125 |
except Exception as e:
|
126 |
logger.error(f"Error en análisis semántico: {str(e)}")
|
127 |
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
|
|
|
|
128 |
|
129 |
# Mostrar resultados previos
|
130 |
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("Se produjo un error. Por favor, intente de nuevo.")
|
142 |
|
143 |
+
# [Resto del código igual...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|