Spaces:
Running
Running
Create semantic_live_interface.py
Browse files
modules/semantic/semantic_live_interface.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
+
}
|
| 37 |
+
|
| 38 |
+
# 2. Crear dos columnas
|
| 39 |
+
col1, col2 = st.columns(2)
|
| 40 |
+
|
| 41 |
+
# Columna izquierda: Entrada de texto
|
| 42 |
+
with col1:
|
| 43 |
+
st.subheader(semantic_t.get('enter_text', 'Ingrese su texto'))
|
| 44 |
+
|
| 45 |
+
# Área de texto para input
|
| 46 |
+
text_input = st.text_area(
|
| 47 |
+
semantic_t.get('text_input_label', 'Escriba o pegue su texto aquí'),
|
| 48 |
+
height=400,
|
| 49 |
+
key=f"semantic_live_text_{st.session_state.semantic_live_state['analysis_count']}"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Botón de análisis
|
| 53 |
+
analyze_button = st.button(
|
| 54 |
+
semantic_t.get('analyze_button', 'Analizar'),
|
| 55 |
+
key=f"semantic_live_analyze_{st.session_state.semantic_live_state['analysis_count']}",
|
| 56 |
+
type="primary",
|
| 57 |
+
icon="🔍",
|
| 58 |
+
disabled=not text_input,
|
| 59 |
+
use_container_width=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Columna derecha: Visualización de resultados
|
| 63 |
+
with col2:
|
| 64 |
+
st.subheader(semantic_t.get('live_results', 'Resultados en vivo'))
|
| 65 |
+
|
| 66 |
+
# Procesar análisis cuando se presiona el botón
|
| 67 |
+
if analyze_button and text_input:
|
| 68 |
+
try:
|
| 69 |
+
with st.spinner(semantic_t.get('processing', 'Procesando...')):
|
| 70 |
+
# Realizar análisis
|
| 71 |
+
analysis_result = process_semantic_input(
|
| 72 |
+
text_input,
|
| 73 |
+
lang_code,
|
| 74 |
+
nlp_models,
|
| 75 |
+
semantic_t
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if analysis_result['success']:
|
| 79 |
+
# Guardar resultado
|
| 80 |
+
st.session_state.semantic_live_result = analysis_result
|
| 81 |
+
st.session_state.semantic_live_state['analysis_count'] += 1
|
| 82 |
+
|
| 83 |
+
# Guardar en base de datos
|
| 84 |
+
store_student_semantic_result(
|
| 85 |
+
st.session_state.username,
|
| 86 |
+
text_input,
|
| 87 |
+
analysis_result['analysis']
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Mostrar gráfico de conceptos
|
| 91 |
+
if 'concept_graph' in analysis_result['analysis'] and analysis_result['analysis']['concept_graph'] is not None:
|
| 92 |
+
st.image(analysis_result['analysis']['concept_graph'])
|
| 93 |
+
else:
|
| 94 |
+
st.info(semantic_t.get('no_graph', 'No hay gráfico disponible'))
|
| 95 |
+
|
| 96 |
+
# Mostrar tabla de conceptos clave
|
| 97 |
+
if 'key_concepts' in analysis_result['analysis'] and analysis_result['analysis']['key_concepts']:
|
| 98 |
+
st.subheader(semantic_t.get('key_concepts', 'Conceptos Clave'))
|
| 99 |
+
df = pd.DataFrame(
|
| 100 |
+
analysis_result['analysis']['key_concepts'],
|
| 101 |
+
columns=[
|
| 102 |
+
semantic_t.get('concept', 'Concepto'),
|
| 103 |
+
semantic_t.get('frequency', 'Frecuencia')
|
| 104 |
+
]
|
| 105 |
+
)
|
| 106 |
+
st.dataframe(
|
| 107 |
+
df,
|
| 108 |
+
hide_index=True,
|
| 109 |
+
column_config={
|
| 110 |
+
semantic_t.get('frequency', 'Frecuencia'): st.column_config.NumberColumn(
|
| 111 |
+
format="%.2f"
|
| 112 |
+
)
|
| 113 |
+
}
|
| 114 |
+
)
|
| 115 |
+
else:
|
| 116 |
+
st.error(analysis_result['message'])
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.error(f"Error en análisis semántico en vivo: {str(e)}")
|
| 120 |
+
st.error(semantic_t.get('error_processing', f'Error al procesar el texto: {str(e)}'))
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
logger.error(f"Error general en interfaz semántica en vivo: {str(e)}")
|
| 124 |
+
st.error(semantic_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|