Update modules/discourse/discourse_interface.py
Browse files
modules/discourse/discourse_interface.py
CHANGED
@@ -12,6 +12,126 @@ from ..database.discourse_mongo_db import store_student_discourse_result
|
|
12 |
logger = logging.getLogger(__name__)
|
13 |
#############################################################################################
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def display_discourse_results(result, lang_code, discourse_t):
|
16 |
"""
|
17 |
Muestra los resultados del an谩lisis del discurso con conceptos en formato horizontal
|
@@ -121,54 +241,3 @@ def display_discourse_results(result, lang_code, discourse_t):
|
|
121 |
# Nota informativa sobre la comparaci贸n
|
122 |
st.info(discourse_t.get('comparison_note',
|
123 |
'La funcionalidad de comparaci贸n detallada estar谩 disponible en una pr贸xima actualizaci贸n.'))
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
##########################################################################################
|
130 |
-
def display_discourse_results(result, lang_code, discourse_t):
|
131 |
-
"""
|
132 |
-
Muestra los resultados del an谩lisis del discurso
|
133 |
-
"""
|
134 |
-
if not result.get('success'):
|
135 |
-
st.warning(discourse_t.get('no_results', 'No hay resultados disponibles'))
|
136 |
-
return
|
137 |
-
|
138 |
-
col1, col2 = st.columns(2)
|
139 |
-
|
140 |
-
# Documento 1
|
141 |
-
with col1:
|
142 |
-
with st.expander(discourse_t.get('doc1_title', 'Documento 1'), expanded=True):
|
143 |
-
st.subheader(discourse_t.get('key_concepts', 'Conceptos Clave'))
|
144 |
-
if 'key_concepts1' in result:
|
145 |
-
df1 = pd.DataFrame(result['key_concepts1'], columns=['Concepto', 'Frecuencia'])
|
146 |
-
df1['Frecuencia'] = df1['Frecuencia'].round(2)
|
147 |
-
st.table(df1)
|
148 |
-
|
149 |
-
if 'graph1' in result:
|
150 |
-
st.pyplot(result['graph1'])
|
151 |
-
else:
|
152 |
-
st.warning(discourse_t.get('graph_not_available', 'Gr谩fico no disponible'))
|
153 |
-
else:
|
154 |
-
st.warning(discourse_t.get('concepts_not_available', 'Conceptos no disponibles'))
|
155 |
-
|
156 |
-
# Documento 2
|
157 |
-
with col2:
|
158 |
-
with st.expander(discourse_t.get('doc2_title', 'Documento 2'), expanded=True):
|
159 |
-
st.subheader(discourse_t.get('key_concepts', 'Conceptos Clave'))
|
160 |
-
if 'key_concepts2' in result:
|
161 |
-
df2 = pd.DataFrame(result['key_concepts2'], columns=['Concepto', 'Frecuencia'])
|
162 |
-
df2['Frecuencia'] = df2['Frecuencia'].round(2)
|
163 |
-
st.table(df2)
|
164 |
-
|
165 |
-
if 'graph2' in result:
|
166 |
-
st.pyplot(result['graph2'])
|
167 |
-
else:
|
168 |
-
st.warning(discourse_t.get('graph_not_available', 'Gr谩fico no disponible'))
|
169 |
-
else:
|
170 |
-
st.warning(discourse_t.get('concepts_not_available', 'Conceptos no disponibles'))
|
171 |
-
|
172 |
-
# Nota informativa sobre la comparaci贸n
|
173 |
-
st.info(discourse_t.get('comparison_note',
|
174 |
-
'La funcionalidad de comparaci贸n detallada estar谩 disponible en una pr贸xima actualizaci贸n.'))
|
|
|
12 |
logger = logging.getLogger(__name__)
|
13 |
#############################################################################################
|
14 |
|
15 |
+
def display_discourse_interface(lang_code, nlp_models, discourse_t):
|
16 |
+
"""
|
17 |
+
Interfaz para el an谩lisis del discurso
|
18 |
+
Args:
|
19 |
+
lang_code: C贸digo del idioma actual
|
20 |
+
nlp_models: Modelos de spaCy cargados
|
21 |
+
discourse_t: Diccionario de traducciones
|
22 |
+
"""
|
23 |
+
try:
|
24 |
+
# 1. Inicializar estado si no existe
|
25 |
+
if 'discourse_state' not in st.session_state:
|
26 |
+
st.session_state.discourse_state = {
|
27 |
+
'analysis_count': 0,
|
28 |
+
'last_analysis': None,
|
29 |
+
'current_files': None
|
30 |
+
}
|
31 |
+
|
32 |
+
# 2. T铆tulo y descripci贸n
|
33 |
+
st.subheader(discourse_t.get('discourse_title', 'An谩lisis del Discurso'))
|
34 |
+
st.info(discourse_t.get('initial_instruction',
|
35 |
+
'Cargue dos archivos de texto para realizar un an谩lisis comparativo del discurso.'))
|
36 |
+
|
37 |
+
# 3. 脕rea de carga de archivos
|
38 |
+
col1, col2 = st.columns(2)
|
39 |
+
with col1:
|
40 |
+
st.markdown(discourse_t.get('file1_label', "**Documento 1 (Patr贸n)**"))
|
41 |
+
uploaded_file1 = st.file_uploader(
|
42 |
+
discourse_t.get('file_uploader1', "Cargar archivo 1"),
|
43 |
+
type=['txt'],
|
44 |
+
key=f"discourse_file1_{st.session_state.discourse_state['analysis_count']}"
|
45 |
+
)
|
46 |
+
|
47 |
+
with col2:
|
48 |
+
st.markdown(discourse_t.get('file2_label', "**Documento 2 (Comparaci贸n)**"))
|
49 |
+
uploaded_file2 = st.file_uploader(
|
50 |
+
discourse_t.get('file_uploader2', "Cargar archivo 2"),
|
51 |
+
type=['txt'],
|
52 |
+
key=f"discourse_file2_{st.session_state.discourse_state['analysis_count']}"
|
53 |
+
)
|
54 |
+
|
55 |
+
# 4. Bot贸n de an谩lisis
|
56 |
+
col1, col2, col3 = st.columns([1,2,1])
|
57 |
+
with col1:
|
58 |
+
analyze_button = st.button(
|
59 |
+
discourse_t.get('discourse_analyze_button', 'Analizar Discurso'),
|
60 |
+
key=generate_unique_key("discourse", "analyze_button"),
|
61 |
+
type="primary",
|
62 |
+
icon="馃攳",
|
63 |
+
disabled=not (uploaded_file1 and uploaded_file2),
|
64 |
+
use_container_width=True
|
65 |
+
)
|
66 |
+
|
67 |
+
# 5. Proceso de an谩lisis
|
68 |
+
if analyze_button and uploaded_file1 and uploaded_file2:
|
69 |
+
try:
|
70 |
+
with st.spinner(discourse_t.get('processing', 'Procesando an谩lisis...')):
|
71 |
+
# Leer contenido de archivos
|
72 |
+
text1 = uploaded_file1.getvalue().decode('utf-8')
|
73 |
+
text2 = uploaded_file2.getvalue().decode('utf-8')
|
74 |
+
|
75 |
+
# Realizar an谩lisis
|
76 |
+
result = perform_discourse_analysis(
|
77 |
+
text1,
|
78 |
+
text2,
|
79 |
+
nlp_models[lang_code],
|
80 |
+
lang_code
|
81 |
+
)
|
82 |
+
|
83 |
+
if result['success']:
|
84 |
+
# Guardar estado
|
85 |
+
st.session_state.discourse_result = result
|
86 |
+
st.session_state.discourse_state['analysis_count'] += 1
|
87 |
+
st.session_state.discourse_state['current_files'] = (
|
88 |
+
uploaded_file1.name,
|
89 |
+
uploaded_file2.name
|
90 |
+
)
|
91 |
+
|
92 |
+
# Guardar en base de datos
|
93 |
+
if store_student_discourse_result(
|
94 |
+
st.session_state.username,
|
95 |
+
text1,
|
96 |
+
text2,
|
97 |
+
result
|
98 |
+
):
|
99 |
+
st.success(discourse_t.get('success_message', 'An谩lisis guardado correctamente'))
|
100 |
+
|
101 |
+
# Mostrar resultados
|
102 |
+
display_discourse_results(result, lang_code, discourse_t)
|
103 |
+
else:
|
104 |
+
st.error(discourse_t.get('error_message', 'Error al guardar el an谩lisis'))
|
105 |
+
else:
|
106 |
+
st.error(discourse_t.get('analysis_error', 'Error en el an谩lisis'))
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
logger.error(f"Error en an谩lisis del discurso: {str(e)}")
|
110 |
+
st.error(discourse_t.get('error_processing', f'Error procesando archivos: {str(e)}'))
|
111 |
+
|
112 |
+
# 6. Mostrar resultados previos
|
113 |
+
elif 'discourse_result' in st.session_state and st.session_state.discourse_result is not None:
|
114 |
+
if st.session_state.discourse_state.get('current_files'):
|
115 |
+
st.info(
|
116 |
+
discourse_t.get('current_analysis_message', 'Mostrando an谩lisis de los archivos: {} y {}')
|
117 |
+
.format(*st.session_state.discourse_state['current_files'])
|
118 |
+
)
|
119 |
+
display_discourse_results(
|
120 |
+
st.session_state.discourse_result,
|
121 |
+
lang_code,
|
122 |
+
discourse_t
|
123 |
+
)
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
logger.error(f"Error general en interfaz del discurso: {str(e)}")
|
127 |
+
st.error(discourse_t.get('general_error', 'Se produjo un error. Por favor, intente de nuevo.'))
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
##########################################################################################
|
134 |
+
|
135 |
def display_discourse_results(result, lang_code, discourse_t):
|
136 |
"""
|
137 |
Muestra los resultados del an谩lisis del discurso con conceptos en formato horizontal
|
|
|
241 |
# Nota informativa sobre la comparaci贸n
|
242 |
st.info(discourse_t.get('comparison_note',
|
243 |
'La funcionalidad de comparaci贸n detallada estar谩 disponible en una pr贸xima actualizaci贸n.'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|