File size: 7,171 Bytes
55c034b 831e193 b6ee9f7 831e193 72b2a4f 1ee3f25 1966cb7 1910fa3 831e193 1910fa3 72b2a4f 87370f4 72b2a4f 87370f4 40c546c 72b2a4f 1910fa3 40c546c 1910fa3 87370f4 1910fa3 87370f4 1910fa3 87370f4 1910fa3 87370f4 054d2c2 1910fa3 87370f4 1910fa3 9a2229f 1910fa3 87370f4 40c546c 1910fa3 87370f4 1910fa3 87370f4 1910fa3 f0d8559 1910fa3 9a2229f f0d8559 1910fa3 87370f4 1910fa3 87370f4 831e193 1910fa3 ebc275b 407e78c 9a2229f 407e78c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
#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 consistente con keys 煤nicas
if 'morpho_state' not in st.session_state:
st.session_state.morpho_state = {
'original_text': '',
'original_analysis': None,
'iteration_text': '',
'iteration_analysis': None,
'analysis_count': 0
}
# Texto original - Key 煤nica basada en timestamp
text_key = f"original_text_{st.session_state.morpho_state['analysis_count']}"
text_input = st.text_area(
"",
value=st.session_state.morpho_state['original_text'],
key=text_key,
height=100
)
# Bot贸n analizar original
col1, col2, col3 = st.columns([2,1,2])
with col1:
analyze_button = st.button(
"Analizar Texto Original",
type="primary",
use_container_width=True
)
# 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]
)
# Actualizar estado
st.session_state.morpho_state.update({
'original_text': text_input,
'original_analysis': {'doc': doc, 'analysis': analysis},
'iteration_text': text_input,
'analysis_count': st.session_state.morpho_state['analysis_count'] + 1
})
# Guardar en base de datos
if store_student_morphosyntax_result(
username=st.session_state.username,
text=text_input,
arc_diagrams=analysis['arc_diagrams']
):
# Mostrar diagrama original
display_morphosyntax_results({'doc': doc, 'analysis': analysis}, lang_code, morpho_t)
st.markdown("---")
# Campo para iteraci贸n
iteration_key = f"iteration_{st.session_state.morpho_state['analysis_count']}"
iteration_text = st.text_area(
"",
value=st.session_state.morpho_state['iteration_text'],
key=iteration_key,
height=100
)
# Bot贸n analizar iteraci贸n
col1, col2, col3 = st.columns([2,1,2])
with col1:
iterate_button = st.button(
"Analizar Cambios",
type="primary",
use_container_width=True
)
# Procesar iteraci贸n
if iterate_button and iteration_text.strip():
try:
doc = nlp_models[lang_code](iteration_text)
analysis = perform_advanced_morphosyntactic_analysis(
iteration_text,
nlp_models[lang_code]
)
# Actualizar estado
st.session_state.morpho_state.update({
'iteration_text': iteration_text,
'iteration_analysis': {'doc': doc, 'analysis': analysis}
})
# Guardar y mostrar
if store_student_morphosyntax_result(
username=st.session_state.username,
text=iteration_text,
arc_diagrams=analysis['arc_diagrams']
):
display_morphosyntax_results({'doc': doc, 'analysis': analysis}, lang_code, morpho_t)
except Exception as e:
st.error("Error al procesar iteraci贸n")
logger.error(f"Error: {str(e)}")
except Exception as e:
st.error("Error al procesar texto original")
logger.error(f"Error: {str(e)}")
except Exception as e:
st.error("Error general en la interfaz")
logger.error(f"Error: {str(e)}")
def display_morphosyntax_results(doc, morpho_t):
"""Muestra solo el diagrama de arco"""
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})
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)}")
|