Update modules/studentact/current_situation_interface.py
Browse files
modules/studentact/current_situation_interface.py
CHANGED
@@ -55,13 +55,18 @@ def display_current_situation_interface(lang_code, nlp_models, t):
|
|
55 |
input_col, results_col = st.columns([1,2])
|
56 |
|
57 |
with input_col:
|
|
|
|
|
|
|
|
|
|
|
58 |
# Text area con manejo de estado
|
59 |
text_input = st.text_area(
|
60 |
t.get('input_prompt', "Escribe o pega tu texto aquí:"),
|
61 |
height=400,
|
62 |
key="text_area",
|
63 |
value=st.session_state.text_input,
|
64 |
-
on_change=
|
65 |
help="Este texto será analizado para darte recomendaciones personalizadas"
|
66 |
)
|
67 |
|
@@ -76,7 +81,6 @@ def display_current_situation_interface(lang_code, nlp_models, t):
|
|
76 |
doc = nlp_models[lang_code](text_input)
|
77 |
metrics = analyze_text_dimensions(doc)
|
78 |
|
79 |
-
# Guardar en MongoDB
|
80 |
storage_success = store_current_situation_result(
|
81 |
username=st.session_state.username,
|
82 |
text=text_input,
|
@@ -99,130 +103,73 @@ def display_current_situation_interface(lang_code, nlp_models, t):
|
|
99 |
# Mostrar resultados en la columna derecha
|
100 |
with results_col:
|
101 |
if st.session_state.show_results and st.session_state.current_metrics is not None:
|
102 |
-
|
103 |
-
|
104 |
-
except Exception as e:
|
105 |
-
logger.error(f"Error en interfaz: {str(e)}")
|
106 |
-
st.error("Ocurrió un error. Por favor, intente de nuevo.")
|
107 |
|
108 |
-
def
|
109 |
"""
|
110 |
-
Muestra
|
111 |
"""
|
112 |
try:
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
"Estructura",
|
129 |
-
f"{metrics['structure']['normalized_score']:.2f}",
|
130 |
-
"Meta: 1.00",
|
131 |
-
delta_color="off",
|
132 |
-
help="Organización y complejidad de las oraciones"
|
133 |
-
)
|
134 |
-
|
135 |
-
# Cohesión
|
136 |
-
metric_cols[2].metric(
|
137 |
-
"Cohesión",
|
138 |
-
f"{metrics['cohesion']['normalized_score']:.2f}",
|
139 |
-
"Meta: 1.00",
|
140 |
-
delta_color="off",
|
141 |
-
help="Conexión y fluidez entre ideas"
|
142 |
-
)
|
143 |
-
|
144 |
-
# Claridad
|
145 |
-
metric_cols[3].metric(
|
146 |
-
"Claridad",
|
147 |
-
f"{metrics['clarity']['normalized_score']:.2f}",
|
148 |
"Meta: 1.00",
|
149 |
delta_color="off",
|
150 |
-
help=
|
151 |
)
|
152 |
|
153 |
-
# Espacio
|
154 |
st.markdown("<div style='margin-top: 1rem;'></div>", unsafe_allow_html=True)
|
155 |
-
|
156 |
-
# Contenedor para el gráfico radar centrado
|
157 |
-
with st.container():
|
158 |
-
# Usar columnas para centrar el gráfico
|
159 |
-
left_space, graph_col, right_space = st.columns([1, 2, 1])
|
160 |
-
|
161 |
-
with graph_col:
|
162 |
-
display_radar_chart(metrics)
|
163 |
-
|
164 |
-
except Exception as e:
|
165 |
-
logger.error(f"Error mostrando métricas y gráfico: {str(e)}")
|
166 |
-
st.error("Error al mostrar los resultados")
|
167 |
-
|
168 |
-
########################################
|
169 |
-
|
170 |
-
def display_radar_chart(metrics):
|
171 |
-
"""
|
172 |
-
Muestra solo el gráfico de radar.
|
173 |
-
"""
|
174 |
-
try:
|
175 |
-
# Preparar datos para el gráfico
|
176 |
-
categories = ['Vocabulario', 'Estructura', 'Cohesión', 'Claridad']
|
177 |
-
values_user = [
|
178 |
-
metrics['vocabulary']['normalized_score'],
|
179 |
-
metrics['structure']['normalized_score'],
|
180 |
-
metrics['cohesion']['normalized_score'],
|
181 |
-
metrics['clarity']['normalized_score']
|
182 |
-
]
|
183 |
-
values_pattern = [1.0, 1.0, 1.0, 1.0]
|
184 |
-
|
185 |
-
# Crear figura más compacta
|
186 |
-
fig = plt.figure(figsize=(6, 6))
|
187 |
-
ax = fig.add_subplot(111, projection='polar')
|
188 |
-
|
189 |
-
# Número de variables
|
190 |
-
num_vars = len(categories)
|
191 |
-
|
192 |
-
# Calcular ángulos
|
193 |
-
angles = [n / float(num_vars) * 2 * np.pi for n in range(num_vars)]
|
194 |
-
angles += angles[:1]
|
195 |
-
|
196 |
-
# Extender valores para cerrar polígonos
|
197 |
-
values_user += values_user[:1]
|
198 |
-
values_pattern += values_pattern[:1]
|
199 |
-
|
200 |
-
# Configurar ejes y etiquetas
|
201 |
-
ax.set_xticks(angles[:-1])
|
202 |
-
ax.set_xticklabels(categories, fontsize=8)
|
203 |
-
|
204 |
-
# Círculos concéntricos y etiquetas
|
205 |
-
circle_ticks = np.arange(0, 1.1, 0.2) # Reducido a 5 niveles
|
206 |
-
ax.set_yticks(circle_ticks)
|
207 |
-
ax.set_yticklabels([f'{tick:.1f}' for tick in circle_ticks], fontsize=8)
|
208 |
-
ax.set_ylim(0, 1)
|
209 |
-
|
210 |
-
# Dibujar patrón ideal
|
211 |
-
ax.plot(angles, values_pattern, 'g--', linewidth=1, label='Patrón', alpha=0.5)
|
212 |
-
ax.fill(angles, values_pattern, 'g', alpha=0.1)
|
213 |
-
|
214 |
-
# Dibujar valores del usuario
|
215 |
-
ax.plot(angles, values_user, 'b-', linewidth=2, label='Tu escritura')
|
216 |
-
ax.fill(angles, values_user, 'b', alpha=0.2)
|
217 |
-
|
218 |
-
# Leyenda
|
219 |
-
ax.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1), fontsize=8)
|
220 |
|
221 |
-
#
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
except Exception as e:
|
227 |
-
logger.error(f"Error
|
228 |
-
st.error("Error al
|
|
|
55 |
input_col, results_col = st.columns([1,2])
|
56 |
|
57 |
with input_col:
|
58 |
+
# Definir función para manejar cambios de texto
|
59 |
+
def on_text_change():
|
60 |
+
st.session_state.text_input = st.session_state.text_area
|
61 |
+
st.session_state.show_results = False
|
62 |
+
|
63 |
# Text area con manejo de estado
|
64 |
text_input = st.text_area(
|
65 |
t.get('input_prompt', "Escribe o pega tu texto aquí:"),
|
66 |
height=400,
|
67 |
key="text_area",
|
68 |
value=st.session_state.text_input,
|
69 |
+
on_change=on_text_change,
|
70 |
help="Este texto será analizado para darte recomendaciones personalizadas"
|
71 |
)
|
72 |
|
|
|
81 |
doc = nlp_models[lang_code](text_input)
|
82 |
metrics = analyze_text_dimensions(doc)
|
83 |
|
|
|
84 |
storage_success = store_current_situation_result(
|
85 |
username=st.session_state.username,
|
86 |
text=text_input,
|
|
|
103 |
# Mostrar resultados en la columna derecha
|
104 |
with results_col:
|
105 |
if st.session_state.show_results and st.session_state.current_metrics is not None:
|
106 |
+
display_results(st.session_state.current_metrics)
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
def display_results(metrics):
|
109 |
"""
|
110 |
+
Muestra los resultados del análisis: métricas y gráfico radar.
|
111 |
"""
|
112 |
try:
|
113 |
+
# Métricas en una fila con columnas uniformes
|
114 |
+
metric_cols = st.columns(4, gap="small", vertical_alignment="center", border=True)
|
115 |
+
|
116 |
+
metrics_config = [
|
117 |
+
("Vocabulario", metrics['vocabulary']['normalized_score'], "Riqueza y variedad del vocabulario"),
|
118 |
+
("Estructura", metrics['structure']['normalized_score'], "Organización y complejidad de oraciones"),
|
119 |
+
("Cohesión", metrics['cohesion']['normalized_score'], "Conexión y fluidez entre ideas"),
|
120 |
+
("Claridad", metrics['clarity']['normalized_score'], "Facilidad de comprensión del texto")
|
121 |
+
]
|
122 |
+
|
123 |
+
# Mostrar métricas
|
124 |
+
for i, (label, value, help_text) in enumerate(metrics_config):
|
125 |
+
metric_cols[i].metric(
|
126 |
+
label,
|
127 |
+
f"{value:.2f}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
"Meta: 1.00",
|
129 |
delta_color="off",
|
130 |
+
help=help_text
|
131 |
)
|
132 |
|
133 |
+
# Espacio entre métricas y gráfico
|
134 |
st.markdown("<div style='margin-top: 1rem;'></div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
# Gráfico radar centrado
|
137 |
+
left_space, graph_col, right_space = st.columns([1, 2, 1])
|
138 |
+
with graph_col:
|
139 |
+
# Preparar datos para el gráfico
|
140 |
+
categories = [m[0] for m in metrics_config]
|
141 |
+
values_user = [m[1] for m in metrics_config]
|
142 |
+
values_pattern = [1.0] * len(categories)
|
143 |
+
|
144 |
+
# Crear y configurar gráfico
|
145 |
+
fig = plt.figure(figsize=(6, 6))
|
146 |
+
ax = fig.add_subplot(111, projection='polar')
|
147 |
+
|
148 |
+
# Configurar gráfico radar
|
149 |
+
angles = [n / float(len(categories)) * 2 * np.pi for n in range(len(categories))]
|
150 |
+
angles += angles[:1]
|
151 |
+
values_user += values_user[:1]
|
152 |
+
values_pattern += values_pattern[:1]
|
153 |
+
|
154 |
+
# Configurar ejes
|
155 |
+
ax.set_xticks(angles[:-1])
|
156 |
+
ax.set_xticklabels(categories, fontsize=8)
|
157 |
+
circle_ticks = np.arange(0, 1.1, 0.2)
|
158 |
+
ax.set_yticks(circle_ticks)
|
159 |
+
ax.set_yticklabels([f'{tick:.1f}' for tick in circle_ticks], fontsize=8)
|
160 |
+
ax.set_ylim(0, 1)
|
161 |
+
|
162 |
+
# Dibujar gráfico
|
163 |
+
ax.plot(angles, values_pattern, 'g--', linewidth=1, label='Patrón', alpha=0.5)
|
164 |
+
ax.fill(angles, values_pattern, 'g', alpha=0.1)
|
165 |
+
ax.plot(angles, values_user, 'b-', linewidth=2, label='Tu escritura')
|
166 |
+
ax.fill(angles, values_user, 'b', alpha=0.2)
|
167 |
+
ax.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1), fontsize=8)
|
168 |
+
|
169 |
+
plt.tight_layout()
|
170 |
+
st.pyplot(fig)
|
171 |
+
plt.close()
|
172 |
|
173 |
except Exception as e:
|
174 |
+
logger.error(f"Error mostrando resultados: {str(e)}")
|
175 |
+
st.error("Error al mostrar los resultados")
|