Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -28,104 +28,218 @@ from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
|
28 |
import logging
|
29 |
logger = logging.getLogger(__name__)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
33 |
try:
|
34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
if 'morphosyntax_state' not in st.session_state:
|
36 |
st.session_state.morphosyntax_state = {
|
37 |
-
'
|
38 |
-
'
|
39 |
-
'
|
40 |
-
'
|
|
|
|
|
41 |
}
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
# Recuperar el texto anterior si existe
|
52 |
-
default_text = st.session_state.morphosyntax_state.get('current_text', '')
|
53 |
-
|
54 |
-
sentence_input = st.text_area(
|
55 |
-
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
56 |
-
value=default_text, # Usar el texto guardado
|
57 |
-
height=150,
|
58 |
-
key=text_key,
|
59 |
-
on_change=on_text_change,
|
60 |
-
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...')
|
61 |
-
)
|
62 |
-
|
63 |
-
# Bot贸n de an谩lisis
|
64 |
-
col1, col2, col3 = st.columns([2,1,2])
|
65 |
-
with col1:
|
66 |
-
analyze_button = st.button(
|
67 |
-
morpho_t.get('morpho_analyze_button', 'Analyze Morphosyntax'),
|
68 |
-
key=f"morpho_button_{st.session_state.morphosyntax_state['analysis_count']}",
|
69 |
-
type="primary",
|
70 |
-
icon="馃攳",
|
71 |
-
disabled=not bool(sentence_input.strip()),
|
72 |
-
use_container_width=True
|
73 |
)
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
82 |
nlp_models[lang_code]
|
83 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
'doc': doc,
|
87 |
-
'advanced_analysis':
|
88 |
}
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
# Mostrar
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
display_morphosyntax_results(
|
105 |
-
|
106 |
lang_code,
|
107 |
morpho_t
|
108 |
)
|
109 |
-
|
110 |
-
except Exception as e:
|
111 |
-
logger.error(f"Error en an谩lisis morfosint谩ctico: {str(e)}")
|
112 |
-
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
113 |
-
|
114 |
-
# Mostrar resultados previos si existen
|
115 |
-
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result:
|
116 |
-
with st.container():
|
117 |
-
display_morphosyntax_results(
|
118 |
-
st.session_state.morphosyntax_result,
|
119 |
-
lang_code,
|
120 |
-
morpho_t
|
121 |
-
)
|
122 |
|
123 |
except Exception as e:
|
124 |
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
|
125 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
126 |
|
|
|
|
|
|
|
127 |
|
128 |
|
|
|
129 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
130 |
"""
|
131 |
Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
|
|
|
28 |
import logging
|
29 |
logger = logging.getLogger(__name__)
|
30 |
|
31 |
+
###########################################################################
|
32 |
+
|
33 |
+
import streamlit as st
|
34 |
+
from streamlit_float import *
|
35 |
+
from streamlit_antd_components import *
|
36 |
+
from streamlit.components.v1 import html
|
37 |
+
import spacy
|
38 |
+
from spacy import displacy
|
39 |
+
import spacy_streamlit
|
40 |
+
import pandas as pd
|
41 |
+
import base64
|
42 |
+
import re
|
43 |
|
44 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
45 |
try:
|
46 |
+
# CSS para mejorar la estabilidad y dise帽o
|
47 |
+
st.markdown("""
|
48 |
+
<style>
|
49 |
+
.stTextArea textarea {
|
50 |
+
font-size: 1rem;
|
51 |
+
line-height: 1.5;
|
52 |
+
padding: 0.5rem;
|
53 |
+
border-radius: 0.375rem;
|
54 |
+
border: 1px solid #e2e8f0;
|
55 |
+
background-color: white;
|
56 |
+
}
|
57 |
+
.original-content {
|
58 |
+
background-color: #f8fafc;
|
59 |
+
padding: 1rem;
|
60 |
+
border-radius: 0.5rem;
|
61 |
+
margin-bottom: 1rem;
|
62 |
+
}
|
63 |
+
.iteration-content {
|
64 |
+
background-color: white;
|
65 |
+
padding: 1rem;
|
66 |
+
border-radius: 0.5rem;
|
67 |
+
border: 1px solid #e2e8f0;
|
68 |
+
}
|
69 |
+
.arc-diagram-container {
|
70 |
+
background-color: white;
|
71 |
+
padding: 1rem;
|
72 |
+
border-radius: 0.5rem;
|
73 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
74 |
+
margin-top: 1rem;
|
75 |
+
}
|
76 |
+
</style>
|
77 |
+
""", unsafe_allow_html=True)
|
78 |
+
|
79 |
+
# Inicializar el estado
|
80 |
if 'morphosyntax_state' not in st.session_state:
|
81 |
st.session_state.morphosyntax_state = {
|
82 |
+
'has_original': False,
|
83 |
+
'original_text': '',
|
84 |
+
'original_analysis': None,
|
85 |
+
'current_text': '',
|
86 |
+
'iterations': [],
|
87 |
+
'analysis_count': 0
|
88 |
}
|
89 |
|
90 |
+
# Texto original (si a煤n no se ha ingresado)
|
91 |
+
if not st.session_state.morphosyntax_state['has_original']:
|
92 |
+
st.markdown("### Original Text")
|
93 |
+
original_text = st.text_area(
|
94 |
+
"Enter your initial text",
|
95 |
+
key="original_text_input",
|
96 |
+
height=150
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
)
|
98 |
+
|
99 |
+
col1, col2, col3 = st.columns([2,1,2])
|
100 |
+
with col1:
|
101 |
+
if st.button(
|
102 |
+
"Set as Original Text",
|
103 |
+
type="primary",
|
104 |
+
use_container_width=True,
|
105 |
+
disabled=not bool(original_text.strip())
|
106 |
+
):
|
107 |
+
# Procesar texto original
|
108 |
+
doc = nlp_models[lang_code](original_text)
|
109 |
+
analysis = perform_advanced_morphosyntactic_analysis(
|
110 |
+
original_text,
|
111 |
nlp_models[lang_code]
|
112 |
)
|
113 |
+
|
114 |
+
# Guardar estado original
|
115 |
+
st.session_state.morphosyntax_state.update({
|
116 |
+
'has_original': True,
|
117 |
+
'original_text': original_text,
|
118 |
+
'original_analysis': {
|
119 |
+
'doc': doc,
|
120 |
+
'advanced_analysis': analysis
|
121 |
+
}
|
122 |
+
})
|
123 |
+
|
124 |
+
# Guardar en base de datos
|
125 |
+
store_student_morphosyntax_result(
|
126 |
+
username=st.session_state.username,
|
127 |
+
text=original_text,
|
128 |
+
arc_diagrams=analysis['arc_diagrams']
|
129 |
+
)
|
130 |
+
st.rerun()
|
131 |
|
132 |
+
# Mostrar contenido original y 谩rea de iteraci贸n
|
133 |
+
else:
|
134 |
+
# Secci贸n Original
|
135 |
+
st.markdown("### Original Version")
|
136 |
+
with st.container():
|
137 |
+
col1, col2 = st.columns([1,1])
|
138 |
+
|
139 |
+
with col1:
|
140 |
+
st.markdown("#### Original Text")
|
141 |
+
st.markdown(f"<div class='original-content'>{st.session_state.morphosyntax_state['original_text']}</div>",
|
142 |
+
unsafe_allow_html=True)
|
143 |
+
|
144 |
+
with col2:
|
145 |
+
st.markdown("#### Original Analysis")
|
146 |
+
display_morphosyntax_results(
|
147 |
+
st.session_state.morphosyntax_state['original_analysis'],
|
148 |
+
lang_code,
|
149 |
+
morpho_t
|
150 |
+
)
|
151 |
+
|
152 |
+
# Separador
|
153 |
+
st.markdown("---")
|
154 |
+
|
155 |
+
# Secci贸n de Iteraci贸n
|
156 |
+
st.markdown("### Current Iteration")
|
157 |
+
|
158 |
+
# Campo para la nueva versi贸n
|
159 |
+
iteration_text = st.text_area(
|
160 |
+
"Modify your text",
|
161 |
+
value=st.session_state.morphosyntax_state.get('current_text',
|
162 |
+
st.session_state.morphosyntax_state['original_text']),
|
163 |
+
key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}",
|
164 |
+
height=150
|
165 |
+
)
|
166 |
+
|
167 |
+
# Bot贸n de an谩lisis
|
168 |
+
col1, col2, col3 = st.columns([2,1,2])
|
169 |
+
with col1:
|
170 |
+
analyze_button = st.button(
|
171 |
+
"Analyze Changes",
|
172 |
+
type="primary",
|
173 |
+
icon="馃攳",
|
174 |
+
key=f"analyze_{st.session_state.morphosyntax_state['analysis_count']}",
|
175 |
+
disabled=not bool(iteration_text.strip()),
|
176 |
+
use_container_width=True
|
177 |
+
)
|
178 |
+
|
179 |
+
# Procesar nueva iteraci贸n
|
180 |
+
if analyze_button and iteration_text.strip():
|
181 |
+
with st.spinner("Processing changes..."):
|
182 |
+
# Realizar an谩lisis
|
183 |
+
doc = nlp_models[lang_code](iteration_text)
|
184 |
+
analysis = perform_advanced_morphosyntactic_analysis(
|
185 |
+
iteration_text,
|
186 |
+
nlp_models[lang_code]
|
187 |
+
)
|
188 |
+
|
189 |
+
# Actualizar estado
|
190 |
+
current_analysis = {
|
191 |
'doc': doc,
|
192 |
+
'advanced_analysis': analysis
|
193 |
}
|
194 |
|
195 |
+
# Guardar iteraci贸n
|
196 |
+
st.session_state.morphosyntax_state['iterations'].append({
|
197 |
+
'text': iteration_text,
|
198 |
+
'analysis': current_analysis,
|
199 |
+
'timestamp': pd.Timestamp.now()
|
200 |
+
})
|
201 |
+
|
202 |
+
st.session_state.morphosyntax_state['current_text'] = iteration_text
|
203 |
+
st.session_state.morphosyntax_state['analysis_count'] += 1
|
204 |
|
205 |
+
# Guardar en base de datos
|
206 |
+
store_student_morphosyntax_result(
|
207 |
+
username=st.session_state.username,
|
208 |
+
text=iteration_text,
|
209 |
+
arc_diagrams=analysis['arc_diagrams']
|
210 |
+
)
|
211 |
|
212 |
+
# Mostrar an谩lisis actual
|
213 |
+
st.markdown("#### Current Analysis")
|
214 |
+
display_morphosyntax_results(
|
215 |
+
current_analysis,
|
216 |
+
lang_code,
|
217 |
+
morpho_t
|
218 |
+
)
|
219 |
+
|
220 |
+
# Mostrar historial de iteraciones
|
221 |
+
if st.session_state.morphosyntax_state['iterations']:
|
222 |
+
with st.expander("View Iteration History"):
|
223 |
+
for idx, iteration in enumerate(reversed(st.session_state.morphosyntax_state['iterations']), 1):
|
224 |
+
st.markdown(f"**Iteration {idx} - {iteration['timestamp'].strftime('%H:%M:%S')}**")
|
225 |
+
st.markdown(f"```\n{iteration['text']}\n```")
|
226 |
display_morphosyntax_results(
|
227 |
+
iteration['analysis'],
|
228 |
lang_code,
|
229 |
morpho_t
|
230 |
)
|
231 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
except Exception as e:
|
234 |
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
|
235 |
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
236 |
|
237 |
+
# El resto del c贸digo permanece igual...
|
238 |
+
|
239 |
+
|
240 |
|
241 |
|
242 |
+
#########################################################################3
|
243 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
244 |
"""
|
245 |
Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
|