Spaces:
Sleeping
Sleeping
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -1,89 +1,148 @@
|
|
| 1 |
-
#
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
import io
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
-
import plotly.graph_objects as go
|
| 9 |
import pandas as pd
|
| 10 |
-
import
|
| 11 |
-
import time
|
| 12 |
-
from datetime import datetime
|
| 13 |
-
from streamlit_player import st_player # Necesitar谩s instalar esta librer铆a: pip install streamlit-player
|
| 14 |
-
from spacy import displacy
|
| 15 |
-
import logging
|
| 16 |
-
import random
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
from ..utils.widget_utils import generate_unique_key
|
| 20 |
-
|
| 21 |
-
from ..database.
|
| 22 |
-
from ..database.chat_db import store_chat_history
|
| 23 |
-
from ..database.morphosintaxis_export import export_user_interactions
|
| 24 |
|
| 25 |
import logging
|
| 26 |
logger = logging.getLogger(__name__)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
text_input = st.text_area(
|
| 37 |
-
|
| 38 |
height=150,
|
| 39 |
-
placeholder=
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
# Opci贸n para cargar archivo
|
| 43 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
if st.button(
|
|
|
|
|
|
|
|
|
|
| 46 |
if text_input or uploaded_file is not None:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
-
st.warning(
|
| 68 |
-
|
| 69 |
-
elif 'semantic_result' in st.session_state:
|
| 70 |
-
|
| 71 |
-
# Si hay un resultado guardado, mostrarlo
|
| 72 |
-
display_semantic_results(st.session_state.semantic_result, lang_code, t)
|
| 73 |
-
|
| 74 |
-
else:
|
| 75 |
-
st.info(t['initial_message']) # Aseg煤rate de que 'initial_message' est茅 en tus traducciones
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return
|
| 81 |
|
|
|
|
|
|
|
| 82 |
# Mostrar conceptos clave
|
| 83 |
-
with st.expander(
|
| 84 |
-
concept_text = " | ".join([
|
|
|
|
|
|
|
|
|
|
| 85 |
st.write(concept_text)
|
| 86 |
|
| 87 |
-
# Mostrar
|
| 88 |
-
with st.expander(
|
| 89 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#modules/semantic/semantic_interface.py
|
| 2 |
import streamlit as st
|
| 3 |
+
from streamlit_float import *
|
| 4 |
+
from streamlit_antd_components import *
|
| 5 |
+
from streamlit.components.v1 import html
|
| 6 |
import io
|
| 7 |
from io import BytesIO
|
| 8 |
import base64
|
| 9 |
import matplotlib.pyplot as plt
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
from .semantic_process import (
|
| 14 |
+
process_semantic_input,
|
| 15 |
+
format_semantic_results
|
| 16 |
+
)
|
| 17 |
|
| 18 |
from ..utils.widget_utils import generate_unique_key
|
| 19 |
+
from ..database.semantic_mongo_db import store_student_semantic_result
|
| 20 |
+
from ..database.semantics_export import export_user_interactions
|
|
|
|
|
|
|
| 21 |
|
| 22 |
import logging
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
+
def display_semantic_interface(lang_code, nlp_models, semantic_t):
|
| 26 |
+
"""
|
| 27 |
+
Interfaz para el an谩lisis sem谩ntico
|
| 28 |
+
Args:
|
| 29 |
+
lang_code: C贸digo del idioma actual
|
| 30 |
+
nlp_models: Modelos de spaCy cargados
|
| 31 |
+
semantic_t: Diccionario de traducciones sem谩nticas
|
| 32 |
+
"""
|
| 33 |
+
# Inicializar el estado de la entrada
|
| 34 |
+
input_key = f"semantic_input_{lang_code}"
|
| 35 |
+
if input_key not in st.session_state:
|
| 36 |
+
st.session_state[input_key] = ""
|
| 37 |
+
|
| 38 |
+
# Inicializar contador de an谩lisis si no existe
|
| 39 |
+
if 'semantic_analysis_counter' not in st.session_state:
|
| 40 |
+
st.session_state.semantic_analysis_counter = 0
|
| 41 |
+
|
| 42 |
+
# Campo de entrada de texto
|
| 43 |
text_input = st.text_area(
|
| 44 |
+
semantic_t.get('text_input_label', 'Enter text to analyze'),
|
| 45 |
height=150,
|
| 46 |
+
placeholder=semantic_t.get('text_input_placeholder', 'Enter your text here...'),
|
| 47 |
+
value=st.session_state[input_key],
|
| 48 |
+
key=f"text_area_{lang_code}_{st.session_state.semantic_analysis_counter}"
|
| 49 |
)
|
| 50 |
|
| 51 |
# Opci贸n para cargar archivo
|
| 52 |
+
uploaded_file = st.file_uploader(
|
| 53 |
+
semantic_t.get('file_uploader', 'Or upload a text file'),
|
| 54 |
+
type=['txt'],
|
| 55 |
+
key=f"file_uploader_{lang_code}_{st.session_state.semantic_analysis_counter}"
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
if st.button(
|
| 59 |
+
semantic_t.get('analyze_button', 'Analyze text'),
|
| 60 |
+
key=f"analyze_button_{lang_code}_{st.session_state.semantic_analysis_counter}"
|
| 61 |
+
):
|
| 62 |
if text_input or uploaded_file is not None:
|
| 63 |
+
try:
|
| 64 |
+
with st.spinner(semantic_t.get('processing', 'Processing...')):
|
| 65 |
+
# Obtener el texto a analizar
|
| 66 |
+
text_content = uploaded_file.getvalue().decode('utf-8') if uploaded_file else text_input
|
| 67 |
+
|
| 68 |
+
# Realizar el an谩lisis
|
| 69 |
+
analysis_result = process_semantic_input(
|
| 70 |
+
text_content,
|
| 71 |
+
lang_code,
|
| 72 |
+
nlp_models,
|
| 73 |
+
semantic_t
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Guardar resultado en el estado de la sesi贸n
|
| 77 |
+
st.session_state.semantic_result = analysis_result
|
| 78 |
+
st.session_state.semantic_analysis_counter += 1
|
| 79 |
+
|
| 80 |
+
# Mostrar resultados
|
| 81 |
+
display_semantic_results(
|
| 82 |
+
st.session_state.semantic_result,
|
| 83 |
+
lang_code,
|
| 84 |
+
semantic_t
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logger.error(f"Error en an谩lisis sem谩ntico: {str(e)}")
|
| 89 |
+
st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
|
| 90 |
else:
|
| 91 |
+
st.warning(semantic_t.get('warning_message', 'Please enter text or upload a file'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Si no se presion贸 el bot贸n, verificar si hay resultados previos
|
| 94 |
+
elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
|
| 95 |
+
display_semantic_results(
|
| 96 |
+
st.session_state.semantic_result,
|
| 97 |
+
lang_code,
|
| 98 |
+
semantic_t
|
| 99 |
+
)
|
| 100 |
+
else:
|
| 101 |
+
st.info(semantic_t.get('initial_message', 'Enter text to begin analysis'))
|
| 102 |
+
|
| 103 |
+
def display_semantic_results(result, lang_code, semantic_t):
|
| 104 |
+
"""
|
| 105 |
+
Muestra los resultados del an谩lisis sem谩ntico
|
| 106 |
+
Args:
|
| 107 |
+
result: Resultados del an谩lisis
|
| 108 |
+
lang_code: C贸digo del idioma
|
| 109 |
+
semantic_t: Diccionario de traducciones
|
| 110 |
+
"""
|
| 111 |
+
if result is None or not result['success']:
|
| 112 |
+
st.warning(semantic_t.get('no_results', 'No results available'))
|
| 113 |
return
|
| 114 |
|
| 115 |
+
analysis = result['analysis']
|
| 116 |
+
|
| 117 |
# Mostrar conceptos clave
|
| 118 |
+
with st.expander(semantic_t.get('key_concepts', 'Key Concepts'), expanded=True):
|
| 119 |
+
concept_text = " | ".join([
|
| 120 |
+
f"{concept} ({frequency:.2f})"
|
| 121 |
+
for concept, frequency in analysis['key_concepts']
|
| 122 |
+
])
|
| 123 |
st.write(concept_text)
|
| 124 |
|
| 125 |
+
# Mostrar gr谩fico de relaciones conceptuales
|
| 126 |
+
with st.expander(semantic_t.get('conceptual_relations', 'Conceptual Relations'), expanded=True):
|
| 127 |
+
st.image(analysis['concept_graph'])
|
| 128 |
+
|
| 129 |
+
# Mostrar gr谩fico de entidades
|
| 130 |
+
with st.expander(semantic_t.get('entity_relations', 'Entity Relations'), expanded=True):
|
| 131 |
+
st.image(analysis['entity_graph'])
|
| 132 |
+
|
| 133 |
+
# Mostrar entidades identificadas
|
| 134 |
+
if 'entities' in analysis:
|
| 135 |
+
with st.expander(semantic_t.get('identified_entities', 'Identified Entities'), expanded=True):
|
| 136 |
+
for entity_type, entities in analysis['entities'].items():
|
| 137 |
+
st.subheader(entity_type)
|
| 138 |
+
st.write(", ".join(entities))
|
| 139 |
+
|
| 140 |
+
# Bot贸n de exportaci贸n
|
| 141 |
+
if st.button(semantic_t.get('export_button', 'Export Analysis')):
|
| 142 |
+
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
|
| 143 |
+
st.download_button(
|
| 144 |
+
label=semantic_t.get('download_pdf', 'Download PDF'),
|
| 145 |
+
data=pdf_buffer,
|
| 146 |
+
file_name="semantic_analysis.pdf",
|
| 147 |
+
mime="application/pdf"
|
| 148 |
+
)
|