Spaces:
Sleeping
Sleeping
Delete modules/semantic/semantic_process.py
Browse files
modules/semantic/semantic_process.py
DELETED
@@ -1,116 +0,0 @@
|
|
1 |
-
# modules/semantic/semantic_process.py
|
2 |
-
import streamlit as st
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
import io
|
5 |
-
import base64
|
6 |
-
import logging
|
7 |
-
|
8 |
-
##############################################################
|
9 |
-
from ..text_analysis.semantic_analysis import (
|
10 |
-
perform_semantic_analysis,
|
11 |
-
identify_key_concepts,
|
12 |
-
create_concept_graph,
|
13 |
-
visualize_concept_graph
|
14 |
-
)
|
15 |
-
|
16 |
-
##############################################################################
|
17 |
-
from ..database.semantic_mongo_db import store_student_semantic_result
|
18 |
-
#####################################################################
|
19 |
-
logger = logging.getLogger(__name__)
|
20 |
-
#########################################################
|
21 |
-
|
22 |
-
def process_semantic_input(text, lang_code, nlp_models, t, semantic_t):
|
23 |
-
"""
|
24 |
-
Procesa el texto ingresado para realizar el análisis semántico.
|
25 |
-
"""
|
26 |
-
try:
|
27 |
-
logger.info(f"Iniciando análisis semántico para texto de {len(text)} caracteres")
|
28 |
-
|
29 |
-
# Realizar el análisis semántico
|
30 |
-
nlp = nlp_models[lang_code]
|
31 |
-
####################################################################
|
32 |
-
analysis_result = perform_semantic_analysis(text, nlp, lang_code, semantic_t)
|
33 |
-
#########################################################################
|
34 |
-
|
35 |
-
if not analysis_result['success']:
|
36 |
-
return {
|
37 |
-
'success': False,
|
38 |
-
'message': analysis_result['error'],
|
39 |
-
'analysis': None
|
40 |
-
}
|
41 |
-
|
42 |
-
logger.info("Análisis semántico completado. Guardando resultados...")
|
43 |
-
|
44 |
-
# Intentar guardar en la base de datos
|
45 |
-
try:
|
46 |
-
store_result = store_student_semantic_result(
|
47 |
-
st.session_state.username,
|
48 |
-
text,
|
49 |
-
analysis_result
|
50 |
-
)
|
51 |
-
if not store_result:
|
52 |
-
logger.warning("No se pudo guardar el análisis en la base de datos")
|
53 |
-
except Exception as db_error:
|
54 |
-
logger.error(f"Error al guardar en base de datos: {str(db_error)}")
|
55 |
-
|
56 |
-
# Devolver el resultado incluso si falla el guardado
|
57 |
-
return {
|
58 |
-
'success': True,
|
59 |
-
'message': t.get('success_message', 'Analysis completed successfully'),
|
60 |
-
'analysis': {
|
61 |
-
'key_concepts': analysis_result['key_concepts'],
|
62 |
-
'concept_graph': analysis_result['concept_graph']
|
63 |
-
}
|
64 |
-
}
|
65 |
-
|
66 |
-
except Exception as e:
|
67 |
-
logger.error(f"Error en process_semantic_input: {str(e)}")
|
68 |
-
return {
|
69 |
-
'success': False,
|
70 |
-
'message': str(e),
|
71 |
-
'analysis': None
|
72 |
-
}
|
73 |
-
|
74 |
-
|
75 |
-
##################################################################################
|
76 |
-
def format_semantic_results(analysis_result, t):
|
77 |
-
"""
|
78 |
-
Formatea los resultados del análisis para su visualización.
|
79 |
-
"""
|
80 |
-
try:
|
81 |
-
if not analysis_result['success']:
|
82 |
-
return {
|
83 |
-
'formatted_text': analysis_result['message'],
|
84 |
-
'visualizations': None
|
85 |
-
}
|
86 |
-
|
87 |
-
formatted_sections = []
|
88 |
-
analysis = analysis_result['analysis']
|
89 |
-
|
90 |
-
# Formatear conceptos clave
|
91 |
-
if 'key_concepts' in analysis:
|
92 |
-
concepts_section = [f"### {t.get('key_concepts', 'Key Concepts')}"]
|
93 |
-
concepts_section.extend([
|
94 |
-
f"- {concept}: {frequency:.2f}"
|
95 |
-
for concept, frequency in analysis['key_concepts']
|
96 |
-
])
|
97 |
-
formatted_sections.append('\n'.join(concepts_section))
|
98 |
-
|
99 |
-
return {
|
100 |
-
'formatted_text': '\n\n'.join(formatted_sections),
|
101 |
-
'visualizations': {
|
102 |
-
'concept_graph': analysis.get('concept_graph')
|
103 |
-
}
|
104 |
-
}
|
105 |
-
|
106 |
-
except Exception as e:
|
107 |
-
logger.error(f"Error en format_semantic_results: {str(e)}")
|
108 |
-
return {
|
109 |
-
'formatted_text': str(e),
|
110 |
-
'visualizations': None
|
111 |
-
}
|
112 |
-
|
113 |
-
__all__ = [
|
114 |
-
'process_semantic_input',
|
115 |
-
'format_semantic_results'
|
116 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|