Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -21,16 +21,76 @@ from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
|
| 21 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
| 22 |
"""
|
| 23 |
Interfaz para el an谩lisis sem谩ntico en vivo
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
try:
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Columna derecha: Visualizaci贸n de resultados
|
| 29 |
-
with
|
| 30 |
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Mostrar resultados (ya sea nuevos o previos)
|
| 36 |
if 'last_result' in st.session_state.semantic_live_state and \
|
|
@@ -71,7 +131,7 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 71 |
</style>
|
| 72 |
""", unsafe_allow_html=True)
|
| 73 |
|
| 74 |
-
# Crear la visualizaci贸n horizontal de conceptos
|
| 75 |
concepts_html = ['<div class="concept-table">']
|
| 76 |
concepts_html.extend(
|
| 77 |
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
|
@@ -100,10 +160,10 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 100 |
with st.container():
|
| 101 |
st.markdown('<div class="graph-container">', unsafe_allow_html=True)
|
| 102 |
|
| 103 |
-
# Mostrar grafo
|
| 104 |
st.image(
|
| 105 |
analysis['concept_graph'],
|
| 106 |
-
use_container_width=True,
|
| 107 |
caption=semantic_t.get(
|
| 108 |
'graph_description',
|
| 109 |
'Visualizaci贸n de relaciones entre conceptos clave identificados en el texto.'
|
|
@@ -113,8 +173,8 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 113 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 114 |
|
| 115 |
# Contenedor para botones
|
| 116 |
-
|
| 117 |
-
with
|
| 118 |
st.download_button(
|
| 119 |
label="馃摜 " + semantic_t.get('download_graph', "Download"),
|
| 120 |
data=analysis['concept_graph'],
|
|
@@ -140,4 +200,4 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
logger.error(f"Error general en interfaz sem谩ntica en vivo: {str(e)}")
|
| 143 |
-
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
|
| 21 |
def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
| 22 |
"""
|
| 23 |
Interfaz para el an谩lisis sem谩ntico en vivo
|
| 24 |
+
Args:
|
| 25 |
+
lang_code: C贸digo del idioma actual
|
| 26 |
+
nlp_models: Modelos de spaCy cargados
|
| 27 |
+
semantic_t: Diccionario de traducciones sem谩nticas
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
+
# 1. Inicializar el estado de la sesi贸n para el an谩lisis en vivo
|
| 31 |
+
if 'semantic_live_state' not in st.session_state:
|
| 32 |
+
st.session_state.semantic_live_state = {
|
| 33 |
+
'analysis_count': 0,
|
| 34 |
+
'last_analysis': None,
|
| 35 |
+
'current_text': '',
|
| 36 |
+
'last_result': None
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# 2. Crear dos columnas principales
|
| 40 |
+
input_col, result_col = st.columns(2)
|
| 41 |
+
|
| 42 |
+
# Columna izquierda: Entrada de texto
|
| 43 |
+
with input_col:
|
| 44 |
+
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
| 45 |
+
|
| 46 |
+
# 脕rea de texto para input
|
| 47 |
+
text_input = st.text_area(
|
| 48 |
+
semantic_t.get('text_input_label', 'Escriba o pegue su texto aqu铆'),
|
| 49 |
+
height=400,
|
| 50 |
+
key="semantic_live_text",
|
| 51 |
+
value=st.session_state.semantic_live_state.get('current_text', '')
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Actualizar el texto actual en el estado
|
| 55 |
+
st.session_state.semantic_live_state['current_text'] = text_input
|
| 56 |
+
|
| 57 |
+
# Bot贸n de an谩lisis
|
| 58 |
+
analyze_button = st.button(
|
| 59 |
+
semantic_t.get('analyze_button', 'Analizar'),
|
| 60 |
+
key="semantic_live_analyze",
|
| 61 |
+
type="primary",
|
| 62 |
+
icon="馃攳",
|
| 63 |
+
disabled=not text_input,
|
| 64 |
+
use_container_width=True
|
| 65 |
+
)
|
| 66 |
|
| 67 |
# Columna derecha: Visualizaci贸n de resultados
|
| 68 |
+
with result_col:
|
| 69 |
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
# Procesar an谩lisis cuando se presiona el bot贸n
|
| 73 |
+
if analyze_button and text_input:
|
| 74 |
+
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 75 |
+
# Realizar an谩lisis
|
| 76 |
+
analysis_result = process_semantic_input(
|
| 77 |
+
text_input,
|
| 78 |
+
lang_code,
|
| 79 |
+
nlp_models,
|
| 80 |
+
semantic_t
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if analysis_result['success']:
|
| 84 |
+
# Guardar resultado en el estado
|
| 85 |
+
st.session_state.semantic_live_state['last_result'] = analysis_result
|
| 86 |
+
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 87 |
+
|
| 88 |
+
# Guardar en base de datos
|
| 89 |
+
store_student_semantic_result(
|
| 90 |
+
st.session_state.username,
|
| 91 |
+
text_input,
|
| 92 |
+
analysis_result['analysis']
|
| 93 |
+
)
|
| 94 |
|
| 95 |
# Mostrar resultados (ya sea nuevos o previos)
|
| 96 |
if 'last_result' in st.session_state.semantic_live_state and \
|
|
|
|
| 131 |
</style>
|
| 132 |
""", unsafe_allow_html=True)
|
| 133 |
|
| 134 |
+
# Crear la visualizaci贸n horizontal de conceptos
|
| 135 |
concepts_html = ['<div class="concept-table">']
|
| 136 |
concepts_html.extend(
|
| 137 |
f'<div class="concept-item"><span class="concept-name">{concept}</span>'
|
|
|
|
| 160 |
with st.container():
|
| 161 |
st.markdown('<div class="graph-container">', unsafe_allow_html=True)
|
| 162 |
|
| 163 |
+
# Mostrar grafo
|
| 164 |
st.image(
|
| 165 |
analysis['concept_graph'],
|
| 166 |
+
use_container_width=True,
|
| 167 |
caption=semantic_t.get(
|
| 168 |
'graph_description',
|
| 169 |
'Visualizaci贸n de relaciones entre conceptos clave identificados en el texto.'
|
|
|
|
| 173 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 174 |
|
| 175 |
# Contenedor para botones
|
| 176 |
+
button_col, spacer_col = st.columns([1,4])
|
| 177 |
+
with button_col:
|
| 178 |
st.download_button(
|
| 179 |
label="馃摜 " + semantic_t.get('download_graph', "Download"),
|
| 180 |
data=analysis['concept_graph'],
|
|
|
|
| 200 |
|
| 201 |
except Exception as e:
|
| 202 |
logger.error(f"Error general en interfaz sem谩ntica en vivo: {str(e)}")
|
| 203 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|