Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
@@ -26,6 +26,9 @@ from ..database.semantic_mongo_db import store_student_semantic_result
|
|
26 |
from ..database.semantic_export import export_user_interactions
|
27 |
|
28 |
|
|
|
|
|
|
|
29 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
30 |
"""
|
31 |
Interfaz para el análisis semántico
|
@@ -35,75 +38,64 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
35 |
semantic_t: Diccionario de traducciones semánticas
|
36 |
"""
|
37 |
try:
|
38 |
-
# Inicializar el estado
|
39 |
-
input_key = f"semantic_input_{lang_code}"
|
40 |
-
if input_key not in st.session_state:
|
41 |
-
st.session_state[input_key] = ""
|
42 |
-
|
43 |
if 'semantic_analysis_counter' not in st.session_state:
|
44 |
st.session_state.semantic_analysis_counter = 0
|
45 |
|
46 |
-
# Campo de entrada de texto con key única
|
47 |
-
text_input = st.text_area(
|
48 |
-
semantic_t.get('text_input_label', 'Enter text to analyze'),
|
49 |
-
height=150,
|
50 |
-
placeholder=semantic_t.get('text_input_placeholder', 'Enter your text here...'),
|
51 |
-
value=st.session_state[input_key],
|
52 |
-
key=f"semantic_text_area_{st.session_state.semantic_analysis_counter}"
|
53 |
-
)
|
54 |
-
|
55 |
# Opción para cargar archivo con key única
|
56 |
uploaded_file = st.file_uploader(
|
57 |
-
semantic_t.get('file_uploader', '
|
58 |
type=['txt'],
|
59 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
|
60 |
)
|
61 |
|
62 |
# Botón de análisis con key única
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
if analyze_button:
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
st.session_state.
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
analysis_result,
|
95 |
-
lang_code,
|
96 |
-
semantic_t
|
97 |
-
)
|
98 |
-
else:
|
99 |
-
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
100 |
else:
|
101 |
-
st.error(
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
st.
|
|
|
|
|
107 |
|
108 |
# Mostrar resultados previos
|
109 |
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
@@ -113,7 +105,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
113 |
semantic_t
|
114 |
)
|
115 |
else:
|
116 |
-
st.info(semantic_t.get('initial_message', '
|
117 |
|
118 |
except Exception as e:
|
119 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
@@ -121,7 +113,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
121 |
|
122 |
def display_semantic_results(result, lang_code, semantic_t):
|
123 |
"""
|
124 |
-
Muestra los resultados del análisis semántico
|
125 |
"""
|
126 |
if result is None or not result['success']:
|
127 |
st.warning(semantic_t.get('no_results', 'No results available'))
|
@@ -129,37 +121,60 @@ def display_semantic_results(result, lang_code, semantic_t):
|
|
129 |
|
130 |
analysis = result['analysis']
|
131 |
|
132 |
-
#
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
from ..database.semantic_export import export_user_interactions
|
27 |
|
28 |
|
29 |
+
#modules/semantic/semantic_interface.py
|
30 |
+
# [Mantener las importaciones igual...]
|
31 |
+
|
32 |
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
33 |
"""
|
34 |
Interfaz para el análisis semántico
|
|
|
38 |
semantic_t: Diccionario de traducciones semánticas
|
39 |
"""
|
40 |
try:
|
41 |
+
# Inicializar el estado si no existe
|
|
|
|
|
|
|
|
|
42 |
if 'semantic_analysis_counter' not in st.session_state:
|
43 |
st.session_state.semantic_analysis_counter = 0
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Opción para cargar archivo con key única
|
46 |
uploaded_file = st.file_uploader(
|
47 |
+
semantic_t.get('file_uploader', 'Upload a text file for analysis'),
|
48 |
type=['txt'],
|
49 |
key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
|
50 |
)
|
51 |
|
52 |
# Botón de análisis con key única
|
53 |
+
col1, col2, col3 = st.columns([2,1,2])
|
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...')):
|
64 |
+
text_content = uploaded_file.getvalue().decode('utf-8')
|
65 |
+
|
66 |
+
analysis_result = process_semantic_input(
|
67 |
+
text_content,
|
68 |
+
lang_code,
|
69 |
+
nlp_models,
|
70 |
+
semantic_t
|
71 |
+
)
|
72 |
+
|
73 |
+
if analysis_result['success']:
|
74 |
+
st.session_state.semantic_result = analysis_result
|
75 |
+
st.session_state.semantic_analysis_counter += 1
|
76 |
|
77 |
+
# Guardar en la base de datos
|
78 |
+
if store_student_semantic_result(
|
79 |
+
st.session_state.username,
|
80 |
+
text_content,
|
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,
|
87 |
+
lang_code,
|
88 |
+
semantic_t
|
89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
+
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
92 |
+
else:
|
93 |
+
st.error(analysis_result['message'])
|
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:
|
|
|
105 |
semantic_t
|
106 |
)
|
107 |
else:
|
108 |
+
st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
|
109 |
|
110 |
except Exception as e:
|
111 |
logger.error(f"Error general en interfaz semántica: {str(e)}")
|
|
|
113 |
|
114 |
def display_semantic_results(result, lang_code, semantic_t):
|
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'))
|
|
|
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 |
+
)
|