|
|
|
import streamlit as st |
|
from streamlit_float import * |
|
from streamlit_antd_components import * |
|
from streamlit.components.v1 import html |
|
import spacy_streamlit |
|
import io |
|
from io import BytesIO |
|
import base64 |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
import re |
|
import logging |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
from .semantic_process import ( |
|
process_semantic_input, |
|
format_semantic_results |
|
) |
|
|
|
from ..utils.widget_utils import generate_unique_key |
|
from ..database.semantic_mongo_db import store_student_semantic_result |
|
from ..database.semantic_export import export_user_interactions |
|
|
|
def display_semantic_interface(lang_code, nlp_models, semantic_t): |
|
""" |
|
Interfaz para el análisis semántico |
|
Args: |
|
lang_code: Código del idioma actual |
|
nlp_models: Modelos de spaCy cargados |
|
semantic_t: Diccionario de traducciones |
|
""" |
|
|
|
st.session_state.page = 'semantic' |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.stButton button { |
|
width: 100%; |
|
padding: 0.5rem; |
|
} |
|
.upload-container { |
|
display: flex; |
|
align-items: center; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
with st.container(): |
|
col_upload, col_analyze, col_export, col_new = st.columns([4,2,2,2]) |
|
|
|
with col_upload: |
|
uploaded_file = st.file_uploader( |
|
semantic_t.get('file_uploader', 'Upload text file'), |
|
type=['txt'], |
|
key="semantic_file_uploader" |
|
) |
|
|
|
with col_analyze: |
|
analyze_button = st.button( |
|
semantic_t.get('semantic_analyze_button', 'Analyze Semantics'), |
|
key="semantic_analysis_button", |
|
disabled=(uploaded_file is None), |
|
use_container_width=True |
|
) |
|
|
|
with col_export: |
|
export_button = st.button( |
|
semantic_t.get('semantic_export_button', 'Export Semantic'), |
|
key="semantic_export_button", |
|
disabled=not ('semantic_result' in st.session_state), |
|
use_container_width=True |
|
) |
|
|
|
with col_new: |
|
new_button = st.button( |
|
semantic_t.get('semantic_new_button', 'New Semantic'), |
|
key="semantic_new_button", |
|
disabled=not ('semantic_result' in st.session_state), |
|
use_container_width=True |
|
) |
|
|
|
|
|
st.markdown("---") |
|
|
|
|
|
if uploaded_file is not None and analyze_button: |
|
try: |
|
|
|
text_content = uploaded_file.getvalue().decode('utf-8') |
|
|
|
with st.spinner(semantic_t.get('processing', 'Processing...')): |
|
|
|
analysis_result = perform_semantic_analysis( |
|
text_content, |
|
nlp_models[lang_code], |
|
lang_code |
|
) |
|
|
|
|
|
st.session_state.semantic_result = analysis_result |
|
|
|
|
|
if store_student_semantic_result( |
|
st.session_state.username, |
|
text_content, |
|
analysis_result |
|
): |
|
st.success(semantic_t.get('success_message', 'Analysis saved successfully')) |
|
else: |
|
st.error(semantic_t.get('error_message', 'Error saving analysis')) |
|
|
|
|
|
display_semantic_results(analysis_result, lang_code, semantic_t) |
|
|
|
except Exception as e: |
|
st.error(f"Error: {str(e)}") |
|
|
|
|
|
if export_button and 'semantic_result' in st.session_state: |
|
try: |
|
pdf_buffer = export_user_interactions(st.session_state.username, 'semantic') |
|
st.download_button( |
|
label=semantic_t.get('download_pdf', 'Download PDF'), |
|
data=pdf_buffer, |
|
file_name="semantic_analysis.pdf", |
|
mime="application/pdf" |
|
) |
|
except Exception as e: |
|
st.error(f"Error exporting: {str(e)}") |
|
|
|
|
|
if new_button: |
|
if 'semantic_result' in st.session_state: |
|
del st.session_state.semantic_result |
|
st.rerun() |
|
|
|
|
|
if 'semantic_result' in st.session_state: |
|
display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t) |
|
elif uploaded_file is None: |
|
st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis')) |
|
|
|
|
|
def display_semantic_results(result, lang_code, semantic_t): |
|
""" |
|
Muestra los resultados del análisis semántico |
|
""" |
|
if result is None or not result['success']: |
|
st.warning(semantic_t.get('no_results', 'No results available')) |
|
return |
|
|
|
analysis = result['analysis'] |
|
|
|
|
|
tab1, tab2 = st.tabs([ |
|
semantic_t.get('concepts_tab', 'Key Concepts Analysis'), |
|
semantic_t.get('entities_tab', 'Entities Analysis') |
|
]) |
|
|
|
|
|
with tab1: |
|
col1, col2 = st.columns(2) |
|
|
|
|
|
with col1: |
|
st.subheader(semantic_t.get('key_concepts', 'Key Concepts')) |
|
concept_text = "\n".join([ |
|
f"• {concept} ({frequency:.2f})" |
|
for concept, frequency in analysis['key_concepts'] |
|
]) |
|
st.markdown(concept_text) |
|
|
|
|
|
with col2: |
|
st.subheader(semantic_t.get('concept_graph', 'Concepts Graph')) |
|
st.image(analysis['concept_graph']) |
|
|
|
|
|
with tab2: |
|
col1, col2 = st.columns(2) |
|
|
|
|
|
with col1: |
|
st.subheader(semantic_t.get('identified_entities', 'Identified Entities')) |
|
if 'entities' in analysis: |
|
for entity_type, entities in analysis['entities'].items(): |
|
st.markdown(f"**{entity_type}**") |
|
st.markdown("• " + "\n• ".join(entities)) |
|
|
|
|
|
with col2: |
|
st.subheader(semantic_t.get('entity_graph', 'Entities Graph')) |
|
st.image(analysis['entity_graph']) |