Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
@@ -29,150 +29,95 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
|
29 |
"""
|
30 |
Interfaz para el an谩lisis sem谩ntico con controles alineados horizontalmente
|
31 |
"""
|
32 |
-
#
|
33 |
st.session_state.page = 'semantic'
|
34 |
-
|
35 |
-
# Inicializar estados
|
36 |
-
if '
|
37 |
-
st.session_state.
|
38 |
-
if '
|
39 |
-
st.session_state.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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,
|
119 |
-
nlp_models,
|
120 |
-
semantic_t
|
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')
|
148 |
-
st.download_button(
|
149 |
-
label=semantic_t.get('download_pdf', 'Download PDF'),
|
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 |
"""
|
|
|
29 |
"""
|
30 |
Interfaz para el an谩lisis sem谩ntico con controles alineados horizontalmente
|
31 |
"""
|
32 |
+
# Forzar la p谩gina a sem谩ntico
|
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 |
+
# Contenedor principal
|
42 |
+
with st.container():
|
43 |
+
# Una sola fila para todos los controles
|
44 |
+
cols = st.columns([4, 2, 2, 2])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Columna 1: Carga de archivo
|
47 |
+
with cols[0]:
|
48 |
+
uploaded_file = st.file_uploader(
|
49 |
+
"Upload text file", # Simplificamos el mensaje
|
50 |
+
type=['txt'],
|
51 |
+
key="semantic_file_upload"
|
52 |
+
)
|
53 |
+
|
54 |
+
# Columna 2: Bot贸n de an谩lisis
|
55 |
+
with cols[1]:
|
56 |
+
can_analyze = uploaded_file is not None and not st.session_state.semantic_analyzed
|
57 |
+
if st.button('Analyze',
|
58 |
+
disabled=not can_analyze,
|
59 |
+
key="semantic_analyze"):
|
60 |
+
if uploaded_file:
|
61 |
+
text_content = uploaded_file.getvalue().decode('utf-8')
|
62 |
+
# Realizar el an谩lisis
|
63 |
+
with st.spinner("Analyzing..."):
|
64 |
+
analysis_result = process_semantic_input(
|
65 |
+
text_content,
|
66 |
+
lang_code,
|
67 |
+
nlp_models,
|
68 |
+
semantic_t
|
69 |
+
)
|
70 |
+
if analysis_result['success']:
|
71 |
+
st.session_state.semantic_result = analysis_result
|
72 |
+
st.session_state.semantic_analyzed = True
|
73 |
+
st.success("Analysis completed!")
|
74 |
+
|
75 |
+
# Mostrar resultados
|
76 |
+
display_semantic_results(
|
77 |
+
analysis_result,
|
78 |
+
lang_code,
|
79 |
+
semantic_t
|
80 |
+
)
|
81 |
+
|
82 |
+
# Columna 3: Bot贸n de exportaci贸n
|
83 |
+
with cols[2]:
|
84 |
+
if st.button('Export',
|
85 |
+
disabled=not st.session_state.semantic_analyzed,
|
86 |
+
key="semantic_export"):
|
87 |
+
if st.session_state.semantic_analyzed:
|
88 |
+
try:
|
89 |
+
pdf_buffer = export_user_interactions(
|
90 |
+
st.session_state.username,
|
91 |
+
'semantic'
|
92 |
+
)
|
93 |
+
st.download_button(
|
94 |
+
"Download PDF",
|
95 |
+
data=pdf_buffer,
|
96 |
+
file_name="semantic_analysis.pdf",
|
97 |
+
mime="application/pdf"
|
98 |
+
)
|
99 |
+
except Exception as e:
|
100 |
+
st.error(f"Error exporting: {str(e)}")
|
101 |
+
|
102 |
+
# Columna 4: Bot贸n de nuevo an谩lisis
|
103 |
+
with cols[3]:
|
104 |
+
if st.button('New Analysis',
|
105 |
+
disabled=not st.session_state.semantic_analyzed,
|
106 |
+
key="semantic_new"):
|
107 |
+
st.session_state.semantic_content = None
|
108 |
+
st.session_state.semantic_analyzed = False
|
109 |
+
st.session_state.semantic_result = None
|
110 |
+
st.rerun()
|
111 |
+
|
112 |
+
# Mostrar resultados si existen
|
113 |
+
if st.session_state.semantic_analyzed and 'semantic_result' in st.session_state:
|
114 |
+
display_semantic_results(
|
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 |
"""
|