Spaces:
Sleeping
Sleeping
Create discourse_live_interface.py
Browse files
modules/discourse/discourse_live_interface.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
# modules/discourse/discourse/discourse_live_interface.py
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from streamlit_float import *
|
| 6 |
+
from streamlit_antd_components import *
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
# Configuraci贸n del logger
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
# Importaciones locales
|
| 14 |
+
from .discourse_process import perform_discourse_analysis
|
| 15 |
+
from ..utils.widget_utils import generate_unique_key
|
| 16 |
+
from ..database.discourse_mongo_db import store_student_discourse_result
|
| 17 |
+
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
| 18 |
+
|
| 19 |
+
def display_discourse_live_interface(lang_code, nlp_models, discourse_t):
|
| 20 |
+
"""
|
| 21 |
+
Interfaz para el an谩lisis del discurso en vivo
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
# 1. Inicializar el estado de la sesi贸n
|
| 25 |
+
if 'discourse_live_state' not in st.session_state:
|
| 26 |
+
st.session_state.discourse_live_state = {
|
| 27 |
+
'analysis_count': 0,
|
| 28 |
+
'current_text1': '',
|
| 29 |
+
'current_text2': '',
|
| 30 |
+
'last_result': None,
|
| 31 |
+
'text_changed': False
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# 2. Funci贸n para manejar cambios en los textos
|
| 35 |
+
def on_text1_change():
|
| 36 |
+
current_text = st.session_state.discourse_live_text1
|
| 37 |
+
st.session_state.discourse_live_state['current_text1'] = current_text
|
| 38 |
+
st.session_state.discourse_live_state['text_changed'] = True
|
| 39 |
+
|
| 40 |
+
def on_text2_change():
|
| 41 |
+
current_text = st.session_state.discourse_live_text2
|
| 42 |
+
st.session_state.discourse_live_state['current_text2'] = current_text
|
| 43 |
+
st.session_state.discourse_live_state['text_changed'] = True
|
| 44 |
+
|
| 45 |
+
# 3. Crear columnas con proporci贸n 1:3
|
| 46 |
+
input_col, result_col = st.columns([1, 3])
|
| 47 |
+
|
| 48 |
+
# Columna izquierda: Entrada de textos
|
| 49 |
+
with input_col:
|
| 50 |
+
st.subheader(discourse_t.get('enter_text', 'Ingrese sus textos'))
|
| 51 |
+
|
| 52 |
+
# Primer 谩rea de texto
|
| 53 |
+
st.markdown("**Texto 1 (Patr贸n)**")
|
| 54 |
+
text_input1 = st.text_area(
|
| 55 |
+
"Texto 1",
|
| 56 |
+
height=250,
|
| 57 |
+
key="discourse_live_text1",
|
| 58 |
+
value=st.session_state.discourse_live_state.get('current_text1', ''),
|
| 59 |
+
on_change=on_text1_change,
|
| 60 |
+
label_visibility="collapsed"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Segundo 谩rea de texto
|
| 64 |
+
st.markdown("**Texto 2 (Comparaci贸n)**")
|
| 65 |
+
text_input2 = st.text_area(
|
| 66 |
+
"Texto 2",
|
| 67 |
+
height=250,
|
| 68 |
+
key="discourse_live_text2",
|
| 69 |
+
value=st.session_state.discourse_live_state.get('current_text2', ''),
|
| 70 |
+
on_change=on_text2_change,
|
| 71 |
+
label_visibility="collapsed"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Bot贸n de an谩lisis
|
| 75 |
+
analyze_button = st.button(
|
| 76 |
+
discourse_t.get('analyze_button', 'Analizar'),
|
| 77 |
+
key="discourse_live_analyze",
|
| 78 |
+
type="primary",
|
| 79 |
+
icon="馃攳",
|
| 80 |
+
disabled=not (text_input1 and text_input2),
|
| 81 |
+
use_container_width=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if analyze_button and text_input1 and text_input2:
|
| 85 |
+
try:
|
| 86 |
+
with st.spinner(discourse_t.get('processing', 'Procesando...')):
|
| 87 |
+
# Realizar an谩lisis
|
| 88 |
+
result = perform_discourse_analysis(
|
| 89 |
+
text_input1,
|
| 90 |
+
text_input2,
|
| 91 |
+
nlp_models[lang_code],
|
| 92 |
+
lang_code
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if result['success']:
|
| 96 |
+
st.session_state.discourse_live_state['last_result'] = result
|
| 97 |
+
st.session_state.discourse_live_state['analysis_count'] += 1
|
| 98 |
+
st.session_state.discourse_live_state['text_changed'] = False
|
| 99 |
+
|
| 100 |
+
# Guardar en base de datos
|
| 101 |
+
store_student_discourse_result(
|
| 102 |
+
st.session_state.username,
|
| 103 |
+
text_input1,
|
| 104 |
+
text_input2,
|
| 105 |
+
result
|
| 106 |
+
)
|
| 107 |
+
else:
|
| 108 |
+
st.error(result.get('message', 'Error en el an谩lisis'))
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
logger.error(f"Error en an谩lisis: {str(e)}")
|
| 112 |
+
st.error(discourse_t.get('error_processing', 'Error al procesar el texto'))
|
| 113 |
+
|
| 114 |
+
# Columna derecha: Visualizaci贸n de resultados
|
| 115 |
+
with result_col:
|
| 116 |
+
st.subheader(discourse_t.get('live_results', 'Resultados en vivo'))
|
| 117 |
+
|
| 118 |
+
if 'last_result' in st.session_state.discourse_live_state and \
|
| 119 |
+
st.session_state.discourse_live_state['last_result'] is not None:
|
| 120 |
+
|
| 121 |
+
# Mostrar resultados usando la misma funci贸n que el an谩lisis normal
|
| 122 |
+
display_discourse_results(
|
| 123 |
+
st.session_state.discourse_live_state['last_result'],
|
| 124 |
+
lang_code,
|
| 125 |
+
discourse_t
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
elif st.session_state.discourse_live_state.get('text_changed', False):
|
| 129 |
+
st.info(discourse_t.get('changes_pending',
|
| 130 |
+
'Los textos han cambiado. Presione Analizar para ver los nuevos resultados.'))
|
| 131 |
+
else:
|
| 132 |
+
st.info(discourse_t.get('initial_message',
|
| 133 |
+
'Ingrese los textos y presione Analizar para ver los resultados.'))
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
logger.error(f"Error general en interfaz del discurso en vivo: {str(e)}")
|
| 137 |
+
st.error(discourse_t.get('general_error', "Se produjo un error. Por favor, intente de nuevo."))
|
| 138 |
+
```
|