Spaces:
Running
Running
Update modules/semantic/semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
CHANGED
|
@@ -23,7 +23,7 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 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
|
| 27 |
if 'semantic_live_state' not in st.session_state:
|
| 28 |
st.session_state.semantic_live_state = {
|
| 29 |
'analysis_count': 0,
|
|
@@ -32,24 +32,29 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 32 |
'text_changed': False
|
| 33 |
}
|
| 34 |
|
| 35 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
input_col, result_col = st.columns([1, 3])
|
| 37 |
|
| 38 |
# Columna izquierda: Entrada de texto
|
| 39 |
with input_col:
|
| 40 |
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
| 41 |
|
| 42 |
-
# 脕rea de texto
|
| 43 |
text_input = st.text_area(
|
| 44 |
semantic_t.get('text_input_label', 'Escriba o pegue su texto aqu铆'),
|
| 45 |
height=500,
|
| 46 |
key="semantic_live_text",
|
| 47 |
-
value=st.session_state.semantic_live_state.get('current_text', '')
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# Actualizar el texto actual en el estado
|
| 51 |
-
st.session_state.semantic_live_state['current_text'] = text_input
|
| 52 |
-
|
| 53 |
# Bot贸n de an谩lisis y procesamiento
|
| 54 |
analyze_button = st.button(
|
| 55 |
semantic_t.get('analyze_button', 'Analizar'),
|
|
@@ -60,11 +65,9 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 60 |
use_container_width=True
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Procesar an谩lisis cuando se presiona el bot贸n
|
| 64 |
if analyze_button and text_input:
|
| 65 |
try:
|
| 66 |
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 67 |
-
# Realizar an谩lisis
|
| 68 |
analysis_result = process_semantic_input(
|
| 69 |
text_input,
|
| 70 |
lang_code,
|
|
@@ -73,11 +76,10 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 73 |
)
|
| 74 |
|
| 75 |
if analysis_result['success']:
|
| 76 |
-
# Guardar resultado en el estado
|
| 77 |
st.session_state.semantic_live_state['last_result'] = analysis_result
|
| 78 |
st.session_state.semantic_live_state['analysis_count'] += 1
|
|
|
|
| 79 |
|
| 80 |
-
# Guardar en base de datos
|
| 81 |
store_student_semantic_result(
|
| 82 |
st.session_state.username,
|
| 83 |
text_input,
|
|
@@ -94,17 +96,14 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 94 |
with result_col:
|
| 95 |
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
| 96 |
|
| 97 |
-
# Mostrar resultados si existen
|
| 98 |
if 'last_result' in st.session_state.semantic_live_state and \
|
| 99 |
st.session_state.semantic_live_state['last_result'] is not None:
|
| 100 |
|
| 101 |
analysis = st.session_state.semantic_live_state['last_result']['analysis']
|
| 102 |
|
| 103 |
-
# Verificar que tenemos datos para mostrar
|
| 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 |
-
# Estilos para la visualizaci贸n
|
| 108 |
st.markdown("""
|
| 109 |
<style>
|
| 110 |
.unified-container {
|
|
@@ -117,28 +116,31 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 117 |
}
|
| 118 |
.concept-table {
|
| 119 |
display: flex;
|
| 120 |
-
flex-wrap: wrap
|
| 121 |
-
gap:
|
| 122 |
-
padding:
|
| 123 |
background-color: #f8f9fa;
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
.concept-item {
|
| 126 |
background-color: white;
|
| 127 |
-
border-radius:
|
| 128 |
-
padding:
|
| 129 |
-
display: flex;
|
| 130 |
align-items: center;
|
| 131 |
-
gap:
|
| 132 |
-
box-shadow: 0 1px
|
|
|
|
| 133 |
}
|
| 134 |
.concept-name {
|
| 135 |
font-weight: 500;
|
| 136 |
color: #1f2937;
|
| 137 |
-
font-size: 0.
|
| 138 |
}
|
| 139 |
.concept-freq {
|
| 140 |
color: #6b7280;
|
| 141 |
-
font-size: 0.
|
| 142 |
}
|
| 143 |
.graph-section {
|
| 144 |
padding: 20px;
|
|
@@ -147,9 +149,8 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 147 |
</style>
|
| 148 |
""", unsafe_allow_html=True)
|
| 149 |
|
| 150 |
-
# Mostrar conceptos y grafo
|
| 151 |
with st.container():
|
| 152 |
-
# Conceptos
|
| 153 |
concepts_html = """
|
| 154 |
<div class="unified-container">
|
| 155 |
<div class="concept-table">
|
|
@@ -192,4 +193,5 @@ def display_semantic_live_interface(lang_code, nlp_models, semantic_t):
|
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
logger.error(f"Error general en interfaz sem谩ntica en vivo: {str(e)}")
|
| 195 |
-
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
|
|
|
|
|
| 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,
|
|
|
|
| 32 |
'text_changed': False
|
| 33 |
}
|
| 34 |
|
| 35 |
+
# 2. Funci贸n para manejar cambios en el texto
|
| 36 |
+
def on_text_change():
|
| 37 |
+
current_text = st.session_state.semantic_live_text
|
| 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'),
|
|
|
|
| 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,
|
|
|
|
| 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 {
|
|
|
|
| 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;
|
|
|
|
| 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">
|
|
|
|
| 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 |
+
|