Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
@@ -32,83 +32,87 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
32 |
# Mantener la p谩gina en sem谩ntico
|
33 |
st.session_state.page = 'semantic'
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
""", unsafe_allow_html=True)
|
61 |
|
|
|
62 |
# Contenedor principal con layout fijo
|
63 |
with st.container():
|
64 |
# Una sola fila para todos los controles
|
65 |
-
|
66 |
|
67 |
# Columna 1: Carga de archivo
|
68 |
-
with
|
69 |
uploaded_file = st.file_uploader(
|
70 |
semantic_t.get('file_uploader', 'Upload TXT file'),
|
71 |
type=['txt'],
|
72 |
-
key=f"
|
73 |
)
|
74 |
|
75 |
-
# Actualizar el estado cuando se sube un archivo
|
76 |
if uploaded_file is not None:
|
77 |
-
|
78 |
-
|
79 |
-
st.session_state.
|
|
|
|
|
80 |
|
81 |
# Columna 2: Bot贸n de an谩lisis
|
82 |
-
with
|
|
|
83 |
analyze_button = st.button(
|
84 |
semantic_t.get('analyze_button', 'Analyze Text'),
|
85 |
-
disabled=
|
86 |
-
key=f"
|
|
|
87 |
)
|
88 |
|
89 |
# Columna 3: Bot贸n de exportaci贸n
|
90 |
-
with
|
91 |
export_button = st.button(
|
92 |
semantic_t.get('export_button', 'Export'),
|
93 |
disabled=not st.session_state.semantic_analysis_done,
|
94 |
-
key=f"
|
|
|
95 |
)
|
96 |
|
97 |
# Columna 4: Bot贸n de nuevo an谩lisis
|
98 |
-
with
|
99 |
-
|
100 |
-
semantic_t.get('new_analysis', 'New'),
|
101 |
disabled=not st.session_state.semantic_analysis_done,
|
102 |
-
key=f"
|
|
|
103 |
)
|
104 |
|
105 |
-
# Separador
|
106 |
st.markdown("<hr style='margin: 1em 0; opacity: 0.3'>", unsafe_allow_html=True)
|
107 |
|
108 |
-
# Procesar an谩lisis
|
109 |
-
if analyze_button and
|
110 |
-
|
111 |
-
|
112 |
analysis_result = process_semantic_input(
|
113 |
st.session_state.semantic_file_content,
|
114 |
lang_code,
|
@@ -117,32 +121,27 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
117 |
)
|
118 |
|
119 |
if analysis_result['success']:
|
|
|
120 |
st.session_state.semantic_result = analysis_result
|
121 |
st.session_state.semantic_analysis_done = True
|
122 |
|
123 |
-
# Guardar en
|
124 |
if store_student_semantic_result(
|
125 |
st.session_state.username,
|
126 |
st.session_state.semantic_file_content,
|
127 |
analysis_result['analysis']
|
128 |
):
|
129 |
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
130 |
-
|
131 |
-
display_semantic_results(
|
132 |
-
analysis_result,
|
133 |
-
lang_code,
|
134 |
-
semantic_t
|
135 |
-
)
|
136 |
else:
|
137 |
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
138 |
else:
|
139 |
st.error(analysis_result['message'])
|
|
|
|
|
|
|
140 |
|
141 |
-
|
142 |
-
logger.error(f"Error en an谩lisis sem谩ntico: {str(e)}")
|
143 |
-
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
144 |
-
|
145 |
-
# Manejo de exportaci贸n
|
146 |
if export_button and st.session_state.semantic_analysis_done:
|
147 |
try:
|
148 |
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
@@ -151,32 +150,29 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
151 |
data=pdf_buffer,
|
152 |
file_name="semantic_analysis.pdf",
|
153 |
mime="application/pdf",
|
154 |
-
key=f"
|
155 |
)
|
156 |
except Exception as e:
|
157 |
-
st.error(f"Error exporting
|
158 |
|
159 |
-
#
|
160 |
-
if
|
161 |
st.session_state.semantic_file_content = None
|
162 |
st.session_state.semantic_analysis_done = False
|
163 |
st.session_state.semantic_result = None
|
164 |
st.session_state.semantic_analysis_counter += 1
|
165 |
-
st.
|
166 |
|
167 |
-
# Mostrar resultados o mensaje inicial
|
168 |
if st.session_state.semantic_analysis_done and 'semantic_result' in st.session_state:
|
169 |
-
display_semantic_results(
|
170 |
-
st.session_state.semantic_result,
|
171 |
-
lang_code,
|
172 |
-
semantic_t
|
173 |
-
)
|
174 |
elif not uploaded_file:
|
175 |
st.info(semantic_t.get('initial_message', 'Upload a TXT file to begin analysis'))
|
176 |
|
177 |
except Exception as e:
|
178 |
-
logger.error(f"Error general
|
179 |
-
st.error("
|
|
|
180 |
|
181 |
def display_semantic_results(result, lang_code, semantic_t):
|
182 |
"""
|
|
|
32 |
# Mantener la p谩gina en sem谩ntico
|
33 |
st.session_state.page = 'semantic'
|
34 |
|
35 |
+
# Inicializar estados si no existen
|
36 |
+
if 'semantic_file_content' not in st.session_state:
|
37 |
+
st.session_state.semantic_file_content = None
|
38 |
+
if 'semantic_analysis_done' not in st.session_state:
|
39 |
+
st.session_state.semantic_analysis_done = False
|
40 |
+
if 'semantic_analysis_counter' not in st.session_state:
|
41 |
+
st.session_state.semantic_analysis_counter = 0
|
42 |
+
|
43 |
+
# Estilos CSS para alinear los botones
|
44 |
+
st.markdown("""
|
45 |
+
<style>
|
46 |
+
.stButton > button {
|
47 |
+
width: 100%;
|
48 |
+
height: 38px;
|
49 |
+
}
|
50 |
+
.stUploadButton > button {
|
51 |
+
width: 100%;
|
52 |
+
height: 38px;
|
53 |
+
}
|
54 |
+
div.row-widget.stButton {
|
55 |
+
margin-top: 1px;
|
56 |
+
margin-bottom: 1px;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
""", unsafe_allow_html=True)
|
|
|
60 |
|
61 |
+
try:
|
62 |
# Contenedor principal con layout fijo
|
63 |
with st.container():
|
64 |
# Una sola fila para todos los controles
|
65 |
+
col_file, col_analyze, col_export, col_new = st.columns([4, 2, 2, 2])
|
66 |
|
67 |
# Columna 1: Carga de archivo
|
68 |
+
with col_file:
|
69 |
uploaded_file = st.file_uploader(
|
70 |
semantic_t.get('file_uploader', 'Upload TXT file'),
|
71 |
type=['txt'],
|
72 |
+
key=f"semantic_uploader_{st.session_state.semantic_analysis_counter}"
|
73 |
)
|
74 |
|
|
|
75 |
if uploaded_file is not None:
|
76 |
+
# Actualizar el contenido del archivo
|
77 |
+
file_content = uploaded_file.getvalue().decode('utf-8')
|
78 |
+
if file_content != st.session_state.semantic_file_content:
|
79 |
+
st.session_state.semantic_file_content = file_content
|
80 |
+
st.session_state.semantic_analysis_done = False
|
81 |
|
82 |
# Columna 2: Bot贸n de an谩lisis
|
83 |
+
with col_analyze:
|
84 |
+
analyze_enabled = uploaded_file is not None and not st.session_state.semantic_analysis_done
|
85 |
analyze_button = st.button(
|
86 |
semantic_t.get('analyze_button', 'Analyze Text'),
|
87 |
+
disabled=not analyze_enabled,
|
88 |
+
key=f"analyze_button_{st.session_state.semantic_analysis_counter}",
|
89 |
+
use_container_width=True
|
90 |
)
|
91 |
|
92 |
# Columna 3: Bot贸n de exportaci贸n
|
93 |
+
with col_export:
|
94 |
export_button = st.button(
|
95 |
semantic_t.get('export_button', 'Export'),
|
96 |
disabled=not st.session_state.semantic_analysis_done,
|
97 |
+
key=f"export_button_{st.session_state.semantic_analysis_counter}",
|
98 |
+
use_container_width=True
|
99 |
)
|
100 |
|
101 |
# Columna 4: Bot贸n de nuevo an谩lisis
|
102 |
+
with col_new:
|
103 |
+
new_button = st.button(
|
104 |
+
semantic_t.get('new_analysis', 'New Analysis'),
|
105 |
disabled=not st.session_state.semantic_analysis_done,
|
106 |
+
key=f"new_button_{st.session_state.semantic_analysis_counter}",
|
107 |
+
use_container_width=True
|
108 |
)
|
109 |
|
|
|
110 |
st.markdown("<hr style='margin: 1em 0; opacity: 0.3'>", unsafe_allow_html=True)
|
111 |
|
112 |
+
# Procesar an谩lisis cuando se presiona el bot贸n
|
113 |
+
if analyze_button and st.session_state.semantic_file_content:
|
114 |
+
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
115 |
+
try:
|
116 |
analysis_result = process_semantic_input(
|
117 |
st.session_state.semantic_file_content,
|
118 |
lang_code,
|
|
|
121 |
)
|
122 |
|
123 |
if analysis_result['success']:
|
124 |
+
# Guardar resultados y actualizar estado
|
125 |
st.session_state.semantic_result = analysis_result
|
126 |
st.session_state.semantic_analysis_done = True
|
127 |
|
128 |
+
# Guardar en base de datos
|
129 |
if store_student_semantic_result(
|
130 |
st.session_state.username,
|
131 |
st.session_state.semantic_file_content,
|
132 |
analysis_result['analysis']
|
133 |
):
|
134 |
st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
|
135 |
+
display_semantic_results(analysis_result, lang_code, semantic_t)
|
|
|
|
|
|
|
|
|
|
|
136 |
else:
|
137 |
st.error(semantic_t.get('error_message', 'Error saving analysis'))
|
138 |
else:
|
139 |
st.error(analysis_result['message'])
|
140 |
+
except Exception as e:
|
141 |
+
logger.error(f"Error en an谩lisis: {str(e)}")
|
142 |
+
st.error(semantic_t.get('error_processing', f'Error: {str(e)}'))
|
143 |
|
144 |
+
# Manejar exportaci贸n
|
|
|
|
|
|
|
|
|
145 |
if export_button and st.session_state.semantic_analysis_done:
|
146 |
try:
|
147 |
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
|
|
150 |
data=pdf_buffer,
|
151 |
file_name="semantic_analysis.pdf",
|
152 |
mime="application/pdf",
|
153 |
+
key=f"download_{st.session_state.semantic_analysis_counter}"
|
154 |
)
|
155 |
except Exception as e:
|
156 |
+
st.error(f"Error exporting: {str(e)}")
|
157 |
|
158 |
+
# Manejar nuevo an谩lisis
|
159 |
+
if new_button:
|
160 |
st.session_state.semantic_file_content = None
|
161 |
st.session_state.semantic_analysis_done = False
|
162 |
st.session_state.semantic_result = None
|
163 |
st.session_state.semantic_analysis_counter += 1
|
164 |
+
st.rerun()
|
165 |
|
166 |
+
# Mostrar resultados existentes o mensaje inicial
|
167 |
if st.session_state.semantic_analysis_done and 'semantic_result' in st.session_state:
|
168 |
+
display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t)
|
|
|
|
|
|
|
|
|
169 |
elif not uploaded_file:
|
170 |
st.info(semantic_t.get('initial_message', 'Upload a TXT file to begin analysis'))
|
171 |
|
172 |
except Exception as e:
|
173 |
+
logger.error(f"Error general: {str(e)}")
|
174 |
+
st.error("Error in semantic interface. Please try again.")
|
175 |
+
|
176 |
|
177 |
def display_semantic_results(result, lang_code, semantic_t):
|
178 |
"""
|