AIdeaText commited on
Commit
38a195b
verified
1 Parent(s): 69ed6a9

Create morphosyntax_interface_vOk-30-12-24.py

Browse files
modules/morphosyntax/morphosyntax_interface_vOk-30-12-24.py ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modules/morphosyntax/morphosyntax_interface.py
2
+
3
+ import streamlit as st
4
+ import re
5
+ import logging
6
+ from spacy import displacy
7
+
8
+ # Se asume que la funci贸n perform_advanced_morphosyntactic_analysis
9
+ # y los m茅todos store_student_morphosyntax_base/iteration existen.
10
+ from ..morphosyntax.morphosyntax_process import perform_advanced_morphosyntactic_analysis
11
+ from ..database.morphosyntax_iterative_mongo_db import (
12
+ store_student_morphosyntax_base,
13
+ store_student_morphosyntax_iteration,
14
+ )
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ ###########################################################################
19
+ def initialize_arc_analysis_state():
20
+ """
21
+ Inicializa el estado de an谩lisis de arcos (base e iteraciones) si no existe.
22
+ """
23
+ if "arc_analysis_state" not in st.session_state:
24
+ st.session_state.arc_analysis_state = {
25
+ "base_id": None,
26
+ "base_text": "",
27
+ "base_diagram": None,
28
+ "iteration_text": "",
29
+ "iteration_diagram": None,
30
+ }
31
+ logger.info("Estado de an谩lisis de arcos inicializado.")
32
+
33
+ ###########################################################################
34
+ def reset_arc_analysis_state():
35
+ """
36
+ Resetea completamente el estado de an谩lisis de arcos.
37
+ """
38
+ st.session_state.arc_analysis_state = {
39
+ "base_id": None,
40
+ "base_text": "",
41
+ "base_diagram": None,
42
+ "iteration_text": "",
43
+ "iteration_diagram": None,
44
+ }
45
+ logger.info("Estado de arcos reseteado.")
46
+
47
+ ###########################################################################
48
+ def display_arc_diagram(doc):
49
+ """
50
+ Genera y retorna el HTML del diagrama de arco para un `Doc` de spaCy.
51
+ No imprime directamente en pantalla; regresa el HTML para
52
+ usar con `st.write(..., unsafe_allow_html=True)`.
53
+ """
54
+ try:
55
+ diagram_html = ""
56
+ for sent in doc.sents:
57
+ svg_html = displacy.render(
58
+ sent,
59
+ style="dep",
60
+ options={
61
+ "distance": 100,
62
+ "arrow_spacing": 20,
63
+ "word_spacing": 30
64
+ }
65
+ )
66
+ # Ajustar tama帽os
67
+ svg_html = svg_html.replace('height="375"', 'height="200"')
68
+ svg_html = re.sub(
69
+ r'<svg[^>]*>',
70
+ lambda m: m.group(0).replace('height="450"', 'height="300"'),
71
+ svg_html
72
+ )
73
+ svg_html = re.sub(
74
+ r'<g [^>]*transform="translate\((\d+),(\d+)\)"',
75
+ lambda m: f'<g transform="translate({m.group(1)},50)"',
76
+ svg_html
77
+ )
78
+ # Envolver en contenedor
79
+ diagram_html += f'<div class="arc-diagram-container">{svg_html}</div>'
80
+ return diagram_html
81
+
82
+ except Exception as e:
83
+ logger.error(f"Error en display_arc_diagram: {str(e)}")
84
+ return "<p style='color:red;'>Error generando diagrama</p>"
85
+
86
+ ###########################################################################
87
+ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
88
+ """
89
+ Interfaz principal para la visualizaci贸n de diagramas de arco
90
+ (Texto Base vs Iteraciones).
91
+ """
92
+ # CSS para layout vertical y estable
93
+ st.markdown("""
94
+ <style>
95
+ .stTextArea textarea {
96
+ font-size: 1rem;
97
+ line-height: 1.5;
98
+ min-height: 100px !important;
99
+ height: 100px !important;
100
+ }
101
+ .arc-diagram-container {
102
+ width: 100%;
103
+ padding: 0.5rem;
104
+ margin: 0.5rem 0;
105
+ }
106
+ .divider {
107
+ height: 3px;
108
+ border: none;
109
+ background-color: #333;
110
+ margin: 2rem 0;
111
+ }
112
+ </style>
113
+ """, unsafe_allow_html=True)
114
+
115
+ # 1) Inicializar estados
116
+ initialize_arc_analysis_state()
117
+ arc_state = st.session_state.arc_analysis_state
118
+
119
+ # 2) Creamos pesta帽as: "Texto Base" y "Iteraciones"
120
+ tabs = st.tabs(["Texto Base", "Iteraciones"])
121
+
122
+ # =================== PESTA脩A 1: Texto Base ==========================
123
+ with tabs[0]:
124
+ st.subheader("An谩lisis de Texto Base")
125
+
126
+ # Bot贸n para iniciar nuevo an谩lisis
127
+ if st.button("Nuevo An谩lisis", key="btn_reset_base"):
128
+ # Solo limpiamos el estado; si requieres forzar reload,
129
+ # descomenta la siguiente l铆nea:
130
+ # st.experimental_rerun()
131
+ reset_arc_analysis_state()
132
+
133
+ # Textarea de texto base
134
+ arc_state["base_text"] = st.text_area(
135
+ "Ingrese su texto inicial",
136
+ value=arc_state["base_text"],
137
+ key="base_text_input",
138
+ height=150
139
+ )
140
+
141
+ # Bot贸n para analizar texto base
142
+ if st.button("Analizar Texto Base", key="btn_analyze_base"):
143
+ if not arc_state["base_text"].strip():
144
+ st.warning("Ingrese un texto para analizar.")
145
+ else:
146
+ try:
147
+ # Procesar con spaCy
148
+ doc = nlp_models[lang_code](arc_state["base_text"])
149
+ # Generar HTML del arco
150
+ arc_html = display_arc_diagram(doc)
151
+ arc_state["base_diagram"] = arc_html
152
+
153
+ # Guardar en Mongo
154
+ analysis = perform_advanced_morphosyntactic_analysis(
155
+ arc_state["base_text"],
156
+ nlp_models[lang_code]
157
+ )
158
+ base_id = store_student_morphosyntax_base(
159
+ username=st.session_state.username,
160
+ text=arc_state["base_text"],
161
+ arc_diagrams=analysis["arc_diagrams"]
162
+ )
163
+ if base_id:
164
+ arc_state["base_id"] = base_id
165
+ st.success(f"An谩lisis base guardado. ID: {base_id}")
166
+
167
+ except Exception as exc:
168
+ st.error("Error procesando texto base")
169
+ logger.error(f"Error en an谩lisis base: {str(exc)}")
170
+
171
+ # Mostrar diagrama base
172
+ if arc_state["base_diagram"]:
173
+ st.markdown("<hr class='divider'>", unsafe_allow_html=True)
174
+ st.markdown("#### Diagrama de Arco (Texto Base)")
175
+ st.write(arc_state["base_diagram"], unsafe_allow_html=True)
176
+
177
+ # ================== PESTA脩A 2: Iteraciones ==========================
178
+ with tabs[1]:
179
+ st.subheader("An谩lisis de Cambios / Iteraciones")
180
+
181
+ # Verificar que exista texto base analizado
182
+ if not arc_state["base_id"]:
183
+ st.info("Primero analiza un texto base en la pesta帽a anterior.")
184
+ return
185
+
186
+ # Mostrar texto base como referencia (solo lectura)
187
+ st.text_area(
188
+ "Texto Base (solo lectura)",
189
+ value=arc_state["base_text"],
190
+ height=80,
191
+ disabled=True
192
+ )
193
+
194
+ # Caja de texto para la iteraci贸n
195
+ arc_state["iteration_text"] = st.text_area(
196
+ "Texto de Iteraci贸n",
197
+ value=arc_state["iteration_text"],
198
+ height=150
199
+ )
200
+
201
+ # Bot贸n analizar iteraci贸n
202
+ if st.button("Analizar Cambios", key="btn_analyze_iteration"):
203
+ if not arc_state["iteration_text"].strip():
204
+ st.warning("Ingrese texto de iteraci贸n.")
205
+ else:
206
+ try:
207
+ # Procesar con spaCy
208
+ doc_iter = nlp_models[lang_code](arc_state["iteration_text"])
209
+ arc_html_iter = display_arc_diagram(doc_iter)
210
+ arc_state["iteration_diagram"] = arc_html_iter
211
+
212
+ # Guardar en Mongo
213
+ analysis_iter = perform_advanced_morphosyntactic_analysis(
214
+ arc_state["iteration_text"],
215
+ nlp_models[lang_code]
216
+ )
217
+ iteration_id = store_student_morphosyntax_iteration(
218
+ username=st.session_state.username,
219
+ base_id=arc_state["base_id"],
220
+ original_text=arc_state["base_text"],
221
+ iteration_text=arc_state["iteration_text"],
222
+ arc_diagrams=analysis_iter["arc_diagrams"]
223
+ )
224
+ if iteration_id:
225
+ st.success(f"Iteraci贸n guardada. ID: {iteration_id}")
226
+
227
+ except Exception as exc:
228
+ st.error("Error procesando iteraci贸n")
229
+ logger.error(f"Error en iteraci贸n: {str(exc)}")
230
+
231
+ # Mostrar diagrama de iteraci贸n
232
+ if arc_state["iteration_diagram"]:
233
+ st.markdown("<hr class='divider'>", unsafe_allow_html=True)
234
+ st.markdown("#### Diagrama de Arco (Iteraci贸n)")
235
+ st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)
236
+
237
+ # Comparaci贸n vertical (uno abajo del otro)
238
+ if arc_state["base_diagram"] and arc_state["iteration_diagram"]:
239
+ st.markdown("<hr class='divider'>", unsafe_allow_html=True)
240
+ st.markdown("### Comparaci贸n Vertical: Base vs. Iteraci贸n")
241
+
242
+ st.markdown("**Diagrama Base**")
243
+ st.write(arc_state["base_diagram"], unsafe_allow_html=True)
244
+
245
+ st.markdown("---")
246
+ st.markdown("**Diagrama Iterado**")
247
+ st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)