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