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 layout estable | |
st.markdown(""" | |
<style> | |
.stTextArea textarea { | |
font-size: 1rem; | |
line-height: 1.5; | |
min-height: 100px !important; | |
height: 100px !important; | |
} | |
.arc-diagram-container { | |
width: 100%; | |
padding: 0.5rem; | |
margin: 0.5rem 0; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Estado para subtabs | |
if 'morpho_subtab' not in st.session_state: | |
st.session_state.morpho_subtab = 0 | |
# Crear subtabs | |
subtabs = st.tabs([ | |
"Análisis de Diagramas de Arco", | |
"Análisis de Categorías", | |
"Análisis Morfológico" | |
]) | |
# Tab de Diagramas de Arco | |
with subtabs[0]: | |
# Estado para análisis | |
if 'arc_analysis_state' not in st.session_state: | |
st.session_state.arc_analysis_state = { | |
'original_text': '', | |
'original_analysis': None, | |
'iteration_text': '', | |
'iteration_analysis': None, | |
'analysis_count': 0 | |
} | |
# Contenedor para texto original | |
original_container = st.container() | |
with original_container: | |
text_key = f"original_text_{st.session_state.arc_analysis_state['analysis_count']}" | |
text_input = st.text_area( | |
"", | |
value=st.session_state.arc_analysis_state['original_text'], | |
key=text_key, | |
height=100 | |
) | |
col1, col2, col3 = st.columns([2,1,2]) | |
with col1: | |
analyze_button = st.button( | |
"Analizar Texto Original", | |
type="primary", | |
use_container_width=True, | |
key=f"analyze_original_{st.session_state.arc_analysis_state['analysis_count']}" | |
) | |
# Contenedor para diagramas | |
diagrams_container = st.container() | |
# Procesar texto original | |
if analyze_button and text_input.strip(): | |
try: | |
doc = nlp_models[lang_code](text_input) | |
analysis = perform_advanced_morphosyntactic_analysis( | |
text_input, | |
nlp_models[lang_code] | |
) | |
st.session_state.arc_analysis_state.update({ | |
'original_text': text_input, | |
'original_analysis': {'doc': doc, 'analysis': analysis}, | |
'iteration_text': text_input, | |
'analysis_count': st.session_state.arc_analysis_state['analysis_count'] + 1 | |
}) | |
if store_student_morphosyntax_result( | |
username=st.session_state.username, | |
text=text_input, | |
arc_diagrams=analysis['arc_diagrams'] | |
): | |
with diagrams_container: | |
st.subheader("Análisis Original") | |
display_morphosyntax_results( | |
st.session_state.arc_analysis_state['original_analysis'], | |
lang_code, | |
morpho_t | |
) | |
except Exception as e: | |
st.error("Error al procesar texto original") | |
logger.error(f"Error: {str(e)}") | |
# Contenedor para iteración | |
if st.session_state.arc_analysis_state.get('original_analysis'): | |
iteration_container = st.container() | |
with iteration_container: | |
st.markdown("---") | |
iteration_key = f"iteration_{st.session_state.arc_analysis_state['analysis_count']}" | |
iteration_text = st.text_area( | |
"", | |
value=st.session_state.arc_analysis_state['iteration_text'], | |
key=iteration_key, | |
height=100, | |
on_change=lambda: None # Prevenir recarga en cambio de foco | |
) | |
col1, col2, col3 = st.columns([2,1,2]) | |
with col1: | |
iterate_button = st.button( | |
"Analizar Cambios", | |
type="primary", | |
use_container_width=True, | |
key=f"iterate_{st.session_state.arc_analysis_state['analysis_count']}" | |
) | |
if iterate_button and iteration_text.strip(): | |
try: | |
doc_iter = nlp_models[lang_code](iteration_text) | |
analysis_iter = perform_advanced_morphosyntactic_analysis( | |
iteration_text, | |
nlp_models[lang_code] | |
) | |
st.session_state.arc_analysis_state.update({ | |
'iteration_text': iteration_text, | |
'iteration_analysis': {'doc': doc_iter, 'analysis': analysis_iter} | |
}) | |
if store_student_morphosyntax_result( | |
username=st.session_state.username, | |
text=iteration_text, | |
arc_diagrams=analysis_iter['arc_diagrams'] | |
): | |
with diagrams_container: | |
# Mostrar original | |
st.subheader("Análisis Original") | |
display_morphosyntax_results( | |
st.session_state.arc_analysis_state['original_analysis'], | |
lang_code, | |
morpho_t | |
) | |
st.markdown("---") | |
# Mostrar iteración | |
st.subheader("Análisis de Cambios") | |
display_morphosyntax_results( | |
{'doc': doc_iter, 'analysis': analysis_iter}, | |
lang_code, | |
morpho_t | |
) | |
except Exception as e: | |
st.error("Error al procesar iteración") | |
logger.error(f"Error: {str(e)}") | |
# Otros subtabs aquí... | |
with subtabs[1]: | |
st.info("Análisis de Categorías en desarrollo...") | |
with subtabs[2]: | |
st.info("Análisis Morfológico en desarrollo...") | |
except Exception as e: | |
st.error("Error general en la interfaz") | |
logger.error(f"Error: {str(e)}") | |
############################################################################ | |
def display_morphosyntax_results(result, lang_code, morpho_t): | |
""" | |
Muestra solo el diagrama de arco. | |
Args: | |
result: Diccionario con el documento procesado y su análisis | |
lang_code: Código del idioma | |
morpho_t: Diccionario de traducciones | |
""" | |
if result is None: | |
return | |
try: | |
doc = result['doc'] | |
sentences = list(doc.sents) | |
for i, sent in enumerate(sentences): | |
try: | |
st.subheader(f"{morpho_t.get('sentence', 'Sentence')} {i+1}") | |
html = displacy.render(sent, style="dep", options={ | |
"distance": 100, | |
"arrow_spacing": 20, | |
"word_spacing": 30 | |
}) | |
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) | |
html = f'<div class="arc-diagram-container">{html}</div>' | |
st.write(html, unsafe_allow_html=True) | |
except Exception as e: | |
logger.error(f"Error en diagrama {i}: {str(e)}") | |
continue | |
except Exception as e: | |
logger.error(f"Error en display_morphosyntax_results: {str(e)}") | |