Spaces:
Running
Running
#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 para prevenir saltos y mejorar estabilidad | |
st.markdown(""" | |
<style> | |
.element-container:has(.stTextArea) { | |
position: sticky !important; | |
z-index: 1; | |
} | |
.diagram-container { | |
position: relative; | |
z-index: 0; | |
} | |
[data-testid="stVerticalBlock"] { | |
gap: 0rem !important; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Controlar estado de la p谩gina | |
if 'active_tab' not in st.session_state: | |
st.session_state.active_tab = 'morphosyntax' | |
# Manejar cambios de texto sin recargar | |
def handle_text_change(): | |
text = st.session_state.get(st.session_state.text_key, '') | |
st.session_state.current_text = text | |
# No provocar rerun aqu铆 | |
# Secci贸n de texto original | |
with st.container(): | |
st.markdown("### Texto Original") | |
# Clave 煤nica para el texto | |
st.session_state.text_key = f"text_input_{st.session_state.get('analysis_count', 0)}" | |
original_text = st.text_area( | |
"Ingrese una oraci贸n", | |
value=st.session_state.get('current_text', ''), | |
key=st.session_state.text_key, | |
on_change=handle_text_change, | |
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.", | |
height=100 | |
) | |
col1, col2, col3 = st.columns([2,1,2]) | |
with col1: | |
if st.button( | |
"Analizar Texto Original", | |
use_container_width=True, | |
disabled=not bool(original_text.strip()) | |
): | |
with st.spinner("Analizando..."): | |
try: | |
doc = nlp_models[lang_code](original_text) | |
analysis = perform_advanced_morphosyntactic_analysis( | |
original_text, | |
nlp_models[lang_code] | |
) | |
# Actualizar solo el diagrama | |
with st.container(): | |
st.markdown("### Diagrama Original") | |
display_morphosyntax_results( | |
{'doc': doc, 'analysis': analysis}, | |
lang_code, | |
morpho_t | |
) | |
# Guardar estado sin recargar | |
st.session_state.original_analysis = { | |
'doc': doc, | |
'analysis': analysis | |
} | |
st.session_state.current_text = original_text | |
if store_student_morphosyntax_result( | |
username=st.session_state.username, | |
text=original_text, | |
arc_diagrams=analysis['arc_diagrams'] | |
): | |
st.success("An谩lisis guardado") | |
except Exception as e: | |
logger.error(f"Error en an谩lisis: {str(e)}") | |
st.error("Error al procesar el texto") | |
# Secci贸n de iteraciones | |
if 'original_analysis' in st.session_state: | |
st.markdown("---") | |
st.markdown("### Iteraci贸n Actual") | |
iteration_key = f"iteration_{st.session_state.get('iteration_count', 0)}" | |
iteration_text = st.text_area( | |
"Modifique la oraci贸n", | |
value=st.session_state.get('current_text', ''), | |
key=iteration_key, | |
height=100, | |
placeholder="Modifique el texto para mejorar la estructura" | |
) | |
col1, col2, col3 = st.columns([2,1,2]) | |
with col1: | |
if st.button( | |
"Analizar Cambios", | |
use_container_width=True, | |
disabled=not bool(iteration_text.strip()) | |
): | |
with st.spinner("Procesando cambios..."): | |
try: | |
doc = nlp_models[lang_code](iteration_text) | |
analysis = perform_advanced_morphosyntactic_analysis( | |
iteration_text, | |
nlp_models[lang_code] | |
) | |
# Actualizar solo el diagrama de iteraci贸n | |
with st.container(): | |
st.markdown("### Diagrama de Iteraci贸n") | |
display_morphosyntax_results( | |
{'doc': doc, 'analysis': analysis}, | |
lang_code, | |
morpho_t | |
) | |
# Guardar iteraci贸n | |
if 'iterations' not in st.session_state: | |
st.session_state.iterations = [] | |
st.session_state.iterations.append({ | |
'text': iteration_text, | |
'analysis': {'doc': doc, 'analysis': analysis}, | |
'timestamp': pd.Timestamp.now() | |
}) | |
st.session_state.current_text = iteration_text | |
st.session_state.iteration_count = len(st.session_state.iterations) | |
if store_student_morphosyntax_result( | |
username=st.session_state.username, | |
text=iteration_text, | |
arc_diagrams=analysis['arc_diagrams'] | |
): | |
st.success("Iteraci贸n guardada") | |
except Exception as e: | |
logger.error(f"Error en iteraci贸n: {str(e)}") | |
st.error("Error al procesar los cambios") | |
# Historial de iteraciones | |
if st.session_state.get('iterations'): | |
with st.expander("Historial de Iteraciones", expanded=False): | |
for idx, iteration in enumerate(reversed(st.session_state.iterations)): | |
st.text_area( | |
f"Texto de iteraci贸n {idx + 1}", | |
value=iteration['text'], | |
disabled=True, | |
height=100 | |
) | |
display_morphosyntax_results( | |
iteration['analysis'], | |
lang_code, | |
morpho_t | |
) | |
st.markdown("---") | |
except Exception as e: | |
logger.error(f"Error general: {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}") |