Update modules/database.py
Browse files- modules/database.py +88 -68
modules/database.py
CHANGED
@@ -8,6 +8,8 @@ import certifi
|
|
8 |
from datetime import datetime
|
9 |
import io
|
10 |
import base64
|
|
|
|
|
11 |
import bcrypt
|
12 |
print(f"Bcrypt version: {bcrypt.__version__}")
|
13 |
|
@@ -127,71 +129,93 @@ def get_user(username):
|
|
127 |
|
128 |
################################################################################
|
129 |
# Funciones para Cosmos DB MongoDB API (análisis de texto)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
def get_student_data(username):
|
132 |
-
if analysis_collection is None:
|
133 |
-
logger.error("La conexión a MongoDB no está inicializada")
|
134 |
-
return None
|
135 |
-
|
136 |
-
try:
|
137 |
-
logger.info(f"Buscando datos para el usuario: {username}")
|
138 |
-
cursor = analysis_collection.find({"username": username})
|
139 |
-
|
140 |
-
formatted_data = {
|
141 |
-
"username": username,
|
142 |
-
"entries": [],
|
143 |
-
"entries_count": 0,
|
144 |
-
"word_count": {},
|
145 |
-
"semantic_analyses": [],
|
146 |
-
"discourse_analyses": [],
|
147 |
-
"chat_history": []
|
148 |
-
}
|
149 |
-
|
150 |
-
for entry in cursor:
|
151 |
-
formatted_entry = {
|
152 |
-
"timestamp": entry["timestamp"],
|
153 |
-
"text": entry["text"],
|
154 |
-
"analysis_type": entry.get("analysis_type", "morphosyntax")
|
155 |
-
}
|
156 |
-
|
157 |
-
if formatted_entry["analysis_type"] == "morphosyntax":
|
158 |
-
formatted_entry.update({
|
159 |
-
"word_count": entry.get("word_count", {}),
|
160 |
-
"arc_diagrams": entry.get("arc_diagrams", [])
|
161 |
-
})
|
162 |
-
for category, count in formatted_entry["word_count"].items():
|
163 |
-
formatted_data["word_count"][category] = formatted_data["word_count"].get(category, 0) + count
|
164 |
-
|
165 |
-
elif formatted_entry["analysis_type"] == "semantic":
|
166 |
-
formatted_entry["network_diagram"] = entry.get("network_diagram", "")
|
167 |
-
formatted_data["semantic_analyses"].append(formatted_entry)
|
168 |
-
|
169 |
-
elif formatted_entry["analysis_type"] == "discourse":
|
170 |
-
formatted_entry.update({
|
171 |
-
"graph1": entry.get("graph1", ""),
|
172 |
-
"graph2": entry.get("graph2", "")
|
173 |
-
})
|
174 |
-
formatted_data["discourse_analyses"].append(formatted_entry)
|
175 |
-
|
176 |
-
formatted_data["entries"].append(formatted_entry)
|
177 |
-
|
178 |
-
formatted_data["entries_count"] = len(formatted_data["entries"])
|
179 |
-
formatted_data["entries"].sort(key=lambda x: x["timestamp"], reverse=True)
|
180 |
-
|
181 |
-
for entry in formatted_data["entries"]:
|
182 |
-
entry["timestamp"] = entry["timestamp"].isoformat()
|
183 |
-
|
184 |
-
# Obtener el historial del chat
|
185 |
-
chat_cursor = chat_collection.find({"username": username})
|
186 |
-
formatted_data["chat_history"] = list(chat_cursor)
|
187 |
-
|
188 |
-
logger.info(f"Datos formateados para {username}: {formatted_data}")
|
189 |
-
return formatted_data
|
190 |
-
|
191 |
-
except Exception as e:
|
192 |
-
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
193 |
-
return None
|
194 |
-
|
195 |
#######################################################################################################
|
196 |
|
197 |
def store_morphosyntax_result(username, text, repeated_words, arc_diagrams):
|
@@ -247,10 +271,6 @@ def store_semantic_result(username, text, network_diagram):
|
|
247 |
return False
|
248 |
|
249 |
###############################################################################################################
|
250 |
-
import io
|
251 |
-
import base64
|
252 |
-
import matplotlib.pyplot as plt
|
253 |
-
from matplotlib.figure import Figure
|
254 |
|
255 |
def store_discourse_analysis_result(username, text1, text2, graph1, graph2):
|
256 |
try:
|
|
|
8 |
from datetime import datetime
|
9 |
import io
|
10 |
import base64
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
from matplotlib.figure import Figure
|
13 |
import bcrypt
|
14 |
print(f"Bcrypt version: {bcrypt.__version__}")
|
15 |
|
|
|
129 |
|
130 |
################################################################################
|
131 |
# Funciones para Cosmos DB MongoDB API (análisis de texto)
|
132 |
+
def display_student_progress(username, lang_code='es'):
|
133 |
+
student_data = get_student_data(username)
|
134 |
+
|
135 |
+
if student_data is None:
|
136 |
+
st.warning("No se encontraron datos para este estudiante.")
|
137 |
+
st.info("Intenta realizar algunos análisis de texto primero.")
|
138 |
+
return
|
139 |
+
|
140 |
+
st.title(f"Progreso de {username}")
|
141 |
+
|
142 |
+
if student_data['entries_count'] > 0:
|
143 |
+
# Mostrar el conteo de palabras
|
144 |
+
if student_data['word_count']:
|
145 |
+
with st.expander("Total de palabras por categoría gramatical", expanded=False):
|
146 |
+
df = pd.DataFrame(list(student_data['word_count'].items()), columns=['category', 'count'])
|
147 |
+
df['label'] = df.apply(lambda x: f"{POS_TRANSLATIONS[lang_code].get(x['category'], x['category'])}", axis=1)
|
148 |
+
df = df.sort_values('count', ascending=False)
|
149 |
+
|
150 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
151 |
+
bars = ax.bar(df['label'], df['count'], color=df['category'])
|
152 |
+
|
153 |
+
ax.set_xlabel('Categoría Gramatical')
|
154 |
+
ax.set_ylabel('Cantidad de Palabras')
|
155 |
+
ax.set_title('Total de palabras por categoría gramatical')
|
156 |
+
plt.xticks(rotation=45, ha='right')
|
157 |
+
|
158 |
+
for bar in bars:
|
159 |
+
height = bar.get_height()
|
160 |
+
ax.text(bar.get_x() + bar.get_width()/2., height, f'{height}', ha='center', va='bottom')
|
161 |
+
|
162 |
+
plt.tight_layout()
|
163 |
+
st.pyplot(fig)
|
164 |
+
|
165 |
+
# Mostrar análisis morfosintáctico
|
166 |
+
morphosyntax_entries = [entry for entry in student_data['entries'] if entry['analysis_type'] == 'morphosyntax']
|
167 |
+
if morphosyntax_entries:
|
168 |
+
with st.expander("Análisis Morfosintáctico - Diagramas de Arco", expanded=False):
|
169 |
+
for i, entry in enumerate(morphosyntax_entries):
|
170 |
+
st.subheader(f"Análisis {i+1} - {entry['timestamp']}")
|
171 |
+
st.write(entry['text'])
|
172 |
+
for j, diagram in enumerate(entry.get('arc_diagrams', [])):
|
173 |
+
st.subheader(f"Diagrama de Arco {j+1}")
|
174 |
+
st.write(diagram, unsafe_allow_html=True)
|
175 |
+
|
176 |
+
# Mostrar análisis semántico
|
177 |
+
if student_data['semantic_analyses']:
|
178 |
+
with st.expander("Análisis Semántico - Diagramas de Red", expanded=False):
|
179 |
+
for i, entry in enumerate(student_data['semantic_analyses']):
|
180 |
+
st.subheader(f"Análisis Semántico {i+1} - {entry['timestamp']}")
|
181 |
+
st.write(entry['text'])
|
182 |
+
if 'network_diagram' in entry:
|
183 |
+
image_bytes = base64.b64decode(entry['network_diagram'])
|
184 |
+
st.image(image_bytes)
|
185 |
+
|
186 |
+
# Mostrar análisis del discurso
|
187 |
+
if student_data['discourse_analyses']:
|
188 |
+
with st.expander("Análisis del Discurso - Comparación de Grafos", expanded=False):
|
189 |
+
for i, entry in enumerate(student_data['discourse_analyses']):
|
190 |
+
st.subheader(f"Análisis del Discurso {i+1} - {entry['timestamp']}")
|
191 |
+
st.write("Texto del documento patrón:")
|
192 |
+
st.write(entry.get('text1', 'No disponible'))
|
193 |
+
st.write("Texto del documento comparado:")
|
194 |
+
st.write(entry.get('text2', 'No disponible'))
|
195 |
+
if 'graph1' in entry:
|
196 |
+
st.image(base64.b64decode(entry['graph1']))
|
197 |
+
if 'graph2' in entry:
|
198 |
+
st.image(base64.b64decode(entry['graph2']))
|
199 |
+
|
200 |
+
# Mostrar conversaciones del chat
|
201 |
+
if student_data['chat_history']:
|
202 |
+
with st.expander("Historial de Conversaciones del Chat", expanded=False):
|
203 |
+
for i, chat in enumerate(student_data['chat_history']):
|
204 |
+
st.subheader(f"Conversación {i+1} - {chat['timestamp']}")
|
205 |
+
for message in chat['messages']:
|
206 |
+
if message['role'] == 'user':
|
207 |
+
st.write("Usuario: " + message['content'])
|
208 |
+
else:
|
209 |
+
st.write("Asistente: " + message['content'])
|
210 |
+
st.write("---")
|
211 |
+
else:
|
212 |
+
st.warning("No se encontraron entradas para este estudiante.")
|
213 |
+
st.info("Intenta realizar algunos análisis de texto primero.")
|
214 |
+
|
215 |
+
# Añadir logs para depuración
|
216 |
+
st.write("Datos del estudiante (para depuración):")
|
217 |
+
st.json(student_data)
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
#######################################################################################################
|
220 |
|
221 |
def store_morphosyntax_result(username, text, repeated_words, arc_diagrams):
|
|
|
271 |
return False
|
272 |
|
273 |
###############################################################################################################
|
|
|
|
|
|
|
|
|
274 |
|
275 |
def store_discourse_analysis_result(username, text1, text2, graph1, graph2):
|
276 |
try:
|