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