Update modules/studentact/student_activities_v2.py
Browse files
modules/studentact/student_activities_v2.py
CHANGED
@@ -16,119 +16,169 @@ import base64
|
|
16 |
import seaborn as sns
|
17 |
import logging
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
###################################################################################
|
22 |
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
ax.set_xlabel(t.get("analysis_type", "Tipo de análisis"))
|
43 |
-
ax.set_ylabel(t.get("count", "Cantidad"))
|
44 |
-
st.pyplot(fig)
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
if not morphosyntax_entries:
|
50 |
-
st.warning("No se encontraron análisis morfosintácticos.")
|
51 |
-
for entry in morphosyntax_entries:
|
52 |
-
st.subheader(f"{t.get('analysis_of', 'Análisis del')} {entry['timestamp']}")
|
53 |
-
if 'arc_diagrams' in entry and entry['arc_diagrams']:
|
54 |
-
try:
|
55 |
-
st.write(entry['arc_diagrams'][0], unsafe_allow_html=True)
|
56 |
-
except Exception as e:
|
57 |
-
logger.error(f"Error al mostrar diagrama de arco: {str(e)}")
|
58 |
-
st.error("Error al mostrar el diagrama de arco.")
|
59 |
-
else:
|
60 |
-
st.write(t.get("no_arc_diagram", "No se encontró diagrama de arco para este análisis."))
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
if not semantic_entries:
|
66 |
-
st.warning("No se encontraron análisis semánticos.")
|
67 |
-
for entry in semantic_entries:
|
68 |
-
st.subheader(f"{t.get('analysis_of', 'Análisis del')} {entry['timestamp']}")
|
69 |
-
if 'key_concepts' in entry:
|
70 |
-
st.write(t.get("key_concepts", "Conceptos clave:"))
|
71 |
-
concepts_str = " | ".join([f"{concept} ({frequency:.2f})" for concept, frequency in entry['key_concepts']])
|
72 |
-
st.markdown(f"<div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px;'>{concepts_str}</div>", unsafe_allow_html=True)
|
73 |
-
if 'graph' in entry:
|
74 |
-
try:
|
75 |
-
img_bytes = base64.b64decode(entry['graph'])
|
76 |
-
st.image(img_bytes, caption=t.get("conceptual_relations_graph", "Gráfico de relaciones conceptuales"))
|
77 |
-
except Exception as e:
|
78 |
-
logger.error(f"Error al mostrar gráfico semántico: {str(e)}")
|
79 |
-
st.error(t.get("graph_display_error", f"No se pudo mostrar el gráfico: {str(e)}"))
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
st.write(f"{t.get('key_concepts', 'Conceptos clave')} {t.get('document', 'documento')} {i}:")
|
89 |
-
concepts_str = " | ".join([f"{concept} ({frequency:.2f})" for concept, frequency in entry[f'key_concepts{i}']])
|
90 |
-
st.markdown(f"<div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px;'>{concepts_str}</div>", unsafe_allow_html=True)
|
91 |
-
try:
|
92 |
-
if 'combined_graph' in entry and entry['combined_graph']:
|
93 |
-
img_bytes = base64.b64decode(entry['combined_graph'])
|
94 |
-
st.image(img_bytes, caption=t.get("combined_graph", "Gráfico combinado"))
|
95 |
-
elif 'graph1' in entry and 'graph2' in entry:
|
96 |
-
col1, col2 = st.columns(2)
|
97 |
-
with col1:
|
98 |
-
if entry['graph1']:
|
99 |
-
img_bytes1 = base64.b64decode(entry['graph1'])
|
100 |
-
st.image(img_bytes1, caption=t.get("graph_doc1", "Gráfico documento 1"))
|
101 |
-
with col2:
|
102 |
-
if entry['graph2']:
|
103 |
-
img_bytes2 = base64.b64decode(entry['graph2'])
|
104 |
-
st.image(img_bytes2, caption=t.get("graph_doc2", "Gráfico documento 2"))
|
105 |
-
except Exception as e:
|
106 |
-
st.error(t.get("graph_display_error", f"No se pudieron mostrar los gráficos: {str(e)}"))
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
for message in chat['messages']:
|
114 |
-
|
115 |
-
st.
|
116 |
-
else:
|
117 |
-
st.write(f"{t.get('assistant', 'Asistente')}: {message['content']}")
|
118 |
-
st.write("---")
|
119 |
-
else:
|
120 |
-
st.write(t.get("no_chat_history", "No se encontraron conversaciones con el ChatBot."))
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
st.
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
# Mostrar conteo de tipos de análisis
|
128 |
-
analysis_types = [entry['analysis_type'] for entry in username['entries']]
|
129 |
-
type_counts = {t: analysis_types.count(t) for t in set(analysis_types)}
|
130 |
-
st.write("Conteo de tipos de análisis:")
|
131 |
-
st.write(type_counts)
|
132 |
|
133 |
|
134 |
|
|
|
16 |
import seaborn as sns
|
17 |
import logging
|
18 |
|
19 |
+
# Importaciones de la base de datos
|
20 |
+
from ..database.morphosintax_mongo_db import get_student_morphosyntax_analysis
|
21 |
+
from ..database.semantic_mongo_db import get_student_semantic_analysis
|
22 |
+
from ..database.discourse_mongo_db import get_student_discourse_analysis
|
23 |
+
from ..database.chat_mongo_db import get_chat_history
|
24 |
+
|
25 |
logger = logging.getLogger(__name__)
|
26 |
|
27 |
###################################################################################
|
28 |
|
29 |
+
def display_student_activities(username: str, lang_code: str, t: dict):
|
30 |
+
"""
|
31 |
+
Muestra todas las actividades del estudiante
|
32 |
+
Args:
|
33 |
+
username: Nombre del estudiante
|
34 |
+
lang_code: Código del idioma
|
35 |
+
t: Diccionario de traducciones
|
36 |
+
"""
|
37 |
+
try:
|
38 |
+
st.header(t.get('activities_title', 'Mis Actividades'))
|
39 |
|
40 |
+
# Tabs para diferentes tipos de análisis
|
41 |
+
tabs = st.tabs([
|
42 |
+
t.get('morpho_activities', 'Análisis Morfosintáctico'),
|
43 |
+
t.get('semantic_activities', 'Análisis Semántico'),
|
44 |
+
t.get('discourse_activities', 'Análisis del Discurso'),
|
45 |
+
t.get('chat_activities', 'Conversaciones con el Asistente')
|
46 |
+
])
|
47 |
|
48 |
+
# Tab de Análisis Morfosintáctico
|
49 |
+
with tabs[0]:
|
50 |
+
display_morphosyntax_activities(username, t)
|
51 |
|
52 |
+
# Tab de Análisis Semántico
|
53 |
+
with tabs[1]:
|
54 |
+
display_semantic_activities(username, t)
|
55 |
|
56 |
+
# Tab de Análisis del Discurso
|
57 |
+
with tabs[2]:
|
58 |
+
display_discourse_activities(username, t)
|
|
|
|
|
|
|
59 |
|
60 |
+
# Tab de Conversaciones del Chat
|
61 |
+
with tabs[3]:
|
62 |
+
display_chat_activities(username, t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"Error mostrando actividades: {str(e)}")
|
66 |
+
st.error(t.get('error_loading_activities', 'Error al cargar las actividades'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
def display_morphosyntax_activities(username: str, t: dict):
|
69 |
+
"""Muestra actividades de análisis morfosintáctico"""
|
70 |
+
try:
|
71 |
+
analyses = get_student_morphosyntax_analysis(username)
|
72 |
+
if not analyses:
|
73 |
+
st.info(t.get('no_morpho_analyses', 'No hay análisis morfosintácticos registrados'))
|
74 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
for analysis in analyses:
|
77 |
+
with st.expander(
|
78 |
+
f"{t.get('analysis_date', 'Fecha')}: {analysis['timestamp']}",
|
79 |
+
expanded=False
|
80 |
+
):
|
81 |
+
st.text(f"{t.get('analyzed_text', 'Texto analizado')}:")
|
82 |
+
st.write(analysis['text'])
|
83 |
+
|
84 |
+
if 'arc_diagrams' in analysis:
|
85 |
+
st.subheader(t.get('syntactic_diagrams', 'Diagramas sintácticos'))
|
86 |
+
for diagram in analysis['arc_diagrams']:
|
87 |
+
st.write(diagram, unsafe_allow_html=True)
|
88 |
+
|
89 |
+
except Exception as e:
|
90 |
+
logger.error(f"Error mostrando análisis morfosintáctico: {str(e)}")
|
91 |
+
st.error(t.get('error_morpho', 'Error al mostrar análisis morfosintáctico'))
|
92 |
+
|
93 |
+
def display_semantic_activities(username: str, t: dict):
|
94 |
+
"""Muestra actividades de análisis semántico"""
|
95 |
+
try:
|
96 |
+
analyses = get_student_semantic_analysis(username)
|
97 |
+
if not analyses:
|
98 |
+
st.info(t.get('no_semantic_analyses', 'No hay análisis semánticos registrados'))
|
99 |
+
return
|
100 |
+
|
101 |
+
for analysis in analyses:
|
102 |
+
with st.expander(
|
103 |
+
f"{t.get('analysis_date', 'Fecha')}: {analysis['timestamp']}",
|
104 |
+
expanded=False
|
105 |
+
):
|
106 |
+
st.text(f"{t.get('analyzed_file', 'Archivo analizado')}:")
|
107 |
+
st.write(analysis['text'])
|
108 |
+
|
109 |
+
if 'key_concepts' in analysis:
|
110 |
+
st.subheader(t.get('key_concepts', 'Conceptos clave'))
|
111 |
+
df = pd.DataFrame(analysis['key_concepts'])
|
112 |
+
st.dataframe(df)
|
113 |
+
|
114 |
+
except Exception as e:
|
115 |
+
logger.error(f"Error mostrando análisis semántico: {str(e)}")
|
116 |
+
st.error(t.get('error_semantic', 'Error al mostrar análisis semántico'))
|
117 |
+
|
118 |
+
def display_discourse_activities(username: str, t: dict):
|
119 |
+
"""Muestra actividades de análisis del discurso"""
|
120 |
+
try:
|
121 |
+
analyses = get_student_discourse_analysis(username)
|
122 |
+
if not analyses:
|
123 |
+
st.info(t.get('no_discourse_analyses', 'No hay análisis del discurso registrados'))
|
124 |
+
return
|
125 |
+
|
126 |
+
for analysis in analyses:
|
127 |
+
with st.expander(
|
128 |
+
f"{t.get('analysis_date', 'Fecha')}: {analysis['timestamp']}",
|
129 |
+
expanded=False
|
130 |
+
):
|
131 |
+
col1, col2 = st.columns(2)
|
132 |
+
with col1:
|
133 |
+
st.text(f"{t.get('text_1', 'Texto 1')}:")
|
134 |
+
st.write(analysis['text1'])
|
135 |
+
with col2:
|
136 |
+
st.text(f"{t.get('text_2', 'Texto 2')}:")
|
137 |
+
st.write(analysis['text2'])
|
138 |
+
|
139 |
+
if 'key_concepts1' in analysis and 'key_concepts2' in analysis:
|
140 |
+
display_discourse_comparison(analysis, t)
|
141 |
+
|
142 |
+
except Exception as e:
|
143 |
+
logger.error(f"Error mostrando análisis del discurso: {str(e)}")
|
144 |
+
st.error(t.get('error_discourse', 'Error al mostrar análisis del discurso'))
|
145 |
+
|
146 |
+
def display_chat_activities(username: str, t: dict):
|
147 |
+
"""Muestra historial de conversaciones del chat"""
|
148 |
+
try:
|
149 |
+
chat_history = get_chat_history(username, limit=50)
|
150 |
+
if not chat_history:
|
151 |
+
st.info(t.get('no_chat_history', 'No hay conversaciones registradas'))
|
152 |
+
return
|
153 |
+
|
154 |
+
for chat in chat_history:
|
155 |
+
with st.expander(
|
156 |
+
f"{t.get('chat_date', 'Fecha de conversación')}: {chat['timestamp']}",
|
157 |
+
expanded=False
|
158 |
+
):
|
159 |
for message in chat['messages']:
|
160 |
+
with st.chat_message(message["role"]):
|
161 |
+
st.markdown(message["content"])
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
except Exception as e:
|
164 |
+
logger.error(f"Error mostrando historial del chat: {str(e)}")
|
165 |
+
st.error(t.get('error_chat', 'Error al mostrar historial del chat'))
|
166 |
+
|
167 |
+
def display_discourse_comparison(analysis: dict, t: dict):
|
168 |
+
"""Muestra la comparación de análisis del discurso"""
|
169 |
+
st.subheader(t.get('comparison_results', 'Resultados de la comparación'))
|
170 |
+
|
171 |
+
col1, col2 = st.columns(2)
|
172 |
+
with col1:
|
173 |
+
st.markdown(f"**{t.get('concepts_text_1', 'Conceptos Texto 1')}**")
|
174 |
+
df1 = pd.DataFrame(analysis['key_concepts1'])
|
175 |
+
st.dataframe(df1)
|
176 |
+
|
177 |
+
with col2:
|
178 |
+
st.markdown(f"**{t.get('concepts_text_2', 'Conceptos Texto 2')}**")
|
179 |
+
df2 = pd.DataFrame(analysis['key_concepts2'])
|
180 |
+
st.dataframe(df2)
|
181 |
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
|
184 |
|