Spaces:
Sleeping
Sleeping
Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
|
@@ -26,52 +26,35 @@ from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
|
| 26 |
import logging
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
|
|
|
| 29 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
| 30 |
try:
|
| 31 |
-
#
|
| 32 |
-
st.markdown("""
|
| 33 |
-
<style>
|
| 34 |
-
.stTextArea textarea {
|
| 35 |
-
font-size: 1rem;
|
| 36 |
-
line-height: 1.5;
|
| 37 |
-
padding: 0.5rem;
|
| 38 |
-
border-radius: 0.375rem;
|
| 39 |
-
border: 1px solid #e2e8f0;
|
| 40 |
-
background-color: white;
|
| 41 |
-
}
|
| 42 |
-
.stTextArea textarea:focus {
|
| 43 |
-
border-color: #3182ce;
|
| 44 |
-
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
|
| 45 |
-
}
|
| 46 |
-
.block-container {
|
| 47 |
-
padding-top: 1rem;
|
| 48 |
-
padding-bottom: 1rem;
|
| 49 |
-
}
|
| 50 |
-
.arc-diagram-container {
|
| 51 |
-
background-color: white;
|
| 52 |
-
padding: 1rem;
|
| 53 |
-
border-radius: 0.5rem;
|
| 54 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 55 |
-
margin-top: 1rem;
|
| 56 |
-
}
|
| 57 |
-
</style>
|
| 58 |
-
""", unsafe_allow_html=True)
|
| 59 |
-
|
| 60 |
-
# Inicializar el estado
|
| 61 |
if 'morphosyntax_state' not in st.session_state:
|
| 62 |
st.session_state.morphosyntax_state = {
|
| 63 |
'analysis_count': 0,
|
|
|
|
| 64 |
'last_analysis': None,
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Campo de entrada de texto
|
| 71 |
sentence_input = st.text_area(
|
| 72 |
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
|
|
|
| 73 |
height=150,
|
| 74 |
-
key=
|
|
|
|
| 75 |
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...')
|
| 76 |
)
|
| 77 |
|
|
@@ -87,59 +70,60 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
|
| 87 |
use_container_width=True
|
| 88 |
)
|
| 89 |
|
| 90 |
-
# Procesar análisis
|
| 91 |
-
if analyze_button and sentence_input.strip():
|
| 92 |
try:
|
| 93 |
with st.spinner(morpho_t.get('processing', 'Processing...')):
|
| 94 |
-
# Procesar el texto
|
| 95 |
doc = nlp_models[lang_code](sentence_input)
|
| 96 |
-
|
| 97 |
-
# Realizar análisis
|
| 98 |
advanced_analysis = perform_advanced_morphosyntactic_analysis(
|
| 99 |
sentence_input,
|
| 100 |
nlp_models[lang_code]
|
| 101 |
)
|
| 102 |
|
| 103 |
-
# Guardar resultado en el estado
|
| 104 |
st.session_state.morphosyntax_result = {
|
| 105 |
'doc': doc,
|
| 106 |
'advanced_analysis': advanced_analysis
|
| 107 |
}
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
display_morphosyntax_results(
|
| 120 |
st.session_state.morphosyntax_result,
|
| 121 |
lang_code,
|
| 122 |
morpho_t
|
| 123 |
)
|
| 124 |
-
else:
|
| 125 |
-
st.error(morpho_t.get('error_message', 'Error saving analysis'))
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
logger.error(f"Error en análisis morfosintáctico: {str(e)}")
|
| 129 |
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 130 |
-
|
| 131 |
-
# Mostrar resultados previos
|
| 132 |
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
|
| 141 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 142 |
|
|
|
|
|
|
|
| 143 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
| 144 |
"""
|
| 145 |
Muestra solo el análisis sintáctico con diagramas de arco.
|
|
|
|
| 26 |
import logging
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
+
|
| 30 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
| 31 |
try:
|
| 32 |
+
# Inicializar el estado si no existe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if 'morphosyntax_state' not in st.session_state:
|
| 34 |
st.session_state.morphosyntax_state = {
|
| 35 |
'analysis_count': 0,
|
| 36 |
+
'current_text': '', # Almacenar el texto actual
|
| 37 |
'last_analysis': None,
|
| 38 |
+
'needs_update': False # Flag para actualización
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# Campo de entrada de texto que mantiene su valor
|
| 42 |
+
text_key = "morpho_text_input"
|
| 43 |
+
|
| 44 |
+
# Función para manejar cambios en el texto
|
| 45 |
+
def on_text_change():
|
| 46 |
+
st.session_state.morphosyntax_state['current_text'] = st.session_state[text_key]
|
| 47 |
+
st.session_state.morphosyntax_state['needs_update'] = True
|
| 48 |
+
|
| 49 |
+
# Recuperar el texto anterior si existe
|
| 50 |
+
default_text = st.session_state.morphosyntax_state.get('current_text', '')
|
| 51 |
|
|
|
|
| 52 |
sentence_input = st.text_area(
|
| 53 |
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
| 54 |
+
value=default_text, # Usar el texto guardado
|
| 55 |
height=150,
|
| 56 |
+
key=text_key,
|
| 57 |
+
on_change=on_text_change,
|
| 58 |
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...')
|
| 59 |
)
|
| 60 |
|
|
|
|
| 70 |
use_container_width=True
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Procesar análisis solo cuando sea necesario
|
| 74 |
+
if (analyze_button or st.session_state.morphosyntax_state['needs_update']) and sentence_input.strip():
|
| 75 |
try:
|
| 76 |
with st.spinner(morpho_t.get('processing', 'Processing...')):
|
|
|
|
| 77 |
doc = nlp_models[lang_code](sentence_input)
|
|
|
|
|
|
|
| 78 |
advanced_analysis = perform_advanced_morphosyntactic_analysis(
|
| 79 |
sentence_input,
|
| 80 |
nlp_models[lang_code]
|
| 81 |
)
|
| 82 |
|
|
|
|
| 83 |
st.session_state.morphosyntax_result = {
|
| 84 |
'doc': doc,
|
| 85 |
'advanced_analysis': advanced_analysis
|
| 86 |
}
|
| 87 |
+
|
| 88 |
+
# Solo guardar en DB si fue un click en el botón
|
| 89 |
+
if analyze_button:
|
| 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 |
+
st.session_state.morphosyntax_state['analysis_count'] += 1
|
| 97 |
+
|
| 98 |
+
st.session_state.morphosyntax_state['needs_update'] = False
|
| 99 |
+
|
| 100 |
+
# Mostrar resultados en un contenedor específico
|
| 101 |
+
with st.container():
|
| 102 |
display_morphosyntax_results(
|
| 103 |
st.session_state.morphosyntax_result,
|
| 104 |
lang_code,
|
| 105 |
morpho_t
|
| 106 |
)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
logger.error(f"Error en análisis morfosintáctico: {str(e)}")
|
| 110 |
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 111 |
+
|
| 112 |
+
# Mostrar resultados previos si existen
|
| 113 |
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result:
|
| 114 |
+
with st.container():
|
| 115 |
+
display_morphosyntax_results(
|
| 116 |
+
st.session_state.morphosyntax_result,
|
| 117 |
+
lang_code,
|
| 118 |
+
morpho_t
|
| 119 |
+
)
|
| 120 |
|
| 121 |
except Exception as e:
|
| 122 |
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
|
| 123 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
| 124 |
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
| 128 |
"""
|
| 129 |
Muestra solo el análisis sintáctico con diagramas de arco.
|