Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
@@ -1,21 +1,29 @@
|
|
1 |
# modules/semantic/semantic_live_interface.py
|
2 |
import streamlit as st
|
|
|
|
|
|
|
3 |
import logging
|
4 |
-
from datetime import datetime, timezone
|
5 |
|
6 |
# Configuración del logger
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
# Importaciones locales
|
10 |
-
from .semantic_process import
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
14 |
"""
|
15 |
-
Interfaz para el análisis semántico en vivo con
|
16 |
"""
|
17 |
try:
|
18 |
-
# 1. Inicializar el estado de la sesión
|
19 |
if 'semantic_live_state' not in st.session_state:
|
20 |
st.session_state.semantic_live_state = {
|
21 |
'analysis_count': 0,
|
@@ -30,34 +38,36 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
30 |
st.session_state.semantic_live_state['current_text'] = current_text
|
31 |
st.session_state.semantic_live_state['text_changed'] = True
|
32 |
|
33 |
-
# 3. Crear columnas
|
34 |
input_col, result_col = st.columns([1, 3])
|
35 |
|
36 |
-
# Columna izquierda: Entrada de texto
|
37 |
with input_col:
|
38 |
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
39 |
|
|
|
40 |
text_input = st.text_area(
|
41 |
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
42 |
-
height=
|
43 |
key="semantic_live_text",
|
44 |
value=st.session_state.semantic_live_state.get('current_text', ''),
|
45 |
on_change=on_text_change,
|
46 |
-
label_visibility="collapsed"
|
47 |
)
|
48 |
|
|
|
49 |
analyze_button = st.button(
|
50 |
semantic_t.get('analyze_button', 'Analizar'),
|
51 |
key="semantic_live_analyze",
|
52 |
type="primary",
|
|
|
53 |
disabled=not text_input,
|
54 |
use_container_width=True
|
55 |
)
|
56 |
|
57 |
-
# 4. Procesar análisis cuando se presiona el botón
|
58 |
if analyze_button and text_input:
|
59 |
-
|
60 |
-
|
61 |
analysis_result = process_semantic_input(
|
62 |
text_input,
|
63 |
lang_code,
|
@@ -66,114 +76,122 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
66 |
)
|
67 |
|
68 |
if analysis_result['success']:
|
69 |
-
# Guardar resultado en sesión y base de datos
|
70 |
st.session_state.semantic_live_state['last_result'] = analysis_result
|
71 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
72 |
st.session_state.semantic_live_state['text_changed'] = False
|
73 |
|
74 |
-
|
75 |
-
store_result = store_student_semantic_live_result(
|
76 |
st.session_state.username,
|
77 |
text_input,
|
78 |
-
analysis_result['analysis']
|
79 |
-
lang_code
|
80 |
)
|
81 |
-
|
82 |
-
if not store_result:
|
83 |
-
st.error(semantic_t.get('error_saving', 'Error al guardar el análisis'))
|
84 |
-
else:
|
85 |
-
st.success(semantic_t.get('analysis_saved', 'Análisis guardado correctamente'))
|
86 |
-
st.rerun() # Forzar actualización para mostrar resultados
|
87 |
else:
|
88 |
st.error(analysis_result.get('message', 'Error en el análisis'))
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
|
94 |
-
# Columna derecha:
|
95 |
with result_col:
|
|
|
|
|
96 |
if 'last_result' in st.session_state.semantic_live_state and \
|
97 |
st.session_state.semantic_live_state['last_result'] is not None:
|
98 |
|
99 |
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
100 |
-
|
101 |
if 'key_concepts' in analysis and analysis['key_concepts'] and \
|
102 |
'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
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 |
else:
|
173 |
-
st.info(semantic_t.get('
|
174 |
-
else:
|
175 |
-
st.info(semantic_t.get('analysis_prompt', 'Realice un análisis para ver los resultados'))
|
176 |
|
177 |
except Exception as e:
|
178 |
-
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}"
|
179 |
-
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
|
1 |
# modules/semantic/semantic_live_interface.py
|
2 |
import streamlit as st
|
3 |
+
from streamlit_float import *
|
4 |
+
from streamlit_antd_components import *
|
5 |
+
import pandas as pd
|
6 |
import logging
|
|
|
7 |
|
8 |
# Configuración del logger
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
# Importaciones locales
|
12 |
+
from .semantic_process import (
|
13 |
+
process_semantic_input,
|
14 |
+
format_semantic_results
|
15 |
+
)
|
16 |
+
|
17 |
+
from ..utils.widget_utils import generate_unique_key
|
18 |
+
from ..database.semantic_mongo_db import store_student_semantic_result
|
19 |
+
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
20 |
|
21 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
22 |
"""
|
23 |
+
Interfaz para el análisis semántico en vivo con proporciones de columna ajustadas
|
24 |
"""
|
25 |
try:
|
26 |
+
# 1. Inicializar el estado de la sesión de manera más robusta
|
27 |
if 'semantic_live_state' not in st.session_state:
|
28 |
st.session_state.semantic_live_state = {
|
29 |
'analysis_count': 0,
|
|
|
38 |
st.session_state.semantic_live_state['current_text'] = current_text
|
39 |
st.session_state.semantic_live_state['text_changed'] = True
|
40 |
|
41 |
+
# 3. Crear columnas con nueva proporción (1:3)
|
42 |
input_col, result_col = st.columns([1, 3])
|
43 |
|
44 |
+
# Columna izquierda: Entrada de texto
|
45 |
with input_col:
|
46 |
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
47 |
|
48 |
+
# Área de texto con manejo de eventos
|
49 |
text_input = st.text_area(
|
50 |
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
51 |
+
height=500,
|
52 |
key="semantic_live_text",
|
53 |
value=st.session_state.semantic_live_state.get('current_text', ''),
|
54 |
on_change=on_text_change,
|
55 |
+
label_visibility="collapsed" # Oculta el label para mayor estabilidad
|
56 |
)
|
57 |
|
58 |
+
# Botón de análisis y procesamiento
|
59 |
analyze_button = st.button(
|
60 |
semantic_t.get('analyze_button', 'Analizar'),
|
61 |
key="semantic_live_analyze",
|
62 |
type="primary",
|
63 |
+
icon="🔍",
|
64 |
disabled=not text_input,
|
65 |
use_container_width=True
|
66 |
)
|
67 |
|
|
|
68 |
if analyze_button and text_input:
|
69 |
+
try:
|
70 |
+
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
71 |
analysis_result = process_semantic_input(
|
72 |
text_input,
|
73 |
lang_code,
|
|
|
76 |
)
|
77 |
|
78 |
if analysis_result['success']:
|
|
|
79 |
st.session_state.semantic_live_state['last_result'] = analysis_result
|
80 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
81 |
st.session_state.semantic_live_state['text_changed'] = False
|
82 |
|
83 |
+
store_student_semantic_result(
|
|
|
84 |
st.session_state.username,
|
85 |
text_input,
|
86 |
+
analysis_result['analysis']
|
|
|
87 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
else:
|
89 |
st.error(analysis_result.get('message', 'Error en el análisis'))
|
90 |
|
91 |
+
except Exception as e:
|
92 |
+
logger.error(f"Error en análisis: {str(e)}")
|
93 |
+
st.error(semantic_t.get('error_processing', 'Error al procesar el texto'))
|
94 |
|
95 |
+
# Columna derecha: Visualización de resultados
|
96 |
with result_col:
|
97 |
+
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
98 |
+
|
99 |
if 'last_result' in st.session_state.semantic_live_state and \
|
100 |
st.session_state.semantic_live_state['last_result'] is not None:
|
101 |
|
102 |
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
103 |
+
|
104 |
if 'key_concepts' in analysis and analysis['key_concepts'] and \
|
105 |
'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
106 |
|
107 |
+
st.markdown("""
|
108 |
+
<style>
|
109 |
+
.unified-container {
|
110 |
+
background-color: white;
|
111 |
+
border-radius: 10px;
|
112 |
+
overflow: hidden;
|
113 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
114 |
+
width: 100%;
|
115 |
+
margin-bottom: 1rem;
|
116 |
+
}
|
117 |
+
.concept-table {
|
118 |
+
display: flex;
|
119 |
+
flex-wrap: nowrap; /* Evita el wrap */
|
120 |
+
gap: 6px; /* Reducido el gap */
|
121 |
+
padding: 10px;
|
122 |
+
background-color: #f8f9fa;
|
123 |
+
overflow-x: auto; /* Permite scroll horizontal si es necesario */
|
124 |
+
white-space: nowrap; /* Mantiene todo en una línea */
|
125 |
+
}
|
126 |
+
.concept-item {
|
127 |
+
background-color: white;
|
128 |
+
border-radius: 4px;
|
129 |
+
padding: 4px 8px; /* Padding reducido */
|
130 |
+
display: inline-flex; /* Cambiado a inline-flex */
|
131 |
+
align-items: center;
|
132 |
+
gap: 4px; /* Gap reducido */
|
133 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
134 |
+
flex-shrink: 0; /* Evita que los items se encojan */
|
135 |
+
}
|
136 |
+
.concept-name {
|
137 |
+
font-weight: 500;
|
138 |
+
color: #1f2937;
|
139 |
+
font-size: 0.8em; /* Tamaño de fuente reducido */
|
140 |
+
}
|
141 |
+
.concept-freq {
|
142 |
+
color: #6b7280;
|
143 |
+
font-size: 0.75em; /* Tamaño de fuente reducido */
|
144 |
+
}
|
145 |
+
.graph-section {
|
146 |
+
padding: 20px;
|
147 |
+
background-color: white;
|
148 |
+
}
|
149 |
+
</style>
|
150 |
+
""", unsafe_allow_html=True)
|
151 |
+
|
152 |
+
with st.container():
|
153 |
+
# Conceptos en una sola línea
|
154 |
+
concepts_html = """
|
155 |
+
<div class="unified-container">
|
156 |
+
<div class="concept-table">
|
157 |
+
"""
|
158 |
+
concepts_html += ''.join(
|
159 |
+
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
160 |
+
f'<span class="concept-freq">({freq:.2f})</span></div>'
|
161 |
+
for concept, freq in analysis['key_concepts']
|
162 |
+
)
|
163 |
+
concepts_html += "</div></div>"
|
164 |
+
st.markdown(concepts_html, unsafe_allow_html=True)
|
165 |
+
|
166 |
+
# Grafo
|
167 |
+
if 'concept_graph' in analysis and analysis['concept_graph'] is not None:
|
168 |
+
st.image(
|
169 |
+
analysis['concept_graph'],
|
170 |
+
use_container_width=True
|
171 |
+
)
|
172 |
+
|
173 |
+
# Botones y controles
|
174 |
+
button_col, spacer_col = st.columns([1,5])
|
175 |
+
with button_col:
|
176 |
+
st.download_button(
|
177 |
+
label="📥 " + semantic_t.get('download_graph', "Download"),
|
178 |
+
data=analysis['concept_graph'],
|
179 |
+
file_name="semantic_live_graph.png",
|
180 |
+
mime="image/png",
|
181 |
+
use_container_width=True
|
182 |
+
)
|
183 |
+
|
184 |
+
with st.expander("📊 " + semantic_t.get('graph_help', "Graph Interpretation")):
|
185 |
+
st.markdown("""
|
186 |
+
- 🔀 Las flechas indican la dirección de la relación entre conceptos
|
187 |
+
- 🎨 Los colores más intensos indican conceptos más centrales en el texto
|
188 |
+
- ⭕ El tamaño de los nodos representa la frecuencia del concepto
|
189 |
+
- ↔️ El grosor de las líneas indica la fuerza de la conexión
|
190 |
+
""")
|
191 |
else:
|
192 |
+
st.info(semantic_t.get('no_graph', 'No hay datos para mostrar'))
|
|
|
|
|
193 |
|
194 |
except Exception as e:
|
195 |
+
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
196 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
197 |
+
|