v4 / modules /morphosyntax /morphosyntax_interface.py
AIdeaText's picture
Update modules/morphosyntax/morphosyntax_interface.py
4361af6 verified
raw
history blame
12.8 kB
#modules/morphosyntax/morphosyntax_interface.py
import streamlit as st
from streamlit_float import *
from streamlit_antd_components import *
from streamlit.components.v1 import html
import spacy
from spacy import displacy
import spacy_streamlit
import pandas as pd
import base64
import re
from .morphosyntax_process import (
process_morphosyntactic_input,
format_analysis_results,
perform_advanced_morphosyntactic_analysis,
get_repeated_words_colors,
highlight_repeated_words,
POS_COLORS,
POS_TRANSLATIONS
)
from ..utils.widget_utils import generate_unique_key
from ..database.morphosintax_mongo_db import store_student_morphosyntax_result
from ..database.chat_mongo_db import store_chat_history, get_chat_history
import logging
logger = logging.getLogger(__name__)
###########################################################################
import streamlit as st
from streamlit_float import *
from streamlit_antd_components import *
from streamlit.components.v1 import html
import spacy
from spacy import displacy
import spacy_streamlit
import pandas as pd
import base64
import re
############################################################################
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
try:
# CSS mejorado para estabilidad y layout vertical
st.markdown("""
<style>
.stTextArea textarea {
font-size: 1rem;
line-height: 1.5;
padding: 0.5rem;
border-radius: 0.375rem;
border: 1px solid #e2e8f0;
background-color: white;
min-height: 100px !important;
height: 100px !important;
}
.block-container {
padding-top: 0.5rem !important;
padding-bottom: 0.5rem !important;
margin: 0 !important;
}
.main-content {
display: flex;
flex-direction: column;
gap: 1rem;
padding: 0.5rem;
}
.arc-diagram-container {
width: 100%;
overflow-x: auto;
background-color: white;
padding: 0.5rem;
border-radius: 0.375rem;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
margin-top: 0.5rem;
}
</style>
""", unsafe_allow_html=True)
# Inicializaci贸n m谩s robusta del estado
if 'morphosyntax_state' not in st.session_state:
st.session_state.morphosyntax_state = {
'original_text': '',
'current_text': '',
'original_analysis': None,
'analysis_count': 0,
'iterations': [] # Inicializaci贸n expl铆cita de iterations como lista vac铆a
}
else:
# Asegurar que todas las claves existan
required_keys = {
'original_text': '',
'current_text': '',
'original_analysis': None,
'analysis_count': 0,
'iterations': []
}
for key, default_value in required_keys.items():
if key not in st.session_state.morphosyntax_state:
st.session_state.morphosyntax_state[key] = default_value
with st.container():
# Secci贸n de texto original
st.markdown("### Texto Original")
# Input para texto original
original_text = st.text_area(
"Ingrese una oraci贸n",
value=st.session_state.morphosyntax_state['original_text'],
key="original_text_input",
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
height=100,
disabled=False
)
# Bot贸n para analizar texto original
col1, col2, col3 = st.columns([2,1,2])
with col1:
analyze_original = st.button(
"Analizar Texto Original",
type="primary",
use_container_width=True,
disabled=not bool(original_text.strip())
)
# Procesar texto original
if analyze_original and original_text.strip():
try:
with st.spinner("Procesando texto original..."):
doc = nlp_models[lang_code](original_text)
analysis = perform_advanced_morphosyntactic_analysis(
original_text,
nlp_models[lang_code]
)
# Actualizar estado de forma segura
st.session_state.morphosyntax_state.update({
'original_text': original_text,
'current_text': original_text,
'original_analysis': {
'doc': doc,
'advanced_analysis': analysis
},
'iterations': [] # Reiniciar iteraciones al cambiar texto original
})
# Guardar en base de datos
if store_student_morphosyntax_result(
username=st.session_state.username,
text=original_text,
arc_diagrams=analysis['arc_diagrams']
):
st.success("Texto original analizado exitosamente")
else:
st.error("Error al guardar el an谩lisis original")
except Exception as e:
logger.error(f"Error procesando texto original: {str(e)}")
st.error("Error al procesar el texto original")
# Mostrar diagrama original
if st.session_state.morphosyntax_state['original_analysis']:
display_morphosyntax_results(
st.session_state.morphosyntax_state['original_analysis'],
lang_code,
morpho_t
)
# Secci贸n de iteraci贸n
st.markdown("---")
st.markdown("### Iteraci贸n Actual")
# Campo para nueva versi贸n
iteration_text = st.text_area(
"Modifique la oraci贸n",
value=st.session_state.morphosyntax_state['current_text'],
key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}",
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
height=100
)
# Bot贸n para analizar iteraci贸n
col1, col2, col3 = st.columns([2,1,2])
with col1:
analyze_iteration = st.button(
"Analizar Cambios",
type="primary",
icon="馃攳",
key=f"analyze_{st.session_state.morphosyntax_state['analysis_count']}",
disabled=not bool(iteration_text.strip()),
use_container_width=True
)
# Procesar iteraci贸n
if analyze_iteration and iteration_text.strip():
try:
with st.spinner("Procesando cambios..."):
doc = nlp_models[lang_code](iteration_text)
analysis = perform_advanced_morphosyntactic_analysis(
iteration_text,
nlp_models[lang_code]
)
current_analysis = {
'doc': doc,
'advanced_analysis': analysis
}
# Crear nueva iteraci贸n
new_iteration = {
'text': iteration_text,
'analysis': current_analysis,
'timestamp': pd.Timestamp.now()
}
# Actualizar estado de forma segura
iterations = st.session_state.morphosyntax_state.get('iterations', [])
iterations.append(new_iteration)
st.session_state.morphosyntax_state.update({
'current_text': iteration_text,
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1,
'iterations': iterations
})
if store_student_morphosyntax_result(
username=st.session_state.username,
text=iteration_text,
arc_diagrams=analysis['arc_diagrams']
):
# Mostrar resultados de la iteraci贸n
display_morphosyntax_results(
current_analysis,
lang_code,
morpho_t
)
else:
st.error("Error al guardar la iteraci贸n")
except Exception as e:
logger.error(f"Error procesando iteraci贸n: {str(e)}")
st.error("Error al procesar los cambios")
# Mostrar historial de iteraciones
if st.session_state.morphosyntax_state.get('iterations', []):
with st.expander("Historial de Iteraciones", expanded=False):
for idx, iteration in enumerate(reversed(st.session_state.morphosyntax_state['iterations'])):
st.markdown(f"**Iteraci贸n {idx + 1} ({iteration['timestamp'].strftime('%H:%M:%S')})**")
st.text_area(
f"Texto {idx + 1}",
value=iteration['text'],
disabled=True,
height=100,
key=f"hist_text_{idx}"
)
display_morphosyntax_results(
iteration['analysis'],
lang_code,
morpho_t
)
st.markdown("---")
except Exception as e:
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
st.error("Se produjo un error. Por favor, intente de nuevo.")
#########################################################################3
def display_morphosyntax_results(result, lang_code, morpho_t):
"""
Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
"""
if result is None:
st.warning(morpho_t.get('no_results', 'No results available'))
return
doc = result['doc']
# An谩lisis sint谩ctico (diagramas de arco)
st.markdown(f"### {morpho_t.get('arc_diagram', 'Syntactic analysis: Arc diagram')}")
with st.container():
sentences = list(doc.sents)
for i, sent in enumerate(sentences):
with st.container():
st.subheader(f"{morpho_t.get('sentence', 'Sentence')} {i+1}")
try:
html = displacy.render(sent, style="dep", options={
"distance": 100,
"arrow_spacing": 20,
"word_spacing": 30
})
# Ajustar dimensiones del SVG
html = html.replace('height="375"', 'height="200"')
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"',
lambda m: f'<g transform="translate({m.group(1)},50)"', html)
# Envolver en un div con clase para estilos
html = f'<div class="arc-diagram-container">{html}</div>'
st.write(html, unsafe_allow_html=True)
except Exception as e:
logger.error(f"Error rendering sentence {i}: {str(e)}")
st.error(f"Error displaying diagram for sentence {i+1}")