Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -81,16 +81,27 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
81 |
</style>
|
82 |
""", unsafe_allow_html=True)
|
83 |
|
84 |
-
# Inicializaci贸n del estado
|
85 |
if 'morphosyntax_state' not in st.session_state:
|
86 |
st.session_state.morphosyntax_state = {
|
87 |
'original_text': '',
|
88 |
'current_text': '',
|
89 |
-
'
|
90 |
'analysis_count': 0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
'original_analysis': None,
|
92 |
-
'
|
|
|
93 |
}
|
|
|
|
|
|
|
94 |
|
95 |
with st.container():
|
96 |
# Secci贸n de texto original
|
@@ -99,11 +110,11 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
99 |
# Input para texto original
|
100 |
original_text = st.text_area(
|
101 |
"Ingrese una oraci贸n",
|
102 |
-
value=st.session_state.morphosyntax_state
|
103 |
key="original_text_input",
|
104 |
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
|
105 |
height=100,
|
106 |
-
disabled=False
|
107 |
)
|
108 |
|
109 |
# Bot贸n para analizar texto original
|
@@ -116,7 +127,7 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
116 |
disabled=not bool(original_text.strip())
|
117 |
)
|
118 |
|
119 |
-
# Procesar texto original
|
120 |
if analyze_original and original_text.strip():
|
121 |
try:
|
122 |
with st.spinner("Procesando texto original..."):
|
@@ -126,14 +137,15 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
126 |
nlp_models[lang_code]
|
127 |
)
|
128 |
|
129 |
-
# Actualizar estado
|
130 |
st.session_state.morphosyntax_state.update({
|
131 |
'original_text': original_text,
|
|
|
132 |
'original_analysis': {
|
133 |
'doc': doc,
|
134 |
'advanced_analysis': analysis
|
135 |
},
|
136 |
-
'
|
137 |
})
|
138 |
|
139 |
# Guardar en base de datos
|
@@ -149,8 +161,8 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
149 |
logger.error(f"Error procesando texto original: {str(e)}")
|
150 |
st.error("Error al procesar el texto original")
|
151 |
|
152 |
-
# Mostrar diagrama
|
153 |
-
if st.session_state.morphosyntax_state
|
154 |
display_morphosyntax_results(
|
155 |
st.session_state.morphosyntax_state['original_analysis'],
|
156 |
lang_code,
|
@@ -164,7 +176,7 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
164 |
# Campo para nueva versi贸n
|
165 |
iteration_text = st.text_area(
|
166 |
"Modifique la oraci贸n",
|
167 |
-
value=st.session_state.morphosyntax_state
|
168 |
key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}",
|
169 |
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
|
170 |
height=100
|
@@ -197,15 +209,21 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
197 |
'advanced_analysis': analysis
|
198 |
}
|
199 |
|
200 |
-
#
|
201 |
-
|
202 |
'text': iteration_text,
|
203 |
'analysis': current_analysis,
|
204 |
'timestamp': pd.Timestamp.now()
|
205 |
-
}
|
206 |
|
207 |
-
|
208 |
-
st.session_state.morphosyntax_state
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
if store_student_morphosyntax_result(
|
211 |
username=st.session_state.username,
|
@@ -225,7 +243,7 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
225 |
st.error("Error al procesar los cambios")
|
226 |
|
227 |
# Mostrar historial de iteraciones
|
228 |
-
if st.session_state.morphosyntax_state
|
229 |
with st.expander("Historial de Iteraciones", expanded=False):
|
230 |
for idx, iteration in enumerate(reversed(st.session_state.morphosyntax_state['iterations'])):
|
231 |
st.markdown(f"**Iteraci贸n {idx + 1} ({iteration['timestamp'].strftime('%H:%M:%S')})**")
|
@@ -251,7 +269,6 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
251 |
|
252 |
|
253 |
|
254 |
-
|
255 |
#########################################################################3
|
256 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
257 |
"""
|
|
|
81 |
</style>
|
82 |
""", unsafe_allow_html=True)
|
83 |
|
84 |
+
# Inicializaci贸n m谩s robusta del estado
|
85 |
if 'morphosyntax_state' not in st.session_state:
|
86 |
st.session_state.morphosyntax_state = {
|
87 |
'original_text': '',
|
88 |
'current_text': '',
|
89 |
+
'original_analysis': None,
|
90 |
'analysis_count': 0,
|
91 |
+
'iterations': [] # Inicializaci贸n expl铆cita de iterations como lista vac铆a
|
92 |
+
}
|
93 |
+
else:
|
94 |
+
# Asegurar que todas las claves existan
|
95 |
+
required_keys = {
|
96 |
+
'original_text': '',
|
97 |
+
'current_text': '',
|
98 |
'original_analysis': None,
|
99 |
+
'analysis_count': 0,
|
100 |
+
'iterations': []
|
101 |
}
|
102 |
+
for key, default_value in required_keys.items():
|
103 |
+
if key not in st.session_state.morphosyntax_state:
|
104 |
+
st.session_state.morphosyntax_state[key] = default_value
|
105 |
|
106 |
with st.container():
|
107 |
# Secci贸n de texto original
|
|
|
110 |
# Input para texto original
|
111 |
original_text = st.text_area(
|
112 |
"Ingrese una oraci贸n",
|
113 |
+
value=st.session_state.morphosyntax_state['original_text'],
|
114 |
key="original_text_input",
|
115 |
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
|
116 |
height=100,
|
117 |
+
disabled=False
|
118 |
)
|
119 |
|
120 |
# Bot贸n para analizar texto original
|
|
|
127 |
disabled=not bool(original_text.strip())
|
128 |
)
|
129 |
|
130 |
+
# Procesar texto original
|
131 |
if analyze_original and original_text.strip():
|
132 |
try:
|
133 |
with st.spinner("Procesando texto original..."):
|
|
|
137 |
nlp_models[lang_code]
|
138 |
)
|
139 |
|
140 |
+
# Actualizar estado de forma segura
|
141 |
st.session_state.morphosyntax_state.update({
|
142 |
'original_text': original_text,
|
143 |
+
'current_text': original_text,
|
144 |
'original_analysis': {
|
145 |
'doc': doc,
|
146 |
'advanced_analysis': analysis
|
147 |
},
|
148 |
+
'iterations': [] # Reiniciar iteraciones al cambiar texto original
|
149 |
})
|
150 |
|
151 |
# Guardar en base de datos
|
|
|
161 |
logger.error(f"Error procesando texto original: {str(e)}")
|
162 |
st.error("Error al procesar el texto original")
|
163 |
|
164 |
+
# Mostrar diagrama original
|
165 |
+
if st.session_state.morphosyntax_state['original_analysis']:
|
166 |
display_morphosyntax_results(
|
167 |
st.session_state.morphosyntax_state['original_analysis'],
|
168 |
lang_code,
|
|
|
176 |
# Campo para nueva versi贸n
|
177 |
iteration_text = st.text_area(
|
178 |
"Modifique la oraci贸n",
|
179 |
+
value=st.session_state.morphosyntax_state['current_text'],
|
180 |
key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}",
|
181 |
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
|
182 |
height=100
|
|
|
209 |
'advanced_analysis': analysis
|
210 |
}
|
211 |
|
212 |
+
# Crear nueva iteraci贸n
|
213 |
+
new_iteration = {
|
214 |
'text': iteration_text,
|
215 |
'analysis': current_analysis,
|
216 |
'timestamp': pd.Timestamp.now()
|
217 |
+
}
|
218 |
|
219 |
+
# Actualizar estado de forma segura
|
220 |
+
iterations = st.session_state.morphosyntax_state.get('iterations', [])
|
221 |
+
iterations.append(new_iteration)
|
222 |
+
st.session_state.morphosyntax_state.update({
|
223 |
+
'current_text': iteration_text,
|
224 |
+
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1,
|
225 |
+
'iterations': iterations
|
226 |
+
})
|
227 |
|
228 |
if store_student_morphosyntax_result(
|
229 |
username=st.session_state.username,
|
|
|
243 |
st.error("Error al procesar los cambios")
|
244 |
|
245 |
# Mostrar historial de iteraciones
|
246 |
+
if st.session_state.morphosyntax_state.get('iterations', []):
|
247 |
with st.expander("Historial de Iteraciones", expanded=False):
|
248 |
for idx, iteration in enumerate(reversed(st.session_state.morphosyntax_state['iterations'])):
|
249 |
st.markdown(f"**Iteraci贸n {idx + 1} ({iteration['timestamp'].strftime('%H:%M:%S')})**")
|
|
|
269 |
|
270 |
|
271 |
|
|
|
272 |
#########################################################################3
|
273 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
274 |
"""
|