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