|
|
|
|
|
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: |
|
|
|
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; |
|
} |
|
.arc-diagram-container { |
|
width: 100%; |
|
overflow-x: auto; |
|
background-color: white; |
|
padding: 0.5rem; |
|
border-radius: 0.375rem; |
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1); |
|
margin: 0.5rem 0; |
|
} |
|
.stTab { |
|
position: sticky; |
|
top: 0; |
|
z-index: 999; |
|
} |
|
[data-testid="stVerticalBlock"] { |
|
gap: 0 !important; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
if 'current_tab' not in st.session_state: |
|
st.session_state.current_tab = "morphosyntax" |
|
|
|
if 'morphosyntax_state' not in st.session_state: |
|
st.session_state.morphosyntax_state = {} |
|
|
|
default_state = { |
|
'original_text': '', |
|
'original_analysis': None, |
|
'current_text': '', |
|
'current_analysis': None, |
|
'analysis_count': 0, |
|
'show_original': False, |
|
'show_iteration': False |
|
} |
|
|
|
for key, default_value in default_state.items(): |
|
if key not in st.session_state.morphosyntax_state: |
|
st.session_state.morphosyntax_state[key] = default_value |
|
|
|
def update_analysis(): |
|
st.session_state.current_tab = "morphosyntax" |
|
|
|
|
|
st.text_area( |
|
"", |
|
value=st.session_state.morphosyntax_state['original_text'], |
|
key="original_text_input", |
|
placeholder="Ingresar solo una oraci贸n hasta el punto y aparte.", |
|
height=100, |
|
on_change=update_analysis |
|
) |
|
|
|
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(st.session_state.get("original_text_input", "").strip()) |
|
) |
|
|
|
|
|
if analyze_original and st.session_state.get("original_text_input", "").strip(): |
|
try: |
|
with st.spinner(""): |
|
doc = nlp_models[lang_code](st.session_state.original_text_input) |
|
analysis = perform_advanced_morphosyntactic_analysis( |
|
st.session_state.original_text_input, |
|
nlp_models[lang_code] |
|
) |
|
|
|
st.session_state.morphosyntax_state.update({ |
|
'original_text': st.session_state.original_text_input, |
|
'original_analysis': {'doc': doc, 'analysis': analysis}, |
|
'current_text': st.session_state.original_text_input, |
|
'show_original': True, |
|
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1 |
|
}) |
|
|
|
if store_student_morphosyntax_result( |
|
username=st.session_state.username, |
|
text=st.session_state.original_text_input, |
|
arc_diagrams=analysis['arc_diagrams'] |
|
): |
|
st.session_state.current_tab = "morphosyntax" |
|
|
|
except Exception as e: |
|
logger.error(f"Error en an谩lisis original: {str(e)}") |
|
st.error("Error al procesar el texto") |
|
|
|
|
|
if st.session_state.morphosyntax_state.get('show_original'): |
|
display_morphosyntax_results( |
|
st.session_state.morphosyntax_state['original_analysis'], |
|
lang_code, |
|
morpho_t |
|
) |
|
|
|
st.markdown("---") |
|
|
|
|
|
iteration_text = st.text_area( |
|
"", |
|
value=st.session_state.morphosyntax_state['current_text'], |
|
key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}", |
|
placeholder="Modifique el texto para mejorar la estructura", |
|
height=100, |
|
on_change=update_analysis |
|
) |
|
|
|
col1, col2, col3 = st.columns([2,1,2]) |
|
with col1: |
|
analyze_iteration = st.button( |
|
"Analizar Cambios", |
|
type="primary", |
|
use_container_width=True, |
|
disabled=not bool(iteration_text.strip()) |
|
) |
|
|
|
|
|
if analyze_iteration and iteration_text.strip(): |
|
try: |
|
with st.spinner(""): |
|
doc = nlp_models[lang_code](iteration_text) |
|
analysis = perform_advanced_morphosyntactic_analysis( |
|
iteration_text, |
|
nlp_models[lang_code] |
|
) |
|
|
|
st.session_state.morphosyntax_state.update({ |
|
'current_text': iteration_text, |
|
'current_analysis': {'doc': doc, 'analysis': analysis}, |
|
'show_iteration': True, |
|
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1 |
|
}) |
|
|
|
if store_student_morphosyntax_result( |
|
username=st.session_state.username, |
|
text=iteration_text, |
|
arc_diagrams=analysis['arc_diagrams'] |
|
): |
|
|
|
display_morphosyntax_results( |
|
st.session_state.morphosyntax_state['current_analysis'], |
|
lang_code, |
|
morpho_t |
|
) |
|
|
|
except Exception as e: |
|
logger.error(f"Error en iteraci贸n: {str(e)}") |
|
st.error("Error al procesar los cambios") |
|
|
|
except Exception as e: |
|
logger.error(f"Error general: {str(e)}") |
|
st.error("Se produjo un error. Por favor, intente de nuevo.") |
|
|
|
|
|
|
|
|
|
|
|
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'] |
|
|
|
|
|
|
|
|
|
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 |
|
}) |
|
|
|
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 rendering sentence {i}: {str(e)}") |
|
st.error(f"Error displaying diagram for sentence {i+1}") |