Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -32,88 +32,89 @@ logger = logging.getLogger(__name__)
|
|
32 |
|
33 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
34 |
try:
|
35 |
-
# Inicializar
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Campo de entrada de texto
|
45 |
sentence_input = st.text_area(
|
46 |
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
47 |
height=150,
|
48 |
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
|
49 |
-
|
50 |
-
key=f"text_area_{lang_code}_{st.session_state.analysis_counter}", # Clave única
|
51 |
-
on_change=lambda: setattr(st.session_state, input_key,
|
52 |
-
st.session_state[f"text_area_{lang_code}_{st.session_state.analysis_counter}"])
|
53 |
)
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
use_container_width=True
|
61 |
-
):
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
# Realizar análisis morfosintáctico
|
72 |
-
advanced_analysis = perform_advanced_morphosyntactic_analysis(
|
73 |
-
current_input,
|
74 |
-
nlp_models[lang_code]
|
75 |
-
)
|
76 |
-
|
77 |
-
# Guardar resultado en el estado de la sesión
|
78 |
-
st.session_state.morphosyntax_result = {
|
79 |
-
'doc': doc,
|
80 |
-
'advanced_analysis': advanced_analysis
|
81 |
-
}
|
82 |
-
|
83 |
-
# Incrementar el contador de análisis
|
84 |
-
st.session_state.analysis_counter += 1
|
85 |
-
|
86 |
# Mostrar resultados
|
87 |
display_morphosyntax_results(
|
88 |
st.session_state.morphosyntax_result,
|
89 |
lang_code,
|
90 |
morpho_t
|
91 |
)
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
):
|
99 |
-
st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
|
100 |
-
else:
|
101 |
-
st.error(morpho_t.get('error_message', 'Error saving analysis'))
|
102 |
-
|
103 |
-
except Exception as e:
|
104 |
-
logger.error(f"Error en análisis morfosintáctico: {str(e)}")
|
105 |
-
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
106 |
-
else:
|
107 |
-
st.warning(morpho_t.get('warning_message', 'Please enter a text to analyze'))
|
108 |
|
109 |
-
#
|
110 |
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
|
111 |
display_morphosyntax_results(
|
112 |
st.session_state.morphosyntax_result,
|
113 |
lang_code,
|
114 |
morpho_t
|
115 |
)
|
116 |
-
|
117 |
st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
|
118 |
|
119 |
except Exception as e:
|
|
|
32 |
|
33 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
34 |
try:
|
35 |
+
# 1. Inicializar estados de sesión de manera consistente
|
36 |
+
if 'morphosyntax_state' not in st.session_state:
|
37 |
+
st.session_state.morphosyntax_state = {
|
38 |
+
'input_text': "",
|
39 |
+
'analysis_counter': 0,
|
40 |
+
'last_analysis': None
|
41 |
+
}
|
42 |
|
43 |
+
# 2. Campo de entrada de texto con key única basada en el contador
|
44 |
+
input_key = f"morpho_input_{st.session_state.morphosyntax_state['analysis_counter']}"
|
45 |
+
|
|
|
|
|
46 |
sentence_input = st.text_area(
|
47 |
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
48 |
height=150,
|
49 |
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
|
50 |
+
key=input_key
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
+
# 3. Actualizar el estado con el texto actual
|
54 |
+
st.session_state.morphosyntax_state['input_text'] = sentence_input
|
55 |
+
|
56 |
+
# 4. Crear columnas para el botón
|
57 |
+
col1, col2, col3 = st.columns([2,1,2])
|
|
|
|
|
58 |
|
59 |
+
# 5. Botón de análisis a la izquierda.
|
60 |
+
with col1:
|
61 |
+
analyze_button = st.button(
|
62 |
+
morpho_t.get('morpho_analyze_button', 'Analyze Morphosyntax'),
|
63 |
+
key=f"morpho_button_{st.session_state.morphosyntax_state['analysis_counter']}",
|
64 |
+
use_container_width=True
|
65 |
+
)
|
66 |
+
|
67 |
+
# 6. Lógica de análisis
|
68 |
+
if analyze_button and sentence_input.strip(): # Verificar que haya texto y no solo espacios
|
69 |
+
try:
|
70 |
+
with st.spinner(morpho_t.get('processing', 'Processing...')):
|
71 |
+
# Procesar el texto
|
72 |
+
doc = nlp_models[lang_code](sentence_input)
|
73 |
+
|
74 |
+
# Realizar análisis morfosintáctico
|
75 |
+
advanced_analysis = perform_advanced_morphosyntactic_analysis(
|
76 |
+
sentence_input,
|
77 |
+
nlp_models[lang_code]
|
78 |
+
)
|
79 |
+
|
80 |
+
# Guardar resultado en el estado de la sesión
|
81 |
+
st.session_state.morphosyntax_result = {
|
82 |
+
'doc': doc,
|
83 |
+
'advanced_analysis': advanced_analysis
|
84 |
+
}
|
85 |
+
|
86 |
+
# Incrementar el contador de análisis
|
87 |
+
st.session_state.morphosyntax_state['analysis_counter'] += 1
|
88 |
+
|
89 |
+
# Guardar el análisis en la base de datos
|
90 |
+
if store_student_morphosyntax_result(
|
91 |
+
username=st.session_state.username,
|
92 |
+
text=sentence_input,
|
93 |
+
arc_diagrams=advanced_analysis['arc_diagrams']
|
94 |
+
):
|
95 |
+
st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
# Mostrar resultados
|
98 |
display_morphosyntax_results(
|
99 |
st.session_state.morphosyntax_result,
|
100 |
lang_code,
|
101 |
morpho_t
|
102 |
)
|
103 |
+
else:
|
104 |
+
st.error(morpho_t.get('error_message', 'Error saving analysis'))
|
105 |
+
|
106 |
+
except Exception as e:
|
107 |
+
logger.error(f"Error en análisis morfosintáctico: {str(e)}")
|
108 |
+
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
# 7. Mostrar resultados previos si existen
|
111 |
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
|
112 |
display_morphosyntax_results(
|
113 |
st.session_state.morphosyntax_result,
|
114 |
lang_code,
|
115 |
morpho_t
|
116 |
)
|
117 |
+
elif not sentence_input.strip():
|
118 |
st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
|
119 |
|
120 |
except Exception as e:
|